Commit cfef0316 authored by Mygod's avatar Mygod Committed by Max Lv

Migrate the rest to fragment result API

parent fdea63e4
...@@ -35,6 +35,7 @@ import androidx.activity.result.component2 ...@@ -35,6 +35,7 @@ import androidx.activity.result.component2
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.core.view.ViewCompat import androidx.core.view.ViewCompat
import androidx.fragment.app.setFragmentResultListener
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.whenCreated import androidx.lifecycle.whenCreated
import androidx.preference.* import androidx.preference.*
...@@ -129,7 +130,24 @@ class ProfileConfigFragment : PreferenceFragmentCompat(), ...@@ -129,7 +130,24 @@ class ProfileConfigFragment : PreferenceFragmentCompat(),
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
ViewCompat.setOnApplyWindowInsetsListener(listView, ListListener) ViewCompat.setOnApplyWindowInsetsListener(listView, ListListener)
AlertDialogFragment.setResultListener<ProfileConfigActivity.UnsavedChangesDialogFragment, Empty>(this) { which, ret -> setFragmentResultListener(PluginPreferenceDialogFragment::class.java.name) { _, bundle ->
val selected = plugin.plugins.lookup.getValue(
bundle.getString(PluginPreferenceDialogFragment.KEY_SELECTED_ID)!!)
val override = pluginConfiguration.pluginsOptions.keys.firstOrNull {
plugin.plugins.lookup[it] == selected
}
pluginConfiguration = PluginConfiguration(pluginConfiguration.pluginsOptions, override ?: selected.id)
DataStore.plugin = pluginConfiguration.toString()
DataStore.dirty = true
plugin.value = pluginConfiguration.selected
pluginConfigure.isEnabled = selected !is NoPlugin
pluginConfigure.text = pluginConfiguration.getOptions().toString()
if (!selected.trusted) {
Snackbar.make(requireView(), R.string.plugin_untrusted, Snackbar.LENGTH_LONG).show()
}
}
AlertDialogFragment.setResultListener<ProfileConfigActivity.UnsavedChangesDialogFragment, Empty>(this) {
which, _ ->
when (which) { when (which) {
DialogInterface.BUTTON_POSITIVE -> saveAndExit() DialogInterface.BUTTON_POSITIVE -> saveAndExit()
DialogInterface.BUTTON_NEGATIVE -> requireActivity().finish() DialogInterface.BUTTON_NEGATIVE -> requireActivity().finish()
...@@ -147,7 +165,6 @@ class ProfileConfigFragment : PreferenceFragmentCompat(), ...@@ -147,7 +165,6 @@ class ProfileConfigFragment : PreferenceFragmentCompat(),
private fun showPluginEditor() { private fun showPluginEditor() {
PluginConfigurationDialogFragment().apply { PluginConfigurationDialogFragment().apply {
setArg(Key.pluginConfigure, pluginConfiguration.selected) setArg(Key.pluginConfigure, pluginConfiguration.selected)
setTargetFragment(this@ProfileConfigFragment, 0)
}.showAllowingStateLoss(parentFragmentManager, Key.pluginConfigure) }.showAllowingStateLoss(parentFragmentManager, Key.pluginConfigure)
} }
...@@ -198,7 +215,6 @@ class ProfileConfigFragment : PreferenceFragmentCompat(), ...@@ -198,7 +215,6 @@ class ProfileConfigFragment : PreferenceFragmentCompat(),
when (preference.key) { when (preference.key) {
Key.plugin -> PluginPreferenceDialogFragment().apply { Key.plugin -> PluginPreferenceDialogFragment().apply {
setArg(Key.plugin) setArg(Key.plugin)
setTargetFragment(this@ProfileConfigFragment, REQUEST_PICK_PLUGIN)
}.showAllowingStateLoss(parentFragmentManager, Key.plugin) }.showAllowingStateLoss(parentFragmentManager, Key.plugin)
Key.pluginConfigure -> { Key.pluginConfigure -> {
val intent = PluginManager.buildIntent(plugin.selectedEntry!!.id, PluginContract.ACTION_CONFIGURE) val intent = PluginManager.buildIntent(plugin.selectedEntry!!.id, PluginContract.ACTION_CONFIGURE)
...@@ -223,26 +239,6 @@ class ProfileConfigFragment : PreferenceFragmentCompat(), ...@@ -223,26 +239,6 @@ class ProfileConfigFragment : PreferenceFragmentCompat(),
} }
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode != REQUEST_PICK_PLUGIN) super.onActivityResult(requestCode, resultCode, data)
else if (resultCode == Activity.RESULT_OK) {
val selected = plugin.plugins.lookup.getValue(
data?.getStringExtra(PluginPreferenceDialogFragment.KEY_SELECTED_ID)!!)
val override = pluginConfiguration.pluginsOptions.keys.firstOrNull {
plugin.plugins.lookup[it] == selected
}
pluginConfiguration = PluginConfiguration(pluginConfiguration.pluginsOptions, override ?: selected.id)
DataStore.plugin = pluginConfiguration.toString()
DataStore.dirty = true
plugin.value = pluginConfiguration.selected
pluginConfigure.isEnabled = selected !is NoPlugin
pluginConfigure.text = pluginConfiguration.getOptions().toString()
if (!selected.trusted) {
Snackbar.make(requireView(), R.string.plugin_untrusted, Snackbar.LENGTH_LONG).show()
}
}
}
override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) { override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
R.id.action_delete -> { R.id.action_delete -> {
DeleteConfirmationDialogFragment().apply { DeleteConfirmationDialogFragment().apply {
......
...@@ -20,7 +20,6 @@ ...@@ -20,7 +20,6 @@
package com.github.shadowsocks.preference package com.github.shadowsocks.preference
import android.app.Activity
import android.app.Dialog import android.app.Dialog
import android.content.ActivityNotFoundException import android.content.ActivityNotFoundException
import android.content.Intent import android.content.Intent
...@@ -37,6 +36,7 @@ import androidx.appcompat.widget.TooltipCompat ...@@ -37,6 +36,7 @@ import androidx.appcompat.widget.TooltipCompat
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import androidx.core.view.isGone import androidx.core.view.isGone
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.fragment.app.setFragmentResult
import androidx.preference.PreferenceDialogFragmentCompat import androidx.preference.PreferenceDialogFragmentCompat
import androidx.recyclerview.widget.DefaultItemAnimator import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
...@@ -134,8 +134,7 @@ class PluginPreferenceDialogFragment : PreferenceDialogFragmentCompat() { ...@@ -134,8 +134,7 @@ class PluginPreferenceDialogFragment : PreferenceDialogFragmentCompat() {
override fun onDialogClosed(positiveResult: Boolean) { override fun onDialogClosed(positiveResult: Boolean) {
val clicked = clicked val clicked = clicked
if (clicked != null && clicked != preference.selectedEntry) { if (clicked != null && clicked != preference.selectedEntry) {
targetFragment!!.onActivityResult(targetRequestCode, Activity.RESULT_OK, setFragmentResult(javaClass.name, bundleOf(KEY_SELECTED_ID to clicked.id))
Intent().putExtra(KEY_SELECTED_ID, clicked.id)) }
} else targetFragment!!.onActivityResult(targetRequestCode, Activity.RESULT_CANCELED, null)
} }
} }
...@@ -45,7 +45,6 @@ class MainFragment : LeanbackSettingsFragmentCompat() { ...@@ -45,7 +45,6 @@ class MainFragment : LeanbackSettingsFragmentCompat() {
as MainPreferenceFragment).state == BaseService.State.Stopped) { as MainPreferenceFragment).state == BaseService.State.Stopped) {
startPreferenceFragment(ProfilesDialogFragment().apply { startPreferenceFragment(ProfilesDialogFragment().apply {
arguments = bundleOf(Pair(LeanbackPreferenceDialogFragmentCompat.ARG_KEY, Key.id)) arguments = bundleOf(Pair(LeanbackPreferenceDialogFragmentCompat.ARG_KEY, Key.id))
setTargetFragment(caller, 0)
}) })
} }
return true return true
...@@ -53,7 +52,6 @@ class MainFragment : LeanbackSettingsFragmentCompat() { ...@@ -53,7 +52,6 @@ class MainFragment : LeanbackSettingsFragmentCompat() {
if (pref is ListPreference && pref !is MultiSelectListPreference) { if (pref is ListPreference && pref !is MultiSelectListPreference) {
startPreferenceFragment(LeanbackSingleListPreferenceDialogFragment().apply { startPreferenceFragment(LeanbackSingleListPreferenceDialogFragment().apply {
arguments = bundleOf(Pair(LeanbackPreferenceDialogFragmentCompat.ARG_KEY, pref.key)) arguments = bundleOf(Pair(LeanbackPreferenceDialogFragmentCompat.ARG_KEY, pref.key))
setTargetFragment(caller, 0)
}) })
return true return true
} }
......
...@@ -27,6 +27,7 @@ import android.os.RemoteException ...@@ -27,6 +27,7 @@ import android.os.RemoteException
import android.text.format.Formatter import android.text.format.Formatter
import android.widget.Toast import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher import androidx.activity.result.ActivityResultLauncher
import androidx.fragment.app.setFragmentResultListener
import androidx.fragment.app.viewModels import androidx.fragment.app.viewModels
import androidx.leanback.preference.LeanbackPreferenceFragmentCompat import androidx.leanback.preference.LeanbackPreferenceFragmentCompat
import androidx.preference.* import androidx.preference.*
...@@ -121,6 +122,7 @@ class MainPreferenceFragment : LeanbackPreferenceFragmentCompat(), ShadowsocksCo ...@@ -121,6 +122,7 @@ class MainPreferenceFragment : LeanbackPreferenceFragmentCompat(), ShadowsocksCo
preferenceManager.preferenceDataStore = DataStore.publicStore preferenceManager.preferenceDataStore = DataStore.publicStore
DataStore.initGlobal() DataStore.initGlobal()
addPreferencesFromResource(R.xml.pref_main) addPreferencesFromResource(R.xml.pref_main)
setFragmentResultListener(ProfilesDialogFragment::class.java.name) { _, _ -> startService() }
fab = findPreference(Key.id)!! fab = findPreference(Key.id)!!
populateProfiles() populateProfiles()
stats = findPreference(Key.controlStats)!! stats = findPreference(Key.controlStats)!!
......
...@@ -27,6 +27,7 @@ import android.view.View ...@@ -27,6 +27,7 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.CompoundButton import android.widget.CompoundButton
import android.widget.TextView import android.widget.TextView
import androidx.fragment.app.setFragmentResult
import androidx.leanback.preference.LeanbackListPreferenceDialogFragmentCompat import androidx.leanback.preference.LeanbackListPreferenceDialogFragmentCompat
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.github.shadowsocks.Core import com.github.shadowsocks.Core
...@@ -46,7 +47,7 @@ class ProfilesDialogFragment : LeanbackListPreferenceDialogFragmentCompat() { ...@@ -46,7 +47,7 @@ class ProfilesDialogFragment : LeanbackListPreferenceDialogFragmentCompat() {
val index = bindingAdapterPosition val index = bindingAdapterPosition
if (index == RecyclerView.NO_POSITION) return if (index == RecyclerView.NO_POSITION) return
Core.switchProfile(adapter.profiles[index].id) Core.switchProfile(adapter.profiles[index].id)
(targetFragment as MainPreferenceFragment).startService() setFragmentResult(ProfilesDialogFragment::class.java.name, Bundle.EMPTY)
parentFragmentManager.popBackStack() parentFragmentManager.popBackStack()
adapter.notifyDataSetChanged() adapter.notifyDataSetChanged()
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment