fix: fragment usage

This commit is contained in:
arm64v8a 2023-05-04 14:48:34 +09:00
parent f5385e1d09
commit 908002530b
3 changed files with 33 additions and 46 deletions

View File

@ -29,7 +29,6 @@ import io.nekohasekai.sagernet.ktx.onMainDispatcher
import io.nekohasekai.sagernet.ktx.runOnDefaultDispatcher
import io.nekohasekai.sagernet.widget.ListListener
import io.nekohasekai.sagernet.widget.OutboundPreference
import io.nekohasekai.sagernet.widget.UserAgentPreference
import kotlinx.parcelize.Parcelize
import moe.matsuri.nb4a.ui.SimpleMenuPreference
@ -163,13 +162,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)
@ -233,9 +225,7 @@ class GroupSettingsActivity(
onMainDispatcher {
supportFragmentManager.beginTransaction()
.replace(R.id.settings, MyPreferenceFragmentCompat().apply {
activity = this@GroupSettingsActivity
})
.replace(R.id.settings, MyPreferenceFragmentCompat())
.commit()
DataStore.dirty = false
@ -299,12 +289,12 @@ class GroupSettingsActivity(
class MyPreferenceFragmentCompat : PreferenceFragmentCompat() {
lateinit var activity: GroupSettingsActivity
var activity: GroupSettingsActivity? = null
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
preferenceManager.preferenceDataStore = DataStore.profileCacheStore
try {
activity.apply {
activity = (requireActivity() as GroupSettingsActivity).apply {
createPreferences(savedInstanceState, rootKey)
}
} catch (e: Exception) {
@ -321,10 +311,6 @@ class GroupSettingsActivity(
super.onViewCreated(view, savedInstanceState)
ViewCompat.setOnApplyWindowInsetsListener(listView, ListListener)
activity.apply {
viewCreated(view, savedInstanceState)
}
}
override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
@ -339,20 +325,15 @@ class GroupSettingsActivity(
}
true
}
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)
else -> false
}
}

View File

@ -230,9 +230,7 @@ class RouteSettingsActivity(
onMainDispatcher {
supportFragmentManager.beginTransaction()
.replace(R.id.settings, MyPreferenceFragmentCompat().apply {
activity = this@RouteSettingsActivity
})
.replace(R.id.settings, MyPreferenceFragmentCompat())
.commit()
DataStore.dirty = false
@ -309,12 +307,12 @@ class RouteSettingsActivity(
class MyPreferenceFragmentCompat : PreferenceFragmentCompat() {
lateinit var activity: RouteSettingsActivity
var activity: RouteSettingsActivity? = null
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
preferenceManager.preferenceDataStore = DataStore.profileCacheStore
try {
activity.apply {
activity = (requireActivity() as RouteSettingsActivity).apply {
createPreferences(savedInstanceState, rootKey)
}
} catch (e: Exception) {
@ -332,7 +330,7 @@ class RouteSettingsActivity(
ViewCompat.setOnApplyWindowInsetsListener(listView, ListListener)
activity.apply {
activity?.apply {
viewCreated(view, savedInstanceState)
}
}
@ -349,17 +347,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)

View File

@ -120,9 +120,8 @@ abstract class ProfileSettingsActivity<T : AbstractBean>(
onMainDispatcher {
supportFragmentManager.beginTransaction()
.replace(R.id.settings, MyPreferenceFragmentCompat().apply {
activity = this@ProfileSettingsActivity
}).commit()
.replace(R.id.settings, MyPreferenceFragmentCompat())
.commit()
}
}
@ -210,12 +209,12 @@ abstract class ProfileSettingsActivity<T : AbstractBean>(
class MyPreferenceFragmentCompat : PreferenceFragmentCompat() {
lateinit var activity: ProfileSettingsActivity<*>
var activity: ProfileSettingsActivity<*>? = null
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
preferenceManager.preferenceDataStore = DataStore.profileCacheStore
try {
activity.apply {
activity = (requireActivity() as ProfileSettingsActivity<*>).apply {
createPreferences(savedInstanceState, rootKey)
}
} catch (e: Exception) {
@ -233,12 +232,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)
}
var callbackCustom: ((String) -> Unit)? = null
@ -273,14 +271,16 @@ abstract class ProfileSettingsActivity<T : AbstractBean>(
}
true
}
R.id.action_apply -> {
runOnDefaultDispatcher {
activity.saveAndExit()
activity?.saveAndExit()
}
true
}
R.id.action_custom_outbound_json -> {
activity.proxyEntity?.apply {
activity?.proxyEntity?.apply {
val bean = requireBean()
DataStore.serverCustomOutbound = bean.customOutboundJson
callbackCustomOutbound = { bean.customOutboundJson = it }
@ -294,8 +294,9 @@ abstract class ProfileSettingsActivity<T : AbstractBean>(
}
true
}
R.id.action_custom_config_json -> {
activity.proxyEntity?.apply {
activity?.proxyEntity?.apply {
val bean = requireBean()
DataStore.serverCustom = bean.customConfigJson
callbackCustom = { bean.customConfigJson = it }
@ -309,7 +310,9 @@ abstract class ProfileSettingsActivity<T : AbstractBean>(
}
true
}
R.id.action_create_shortcut -> {
val activity = requireActivity() as ProfileSettingsActivity<*>
val ent = activity.proxyEntity!!
val shortcut = ShortcutInfoCompat.Builder(activity, "shortcut-profile-${ent.id}")
.setShortLabel(ent.displayName())
@ -326,7 +329,9 @@ abstract class ProfileSettingsActivity<T : AbstractBean>(
}).build()
ShortcutManagerCompat.requestPinShortcut(activity, shortcut, null)
}
R.id.action_move -> {
val activity = requireActivity() as ProfileSettingsActivity<*>
val view = LinearLayout(context).apply {
val ent = activity.proxyEntity!!
orientation = LinearLayout.VERTICAL
@ -362,11 +367,12 @@ abstract class ProfileSettingsActivity<T : AbstractBean>(
MaterialAlertDialogBuilder(activity).setView(scrollView).show()
true
}
else -> false
}
override fun onDisplayPreferenceDialog(preference: Preference) {
activity.apply {
activity?.apply {
if (displayPreferenceDialog(preference)) return
}
super.onDisplayPreferenceDialog(preference)