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,12 +126,13 @@ class SourcePreferencesFragment : PreferenceFragmentCompat() {
private fun populateScreen(): PreferenceScreen {
val sourceId = requireArguments().getLong(SOURCE_ID)
val source = Injekt.get<SourceManager>().get(sourceId)!! as ConfigurableSource
val source = Injekt.get<SourceManager>().getOrStub(sourceId)
val sourceScreen = preferenceManager.createPreferenceScreen(requireContext())
if (source is ConfigurableSource) {
val dataStore = SharedPreferencesDataStore(source.sourcePreferences())
preferenceManager.preferenceDataStore = dataStore
val sourceScreen = preferenceManager.createPreferenceScreen(requireContext())
source.setupPreferenceScreen(sourceScreen)
sourceScreen.forEach { pref ->
pref.isIconSpaceReserved = false
@ -150,6 +150,7 @@ class SourcePreferencesFragment : PreferenceFragmentCompat() {
}
}
}
}
return sourceScreen
}
@ -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)
}
}
}
}