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

138 lines
4.9 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
private var scoreboard: List<ScoreManager.DreamloEntry>? = null
private var usernameUUID: String? = null
var ongoingJob: Job? = null
private fun saveAfterTime() {
ongoingJob?.cancel()
ongoingJob = main {
delay(10000) // dont ddos the scoreboard
saveAndUpload()
}
}
private fun saveAndUpload() {
2021-08-30 22:07:39 +00:00
if (ScoreManager.privateCode.isNullOrBlank()) return
try {
val settingsManager = PreferenceManager.getDefaultSharedPreferences(context)
val uuid = usernameUUID
if (uuid != null) {
settingsManager.edit()
.putString(getString(R.string.benene_count_uuid), uuid)
.putInt(getString(R.string.benene_count), count)
.apply()
thread {
normalSafeApiCall {
ScoreManager.addScore(uuid, count)
}
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
}
}
override fun onPause() {
saveAndUpload()
super.onPause()
}
override fun onDestroy() {
saveAndUpload()
super.onDestroy()
}
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)
usernameUUID =
settingsManager.getString(getString(R.string.benene_count_uuid), UUID.randomUUID().toString())
if (count > 20) {
if (!ScoreManager.privateCode.isNullOrBlank()) {
2021-08-30 21:58:09 +00:00
thread {
scoreboard = normalSafeApiCall { ScoreManager.getScore() }
}
}
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()
var add = ""
val localScoreBoard = scoreboard
if (localScoreBoard != null) {
for ((index, score) in localScoreBoard.withIndex()) {
if (count > (score.score.toIntOrNull() ?: 0)) {
add = " (${index + 1}/${localScoreBoard.size})"
break
2021-08-30 21:58:09 +00:00
}
2021-08-30 21:42:58 +00:00
}
}
2021-08-30 22:07:39 +00:00
it.summary = getString(R.string.benene_count_text).format(count) + add
saveAfterTime()
} 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
}