AquaStream/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsFragment.kt

75 lines
2.8 KiB
Kotlin
Raw Normal View History

2021-06-10 23:00:22 +00:00
package com.lagradost.cloudstream3.ui.settings
import android.os.Bundle
import android.widget.Toast
2021-08-07 01:53:45 +00:00
import androidx.preference.Preference
2021-06-10 23:00:22 +00:00
import androidx.preference.PreferenceFragmentCompat
2021-08-30 21:42:58 +00:00
import androidx.preference.PreferenceManager
import com.lagradost.cloudstream3.MainActivity.Companion.showToast
2021-06-10 23:00:22 +00:00
import com.lagradost.cloudstream3.R
2021-08-30 21:42:58 +00:00
import com.lagradost.cloudstream3.mvvm.normalSafeApiCall
2021-08-07 01:53:45 +00:00
import com.lagradost.cloudstream3.ui.subtitles.SubtitlesFragment
2021-08-30 21:42:58 +00:00
import com.lagradost.cloudstream3.utils.Coroutines.main
import com.lagradost.cloudstream3.utils.InAppUpdater.Companion.runAutoUpdate
2021-08-04 13:30:34 +00:00
import com.lagradost.cloudstream3.utils.UIHelper.hideKeyboard
2021-08-30 21:42:58 +00:00
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import java.util.*
2021-08-30 21:58:09 +00:00
import kotlin.Exception
import kotlin.concurrent.thread
2021-06-10 23:00:22 +00:00
class SettingsFragment : PreferenceFragmentCompat() {
2021-08-30 21:42:58 +00:00
var count = 0
2021-06-10 23:00:22 +00:00
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
2021-08-04 13:30:34 +00:00
hideKeyboard()
2021-06-10 23:00:22 +00:00
setPreferencesFromResource(R.xml.settings, rootKey)
val updatePrefrence = findPreference<Preference>(getString(R.string.manual_check_update_key))!!
2021-08-30 21:42:58 +00:00
val benenePref = findPreference<Preference>(getString(R.string.benene_count))!!
2021-08-30 21:58:09 +00:00
2021-08-30 22:07:39 +00:00
try {
val settingsManager = PreferenceManager.getDefaultSharedPreferences(context)
count = settingsManager.getInt(getString(R.string.benene_count), 0)
2021-09-01 12:02:32 +00:00
2021-08-30 22:07:39 +00:00
benenePref.summary =
if (count <= 0) getString(R.string.benene_count_text_none) else getString(R.string.benene_count_text).format(
count
)
benenePref.setOnPreferenceClickListener {
try {
count++
settingsManager.edit().putInt(getString(R.string.benene_count), count).apply()
2021-09-01 12:02:32 +00:00
it.summary = getString(R.string.benene_count_text).format(count)
2021-08-30 22:07:39 +00:00
} catch (e: Exception) {
e.printStackTrace()
2021-08-30 21:42:58 +00:00
}
2021-08-30 22:07:39 +00:00
return@setOnPreferenceClickListener true
2021-08-30 21:42:58 +00:00
}
2021-08-30 22:07:39 +00:00
} catch (e: Exception) {
e.printStackTrace()
2021-08-30 21:42:58 +00:00
}
updatePrefrence.setOnPreferenceClickListener {
thread {
if (!requireActivity().runAutoUpdate(false)) {
activity?.runOnUiThread {
showToast(activity, "No Update Found", Toast.LENGTH_SHORT)
}
}
}
return@setOnPreferenceClickListener true
}
2021-06-10 23:00:22 +00:00
}
2021-08-07 01:53:45 +00:00
override fun onPreferenceTreeClick(preference: Preference?): Boolean {
if (preference != null) {
if (preference.key == "subtitle_settings_key") {
SubtitlesFragment.push(activity, false)
}
}
return super.onPreferenceTreeClick(preference)
}
2021-06-10 23:00:22 +00:00
}