Dantotsu/app/src/main/java/ani/dantotsu/settings/SettingsActivity.kt
2024-02-01 23:37:34 +05:30

842 lines
34 KiB
Kotlin

package ani.dantotsu.settings
import android.annotation.SuppressLint
import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.Animatable
import android.os.Build.*
import android.os.Build.VERSION.*
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.TextView
import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import androidx.annotation.OptIn
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.updateLayoutParams
import androidx.lifecycle.lifecycleScope
import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.offline.DownloadService
import ani.dantotsu.*
import ani.dantotsu.connections.anilist.Anilist
import ani.dantotsu.connections.discord.Discord
import ani.dantotsu.connections.mal.MAL
import ani.dantotsu.databinding.ActivitySettingsBinding
import ani.dantotsu.download.DownloadedType
import ani.dantotsu.download.DownloadsManager
import ani.dantotsu.download.video.ExoplayerDownloadService
import ani.dantotsu.others.AppUpdater
import ani.dantotsu.others.CustomBottomDialog
import ani.dantotsu.parsers.AnimeSources
import ani.dantotsu.parsers.MangaSources
import ani.dantotsu.settings.saving.PrefName
import ani.dantotsu.settings.saving.PrefWrapper
import ani.dantotsu.subcriptions.Notifications
import ani.dantotsu.subcriptions.Notifications.Companion.openSettings
import ani.dantotsu.subcriptions.Subscription.Companion.defaultTime
import ani.dantotsu.subcriptions.Subscription.Companion.startSubscription
import ani.dantotsu.subcriptions.Subscription.Companion.timeMinutes
import ani.dantotsu.themes.ThemeManager
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.textfield.TextInputEditText
import eltos.simpledialogfragment.SimpleDialog
import eltos.simpledialogfragment.SimpleDialog.OnDialogResultListener.BUTTON_POSITIVE
import eltos.simpledialogfragment.color.SimpleColorDialog
import eu.kanade.domain.base.BasePreferences
import eu.kanade.tachiyomi.network.NetworkPreferences
import io.noties.markwon.Markwon
import io.noties.markwon.SoftBreakAddsNewLinePlugin
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import kotlin.random.Random
class SettingsActivity : AppCompatActivity(), SimpleDialog.OnDialogResultListener {
private val restartMainActivity = object : OnBackPressedCallback(false) {
override fun handleOnBackPressed() = startMainActivity(this@SettingsActivity)
}
lateinit var binding: ActivitySettingsBinding
private val extensionInstaller = Injekt.get<BasePreferences>().extensionInstaller()
private val networkPreferences = Injekt.get<NetworkPreferences>()
private var cursedCounter = 0
@OptIn(UnstableApi::class)
@SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
ThemeManager(this).applyTheme()
binding = ActivitySettingsBinding.inflate(layoutInflater)
setContentView(binding.root)
initActivity(this)
binding.settingsVersion.text = getString(R.string.version_current, BuildConfig.VERSION_NAME)
binding.settingsVersion.setOnLongClickListener {
copyToClipboard(getDeviceInfo(), false)
toast(getString(R.string.copied_device_info))
return@setOnLongClickListener true
}
binding.settingsContainer.updateLayoutParams<ViewGroup.MarginLayoutParams> {
topMargin = statusBarHeight
bottomMargin = navBarHeight
}
onBackPressedDispatcher.addCallback(this, restartMainActivity)
binding.settingsBack.setOnClickListener {
onBackPressedDispatcher.onBackPressed()
}
binding.settingsUseMaterialYou.isChecked = PrefWrapper.getVal(PrefName.UseMaterialYou, false)
binding.settingsUseMaterialYou.setOnCheckedChangeListener { _, isChecked ->
PrefWrapper.setVal(PrefName.UseMaterialYou, isChecked)
if (isChecked) binding.settingsUseCustomTheme.isChecked = false
restartApp()
}
binding.settingsUseCustomTheme.isChecked = PrefWrapper.getVal(PrefName.UseCustomTheme, false)
binding.settingsUseCustomTheme.setOnCheckedChangeListener { _, isChecked ->
PrefWrapper.setVal(PrefName.UseCustomTheme, isChecked)
if (isChecked) {
binding.settingsUseMaterialYou.isChecked = false
}
restartApp()
}
binding.settingsUseSourceTheme.isChecked = PrefWrapper.getVal(PrefName.UseSourceTheme, false)
binding.settingsUseSourceTheme.setOnCheckedChangeListener { _, isChecked ->
PrefWrapper.setVal(PrefName.UseSourceTheme, isChecked)
restartApp()
}
binding.settingsUseOLED.isChecked = PrefWrapper.getVal(PrefName.UseOLED, false)
binding.settingsUseOLED.setOnCheckedChangeListener { _, isChecked ->
PrefWrapper.setVal(PrefName.UseOLED, isChecked)
restartApp()
}
val themeString = PrefWrapper.getVal(PrefName.Theme, "PURPLE")
binding.themeSwitcher.setText(
themeString.substring(0, 1) + themeString.substring(1).lowercase()
)
binding.themeSwitcher.setAdapter(
ArrayAdapter(
this,
R.layout.item_dropdown,
ThemeManager.Companion.Theme.entries
.map { it.theme.substring(0, 1) + it.theme.substring(1).lowercase() })
)
binding.themeSwitcher.setOnItemClickListener { _, _, i, _ ->
PrefWrapper.setVal(PrefName.Theme, ThemeManager.Companion.Theme.entries[i].theme)
//ActivityHelper.shouldRefreshMainActivity = true
binding.themeSwitcher.clearFocus()
restartApp()
}
binding.customTheme.setOnClickListener {
val originalColor = PrefWrapper.getVal(PrefName.CustomThemeInt, Color.parseColor("#6200EE"))
class CustomColorDialog : SimpleColorDialog() { //idk where to put it
override fun onPositiveButtonClick() {
restartApp()
super.onPositiveButtonClick()
}
}
val tag = "colorPicker"
CustomColorDialog().title("Custom Theme")
.colorPreset(originalColor)
.colors(this, SimpleColorDialog.BEIGE_COLOR_PALLET)
.allowCustom(true)
.showOutline(0x46000000)
.gridNumColumn(5)
.choiceMode(SimpleColorDialog.SINGLE_CHOICE)
.neg()
.show(this, tag)
}
binding.settingsPinnedAnimeSources.setOnClickListener {
val animeSourcesWithoutDownloadsSource =
AnimeSources.list.filter { it.name != "Downloaded" }
val names = animeSourcesWithoutDownloadsSource.map { it.name }
val pinnedSourcesBoolean =
animeSourcesWithoutDownloadsSource.map { it.name in AnimeSources.pinnedAnimeSources }
val pinnedSourcesOriginal = PrefWrapper.getVal(PrefName.PinnedAnimeSources, setOf<String>())
val pinnedSources = pinnedSourcesOriginal.toMutableSet() ?: mutableSetOf()
val alertDialog = AlertDialog.Builder(this, R.style.MyPopup)
.setTitle("Pinned Anime Sources")
.setMultiChoiceItems(
names.toTypedArray(),
pinnedSourcesBoolean.toBooleanArray()
) { _, which, isChecked ->
if (isChecked) {
pinnedSources.add(AnimeSources.names[which])
} else {
pinnedSources.remove(AnimeSources.names[which])
}
}
.setPositiveButton("OK") { dialog, _ ->
PrefWrapper.setVal(PrefName.PinnedAnimeSources, pinnedSources)
AnimeSources.pinnedAnimeSources = pinnedSources
AnimeSources.performReorderAnimeSources()
dialog.dismiss()
}
.create()
alertDialog.show()
alertDialog.window?.setDimAmount(0.8f)
}
binding.settingsPlayer.setOnClickListener {
startActivity(Intent(this, PlayerSettingsActivity::class.java))
}
val managers = arrayOf("Default", "1DM", "ADM")
val downloadManagerDialog =
AlertDialog.Builder(this, R.style.MyPopup).setTitle("Download Manager")
var downloadManager = PrefWrapper.getVal(PrefName.DownloadManager, 0)
binding.settingsDownloadManager.setOnClickListener {
val dialog = downloadManagerDialog.setSingleChoiceItems(
managers,
downloadManager
) { dialog, count ->
downloadManager = count
PrefWrapper.setVal(PrefName.DownloadManager, downloadManager)
dialog.dismiss()
}.show()
dialog.window?.setDimAmount(0.8f)
}
binding.purgeAnimeDownloads.setOnClickListener {
val dialog = AlertDialog.Builder(this, R.style.MyPopup)
.setTitle("Purge Anime Downloads")
.setMessage("Are you sure you want to purge all anime downloads?")
.setPositiveButton("Yes") { dialog, _ ->
val downloadsManager = Injekt.get<DownloadsManager>()
downloadsManager.purgeDownloads(DownloadedType.Type.ANIME)
DownloadService.sendRemoveAllDownloads(
this,
ExoplayerDownloadService::class.java,
false
)
dialog.dismiss()
}
.setNegativeButton("No") { dialog, _ ->
dialog.dismiss()
}
.create()
dialog.window?.setDimAmount(0.8f)
dialog.show()
}
binding.purgeMangaDownloads.setOnClickListener {
val dialog = AlertDialog.Builder(this, R.style.MyPopup)
.setTitle("Purge Manga Downloads")
.setMessage("Are you sure you want to purge all manga downloads?")
.setPositiveButton("Yes") { dialog, _ ->
val downloadsManager = Injekt.get<DownloadsManager>()
downloadsManager.purgeDownloads(DownloadedType.Type.MANGA)
dialog.dismiss()
}
.setNegativeButton("No") { dialog, _ ->
dialog.dismiss()
}
.create()
dialog.window?.setDimAmount(0.8f)
dialog.show()
}
binding.purgeNovelDownloads.setOnClickListener {
val dialog = AlertDialog.Builder(this, R.style.MyPopup)
.setTitle("Purge Novel Downloads")
.setMessage("Are you sure you want to purge all novel downloads?")
.setPositiveButton("Yes") { dialog, _ ->
val downloadsManager = Injekt.get<DownloadsManager>()
downloadsManager.purgeDownloads(DownloadedType.Type.NOVEL)
dialog.dismiss()
}
.setNegativeButton("No") { dialog, _ ->
dialog.dismiss()
}
.create()
dialog.window?.setDimAmount(0.8f)
dialog.show()
}
binding.settingsForceLegacyInstall.isChecked =
extensionInstaller.get() == BasePreferences.ExtensionInstaller.LEGACY
binding.settingsForceLegacyInstall.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
extensionInstaller.set(BasePreferences.ExtensionInstaller.LEGACY)
} else {
extensionInstaller.set(BasePreferences.ExtensionInstaller.PACKAGEINSTALLER)
}
}
binding.skipExtensionIcons.isChecked = PrefWrapper.getVal(PrefName.SkipExtensionIcons, false)
binding.skipExtensionIcons.setOnCheckedChangeListener { _, isChecked ->
PrefWrapper.getVal(PrefName.SkipExtensionIcons, isChecked)
}
binding.NSFWExtension.isChecked = PrefWrapper.getVal(PrefName.NSFWExtension, true)
binding.NSFWExtension.setOnCheckedChangeListener { _, isChecked ->
PrefWrapper.setVal(PrefName.NSFWExtension,isChecked)
}
binding.userAgent.setOnClickListener {
val dialogView = layoutInflater.inflate(R.layout.dialog_user_agent, null)
val editText = dialogView.findViewById<TextInputEditText>(R.id.userAgentTextBox)
editText.setText(networkPreferences.defaultUserAgent().get())
val alertDialog = AlertDialog.Builder(this, R.style.MyPopup)
.setTitle("User Agent")
.setView(dialogView)
.setPositiveButton("OK") { dialog, _ ->
networkPreferences.defaultUserAgent().set(editText.text.toString())
dialog.dismiss()
}
.setNeutralButton("Reset") { dialog, _ ->
networkPreferences.defaultUserAgent()
.set("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:110.0) Gecko/20100101 Firefox/110.0")
editText.setText("")
dialog.dismiss()
}
.setNegativeButton("Cancel") { dialog, _ ->
dialog.dismiss()
}
.create()
alertDialog.show()
alertDialog.window?.setDimAmount(0.8f)
}
val exDns = listOf(
"None",
"Cloudflare",
"Google",
"AdGuard",
"Quad9",
"AliDNS",
"DNSPod",
"360",
"Quad101",
"Mullvad",
"Controld",
"Njalla",
"Shecan",
"Libre"
)
binding.settingsExtensionDns.setText(exDns[networkPreferences.dohProvider().get()], false)
binding.settingsExtensionDns.setAdapter(ArrayAdapter(this, R.layout.item_dropdown, exDns))
binding.settingsExtensionDns.setOnItemClickListener { _, _, i, _ ->
networkPreferences.dohProvider().set(i)
binding.settingsExtensionDns.clearFocus()
Toast.makeText(this, "Restart app to apply changes", Toast.LENGTH_LONG).show()
}
binding.settingsDownloadInSd.isChecked = PrefWrapper.getVal(PrefName.SdDl, false)
binding.settingsDownloadInSd.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
val arrayOfFiles = ContextCompat.getExternalFilesDirs(this, null)
if (arrayOfFiles.size > 1 && arrayOfFiles[1] != null) {
PrefWrapper.setVal(PrefName.SdDl, true)
} else {
binding.settingsDownloadInSd.isChecked = false
PrefWrapper.setVal(PrefName.SdDl, true)
snackString(getString(R.string.noSdFound))
}
} else PrefWrapper.setVal(PrefName.SdDl, true)
}
binding.settingsContinueMedia.isChecked = PrefWrapper.getVal(PrefName.ContinueMedia, true)
binding.settingsContinueMedia.setOnCheckedChangeListener { _, isChecked ->
PrefWrapper.setVal(PrefName.ContinueMedia, isChecked)
}
binding.settingsRecentlyListOnly.isChecked = PrefWrapper.getVal(PrefName.RecentlyListOnly, false)
binding.settingsRecentlyListOnly.setOnCheckedChangeListener { _, isChecked ->
PrefWrapper.setVal(PrefName.RecentlyListOnly, isChecked)
}
binding.settingsShareUsername.isChecked = PrefWrapper.getVal(PrefName.SharedUserID, true)
binding.settingsShareUsername.setOnCheckedChangeListener { _, isChecked ->
PrefWrapper.setVal(PrefName.SharedUserID, isChecked)
}
binding.settingsPreferDub.isChecked = PrefWrapper.getVal(PrefName.SettingsPreferDub, false)
binding.settingsPreferDub.setOnCheckedChangeListener { _, isChecked ->
PrefWrapper.setVal(PrefName.SettingsPreferDub, isChecked)
}
binding.settingsPinnedMangaSources.setOnClickListener {
val mangaSourcesWithoutDownloadsSource =
MangaSources.list.filter { it.name != "Downloaded" }
val names = mangaSourcesWithoutDownloadsSource.map { it.name }
val pinnedSourcesBoolean =
mangaSourcesWithoutDownloadsSource.map { it.name in MangaSources.pinnedMangaSources }
val pinnedSourcesOriginal = PrefWrapper.getVal(PrefName.PinnedMangaSources, setOf<String>())
val pinnedSources = pinnedSourcesOriginal.toMutableSet()
val alertDialog = AlertDialog.Builder(this, R.style.MyPopup)
.setTitle("Pinned Manga Sources")
.setMultiChoiceItems(
names.toTypedArray(),
pinnedSourcesBoolean.toBooleanArray()
) { _, which, isChecked ->
if (isChecked) {
pinnedSources.add(MangaSources.names[which])
} else {
pinnedSources.remove(MangaSources.names[which])
}
}
.setPositiveButton("OK") { dialog, _ ->
PrefWrapper.setVal(PrefName.PinnedMangaSources, pinnedSources)
MangaSources.pinnedMangaSources = pinnedSources
MangaSources.performReorderMangaSources()
dialog.dismiss()
}
.create()
alertDialog.show()
alertDialog.window?.setDimAmount(0.8f)
}
binding.settingsReader.setOnClickListener {
startActivity(Intent(this, ReaderSettingsActivity::class.java))
}
val uiSettings: UserInterfaceSettings =
loadData("ui_settings", toast = false)
?: UserInterfaceSettings().apply { saveData("ui_settings", this) }
var previous: View = when (uiSettings.darkMode) {
null -> binding.settingsUiAuto
true -> binding.settingsUiDark
false -> binding.settingsUiLight
}
previous.alpha = 1f
fun uiTheme(mode: Boolean?, current: View) {
previous.alpha = 0.33f
previous = current
current.alpha = 1f
uiSettings.darkMode = mode
saveData("ui_settings", uiSettings)
Refresh.all()
finish()
startActivity(Intent(this, SettingsActivity::class.java))
initActivity(this)
}
binding.settingsUiAuto.setOnClickListener {
uiTheme(null, it)
}
binding.settingsUiLight.setOnClickListener {
binding.settingsUseOLED.isChecked = false
uiTheme(false, it)
}
binding.settingsUiDark.setOnClickListener {
uiTheme(true, it)
}
var previousStart: View = when (uiSettings.defaultStartUpTab) {
0 -> binding.uiSettingsAnime
1 -> binding.uiSettingsHome
2 -> binding.uiSettingsManga
else -> binding.uiSettingsHome
}
previousStart.alpha = 1f
fun uiTheme(mode: Int, current: View) {
previousStart.alpha = 0.33f
previousStart = current
current.alpha = 1f
uiSettings.defaultStartUpTab = mode
saveData("ui_settings", uiSettings)
initActivity(this)
}
binding.uiSettingsAnime.setOnClickListener {
uiTheme(0, it)
}
binding.uiSettingsHome.setOnClickListener {
uiTheme(1, it)
}
binding.uiSettingsManga.setOnClickListener {
uiTheme(2, it)
}
binding.settingsShowYt.isChecked = uiSettings.showYtButton
binding.settingsShowYt.setOnCheckedChangeListener { _, isChecked ->
uiSettings.showYtButton = isChecked
saveData("ui_settings", uiSettings)
}
var previousEp: View = when (uiSettings.animeDefaultView) {
0 -> binding.settingsEpList
1 -> binding.settingsEpGrid
2 -> binding.settingsEpCompact
else -> binding.settingsEpList
}
previousEp.alpha = 1f
fun uiEp(mode: Int, current: View) {
previousEp.alpha = 0.33f
previousEp = current
current.alpha = 1f
uiSettings.animeDefaultView = mode
saveData("ui_settings", uiSettings)
}
binding.settingsEpList.setOnClickListener {
uiEp(0, it)
}
binding.settingsEpGrid.setOnClickListener {
uiEp(1, it)
}
binding.settingsEpCompact.setOnClickListener {
uiEp(2, it)
}
var previousChp: View = when (uiSettings.mangaDefaultView) {
0 -> binding.settingsChpList
1 -> binding.settingsChpCompact
else -> binding.settingsChpList
}
previousChp.alpha = 1f
fun uiChp(mode: Int, current: View) {
previousChp.alpha = 0.33f
previousChp = current
current.alpha = 1f
uiSettings.mangaDefaultView = mode
saveData("ui_settings", uiSettings)
}
binding.settingsChpList.setOnClickListener {
uiChp(0, it)
}
binding.settingsChpCompact.setOnClickListener {
uiChp(1, it)
}
binding.settingBuyMeCoffee.setOnClickListener {
lifecycleScope.launch {
it.pop()
}
openLinkInBrowser("https://www.buymeacoffee.com/rebelonion")
}
lifecycleScope.launch {
binding.settingBuyMeCoffee.pop()
}
binding.loginDiscord.setOnClickListener {
openLinkInBrowser(getString(R.string.discord))
}
binding.loginGithub.setOnClickListener {
openLinkInBrowser(getString(R.string.github))
}
binding.loginTelegram.setOnClickListener {
openLinkInBrowser(getString(R.string.telegram))
}
binding.settingsUi.setOnClickListener {
startActivity(Intent(this, UserInterfaceSettingsActivity::class.java))
}
binding.settingsFAQ.setOnClickListener {
startActivity(Intent(this, FAQActivity::class.java))
}
(binding.settingsLogo.drawable as Animatable).start()
val array = resources.getStringArray(R.array.tips)
binding.settingsLogo.setSafeOnClickListener {
cursedCounter++
(binding.settingsLogo.drawable as Animatable).start()
if (cursedCounter % 7 == 0) {
Toast.makeText(this, "youwu have been cuwsed :pwayge:", Toast.LENGTH_LONG).show()
val url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
openLinkInBrowser(url)
//PrefWrapper.setVal(PrefName.SomethingSpecial, !PrefWrapper.getVal(PrefName.SomethingSpecial, false))
} else {
snackString(array[(Math.random() * array.size).toInt()], this)
}
}
binding.settingsDev.setOnClickListener {
DevelopersDialogFragment().show(supportFragmentManager, "dialog")
}
binding.settingsForks.setOnClickListener {
ForksDialogFragment().show(supportFragmentManager, "dialog")
}
binding.settingsDisclaimer.setOnClickListener {
val title = getString(R.string.disclaimer)
val text = TextView(this)
text.setText(R.string.full_disclaimer)
CustomBottomDialog.newInstance().apply {
setTitleText(title)
addView(text)
setNegativeButton(currContext()!!.getString(R.string.close)) {
dismiss()
}
show(supportFragmentManager, "dialog")
}
}
var curTime = PrefWrapper.getVal(PrefName.SubscriptionsTimeS, defaultTime)
val timeNames = timeMinutes.map {
val mins = it % 60
val hours = it / 60
if (it > 0) "${if (hours > 0) "$hours hrs " else ""}${if (mins > 0) "$mins mins" else ""}"
else getString(R.string.do_not_update)
}.toTypedArray()
binding.settingsSubscriptionsTime.text =
getString(R.string.subscriptions_checking_time_s, timeNames[curTime])
val speedDialog = AlertDialog.Builder(this, R.style.MyPopup)
.setTitle(R.string.subscriptions_checking_time)
binding.settingsSubscriptionsTime.setOnClickListener {
val dialog = speedDialog.setSingleChoiceItems(timeNames, curTime) { dialog, i ->
curTime = i
binding.settingsSubscriptionsTime.text =
getString(R.string.subscriptions_checking_time_s, timeNames[i])
PrefWrapper.setVal(PrefName.SubscriptionsTimeS, curTime)
dialog.dismiss()
startSubscription(true)
}.show()
dialog.window?.setDimAmount(0.8f)
}
binding.settingsSubscriptionsTime.setOnLongClickListener {
startSubscription(true)
true
}
binding.settingsNotificationsCheckingSubscriptions.isChecked =
PrefWrapper.getVal(PrefName.SubscriptionCheckingNotifications, true)
binding.settingsNotificationsCheckingSubscriptions.setOnCheckedChangeListener { _, isChecked ->
PrefWrapper.setVal(PrefName.SubscriptionCheckingNotifications, isChecked)
if (isChecked)
Notifications.createChannel(
this,
null,
"subscription_checking",
getString(R.string.checking_subscriptions),
false
)
else
Notifications.deleteChannel(this, "subscription_checking")
}
binding.settingsNotificationsCheckingSubscriptions.setOnLongClickListener {
openSettings(this, null)
}
binding.settingsCheckUpdate.isChecked = PrefWrapper.getVal(PrefName.CheckUpdate, true)
binding.settingsCheckUpdate.setOnCheckedChangeListener { _, isChecked ->
saveData("check_update", isChecked)
PrefWrapper.setVal(PrefName.CheckUpdate, isChecked)
if (!isChecked) {
snackString(getString(R.string.long_click_to_check_update))
}
}
binding.settingsLogo.setOnLongClickListener {
lifecycleScope.launch(Dispatchers.IO) {
AppUpdater.check(this@SettingsActivity, true)
}
true
}
binding.settingsCheckUpdate.setOnLongClickListener {
lifecycleScope.launch(Dispatchers.IO) {
AppUpdater.check(this@SettingsActivity, true)
}
true
}
binding.settingsAccountHelp.setOnClickListener {
val title = getString(R.string.account_help)
val full = getString(R.string.full_account_help)
CustomBottomDialog.newInstance().apply {
setTitleText(title)
addView(
TextView(it.context).apply {
val markWon = Markwon.builder(it.context)
.usePlugin(SoftBreakAddsNewLinePlugin.create()).build()
markWon.setMarkdown(this, full)
}
)
}.show(supportFragmentManager, "dialog")
}
fun reload() {
if (Anilist.token != null) {
binding.settingsAnilistLogin.setText(R.string.logout)
binding.settingsAnilistLogin.setOnClickListener {
Anilist.removeSavedToken(it.context)
restartMainActivity.isEnabled = true
reload()
}
binding.settingsAnilistUsername.visibility = View.VISIBLE
binding.settingsAnilistUsername.text = Anilist.username
binding.settingsAnilistAvatar.loadImage(Anilist.avatar)
binding.settingsMALLoginRequired.visibility = View.GONE
binding.settingsMALLogin.visibility = View.VISIBLE
binding.settingsMALUsername.visibility = View.VISIBLE
if (MAL.token != null) {
binding.settingsMALLogin.setText(R.string.logout)
binding.settingsMALLogin.setOnClickListener {
MAL.removeSavedToken(it.context)
restartMainActivity.isEnabled = true
reload()
}
binding.settingsMALUsername.visibility = View.VISIBLE
binding.settingsMALUsername.text = MAL.username
binding.settingsMALAvatar.loadImage(MAL.avatar)
} else {
binding.settingsMALAvatar.setImageResource(R.drawable.ic_round_person_24)
binding.settingsMALUsername.visibility = View.GONE
binding.settingsMALLogin.setText(R.string.login)
binding.settingsMALLogin.setOnClickListener {
MAL.loginIntent(this)
}
}
} else {
binding.settingsAnilistAvatar.setImageResource(R.drawable.ic_round_person_24)
binding.settingsAnilistUsername.visibility = View.GONE
binding.settingsAnilistLogin.setText(R.string.login)
binding.settingsAnilistLogin.setOnClickListener {
Anilist.loginIntent(this)
}
binding.settingsMALLoginRequired.visibility = View.VISIBLE
binding.settingsMALLogin.visibility = View.GONE
binding.settingsMALUsername.visibility = View.GONE
}
if (Discord.token != null) {
val id = PrefWrapper.getVal(PrefName.DiscordId, null as String?)
val avatar = PrefWrapper.getVal(PrefName.DiscordAvatar, null as String?)
val username = PrefWrapper.getVal(PrefName.DiscordUserName, null as String?)
if (id != null && avatar != null) {
binding.settingsDiscordAvatar.loadImage("https://cdn.discordapp.com/avatars/$id/$avatar.png")
}
binding.settingsDiscordUsername.visibility = View.VISIBLE
binding.settingsDiscordUsername.text =
username ?: Discord.token?.replace(Regex("."), "*")
binding.settingsDiscordLogin.setText(R.string.logout)
binding.settingsDiscordLogin.setOnClickListener {
Discord.removeSavedToken(this)
restartMainActivity.isEnabled = true
reload()
}
} else {
binding.settingsDiscordAvatar.setImageResource(R.drawable.ic_round_person_24)
binding.settingsDiscordUsername.visibility = View.GONE
binding.settingsDiscordLogin.setText(R.string.login)
binding.settingsDiscordLogin.setOnClickListener {
Discord.warning(this).show(supportFragmentManager, "dialog")
}
}
}
reload()
lifecycleScope.launch(Dispatchers.IO) {
delay(2000)
runOnUiThread {
if (Random.nextInt(0, 100) > 69) {
CustomBottomDialog.newInstance().apply {
title = "Enjoying the App?"
addView(TextView(this@SettingsActivity).apply {
text =
"Consider donating!"
})
setNegativeButton("no moners :(") {
snackString("That's alright, you'll be a rich man soon :prayge:")
dismiss()
}
setPositiveButton("denote :)") {
binding.settingBuyMeCoffee.performClick()
dismiss()
}
show(supportFragmentManager, "dialog")
}
}
}
}
}
override fun onResult(dialogTag: String, which: Int, extras: Bundle): Boolean {
if (which == BUTTON_POSITIVE) {
if (dialogTag == "colorPicker") {
val color = extras.getInt(SimpleColorDialog.COLOR)
PrefWrapper.setVal(PrefName.CustomThemeInt, color)
logger("Custom Theme: $color")
}
}
return true
}
private fun restartApp() {
Snackbar.make(
binding.root,
R.string.restart_app, Snackbar.LENGTH_SHORT
).apply {
val mainIntent =
Intent.makeRestartActivityTask(
context.packageManager.getLaunchIntentForPackage(
context.packageName
)!!.component
)
setAction("Do it!") {
context.startActivity(mainIntent)
Runtime.getRuntime().exit(0)
}
show()
}
}
companion object {
fun getDeviceInfo(): String {
return """
dantotsu Version: ${BuildConfig.VERSION_NAME}
Device: $BRAND $DEVICE
Architecture: ${getArch()}
OS Version: $CODENAME $RELEASE ($SDK_INT)
""".trimIndent()
}
private fun getArch(): String {
SUPPORTED_ABIS.forEach {
when (it) {
"arm64-v8a" -> return "aarch64"
"armeabi-v7a" -> return "arm"
"x86_64" -> return "x86_64"
"x86" -> return "i686"
}
}
return System.getProperty("os.arch") ?: System.getProperty("os.product.cpu.abi")
?: "Unknown Architecture"
}
}
}