Skip to content

Commit

Permalink
removed ads + added tr settings
Browse files Browse the repository at this point in the history
  • Loading branch information
LagradOst committed Aug 8, 2024
1 parent 4a018c3 commit c5524d1
Show file tree
Hide file tree
Showing 8 changed files with 382 additions and 23 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ android {
minSdkVersion 21
targetSdkVersion 34
versionCode 55
versionName "3.1.9"
versionName "3.2.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

Expand Down
19 changes: 18 additions & 1 deletion app/src/main/java/com/lagradost/quicknovel/APIRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,32 @@ import com.lagradost.quicknovel.mvvm.Resource
import com.lagradost.quicknovel.mvvm.logError
import com.lagradost.quicknovel.mvvm.safeApiCall
import com.lagradost.quicknovel.util.Coroutines.threadSafeListOf
import org.jsoup.Jsoup

data class OnGoingSearch(
val apiName: String,
val data: Resource<List<SearchResponse>>
)

// This function is somewhat like preParseHtml
private fun String?.removeAds(): String? {
if (this.isNullOrBlank()) return null
return this.replace("(adsbygoogle = window.adsbygoogle || []).push({});", "")
return try {
val document = Jsoup.parse(this)
//document.select("style").remove() // Style might be good, but is removed in the internal reader
document.select("small.ads-title").remove()
document.select("script").remove()
document.select("iframe").remove()
document.select(".adsbygoogle").remove()

// Remove aside https://html.spec.whatwg.org/multipage/sections.html#the-aside-element?
// https://stackoverflow.com/questions/14384431/html-element-for-ad

document.html()
} catch (t : Throwable) {
logError(t)
this
}
}

class APIRepository(val api: MainAPI) {
Expand Down
27 changes: 27 additions & 0 deletions app/src/main/java/com/lagradost/quicknovel/CommonActivity.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.lagradost.quicknovel

import android.app.Activity
import android.content.Context
import android.content.res.Resources
import android.os.Build
import android.util.Log
import android.view.Gravity
Expand All @@ -18,6 +20,7 @@ import com.lagradost.quicknovel.mvvm.logError
import com.lagradost.quicknovel.util.UIHelper.colorFromAttribute
import com.lagradost.quicknovel.util.toPx
import java.lang.ref.WeakReference
import java.util.Locale

object CommonActivity {
private var _activity: WeakReference<Activity>? = null
Expand Down Expand Up @@ -90,6 +93,30 @@ object CommonActivity {
logError(e)
}
}
fun init(act: Activity) {
this.activity = act
act.updateLocale()
}

fun setLocale(context: Context?, languageCode: String?) {
if (context == null || languageCode == null) return
val locale = Locale(languageCode)
val resources: Resources = context.resources
val config = resources.configuration
Locale.setDefault(locale)
config.setLocale(locale)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
context.createConfigurationContext(config)
resources.updateConfiguration(config, resources.displayMetrics)
}

fun Context?.updateLocale() {
if (this == null) return
val settingsManager = PreferenceManager.getDefaultSharedPreferences(this)
val localeCode = settingsManager.getString(this.getString(R.string.locale_key), null)
setLocale(this, localeCode)
}

fun loadThemes(act: Activity?) {
if (act == null) return
Expand Down
13 changes: 9 additions & 4 deletions app/src/main/java/com/lagradost/quicknovel/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.lagradost.quicknovel
import android.app.Activity
import android.content.Intent
import android.content.res.ColorStateList
import android.content.res.Configuration
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
Expand All @@ -19,7 +20,6 @@ import androidx.navigation.NavDestination
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavOptions
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.preference.PreferenceManager
import com.fasterxml.jackson.databind.DeserializationFeature
Expand All @@ -39,6 +39,7 @@ import com.lagradost.quicknovel.BookDownloader2Helper.createQuickStream
import com.lagradost.quicknovel.BookDownloader2Helper.requestRW
import com.lagradost.quicknovel.CommonActivity.activity
import com.lagradost.quicknovel.CommonActivity.showToast
import com.lagradost.quicknovel.CommonActivity.updateLocale
import com.lagradost.quicknovel.DataStore.getKey
import com.lagradost.quicknovel.DataStore.getKeys
import com.lagradost.quicknovel.NotificationHelper.requestNotifications
Expand All @@ -51,7 +52,6 @@ import com.lagradost.quicknovel.mvvm.observeNullable
import com.lagradost.quicknovel.providers.RedditProvider
import com.lagradost.quicknovel.ui.ReadType
import com.lagradost.quicknovel.ui.download.DownloadFragment
import com.lagradost.quicknovel.ui.mainpage.MainPageFragment
import com.lagradost.quicknovel.ui.result.ResultFragment
import com.lagradost.quicknovel.ui.result.ResultViewModel
import com.lagradost.quicknovel.ui.search.SearchFragment
Expand Down Expand Up @@ -421,15 +421,20 @@ class MainActivity : AppCompatActivity() {
}
}

override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
updateLocale() // android fucks me by chaining lang when rotating the phone
}

var binding: ActivityMainBinding? = null
override fun onCreate(savedInstanceState: Bundle?) {
mainActivity = this
activity = this

window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)

val settingsManager = PreferenceManager.getDefaultSharedPreferences(this)
CommonActivity.loadThemes(this)
CommonActivity.init(this)

super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
Expand Down Expand Up @@ -673,7 +678,7 @@ class MainActivity : AppCompatActivity() {
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
window?.navigationBarColor = colorFromAttribute(R.attr.primaryGrayBackground)
//updateLocale()
updateLocale()

// If we don't disable we end up in a loop with default behavior calling
// this callback as well, so we disable it, run default behavior,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceManager
import com.lagradost.quicknovel.APIRepository.Companion.providersActive
import com.lagradost.quicknovel.CommonActivity
import com.lagradost.quicknovel.CommonActivity.showToast
import com.lagradost.quicknovel.R
import com.lagradost.quicknovel.mvvm.logError
Expand Down Expand Up @@ -43,7 +44,31 @@ class SettingsFragment : PreferenceFragmentCompat() {
}
}


companion object {
fun getCurrentLocale(context: Context): String {
val res = context.resources
val conf = res.configuration

return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
conf?.locales?.get(0)?.toString() ?: "en"
} else {
@Suppress("DEPRECATION")
conf?.locale?.toString() ?: "en"
}
}

// idk, if you find a way of automating this it would be great
// https://www.iemoji.com/view/emoji/1794/flags/antarctica
// Emoji Character Encoding Data --> C/C++/Java Src
// https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes leave blank for auto
val appLanguages = arrayListOf(
/* begin language list */
Triple("", "English", "en"),
Triple("", "Türkçe", "tr"),
/* end language list */
).sortedBy { it.second.lowercase() } //ye, we go alphabetical, so ppl don't put their lang on top

fun showSearchProviders(context: Context?) {
if (context == null) return
val apiNames = apis.map { it.name }
Expand Down Expand Up @@ -176,6 +201,31 @@ class SettingsFragment : PreferenceFragmentCompat() {
return@setOnPreferenceChangeListener true
}*/

getPref(R.string.locale_key)?.setOnPreferenceClickListener { pref ->
val tempLangs = appLanguages.toMutableList()
val current = getCurrentLocale(pref.context)
val languageCodes = tempLangs.map { (_, _, iso) -> iso }
val languageNames = tempLangs.map { (emoji, name, iso) ->
val flag = emoji.ifBlank { SubtitleHelper.getFlagFromIso(iso) ?: "ERROR" }
"$flag $name"
}
val index = languageCodes.indexOf(current)

activity?.showDialog(
languageNames, index, getString(R.string.provider_lang_settings), true, { }
) { languageIndex ->
try {
val code = languageCodes[languageIndex]
CommonActivity.setLocale(activity, code)
settingsManager.edit().putString(getString(R.string.locale_key), code).apply()
activity?.recreate()
} catch (e: Exception) {
logError(e)
}
}
return@setOnPreferenceClickListener true
}

getPref(R.string.backup_key)?.setOnPreferenceClickListener {
activity?.backup()
return@setOnPreferenceClickListener true
Expand Down
Loading

0 comments on commit c5524d1

Please sign in to comment.