ui: only register contracts in constructors

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2023-05-08 13:31:33 +02:00
parent 33a8a51962
commit 51991324a9
2 changed files with 21 additions and 21 deletions

View File

@ -81,6 +81,8 @@ class TunnelListFragment : BaseFragment() {
} }
} }
private val snackbarUpdateShower = SnackbarUpdateShower(this)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
if (savedInstanceState != null) { if (savedInstanceState != null) {
@ -125,12 +127,11 @@ class TunnelListFragment : BaseFragment() {
bottomSheet.showNow(childFragmentManager, "BOTTOM_SHEET") bottomSheet.showNow(childFragmentManager, "BOTTOM_SHEET")
} }
executePendingBindings() executePendingBindings()
snackbarUpdateShower.attach(mainContainer, createFab)
} }
backPressedCallback = requireActivity().onBackPressedDispatcher.addCallback(this) { actionMode?.finish() } backPressedCallback = requireActivity().onBackPressedDispatcher.addCallback(this) { actionMode?.finish() }
backPressedCallback?.isEnabled = false backPressedCallback?.isEnabled = false
SnackbarUpdateShower.attachToActivity(requireActivity(), binding?.mainContainer!!, binding?.createFab)
return binding?.root return binding?.root
} }

View File

@ -7,11 +7,10 @@ package com.wireguard.android.updater
import android.view.View import android.view.View
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.FragmentActivity import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import com.google.android.material.snackbar.BaseTransientBottomBar import com.google.android.material.snackbar.BaseTransientBottomBar
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import com.wireguard.android.BuildConfig
import com.wireguard.android.R import com.wireguard.android.R
import com.wireguard.android.util.ErrorMessages import com.wireguard.android.util.ErrorMessages
import com.wireguard.android.util.QuantityFormatter import com.wireguard.android.util.QuantityFormatter
@ -21,15 +20,20 @@ import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlin.time.Duration.Companion.seconds import kotlin.time.Duration.Companion.seconds
object SnackbarUpdateShower { class SnackbarUpdateShower(private val fragment: Fragment) {
private class SwapableSnackbar(activity: FragmentActivity, view: View, anchor: View?) { private var lastUserIntervention: Updater.Progress.NeedsUserIntervention? = null
val actionSnackbar = makeSnackbar(activity, view, anchor) private val intentLauncher = fragment.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
val statusSnackbar = makeSnackbar(activity, view, anchor) 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 showingAction: Boolean = false
var showingStatus: Boolean = false var showingStatus: Boolean = false
private fun makeSnackbar(activity: FragmentActivity, view: View, anchor: View?): Snackbar { private fun makeSnackbar(fragment: Fragment, view: View, anchor: View?): Snackbar {
val snackbar = Snackbar.make(activity, view, "", Snackbar.LENGTH_INDEFINITE) val snackbar = Snackbar.make(fragment.requireContext(), view, "", Snackbar.LENGTH_INDEFINITE)
if (anchor != null) if (anchor != null)
snackbar.anchorView = anchor snackbar.anchorView = anchor
snackbar.setTextMaxLines(6) snackbar.setTextMaxLines(6)
@ -45,7 +49,7 @@ object SnackbarUpdateShower {
(snackbar == actionSnackbar && !showingAction) || (snackbar == statusSnackbar && !showingStatus) (snackbar == actionSnackbar && !showingAction) || (snackbar == statusSnackbar && !showingStatus)
) )
return return
activity.lifecycleScope.launch { fragment.lifecycleScope.launch {
delay(5.seconds) delay(5.seconds)
snackbar?.show() snackbar?.show()
} }
@ -87,14 +91,9 @@ object SnackbarUpdateShower {
} }
} }
fun attachToActivity(activity: FragmentActivity, view: View, anchor: View?) { fun attach(view: View, anchor: View?) {
val snackbar = SwapableSnackbar(activity, view, anchor) val snackbar = SwapableSnackbar(fragment, view, anchor)
val context = activity.applicationContext val context = fragment.requireContext()
var lastUserIntervention: Updater.Progress.NeedsUserIntervention? = null
val intentLauncher = activity.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
lastUserIntervention?.markAsDone()
}
Updater.state.onEach { progress -> Updater.state.onEach { progress ->
when (progress) { when (progress) {
@ -138,11 +137,11 @@ object SnackbarUpdateShower {
} }
is Updater.Progress.Failure -> { 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) delay(5.seconds)
progress.retry() progress.retry()
} }
} }
}.launchIn(activity.lifecycleScope) }.launchIn(fragment.lifecycleScope)
} }
} }