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() .fillMaxSize()
.padding(contentPadding), .padding(contentPadding),
) { ) {
val fragment = SourcePreferencesFragment.getInstance(sourceId) add(it, SourcePreferencesFragment.getInstance(sourceId), null)
add(it, fragment, null)
} }
} }
} }
@ -127,12 +126,13 @@ class SourcePreferencesFragment : PreferenceFragmentCompat() {
private fun populateScreen(): PreferenceScreen { private fun populateScreen(): PreferenceScreen {
val sourceId = requireArguments().getLong(SOURCE_ID) 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()) val dataStore = SharedPreferencesDataStore(source.sourcePreferences())
preferenceManager.preferenceDataStore = dataStore preferenceManager.preferenceDataStore = dataStore
val sourceScreen = preferenceManager.createPreferenceScreen(requireContext())
source.setupPreferenceScreen(sourceScreen) source.setupPreferenceScreen(sourceScreen)
sourceScreen.forEach { pref -> sourceScreen.forEach { pref ->
pref.isIconSpaceReserved = false pref.isIconSpaceReserved = false
@ -150,6 +150,7 @@ class SourcePreferencesFragment : PreferenceFragmentCompat() {
} }
} }
} }
}
return sourceScreen return sourceScreen
} }
@ -158,9 +159,9 @@ class SourcePreferencesFragment : PreferenceFragmentCompat() {
private const val SOURCE_ID = "source_id" private const val SOURCE_ID = "source_id"
fun getInstance(sourceId: Long): SourcePreferencesFragment { fun getInstance(sourceId: Long): SourcePreferencesFragment {
val fragment = SourcePreferencesFragment() return SourcePreferencesFragment().apply {
fragment.arguments = bundleOf(SOURCE_ID to sourceId) arguments = bundleOf(SOURCE_ID to sourceId)
return fragment }
} }
} }
} }