From 97f1f5eb9029a0d8d61d28cb31c6bd0655566b55 Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Fri, 14 Jan 2022 13:58:40 -0500 Subject: [PATCH] Fix crash of settings activity after resume --- .../java/io/heckel/ntfy/data/Repository.kt | 8 ++++++++ .../java/io/heckel/ntfy/ui/AddFragment.kt | 6 ++---- .../io/heckel/ntfy/ui/SettingsActivity.kt | 20 +++++++++++++------ 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/io/heckel/ntfy/data/Repository.kt b/app/src/main/java/io/heckel/ntfy/data/Repository.kt index 43d7cd1..5198e3a 100644 --- a/app/src/main/java/io/heckel/ntfy/data/Repository.kt +++ b/app/src/main/java/io/heckel/ntfy/data/Repository.kt @@ -1,5 +1,7 @@ package io.heckel.ntfy.data +import android.app.Activity +import android.content.Context import android.content.SharedPreferences import android.os.Build import android.util.Log @@ -330,6 +332,12 @@ class Repository(private val sharedPrefs: SharedPreferences, private val subscri private const val TAG = "NtfyRepository" private var instance: Repository? = null + fun getInstance(activity: Activity): Repository { + val database = Database.getInstance(activity.applicationContext) + val sharedPrefs = activity.getSharedPreferences(SHARED_PREFS_ID, Context.MODE_PRIVATE) + return getInstance(sharedPrefs, database.subscriptionDao(), database.notificationDao()) + } + fun getInstance(sharedPrefs: SharedPreferences, subscriptionDao: SubscriptionDao, notificationDao: NotificationDao): Repository { return synchronized(Repository::class) { val newInstance = instance ?: Repository(sharedPrefs, subscriptionDao, notificationDao) diff --git a/app/src/main/java/io/heckel/ntfy/ui/AddFragment.kt b/app/src/main/java/io/heckel/ntfy/ui/AddFragment.kt index a280c2d..1caea77 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/AddFragment.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/AddFragment.kt @@ -51,10 +51,8 @@ class AddFragment : DialogFragment() { throw IllegalStateException("Activity cannot be null") } - // Dependencies - val database = Database.getInstance(requireActivity().applicationContext) - val sharedPrefs = requireActivity().getSharedPreferences(Repository.SHARED_PREFS_ID, Context.MODE_PRIVATE) - repository = Repository.getInstance(sharedPrefs, database.subscriptionDao(), database.notificationDao()) + // Dependencies (Fragments need a default constructor) + repository = Repository.getInstance(requireActivity()) // Build root view val view = requireActivity().layoutInflater.inflate(R.layout.fragment_add_dialog, null) diff --git a/app/src/main/java/io/heckel/ntfy/ui/SettingsActivity.kt b/app/src/main/java/io/heckel/ntfy/ui/SettingsActivity.kt index f0f1cb4..da7c27c 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/SettingsActivity.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/SettingsActivity.kt @@ -17,6 +17,7 @@ import androidx.preference.Preference.OnPreferenceClickListener import io.heckel.ntfy.BuildConfig import io.heckel.ntfy.R import io.heckel.ntfy.app.Application +import io.heckel.ntfy.data.Database import io.heckel.ntfy.data.Repository import io.heckel.ntfy.service.SubscriberService import io.heckel.ntfy.util.formatBytes @@ -24,7 +25,6 @@ import io.heckel.ntfy.util.formatDateShort import io.heckel.ntfy.util.toPriorityString class SettingsActivity : AppCompatActivity() { - private val repository by lazy { (application as Application).repository } private lateinit var fragment: SettingsFragment override fun onCreate(savedInstanceState: Bundle?) { @@ -34,7 +34,7 @@ class SettingsActivity : AppCompatActivity() { Log.d(TAG, "Create $this") if (savedInstanceState == null) { - fragment = SettingsFragment(repository, supportFragmentManager) + fragment = SettingsFragment(supportFragmentManager) supportFragmentManager .beginTransaction() .replace(R.id.settings_layout, fragment) @@ -48,12 +48,17 @@ class SettingsActivity : AppCompatActivity() { supportActionBar?.setDisplayHomeAsUpEnabled(true) } - class SettingsFragment(val repository: Repository, private val supportFragmentManager: FragmentManager) : PreferenceFragmentCompat() { - private var autoDownloadSelection = repository.getAutoDownloadMaxSize() // Only used for <= Android P, due to permissions request + class SettingsFragment(private val supportFragmentManager: FragmentManager) : PreferenceFragmentCompat() { + private lateinit var repository: Repository + private var autoDownloadSelection = AUTO_DOWNLOAD_SELECTION_NOT_SET override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { setPreferencesFromResource(R.xml.main_preferences, rootKey) + // Dependencies (Fragments need a default constructor) + repository = Repository.getInstance(requireActivity()) + autoDownloadSelection = repository.getAutoDownloadMaxSize() // Only used for <= Android P, due to permissions request + // Important note: We do not use the default shared prefs to store settings. Every // preferenceDataStore is overridden to use the repository. This is convenient, because // everybody has access to the repository. @@ -253,10 +258,12 @@ class SettingsActivity : AppCompatActivity() { } fun setAutoDownload() { + val autoDownloadSelectionCopy = autoDownloadSelection + if (autoDownloadSelectionCopy == AUTO_DOWNLOAD_SELECTION_NOT_SET) return val autoDownloadPrefId = context?.getString(R.string.settings_notifications_auto_download_key) ?: return val autoDownload: ListPreference? = findPreference(autoDownloadPrefId) - autoDownload?.value = autoDownloadSelection.toString() - repository.setAutoDownloadMaxSize(autoDownloadSelection) + autoDownload?.value = autoDownloadSelectionCopy.toString() + repository.setAutoDownloadMaxSize(autoDownloadSelectionCopy) } } @@ -277,5 +284,6 @@ class SettingsActivity : AppCompatActivity() { companion object { private const val TAG = "NtfySettingsActivity" private const val REQUEST_CODE_WRITE_EXTERNAL_STORAGE_PERMISSION_FOR_AUTO_DOWNLOAD = 2586 + private const val AUTO_DOWNLOAD_SELECTION_NOT_SET = -99L } }