Fix crash of settings activity after resume
This commit is contained in:
parent
5019545d10
commit
97f1f5eb90
3 changed files with 24 additions and 10 deletions
|
@ -1,5 +1,7 @@
|
||||||
package io.heckel.ntfy.data
|
package io.heckel.ntfy.data
|
||||||
|
|
||||||
|
import android.app.Activity
|
||||||
|
import android.content.Context
|
||||||
import android.content.SharedPreferences
|
import android.content.SharedPreferences
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
@ -330,6 +332,12 @@ class Repository(private val sharedPrefs: SharedPreferences, private val subscri
|
||||||
private const val TAG = "NtfyRepository"
|
private const val TAG = "NtfyRepository"
|
||||||
private var instance: Repository? = null
|
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 {
|
fun getInstance(sharedPrefs: SharedPreferences, subscriptionDao: SubscriptionDao, notificationDao: NotificationDao): Repository {
|
||||||
return synchronized(Repository::class) {
|
return synchronized(Repository::class) {
|
||||||
val newInstance = instance ?: Repository(sharedPrefs, subscriptionDao, notificationDao)
|
val newInstance = instance ?: Repository(sharedPrefs, subscriptionDao, notificationDao)
|
||||||
|
|
|
@ -51,10 +51,8 @@ class AddFragment : DialogFragment() {
|
||||||
throw IllegalStateException("Activity cannot be null")
|
throw IllegalStateException("Activity cannot be null")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dependencies
|
// Dependencies (Fragments need a default constructor)
|
||||||
val database = Database.getInstance(requireActivity().applicationContext)
|
repository = Repository.getInstance(requireActivity())
|
||||||
val sharedPrefs = requireActivity().getSharedPreferences(Repository.SHARED_PREFS_ID, Context.MODE_PRIVATE)
|
|
||||||
repository = Repository.getInstance(sharedPrefs, database.subscriptionDao(), database.notificationDao())
|
|
||||||
|
|
||||||
// Build root view
|
// Build root view
|
||||||
val view = requireActivity().layoutInflater.inflate(R.layout.fragment_add_dialog, null)
|
val view = requireActivity().layoutInflater.inflate(R.layout.fragment_add_dialog, null)
|
||||||
|
|
|
@ -17,6 +17,7 @@ import androidx.preference.Preference.OnPreferenceClickListener
|
||||||
import io.heckel.ntfy.BuildConfig
|
import io.heckel.ntfy.BuildConfig
|
||||||
import io.heckel.ntfy.R
|
import io.heckel.ntfy.R
|
||||||
import io.heckel.ntfy.app.Application
|
import io.heckel.ntfy.app.Application
|
||||||
|
import io.heckel.ntfy.data.Database
|
||||||
import io.heckel.ntfy.data.Repository
|
import io.heckel.ntfy.data.Repository
|
||||||
import io.heckel.ntfy.service.SubscriberService
|
import io.heckel.ntfy.service.SubscriberService
|
||||||
import io.heckel.ntfy.util.formatBytes
|
import io.heckel.ntfy.util.formatBytes
|
||||||
|
@ -24,7 +25,6 @@ import io.heckel.ntfy.util.formatDateShort
|
||||||
import io.heckel.ntfy.util.toPriorityString
|
import io.heckel.ntfy.util.toPriorityString
|
||||||
|
|
||||||
class SettingsActivity : AppCompatActivity() {
|
class SettingsActivity : AppCompatActivity() {
|
||||||
private val repository by lazy { (application as Application).repository }
|
|
||||||
private lateinit var fragment: SettingsFragment
|
private lateinit var fragment: SettingsFragment
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
@ -34,7 +34,7 @@ class SettingsActivity : AppCompatActivity() {
|
||||||
Log.d(TAG, "Create $this")
|
Log.d(TAG, "Create $this")
|
||||||
|
|
||||||
if (savedInstanceState == null) {
|
if (savedInstanceState == null) {
|
||||||
fragment = SettingsFragment(repository, supportFragmentManager)
|
fragment = SettingsFragment(supportFragmentManager)
|
||||||
supportFragmentManager
|
supportFragmentManager
|
||||||
.beginTransaction()
|
.beginTransaction()
|
||||||
.replace(R.id.settings_layout, fragment)
|
.replace(R.id.settings_layout, fragment)
|
||||||
|
@ -48,12 +48,17 @@ class SettingsActivity : AppCompatActivity() {
|
||||||
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
class SettingsFragment(val repository: Repository, private val supportFragmentManager: FragmentManager) : PreferenceFragmentCompat() {
|
class SettingsFragment(private val supportFragmentManager: FragmentManager) : PreferenceFragmentCompat() {
|
||||||
private var autoDownloadSelection = repository.getAutoDownloadMaxSize() // Only used for <= Android P, due to permissions request
|
private lateinit var repository: Repository
|
||||||
|
private var autoDownloadSelection = AUTO_DOWNLOAD_SELECTION_NOT_SET
|
||||||
|
|
||||||
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
||||||
setPreferencesFromResource(R.xml.main_preferences, rootKey)
|
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
|
// 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
|
// preferenceDataStore is overridden to use the repository. This is convenient, because
|
||||||
// everybody has access to the repository.
|
// everybody has access to the repository.
|
||||||
|
@ -253,10 +258,12 @@ class SettingsActivity : AppCompatActivity() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setAutoDownload() {
|
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 autoDownloadPrefId = context?.getString(R.string.settings_notifications_auto_download_key) ?: return
|
||||||
val autoDownload: ListPreference? = findPreference(autoDownloadPrefId)
|
val autoDownload: ListPreference? = findPreference(autoDownloadPrefId)
|
||||||
autoDownload?.value = autoDownloadSelection.toString()
|
autoDownload?.value = autoDownloadSelectionCopy.toString()
|
||||||
repository.setAutoDownloadMaxSize(autoDownloadSelection)
|
repository.setAutoDownloadMaxSize(autoDownloadSelectionCopy)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -277,5 +284,6 @@ class SettingsActivity : AppCompatActivity() {
|
||||||
companion object {
|
companion object {
|
||||||
private const val TAG = "NtfySettingsActivity"
|
private const val TAG = "NtfySettingsActivity"
|
||||||
private const val REQUEST_CODE_WRITE_EXTERNAL_STORAGE_PERMISSION_FOR_AUTO_DOWNLOAD = 2586
|
private const val REQUEST_CODE_WRITE_EXTERNAL_STORAGE_PERMISSION_FOR_AUTO_DOWNLOAD = 2586
|
||||||
|
private const val AUTO_DOWNLOAD_SELECTION_NOT_SET = -99L
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue