Avoid crashing in SourcePreferencesFragment if source can't be loaded

Should probably wait for sources to definitely be loaded first, but that's
sort of a bigger change and needs to be lifecycle-aware.
This commit is contained in:
arkon 2023-12-16 10:16:05 -05:00
parent add9357257
commit e36a2c68f1

View File

@ -65,8 +65,7 @@ class SourcePreferencesScreen(val sourceId: Long) : Screen() {
.fillMaxSize()
.padding(contentPadding),
) {
val fragment = SourcePreferencesFragment.getInstance(sourceId)
add(it, fragment, null)
add(it, SourcePreferencesFragment.getInstance(sourceId), null)
}
}
}
@ -127,26 +126,28 @@ class SourcePreferencesFragment : PreferenceFragmentCompat() {
private fun populateScreen(): PreferenceScreen {
val sourceId = requireArguments().getLong(SOURCE_ID)
val source = Injekt.get<SourceManager>().get(sourceId)!! as ConfigurableSource
val dataStore = SharedPreferencesDataStore(source.sourcePreferences())
preferenceManager.preferenceDataStore = dataStore
val source = Injekt.get<SourceManager>().getOrStub(sourceId)
val sourceScreen = preferenceManager.createPreferenceScreen(requireContext())
source.setupPreferenceScreen(sourceScreen)
sourceScreen.forEach { pref ->
pref.isIconSpaceReserved = false
pref.isSingleLineTitle = false
if (pref is DialogPreference && pref.dialogTitle.isNullOrEmpty()) {
pref.dialogTitle = pref.title
}
// Apply incognito IME for EditTextPreference
if (pref is EditTextPreference) {
val setListener = pref.getOnBindEditTextListener()
pref.setOnBindEditTextListener {
setListener?.onBindEditText(it)
it.setIncognito(lifecycleScope)
if (source is ConfigurableSource) {
val dataStore = SharedPreferencesDataStore(source.sourcePreferences())
preferenceManager.preferenceDataStore = dataStore
source.setupPreferenceScreen(sourceScreen)
sourceScreen.forEach { pref ->
pref.isIconSpaceReserved = false
pref.isSingleLineTitle = false
if (pref is DialogPreference && pref.dialogTitle.isNullOrEmpty()) {
pref.dialogTitle = pref.title
}
// Apply incognito IME for EditTextPreference
if (pref is EditTextPreference) {
val setListener = pref.getOnBindEditTextListener()
pref.setOnBindEditTextListener {
setListener?.onBindEditText(it)
it.setIncognito(lifecycleScope)
}
}
}
}
@ -158,9 +159,9 @@ class SourcePreferencesFragment : PreferenceFragmentCompat() {
private const val SOURCE_ID = "source_id"
fun getInstance(sourceId: Long): SourcePreferencesFragment {
val fragment = SourcePreferencesFragment()
fragment.arguments = bundleOf(SOURCE_ID to sourceId)
return fragment
return SourcePreferencesFragment().apply {
arguments = bundleOf(SOURCE_ID to sourceId)
}
}
}
}