Skip to content

Commit

Permalink
[skip ci] feat: theme crash fix
Browse files Browse the repository at this point in the history
  • Loading branch information
aayush2622 committed Apr 17, 2024
1 parent cd3bb20 commit 3d1040b
Show file tree
Hide file tree
Showing 15 changed files with 121 additions and 73 deletions.
19 changes: 7 additions & 12 deletions app/src/main/java/ani/dantotsu/Functions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -306,23 +306,18 @@ fun Activity.reloadActivity() {
initActivity(this)
}

fun Context.restartApp(view: View) {
fun Activity.restartApp() {
val mainIntent = Intent.makeRestartActivityTask(
packageManager.getLaunchIntentForPackage(this.packageName)!!.component
)
val component = ComponentName(this@restartApp.packageName, this@restartApp::class.qualifiedName!!)
Snackbar.make(view, R.string.restart_app, Snackbar.LENGTH_INDEFINITE).apply {
setAction(R.string.do_it) {
this.dismiss()
try {
startActivity(Intent().setComponent(component))
} catch (anything: Exception) {
startActivity(mainIntent)
}
Runtime.getRuntime().exit(0)
}
show()
try {
startActivity(Intent().setComponent(component))
} catch (e: Exception) {
startActivity(mainIntent)
}
finishAndRemoveTask()
PrefManager.setCustomVal("reload", true)
}

open class BottomSheetDialogFragment : BottomSheetDialogFragment() {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/ani/dantotsu/settings/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ data class Settings(
val icon : Int,
val onClick: ((ItemSettingsBinding) -> Unit)? = null,
val onLongClick: (() -> Unit)? = null,
var isChecked : Boolean = false,
val switch: ((isChecked:Boolean , view: ItemSettingsSwitchBinding ) -> Unit)? = null,
val attach:((ItemSettingsBinding) -> Unit)? = null,
val isVisible: Boolean = true,
val isActivity: Boolean = false,
val attach:((ViewGroup) -> Unit)? = null
var isChecked : Boolean = false,
)
21 changes: 14 additions & 7 deletions app/src/main/java/ani/dantotsu/settings/SettingsAboutActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import ani.dantotsu.settings.saving.PrefManager
import ani.dantotsu.settings.saving.PrefName
import ani.dantotsu.statusBarHeight
import ani.dantotsu.themes.ThemeManager
import ani.dantotsu.util.Logger

class SettingsAboutActivity : AppCompatActivity() {
private lateinit var binding: ActivitySettingsAboutBinding
Expand All @@ -42,7 +43,7 @@ class SettingsAboutActivity : AppCompatActivity() {
Settings(
type = 1,
name = getString(R.string.faq),
desc = getString(R.string.faq),
desc = getString(R.string.faq_desc),
icon = R.drawable.ic_round_help_24,
onClick = {
startActivity(Intent(context, FAQActivity::class.java))
Expand All @@ -52,7 +53,7 @@ class SettingsAboutActivity : AppCompatActivity() {
Settings(
type = 2,
name = getString(R.string.check_app_updates),
desc = getString(R.string.check_app_updates),
desc = getString(R.string.check_app_updates_desc),
icon = R.drawable.ic_round_new_releases_24,
isChecked = PrefManager.getVal(PrefName.CheckUpdate),
switch = { isChecked, _ ->
Expand All @@ -63,7 +64,7 @@ class SettingsAboutActivity : AppCompatActivity() {
Settings(
type = 2,
name = getString(R.string.share_username_in_crash_reports),
desc = getString(R.string.share_username_in_crash_reports),
desc = getString(R.string.share_username_in_crash_reports_desc),
icon = R.drawable.ic_round_search_24,
isChecked = PrefManager.getVal(PrefName.SharedUserID),
switch = { isChecked, _ ->
Expand All @@ -79,13 +80,19 @@ class SettingsAboutActivity : AppCompatActivity() {
isChecked = PrefManager.getVal(PrefName.LogToFile),
switch = { isChecked, _ ->
PrefManager.setVal(PrefName.LogToFile, isChecked)
restartApp(binding.root)
restartApp()
},
attach = {
it.settingsDesc.setOnLongClickListener {
Logger.shareLog(context)
true
}
}
),
Settings(
type = 1,
name = getString(R.string.devs),
desc= getString(R.string.devs),
desc= getString(R.string.devs_desc),
icon = R.drawable.ic_round_accessible_forward_24,
onClick = {
DevelopersDialogFragment().show(supportFragmentManager, "dialog")
Expand All @@ -94,7 +101,7 @@ class SettingsAboutActivity : AppCompatActivity() {
Settings(
type = 1,
name = getString(R.string.forks),
desc = getString(R.string.forks),
desc = getString(R.string.forks_desc),
icon = R.drawable.ic_round_restaurant_24,
onClick = {
ForksDialogFragment().show(supportFragmentManager, "dialog")
Expand All @@ -103,7 +110,7 @@ class SettingsAboutActivity : AppCompatActivity() {
Settings(
type = 1,
name = getString(R.string.disclaimer),
desc = getString(R.string.disclaimer),
desc = getString(R.string.disclaimer_desc),
icon = R.drawable.ic_round_info_24,
onClick = {
val text = TextView(context)
Expand Down
18 changes: 14 additions & 4 deletions app/src/main/java/ani/dantotsu/settings/SettingsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.os.Build.VERSION.SDK_INT
import android.os.Bundle
import android.view.ViewGroup
import android.widget.TextView
import androidx.activity.addCallback
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.updateLayoutParams
Expand All @@ -28,6 +29,7 @@ import ani.dantotsu.others.AppUpdater
import ani.dantotsu.others.CustomBottomDialog
import ani.dantotsu.pop
import ani.dantotsu.setSafeOnClickListener
import ani.dantotsu.settings.saving.PrefManager
import ani.dantotsu.snackString
import ani.dantotsu.startMainActivity
import ani.dantotsu.statusBarHeight
Expand All @@ -40,9 +42,6 @@ import kotlin.random.Random


class SettingsActivity : AppCompatActivity() {
private val restartMainActivity = object : OnBackPressedCallback(false) {
override fun handleOnBackPressed() = startMainActivity(this@SettingsActivity)
}
lateinit var binding: ActivitySettingsBinding
private var cursedCounter = 0

Expand Down Expand Up @@ -70,7 +69,14 @@ class SettingsActivity : AppCompatActivity() {
bottomMargin = navBarHeight
}

onBackPressedDispatcher.addCallback(context, restartMainActivity)
onBackPressedDispatcher.addCallback(context){
if (PrefManager.getCustomVal("reload", false)) {
startMainActivity(context)
PrefManager.setCustomVal("reload", false)
}else{
finish()
}
}

settingsBack.setOnClickListener {
onBackPressedDispatcher.onBackPressed()
Expand Down Expand Up @@ -262,4 +268,8 @@ class SettingsActivity : AppCompatActivity() {
?: "Unknown Architecture"
}
}
override fun onResume() {
ThemeManager(this).applyTheme()
super.onResume()
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/ani/dantotsu/settings/SettingsAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class SettingsAdapter(private val settings: ArrayList<Settings>) :
b.settingsIconRight.visibility =
if (settings.isActivity) View.VISIBLE else View.GONE
b.attachView.visibility = if (settings.attach != null) View.VISIBLE else View.GONE
settings.attach?.invoke(b.attachView)
settings.attach?.invoke(b)
}

2 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class SettingsAnimeActivity: AppCompatActivity(){
isChecked = PrefManager.getVal(PrefName.IncludeAnimeList),
switch = { isChecked, _ ->
PrefManager.setVal(PrefName.IncludeAnimeList, isChecked)
restartApp(binding.root)
restartApp()
}
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ class SettingsCommonActivity: AppCompatActivity(){
toast(getString(R.string.incorrect_password))
return@passwordAlertDialog
}
if (PreferencePackager.unpack(decryptedJson)) restartApp(binding.root)
if (PreferencePackager.unpack(decryptedJson)) restartApp()
} else {
toast(getString(R.string.password_cannot_be_empty))
}
}
} else if (name.endsWith(".ani")) {
val decryptedJson = jsonString.toString(Charsets.UTF_8)
if (PreferencePackager.unpack(decryptedJson)) restartApp(binding.root)
if (PreferencePackager.unpack(decryptedJson)) restartApp()
} else {
toast(getString(R.string.unknown_file_type))
}
Expand Down Expand Up @@ -127,7 +127,7 @@ class SettingsCommonActivity: AppCompatActivity(){
settingsExtensionDns.setOnItemClickListener { _, _, i, _ ->
PrefManager.setVal(PrefName.DohProvider, i)
settingsExtensionDns.clearFocus()
restartApp(binding.root)
restartApp()
}

settingsRecyclerView.adapter = SettingsAdapter(
Expand Down Expand Up @@ -294,7 +294,7 @@ class SettingsCommonActivity: AppCompatActivity(){
isChecked = PrefManager.getVal(PrefName.AdultOnly),
switch = {isChecked, _ ->
PrefManager.setVal(PrefName.AdultOnly, isChecked)
restartApp(binding.root)
restartApp()
},
isVisible = Anilist.adult

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,12 @@ class SettingsExtensionsActivity: AppCompatActivity() {
desc = getString(R.string.anime_add_repository),
icon = R.drawable.ic_github,
onClick = {
val dialogView = layoutInflater.inflate(R.layout.dialog_user_agent, null)
val editText =
dialogView.findViewById<TextInputEditText>(R.id.userAgentTextBox).apply {
val dialogView = DialogUserAgentBinding.inflate(layoutInflater)
val editText = dialogView.userAgentTextBox.apply {
hint = getString(R.string.anime_add_repository)
}
val alertDialog = AlertDialog.Builder(context, R.style.MyPopup)
.setTitle(R.string.anime_add_repository).setView(dialogView)
.setTitle(R.string.anime_add_repository).setView(dialogView.root)
.setPositiveButton(getString(R.string.ok)) { dialog, _ ->
if (!editText.text.isNullOrBlank()) processUserInput(
editText.text.toString(),
Expand All @@ -183,8 +182,8 @@ class SettingsExtensionsActivity: AppCompatActivity() {
alertDialog.show()
alertDialog.window?.setDimAmount(0.8f)
},
attach = { view ->
setExtensionOutput(view, MediaType.ANIME)
attach = {
setExtensionOutput(it.attachView, MediaType.ANIME)
}
),
Settings(
Expand All @@ -193,13 +192,12 @@ class SettingsExtensionsActivity: AppCompatActivity() {
desc = getString(R.string.manga_add_repository),
icon = R.drawable.ic_github,
onClick = {
val dialogView = layoutInflater.inflate(R.layout.dialog_user_agent, null)
val editText =
dialogView.findViewById<TextInputEditText>(R.id.userAgentTextBox).apply {
val dialogView = DialogUserAgentBinding.inflate(layoutInflater)
val editText = dialogView.userAgentTextBox.apply {
hint = getString(R.string.manga_add_repository)
}
val alertDialog = AlertDialog.Builder(context, R.style.MyPopup)
.setTitle(R.string.manga_add_repository).setView(dialogView)
.setTitle(R.string.manga_add_repository).setView(dialogView.root)
.setPositiveButton(getString(R.string.ok)) { dialog, _ ->
if (!editText.text.isNullOrBlank()) processUserInput(
editText.text.toString(),
Expand All @@ -215,8 +213,8 @@ class SettingsExtensionsActivity: AppCompatActivity() {
alertDialog.show()
alertDialog.window?.setDimAmount(0.8f)
},
attach = { view ->
setExtensionOutput(view, MediaType.MANGA)
attach = {
setExtensionOutput(it.attachView, MediaType.MANGA)
}
),
Settings(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class SettingsMangaActivity: AppCompatActivity(){
isChecked = PrefManager.getVal(PrefName.IncludeMangaList),
switch = {isChecked, _ ->
PrefManager.setVal(PrefName.IncludeMangaList, isChecked)
restartApp(binding.root)
restartApp()
}
),
)
Expand Down
36 changes: 28 additions & 8 deletions app/src/main/java/ani/dantotsu/settings/SettingsThemeActivity.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package ani.dantotsu.settings

import android.content.ComponentName
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import androidx.activity.addCallback
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.updateLayoutParams
import androidx.recyclerview.widget.LinearLayoutManager
Expand Down Expand Up @@ -37,8 +40,21 @@ class SettingsThemeActivity : AppCompatActivity(), SimpleDialog.OnDialogResultLi
topMargin = statusBarHeight
bottomMargin = navBarHeight
}
themeSettingsBack.setOnClickListener { onBackPressedDispatcher.onBackPressed() }

onBackPressedDispatcher.addCallback(context) {
val mainIntent = Intent.makeRestartActivityTask(
packageManager.getLaunchIntentForPackage(packageName)!!.component
)
val component = ComponentName(packageName, SettingsActivity::class.qualifiedName!!)
try {
startActivity(Intent().setComponent(component))
} catch (anything: Exception) {
startActivity(mainIntent)
}
finishAndRemoveTask()
}
themeSettingsBack.setOnClickListener {
onBackPressedDispatcher.onBackPressed()
}
var previous: View = when (PrefManager.getVal<Int>(PrefName.DarkMode)) {
0 -> settingsUiAuto
1 -> settingsUiLight
Expand Down Expand Up @@ -87,7 +103,7 @@ class SettingsThemeActivity : AppCompatActivity(), SimpleDialog.OnDialogResultLi
ThemeManager.Companion.Theme.entries[i].theme
)
clearFocus()
restartApp(binding.root)
reload()
}
}

Expand All @@ -101,7 +117,7 @@ class SettingsThemeActivity : AppCompatActivity(), SimpleDialog.OnDialogResultLi
isChecked = PrefManager.getVal(PrefName.UseOLED),
switch = { isChecked, _ ->
PrefManager.setVal(PrefName.UseOLED, isChecked)
restartApp(binding.root)
reload()
}
),
Settings(
Expand All @@ -113,7 +129,7 @@ class SettingsThemeActivity : AppCompatActivity(), SimpleDialog.OnDialogResultLi
switch = { isChecked, _ ->
PrefManager.setVal(PrefName.UseMaterialYou, isChecked)
if (isChecked) PrefManager.setVal(PrefName.UseCustomTheme, false)
restartApp(binding.root)
reload()
},
isVisible = Build.VERSION.SDK_INT > Build.VERSION_CODES.R
),
Expand All @@ -125,7 +141,6 @@ class SettingsThemeActivity : AppCompatActivity(), SimpleDialog.OnDialogResultLi
isChecked = PrefManager.getVal(PrefName.UseSourceTheme),
switch = { isChecked, _ ->
PrefManager.setVal(PrefName.UseSourceTheme, isChecked)
restartApp(binding.root)
},
isVisible = Build.VERSION.SDK_INT > Build.VERSION_CODES.R
),
Expand All @@ -138,7 +153,7 @@ class SettingsThemeActivity : AppCompatActivity(), SimpleDialog.OnDialogResultLi
switch = { isChecked, _ ->
PrefManager.setVal(PrefName.UseCustomTheme, isChecked)
if (isChecked) PrefManager.setVal(PrefName.UseMaterialYou, false)
restartApp(binding.root)
reload()
},
isVisible = Build.VERSION.SDK_INT > Build.VERSION_CODES.R
),
Expand All @@ -151,7 +166,7 @@ class SettingsThemeActivity : AppCompatActivity(), SimpleDialog.OnDialogResultLi
val originalColor: Int = PrefManager.getVal(PrefName.CustomThemeInt)
class CustomColorDialog : SimpleColorDialog() {
override fun onPositiveButtonClick() {
restartApp(binding.root)
reload()
super.onPositiveButtonClick()
}
}
Expand Down Expand Up @@ -183,4 +198,9 @@ class SettingsThemeActivity : AppCompatActivity(), SimpleDialog.OnDialogResultLi
}
return true
}
fun reload(){
PrefManager.setCustomVal("reload", true)
restartApp()
}

}
Loading

0 comments on commit 3d1040b

Please sign in to comment.