From 51991324a9d0163eedc877ebafcfeda29ab9548b Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Mon, 8 May 2023 13:31:33 +0200 Subject: [PATCH] ui: only register contracts in constructors Signed-off-by: Jason A. Donenfeld --- .../android/fragment/TunnelListFragment.kt | 5 ++- .../android/updater/SnackbarUpdateShower.kt | 37 +++++++++---------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt b/ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt index f59aa0ff..cba7c476 100644 --- a/ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt +++ b/ui/src/main/java/com/wireguard/android/fragment/TunnelListFragment.kt @@ -81,6 +81,8 @@ class TunnelListFragment : BaseFragment() { } } + private val snackbarUpdateShower = SnackbarUpdateShower(this) + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) if (savedInstanceState != null) { @@ -125,12 +127,11 @@ class TunnelListFragment : BaseFragment() { bottomSheet.showNow(childFragmentManager, "BOTTOM_SHEET") } executePendingBindings() + snackbarUpdateShower.attach(mainContainer, createFab) } backPressedCallback = requireActivity().onBackPressedDispatcher.addCallback(this) { actionMode?.finish() } backPressedCallback?.isEnabled = false - SnackbarUpdateShower.attachToActivity(requireActivity(), binding?.mainContainer!!, binding?.createFab) - return binding?.root } diff --git a/ui/src/main/java/com/wireguard/android/updater/SnackbarUpdateShower.kt b/ui/src/main/java/com/wireguard/android/updater/SnackbarUpdateShower.kt index 7b0c3a60..b566a0cf 100644 --- a/ui/src/main/java/com/wireguard/android/updater/SnackbarUpdateShower.kt +++ b/ui/src/main/java/com/wireguard/android/updater/SnackbarUpdateShower.kt @@ -7,11 +7,10 @@ package com.wireguard.android.updater import android.view.View import androidx.activity.result.contract.ActivityResultContracts -import androidx.fragment.app.FragmentActivity +import androidx.fragment.app.Fragment import androidx.lifecycle.lifecycleScope import com.google.android.material.snackbar.BaseTransientBottomBar import com.google.android.material.snackbar.Snackbar -import com.wireguard.android.BuildConfig import com.wireguard.android.R import com.wireguard.android.util.ErrorMessages import com.wireguard.android.util.QuantityFormatter @@ -21,15 +20,20 @@ import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch import kotlin.time.Duration.Companion.seconds -object SnackbarUpdateShower { - private class SwapableSnackbar(activity: FragmentActivity, view: View, anchor: View?) { - val actionSnackbar = makeSnackbar(activity, view, anchor) - val statusSnackbar = makeSnackbar(activity, view, anchor) +class SnackbarUpdateShower(private val fragment: Fragment) { + private var lastUserIntervention: Updater.Progress.NeedsUserIntervention? = null + private val intentLauncher = fragment.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { + lastUserIntervention?.markAsDone() + } + + private class SwapableSnackbar(fragment: Fragment, view: View, anchor: View?) { + val actionSnackbar = makeSnackbar(fragment, view, anchor) + val statusSnackbar = makeSnackbar(fragment, view, anchor) var showingAction: Boolean = false var showingStatus: Boolean = false - private fun makeSnackbar(activity: FragmentActivity, view: View, anchor: View?): Snackbar { - val snackbar = Snackbar.make(activity, view, "", Snackbar.LENGTH_INDEFINITE) + private fun makeSnackbar(fragment: Fragment, view: View, anchor: View?): Snackbar { + val snackbar = Snackbar.make(fragment.requireContext(), view, "", Snackbar.LENGTH_INDEFINITE) if (anchor != null) snackbar.anchorView = anchor snackbar.setTextMaxLines(6) @@ -45,7 +49,7 @@ object SnackbarUpdateShower { (snackbar == actionSnackbar && !showingAction) || (snackbar == statusSnackbar && !showingStatus) ) return - activity.lifecycleScope.launch { + fragment.lifecycleScope.launch { delay(5.seconds) snackbar?.show() } @@ -87,14 +91,9 @@ object SnackbarUpdateShower { } } - fun attachToActivity(activity: FragmentActivity, view: View, anchor: View?) { - val snackbar = SwapableSnackbar(activity, view, anchor) - val context = activity.applicationContext - - var lastUserIntervention: Updater.Progress.NeedsUserIntervention? = null - val intentLauncher = activity.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { - lastUserIntervention?.markAsDone() - } + fun attach(view: View, anchor: View?) { + val snackbar = SwapableSnackbar(fragment, view, anchor) + val context = fragment.requireContext() Updater.state.onEach { progress -> when (progress) { @@ -138,11 +137,11 @@ object SnackbarUpdateShower { } is Updater.Progress.Failure -> { - snackbar.showText( context.getString(R.string.updater_failure, ErrorMessages[progress.error])) + snackbar.showText(context.getString(R.string.updater_failure, ErrorMessages[progress.error])) delay(5.seconds) progress.retry() } } - }.launchIn(activity.lifecycleScope) + }.launchIn(fragment.lifecycleScope) } } \ No newline at end of file