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

View File

@ -230,9 +230,7 @@ class RouteSettingsActivity(
onMainDispatcher { onMainDispatcher {
supportFragmentManager.beginTransaction() supportFragmentManager.beginTransaction()
.replace(R.id.settings, MyPreferenceFragmentCompat().apply { .replace(R.id.settings, MyPreferenceFragmentCompat())
activity = this@RouteSettingsActivity
})
.commit() .commit()
DataStore.dirty = false DataStore.dirty = false
@ -309,12 +307,12 @@ class RouteSettingsActivity(
class MyPreferenceFragmentCompat : PreferenceFragmentCompat() { class MyPreferenceFragmentCompat : PreferenceFragmentCompat() {
lateinit var activity: RouteSettingsActivity var activity: RouteSettingsActivity? = null
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
preferenceManager.preferenceDataStore = DataStore.profileCacheStore preferenceManager.preferenceDataStore = DataStore.profileCacheStore
try { try {
activity.apply { activity = (requireActivity() as RouteSettingsActivity).apply {
createPreferences(savedInstanceState, rootKey) createPreferences(savedInstanceState, rootKey)
} }
} catch (e: Exception) { } catch (e: Exception) {
@ -332,7 +330,7 @@ class RouteSettingsActivity(
ViewCompat.setOnApplyWindowInsetsListener(listView, ListListener) ViewCompat.setOnApplyWindowInsetsListener(listView, ListListener)
activity.apply { activity?.apply {
viewCreated(view, savedInstanceState) viewCreated(view, savedInstanceState)
} }
} }
@ -349,17 +347,19 @@ class RouteSettingsActivity(
} }
true true
} }
R.id.action_apply -> { R.id.action_apply -> {
runOnDefaultDispatcher { runOnDefaultDispatcher {
activity.saveAndExit() activity?.saveAndExit()
} }
true true
} }
else -> false else -> false
} }
override fun onDisplayPreferenceDialog(preference: Preference) { override fun onDisplayPreferenceDialog(preference: Preference) {
activity.apply { activity?.apply {
if (displayPreferenceDialog(preference)) return if (displayPreferenceDialog(preference)) return
} }
super.onDisplayPreferenceDialog(preference) super.onDisplayPreferenceDialog(preference)

View File

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