From 877a018cedddddd3887d8e6a074e3d18acda3205 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Sun, 31 Dec 2023 10:28:46 +0200 Subject: [PATCH] Fix crashes --- .../org/koitharu/kotatsu/core/prefs/AppSettings.kt | 6 +++--- .../kotatsu/core/ui/BasePreferenceFragment.kt | 12 ++++++++++++ .../kotatsu/settings/ServicesSettingsFragment.kt | 6 +----- .../kotatsu/settings/about/AboutSettingsFragment.kt | 2 +- .../settings/tracker/TrackerSettingsFragment.kt | 4 ++-- 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/AppSettings.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/AppSettings.kt index 01d109aa5..caeccc694 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/AppSettings.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/AppSettings.kt @@ -295,13 +295,13 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) { get() = prefs.getBoolean(KEY_READER_SCREEN_ON, true) var readerColorFilter: ReaderColorFilter? - get() { + get() = runCatching { val brightness = prefs.getFloat(KEY_CF_BRIGHTNESS, ReaderColorFilter.EMPTY.brightness) val contrast = prefs.getFloat(KEY_CF_CONTRAST, ReaderColorFilter.EMPTY.contrast) val inverted = prefs.getBoolean(KEY_CF_INVERTED, ReaderColorFilter.EMPTY.isInverted) val grayscale = prefs.getBoolean(KEY_CF_GRAYSCALE, ReaderColorFilter.EMPTY.isGrayscale) - return ReaderColorFilter(brightness, contrast, inverted, grayscale).takeUnless { it.isEmpty } - } + ReaderColorFilter(brightness, contrast, inverted, grayscale).takeUnless { it.isEmpty } + }.getOrNull() set(value) { prefs.edit { val cf = value ?: ReaderColorFilter.EMPTY diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/BasePreferenceFragment.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/BasePreferenceFragment.kt index 331802824..aabadc67a 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/BasePreferenceFragment.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/BasePreferenceFragment.kt @@ -1,5 +1,7 @@ package org.koitharu.kotatsu.core.ui +import android.content.ActivityNotFoundException +import android.content.Intent import android.os.Bundle import android.view.View import androidx.annotation.CallSuper @@ -8,7 +10,9 @@ import androidx.core.graphics.Insets import androidx.core.view.updatePadding import androidx.preference.PreferenceFragmentCompat import androidx.recyclerview.widget.RecyclerView +import com.google.android.material.snackbar.Snackbar import dagger.hilt.android.AndroidEntryPoint +import org.koitharu.kotatsu.R import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.ui.util.RecyclerViewOwner import org.koitharu.kotatsu.core.ui.util.WindowInsetsDelegate @@ -62,4 +66,12 @@ abstract class BasePreferenceFragment(@StringRes private val titleId: Int) : protected fun setTitle(title: CharSequence?) { (activity as? SettingsActivity)?.setSectionTitle(title) } + + protected fun startActivitySafe(intent: Intent) { + try { + startActivity(intent) + } catch (_: ActivityNotFoundException) { + Snackbar.make(listView, R.string.operation_not_supported, Snackbar.LENGTH_SHORT).show() + } + } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/ServicesSettingsFragment.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/ServicesSettingsFragment.kt index 74e04a2a2..fe549eca7 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/ServicesSettingsFragment.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/ServicesSettingsFragment.kt @@ -110,11 +110,7 @@ class ServicesSettingsFragment : BasePreferenceFragment(R.string.services), if (account == null) { am.addAccount(accountType, accountType, null, null, requireActivity(), null, null) } else { - try { - startActivity(SyncSettingsIntent(account)) - } catch (_: ActivityNotFoundException) { - Snackbar.make(listView, R.string.operation_not_supported, Snackbar.LENGTH_SHORT).show() - } + startActivitySafe(SyncSettingsIntent(account)) } true } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/about/AboutSettingsFragment.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/about/AboutSettingsFragment.kt index d4eb912cf..fb2abd9cc 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/about/AboutSettingsFragment.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/about/AboutSettingsFragment.kt @@ -83,7 +83,7 @@ class AboutSettingsFragment : BasePreferenceFragment(R.string.about) { private fun openLink(url: String, title: CharSequence?) { val intent = Intent(Intent.ACTION_VIEW) intent.data = url.toUri() - startActivity( + startActivitySafe( if (title != null) { Intent.createChooser(intent, title) } else { diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/tracker/TrackerSettingsFragment.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/tracker/TrackerSettingsFragment.kt index 3875156a6..c0ccbc413 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/tracker/TrackerSettingsFragment.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/tracker/TrackerSettingsFragment.kt @@ -84,14 +84,14 @@ class TrackerSettingsFragment : Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> { val intent = Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS) .putExtra(Settings.EXTRA_APP_PACKAGE, requireContext().packageName) - startActivity(intent) + startActivitySafe(intent) true } channels.areNotificationsDisabled -> { val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) .setData(Uri.fromParts("package", requireContext().packageName, null)) - startActivity(intent) + startActivitySafe(intent) true }