diff options
Diffstat (limited to 'ui/src/main/java/com/wireguard')
3 files changed, 19 insertions, 14 deletions
diff --git a/ui/src/main/java/com/wireguard/android/fragment/AddTunnelsSheet.kt b/ui/src/main/java/com/wireguard/android/fragment/AddTunnelsSheet.kt index fe3eca3f..17bdac04 100644 --- a/ui/src/main/java/com/wireguard/android/fragment/AddTunnelsSheet.kt +++ b/ui/src/main/java/com/wireguard/android/fragment/AddTunnelsSheet.kt @@ -12,13 +12,13 @@ import android.view.View import android.view.ViewGroup import android.view.ViewTreeObserver import android.widget.FrameLayout -import androidx.fragment.app.Fragment import com.google.android.material.bottomsheet.BottomSheetBehavior import com.google.android.material.bottomsheet.BottomSheetDialog import com.google.android.material.bottomsheet.BottomSheetDialogFragment import com.google.zxing.integration.android.IntentIntegrator import com.wireguard.android.R import com.wireguard.android.activity.TunnelCreatorActivity +import com.wireguard.android.util.requireTargetFragment import com.wireguard.android.util.resolveAttribute class AddTunnelsSheet : BottomSheetDialogFragment() { @@ -81,10 +81,6 @@ class AddTunnelsSheet : BottomSheetDialogFragment() { behavior?.removeBottomSheetCallback(bottomSheetCallback) } - private fun requireTargetFragment(): Fragment { - return requireNotNull(targetFragment) { "A target fragment should always be set" } - } - private fun onRequestCreateConfig() { startActivity(Intent(activity, TunnelCreatorActivity::class.java)) } 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 4fdebd16..35bd3ce9 100644 --- a/ui/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.kt +++ b/ui/src/main/java/com/wireguard/android/fragment/AppListDialogFragment.kt @@ -21,11 +21,12 @@ 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 class AppListDialogFragment : DialogFragment() { - private val appData: ObservableKeyedArrayList<String, ApplicationData> = ObservableKeyedArrayList() + private val appData = ObservableKeyedArrayList<String, ApplicationData>() private var currentlySelectedApps = emptyList<String>() - private var initiallyExcluded: Boolean = false + private var initiallyExcluded = false private var button: Button? = null private var tabs: TabLayout? = null @@ -65,6 +66,7 @@ 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 } @@ -86,12 +88,14 @@ class AppListDialogFragment : DialogFragment() { binding.executePendingBindings() alertDialogBuilder.setView(binding.root) tabs = binding.tabs - tabs!!.selectTab(binding.tabs.getTabAt(if (initiallyExcluded) 0 else 1)) - tabs!!.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener { - override fun onTabReselected(tab: TabLayout.Tab?) = Unit - override fun onTabUnselected(tab: TabLayout.Tab?) = Unit - override fun onTabSelected(tab: TabLayout.Tab?) = setButtonText() - }) + tabs?.apply { + selectTab(binding.tabs.getTabAt(if (initiallyExcluded) 0 else 1)) + addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener { + override fun onTabReselected(tab: TabLayout.Tab?) = Unit + override fun onTabUnselected(tab: TabLayout.Tab?) = Unit + override fun onTabSelected(tab: TabLayout.Tab?) = setButtonText() + }) + } alertDialogBuilder.setPositiveButton(" ") { _, _ -> setSelectionAndDismiss() } alertDialogBuilder.setNegativeButton(R.string.cancel) { dialog, _ -> dialog.dismiss() } alertDialogBuilder.setNeutralButton(R.string.toggle_all) { _, _ -> } @@ -119,7 +123,7 @@ class AppListDialogFragment : DialogFragment() { selectedApps.add(data.packageName) } } - (targetFragment as AppSelectionListener?)!!.onSelectedAppsSelected(selectedApps, tabs?.selectedTabPosition == 0) + (requireTargetFragment() as AppSelectionListener).onSelectedAppsSelected(selectedApps, tabs?.selectedTabPosition == 0) dismiss() } diff --git a/ui/src/main/java/com/wireguard/android/util/Extensions.kt b/ui/src/main/java/com/wireguard/android/util/Extensions.kt index 6b528a85..a705401f 100644 --- a/ui/src/main/java/com/wireguard/android/util/Extensions.kt +++ b/ui/src/main/java/com/wireguard/android/util/Extensions.kt @@ -8,9 +8,14 @@ package com.wireguard.android.util import android.content.Context import android.util.TypedValue import androidx.annotation.AttrRes +import androidx.fragment.app.Fragment fun Context.resolveAttribute(@AttrRes attrRes: Int): Int { val typedValue = TypedValue() theme.resolveAttribute(attrRes, typedValue, true) return typedValue.data } + +fun Fragment.requireTargetFragment(): Fragment { + return requireNotNull(targetFragment) { "A target fragment should always be set for $this" } +} |