summaryrefslogtreecommitdiffhomepage
path: root/ui
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2020-09-17 17:52:12 +0530
committerJason A. Donenfeld <Jason@zx2c4.com>2020-09-17 14:51:50 +0200
commite99ccf901312a0c4f995b2f286feb0fd87ad9c88 (patch)
tree934c09f74744d4072514418d917af454fdffa3fb /ui
parent59935a12b9336855c712d366e542aa6a37e8d041 (diff)
ui: refactor AppListDialogFragment's selection communication
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'ui')
-rw-r--r--ui/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.kt22
-rw-r--r--ui/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.kt42
2 files changed, 31 insertions, 33 deletions
diff --git a/ui/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.kt b/ui/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.kt
index 88701ec3..7a51a757 100644
--- a/ui/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.kt
+++ b/ui/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.kt
@@ -10,9 +10,10 @@ import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
+import androidx.core.os.bundleOf
import androidx.databinding.Observable
import androidx.fragment.app.DialogFragment
-import androidx.fragment.app.Fragment
+import androidx.fragment.app.setFragmentResult
import androidx.lifecycle.lifecycleScope
import com.google.android.material.tabs.TabLayout
import com.wireguard.android.BR
@@ -21,7 +22,6 @@ import com.wireguard.android.databinding.AppListDialogFragmentBinding
import com.wireguard.android.databinding.ObservableKeyedArrayList
import com.wireguard.android.model.ApplicationData
import com.wireguard.android.util.ErrorMessages
-import com.wireguard.android.util.requireTargetFragment
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@@ -73,7 +73,6 @@ class AppListDialogFragment : DialogFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
- require(requireTargetFragment() is AppSelectionListener) { "${requireTargetFragment()} must implement AppSelectionListener" }
currentlySelectedApps = (arguments?.getStringArrayList(KEY_SELECTED_APPS) ?: emptyList())
initiallyExcluded = arguments?.getBoolean(KEY_IS_EXCLUDED) ?: true
}
@@ -130,23 +129,22 @@ class AppListDialogFragment : DialogFragment() {
selectedApps.add(data.packageName)
}
}
- (requireTargetFragment() as AppSelectionListener).onSelectedAppsSelected(selectedApps, tabs?.selectedTabPosition == 0)
+ setFragmentResult(REQUEST_SELECTION, bundleOf(
+ KEY_SELECTED_APPS to selectedApps.toTypedArray(),
+ KEY_IS_EXCLUDED to (tabs?.selectedTabPosition == 0)
+ ))
dismiss()
}
- interface AppSelectionListener {
- fun onSelectedAppsSelected(selectedApps: List<String>, isExcluded: Boolean)
- }
-
companion object {
- private const val KEY_SELECTED_APPS = "selected_apps"
- private const val KEY_IS_EXCLUDED = "is_excluded"
- fun <T> newInstance(selectedApps: ArrayList<String?>?, isExcluded: Boolean, target: T): AppListDialogFragment where T : Fragment?, T : AppSelectionListener? {
+ const val KEY_SELECTED_APPS = "selected_apps"
+ const val KEY_IS_EXCLUDED = "is_excluded"
+ const val REQUEST_SELECTION = "request_selection"
+ fun newInstance(selectedApps: ArrayList<String?>?, isExcluded: Boolean): AppListDialogFragment {
val extras = Bundle()
extras.putStringArrayList(KEY_SELECTED_APPS, selectedApps)
extras.putBoolean(KEY_IS_EXCLUDED, isExcluded)
val fragment = AppListDialogFragment()
- fragment.setTargetFragment(target, 0)
fragment.arguments = extras
return fragment
}
diff --git a/ui/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.kt b/ui/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.kt
index bb09c5eb..f26862ac 100644
--- a/ui/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.kt
+++ b/ui/src/main/java/com/wireguard/android/fragment/TunnelEditorFragment.kt
@@ -24,7 +24,6 @@ import com.wireguard.android.Application
import com.wireguard.android.R
import com.wireguard.android.backend.Tunnel
import com.wireguard.android.databinding.TunnelEditorFragmentBinding
-import com.wireguard.android.fragment.AppListDialogFragment.AppSelectionListener
import com.wireguard.android.model.ObservableTunnel
import com.wireguard.android.util.AdminKnobs
import com.wireguard.android.util.BiometricAuthenticator
@@ -38,7 +37,7 @@ import kotlinx.coroutines.launch
/**
* Fragment for editing a WireGuard configuration.
*/
-class TunnelEditorFragment : BaseFragment(), AppSelectionListener {
+class TunnelEditorFragment : BaseFragment() {
private var haveShownKeys = false
private var binding: TunnelEditorFragmentBinding? = null
private var tunnel: ObservableTunnel? = null
@@ -91,23 +90,6 @@ class TunnelEditorFragment : BaseFragment(), AppSelectionListener {
super.onDestroyView()
}
- override fun onSelectedAppsSelected(selectedApps: List<String>, isExcluded: Boolean) {
- requireNotNull(binding) { "Tried to set excluded/included apps while no view was loaded" }
- if (isExcluded) {
- binding!!.config!!.`interface`.includedApplications.clear()
- binding!!.config!!.`interface`.excludedApplications.apply {
- clear()
- addAll(selectedApps)
- }
- } else {
- binding!!.config!!.`interface`.excludedApplications.clear()
- binding!!.config!!.`interface`.includedApplications.apply {
- clear()
- addAll(selectedApps)
- }
- }
- }
-
private fun onFinished() {
// Hide the keyboard; it rarely goes away on its own.
val activity = activity ?: return
@@ -183,8 +165,26 @@ class TunnelEditorFragment : BaseFragment(), AppSelectionListener {
if (selectedApps.isNotEmpty())
isExcluded = false
}
- val fragment = AppListDialogFragment.newInstance(selectedApps, isExcluded, this)
- fragment.show(parentFragmentManager, null)
+ val fragment = AppListDialogFragment.newInstance(selectedApps, isExcluded)
+ childFragmentManager.setFragmentResultListener(AppListDialogFragment.REQUEST_SELECTION, viewLifecycleOwner) { _, bundle ->
+ requireNotNull(binding) { "Tried to set excluded/included apps while no view was loaded" }
+ val newSelections = requireNotNull(bundle.getStringArray(AppListDialogFragment.KEY_SELECTED_APPS))
+ val excluded = requireNotNull(bundle.getBoolean(AppListDialogFragment.KEY_IS_EXCLUDED))
+ if (excluded) {
+ binding!!.config!!.`interface`.includedApplications.clear()
+ binding!!.config!!.`interface`.excludedApplications.apply {
+ clear()
+ addAll(newSelections)
+ }
+ } else {
+ binding!!.config!!.`interface`.excludedApplications.clear()
+ binding!!.config!!.`interface`.includedApplications.apply {
+ clear()
+ addAll(newSelections)
+ }
+ }
+ }
+ fragment.show(childFragmentManager, null)
}
}