2021-06-10 23:00:22 +00:00
|
|
|
package com.lagradost.cloudstream3.ui.settings
|
|
|
|
|
|
|
|
import android.os.Bundle
|
2021-08-15 17:38:41 +00:00
|
|
|
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
|
2021-08-24 22:19:15 +00:00
|
|
|
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
|
2021-08-15 17:38:41 +00:00
|
|
|
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
|
2021-08-15 17:38:41 +00:00
|
|
|
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() {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-30 21:42:58 +00:00
|
|
|
|
|
|
|
|
2021-08-04 13:30:34 +00:00
|
|
|
hideKeyboard()
|
2021-06-10 23:00:22 +00:00
|
|
|
setPreferencesFromResource(R.xml.settings, rootKey)
|
2021-08-15 17:38:41 +00:00
|
|
|
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
|
|
|
if (ScoreManager.privateCode.isNullOrBlank()) {
|
|
|
|
benenePref.isVisible = false
|
|
|
|
} else {
|
|
|
|
benenePref.isVisible = true
|
2021-08-30 21:42:58 +00:00
|
|
|
try {
|
2021-08-30 21:58:09 +00:00
|
|
|
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) {
|
|
|
|
thread {
|
|
|
|
scoreboard = normalSafeApiCall { ScoreManager.getScore() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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:42:58 +00:00
|
|
|
}
|
2021-08-30 21:58:09 +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 21:58:09 +00:00
|
|
|
|
|
|
|
return@setOnPreferenceClickListener true
|
2021-08-30 21:42:58 +00:00
|
|
|
}
|
|
|
|
} catch (e: Exception) {
|
|
|
|
e.printStackTrace()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 21:58:09 +00:00
|
|
|
|
2021-08-15 17:38:41 +00:00
|
|
|
updatePrefrence.setOnPreferenceClickListener {
|
|
|
|
thread {
|
|
|
|
if (!requireActivity().runAutoUpdate(false)) {
|
|
|
|
activity?.runOnUiThread {
|
2021-08-24 22:19:15 +00:00
|
|
|
showToast(activity, "No Update Found", Toast.LENGTH_SHORT)
|
2021-08-15 17:38:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|