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,26 +126,28 @@ 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 dataStore = SharedPreferencesDataStore(source.sourcePreferences())
preferenceManager.preferenceDataStore = dataStore
val sourceScreen = preferenceManager.createPreferenceScreen(requireContext()) 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 (source is ConfigurableSource) {
if (pref is EditTextPreference) { val dataStore = SharedPreferencesDataStore(source.sourcePreferences())
val setListener = pref.getOnBindEditTextListener() preferenceManager.preferenceDataStore = dataStore
pref.setOnBindEditTextListener {
setListener?.onBindEditText(it) source.setupPreferenceScreen(sourceScreen)
it.setIncognito(lifecycleScope) 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" 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 }
} }
} }
} }