Skip to content

Commit

Permalink
fix: fragment usage
Browse files Browse the repository at this point in the history
  • Loading branch information
arm64v8a committed May 15, 2023
1 parent 0f2940d commit 4cc88e5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,6 @@ class GroupSettingsActivity(
}
}

fun PreferenceFragmentCompat.viewCreated(view: View, savedInstanceState: Bundle?) {
}

fun PreferenceFragmentCompat.displayPreferenceDialog(preference: Preference): Boolean {
return false
}

class UnsavedChangesDialogFragment : AlertDialogFragment<Empty, Empty>() {
override fun AlertDialog.Builder.prepare(listener: DialogInterface.OnClickListener) {
setTitle(R.string.unsaved_changes_prompt)
Expand Down Expand Up @@ -227,9 +220,7 @@ class GroupSettingsActivity(

onMainDispatcher {
supportFragmentManager.beginTransaction()
.replace(R.id.settings, MyPreferenceFragmentCompat().apply {
activity = this@GroupSettingsActivity
})
.replace(R.id.settings, MyPreferenceFragmentCompat())
.commit()

DataStore.dirty = false
Expand Down Expand Up @@ -293,12 +284,12 @@ class GroupSettingsActivity(

class MyPreferenceFragmentCompat : PreferenceFragmentCompat() {

lateinit var activity: GroupSettingsActivity
var activity: GroupSettingsActivity? = null

override fun onCreatePreferencesFix(savedInstanceState: Bundle?, rootKey: String?) {
preferenceManager.preferenceDataStore = DataStore.profileCacheStore
try {
activity.apply {
activity = (requireActivity() as GroupSettingsActivity).apply {
createPreferences(savedInstanceState, rootKey)
}
} catch (e: Exception) {
Expand All @@ -315,10 +306,6 @@ class GroupSettingsActivity(
super.onViewCreated(view, savedInstanceState)

ViewCompat.setOnApplyWindowInsetsListener(listView, ListListener)

activity.apply {
viewCreated(view, savedInstanceState)
}
}

override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
Expand All @@ -336,21 +323,14 @@ class GroupSettingsActivity(

R.id.action_apply -> {
runOnDefaultDispatcher {
activity.saveAndExit()
activity?.saveAndExit()
}
true
}

else -> false
}

override fun onDisplayPreferenceDialog(preference: Preference) {
activity.apply {
if (displayPreferenceDialog(preference)) return
}
super.onDisplayPreferenceDialog(preference)
}

}

object PasswordSummaryProvider : Preference.SummaryProvider<EditTextPreference> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,7 @@ class RouteSettingsActivity(

onMainDispatcher {
supportFragmentManager.beginTransaction()
.replace(R.id.settings, MyPreferenceFragmentCompat().apply {
activity = this@RouteSettingsActivity
})
.replace(R.id.settings, MyPreferenceFragmentCompat())
.commit()

DataStore.dirty = false
Expand Down Expand Up @@ -354,12 +352,12 @@ class RouteSettingsActivity(

class MyPreferenceFragmentCompat : PreferenceFragmentCompat() {

lateinit var activity: RouteSettingsActivity
var activity: RouteSettingsActivity? = null

override fun onCreatePreferencesFix(savedInstanceState: Bundle?, rootKey: String?) {
preferenceManager.preferenceDataStore = DataStore.profileCacheStore
try {
activity.apply {
activity = (requireActivity() as RouteSettingsActivity).apply {
createPreferences(savedInstanceState, rootKey)
}
} catch (e: Exception) {
Expand All @@ -377,7 +375,7 @@ class RouteSettingsActivity(

ViewCompat.setOnApplyWindowInsetsListener(listView, ListListener)

activity.apply {
activity?.apply {
viewCreated(view, savedInstanceState)
}
}
Expand All @@ -394,17 +392,19 @@ class RouteSettingsActivity(
}
true
}

R.id.action_apply -> {
runOnDefaultDispatcher {
activity.saveAndExit()
activity?.saveAndExit()
}
true
}

else -> false
}

override fun onDisplayPreferenceDialog(preference: Preference) {
activity.apply {
activity?.apply {
if (displayPreferenceDialog(preference)) return
}
super.onDisplayPreferenceDialog(preference)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,7 @@ abstract class ProfileSettingsActivity<T : AbstractBean>(

onMainDispatcher {
supportFragmentManager.beginTransaction()
.replace(R.id.settings, MyPreferenceFragmentCompat().apply {
activity = this@ProfileSettingsActivity
})
.replace(R.id.settings, MyPreferenceFragmentCompat())
.commit()
}
}
Expand Down Expand Up @@ -224,12 +222,12 @@ abstract class ProfileSettingsActivity<T : AbstractBean>(

class MyPreferenceFragmentCompat : PreferenceFragmentCompat() {

lateinit var activity: ProfileSettingsActivity<*>
var activity: ProfileSettingsActivity<*>? = null

override fun onCreatePreferencesFix(savedInstanceState: Bundle?, rootKey: String?) {
preferenceManager.preferenceDataStore = DataStore.profileCacheStore
try {
activity.apply {
activity = (requireActivity() as ProfileSettingsActivity<*>).apply {
createPreferences(savedInstanceState, rootKey)
}
} catch (e: Exception) {
Expand All @@ -247,12 +245,11 @@ abstract class ProfileSettingsActivity<T : AbstractBean>(

ViewCompat.setOnApplyWindowInsetsListener(listView, ListListener)

activity.apply {
activity?.apply {
viewCreated(view, savedInstanceState)
DataStore.dirty = false
DataStore.profileCacheStore.registerChangeListener(this)
}

DataStore.dirty = false
DataStore.profileCacheStore.registerChangeListener(activity)
}

override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
Expand All @@ -274,13 +271,13 @@ abstract class ProfileSettingsActivity<T : AbstractBean>(

R.id.action_apply -> {
runOnDefaultDispatcher {
activity.saveAndExit()
activity?.saveAndExit()
}
true
}

R.id.action_create_shortcut -> {
val ctx = requireContext()
val activity = requireActivity() as ProfileSettingsActivity<*>
val ent = activity.proxyEntity!!
val shortcut =
ShortcutInfoCompat.Builder(requireContext(), "shortcut-profile-${ent.id}")
Expand All @@ -305,6 +302,7 @@ abstract class ProfileSettingsActivity<T : AbstractBean>(
}

R.id.action_move -> {
val activity = requireActivity() as ProfileSettingsActivity<*>
val view = LinearLayout(context).apply {
val ent = activity.proxyEntity!!
orientation = LinearLayout.VERTICAL
Expand Down Expand Up @@ -345,7 +343,7 @@ abstract class ProfileSettingsActivity<T : AbstractBean>(
}

override fun onDisplayPreferenceDialog(preference: Preference) {
activity.apply {
activity?.apply {
if (displayPreferenceDialog(preference)) return
}
super.onDisplayPreferenceDialog(preference)
Expand Down

0 comments on commit 4cc88e5

Please sign in to comment.