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
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import androidx.core.view.ViewCompat
import androidx.fragment.app.setFragmentResultListener
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.whenCreated
import androidx.preference.*
......@@ -129,7 +130,24 @@ class ProfileConfigFragment : PreferenceFragmentCompat(),
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
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) {
DialogInterface.BUTTON_POSITIVE -> saveAndExit()
DialogInterface.BUTTON_NEGATIVE -> requireActivity().finish()
......@@ -147,7 +165,6 @@ class ProfileConfigFragment : PreferenceFragmentCompat(),
private fun showPluginEditor() {
PluginConfigurationDialogFragment().apply {
setArg(Key.pluginConfigure, pluginConfiguration.selected)
setTargetFragment(this@ProfileConfigFragment, 0)
}.showAllowingStateLoss(parentFragmentManager, Key.pluginConfigure)
}
......@@ -198,7 +215,6 @@ class ProfileConfigFragment : PreferenceFragmentCompat(),
when (preference.key) {
Key.plugin -> PluginPreferenceDialogFragment().apply {
setArg(Key.plugin)
setTargetFragment(this@ProfileConfigFragment, REQUEST_PICK_PLUGIN)
}.showAllowingStateLoss(parentFragmentManager, Key.plugin)
Key.pluginConfigure -> {
val intent = PluginManager.buildIntent(plugin.selectedEntry!!.id, PluginContract.ACTION_CONFIGURE)
......@@ -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) {
R.id.action_delete -> {
DeleteConfirmationDialogFragment().apply {
......
......@@ -20,7 +20,6 @@
package com.github.shadowsocks.preference
import android.app.Activity
import android.app.Dialog
import android.content.ActivityNotFoundException
import android.content.Intent
......@@ -37,6 +36,7 @@ import androidx.appcompat.widget.TooltipCompat
import androidx.core.os.bundleOf
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.fragment.app.setFragmentResult
import androidx.preference.PreferenceDialogFragmentCompat
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
......@@ -134,8 +134,7 @@ class PluginPreferenceDialogFragment : PreferenceDialogFragmentCompat() {
override fun onDialogClosed(positiveResult: Boolean) {
val clicked = clicked
if (clicked != null && clicked != preference.selectedEntry) {
targetFragment!!.onActivityResult(targetRequestCode, Activity.RESULT_OK,
Intent().putExtra(KEY_SELECTED_ID, clicked.id))
} else targetFragment!!.onActivityResult(targetRequestCode, Activity.RESULT_CANCELED, null)
setFragmentResult(javaClass.name, bundleOf(KEY_SELECTED_ID to clicked.id))
}
}
}
......@@ -45,7 +45,6 @@ class MainFragment : LeanbackSettingsFragmentCompat() {
as MainPreferenceFragment).state == BaseService.State.Stopped) {
startPreferenceFragment(ProfilesDialogFragment().apply {
arguments = bundleOf(Pair(LeanbackPreferenceDialogFragmentCompat.ARG_KEY, Key.id))
setTargetFragment(caller, 0)
})
}
return true
......@@ -53,7 +52,6 @@ class MainFragment : LeanbackSettingsFragmentCompat() {
if (pref is ListPreference && pref !is MultiSelectListPreference) {
startPreferenceFragment(LeanbackSingleListPreferenceDialogFragment().apply {
arguments = bundleOf(Pair(LeanbackPreferenceDialogFragmentCompat.ARG_KEY, pref.key))
setTargetFragment(caller, 0)
})
return true
}
......
......@@ -27,6 +27,7 @@ import android.os.RemoteException
import android.text.format.Formatter
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.fragment.app.setFragmentResultListener
import androidx.fragment.app.viewModels
import androidx.leanback.preference.LeanbackPreferenceFragmentCompat
import androidx.preference.*
......@@ -121,6 +122,7 @@ class MainPreferenceFragment : LeanbackPreferenceFragmentCompat(), ShadowsocksCo
preferenceManager.preferenceDataStore = DataStore.publicStore
DataStore.initGlobal()
addPreferencesFromResource(R.xml.pref_main)
setFragmentResultListener(ProfilesDialogFragment::class.java.name) { _, _ -> startService() }
fab = findPreference(Key.id)!!
populateProfiles()
stats = findPreference(Key.controlStats)!!
......
......@@ -27,6 +27,7 @@ import android.view.View
import android.view.ViewGroup
import android.widget.CompoundButton
import android.widget.TextView
import androidx.fragment.app.setFragmentResult
import androidx.leanback.preference.LeanbackListPreferenceDialogFragmentCompat
import androidx.recyclerview.widget.RecyclerView
import com.github.shadowsocks.Core
......@@ -46,7 +47,7 @@ class ProfilesDialogFragment : LeanbackListPreferenceDialogFragmentCompat() {
val index = bindingAdapterPosition
if (index == RecyclerView.NO_POSITION) return
Core.switchProfile(adapter.profiles[index].id)
(targetFragment as MainPreferenceFragment).startService()
setFragmentResult(ProfilesDialogFragment::class.java.name, Bundle.EMPTY)
parentFragmentManager.popBackStack()
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