Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/src/main/java/to/bitkit/models/Toast.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ data class Toast(
val isLoading: Boolean = false,
/** False for a one-time warning that later toasts wait behind instead of replacing. */
val interruptible: Boolean = true,
/** True for a progress toast that the next toast replaces, even while it waits in the queue. */
val replacedByNext: Boolean = false,
/** A queued toast is replaced by a newer one with the same key, so only the latest status shows. */
val replaceKey: String? = null,
) {
enum class ToastType { SUCCESS, INFO, LIGHTNING, WARNING, ERROR }

Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/to/bitkit/ui/components/IsOnlineTracker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import to.bitkit.models.Toast
import to.bitkit.repositories.ConnectivityState
import to.bitkit.viewmodels.AppViewModel

/** A connection status waiting behind another toast is replaced by the newer status. */
private const val CONNECTIVITY_TOAST_KEY = "Connectivity"

@Composable
fun IsOnlineTracker(
app: AppViewModel,
Expand All @@ -34,6 +37,7 @@ fun IsOnlineTracker(
type = Toast.ToastType.SUCCESS,
title = resources.getString(R.string.other__connection_back_title),
description = resources.getString(R.string.other__connection_back_msg),
replaceKey = CONNECTIVITY_TOAST_KEY,
)
}

Expand All @@ -42,6 +46,7 @@ fun IsOnlineTracker(
type = Toast.ToastType.WARNING,
title = resources.getString(R.string.other__connection_issue),
description = resources.getString(R.string.other__connection_issue_explain),
replaceKey = CONNECTIVITY_TOAST_KEY,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ import to.bitkit.repositories.WalletRepo
import to.bitkit.ui.shared.toast.ToastEventBus
import javax.inject.Inject

/** The progress toast waiting in the queue is replaced by its result. */
private const val ADDRESS_TYPE_TOAST_KEY = "AddressType"

@HiltViewModel
class AddressTypePreferenceViewModel @Inject constructor(
@ApplicationContext private val context: Context,
Expand Down Expand Up @@ -76,7 +79,7 @@ class AddressTypePreferenceViewModel @Inject constructor(
title = context.getString(R.string.settings__addr_type__applying),
autoHide = false,
testTag = "AddressTypeApplyingToast",
replacedByNext = true,
replaceKey = ADDRESS_TYPE_TOAST_KEY,
)

val currentMonitored = _uiState.value.monitoredTypes.toMutableSet()
Expand All @@ -100,12 +103,14 @@ class AddressTypePreferenceViewModel @Inject constructor(
description = context.getString(R.string.settings__addr_type__now_using)
.replace("{type}", typeName),
testTag = "AddressTypeSettingsUpdatedToast",
replaceKey = ADDRESS_TYPE_TOAST_KEY,
)
} else {
ToastEventBus.send(
type = Toast.ToastType.WARNING,
title = context.getString(R.string.common__error),
description = result.exceptionOrNull()?.message,
replaceKey = ADDRESS_TYPE_TOAST_KEY,
)
}
}
Expand All @@ -123,7 +128,7 @@ class AddressTypePreferenceViewModel @Inject constructor(
type = Toast.ToastType.INFO,
title = context.getString(R.string.settings__addr_type__applying),
autoHide = false,
replacedByNext = true,
replaceKey = ADDRESS_TYPE_TOAST_KEY,
)

val repoResult = lightningRepo.setMonitoring(addressType, enabled)
Expand All @@ -138,12 +143,14 @@ class AddressTypePreferenceViewModel @Inject constructor(
ToastEventBus.send(
type = Toast.ToastType.SUCCESS,
title = context.getString(R.string.settings__addr_type__settings_updated),
replaceKey = ADDRESS_TYPE_TOAST_KEY,
)
} else {
ToastEventBus.send(
type = Toast.ToastType.WARNING,
title = context.getString(R.string.common__error),
description = monitoringErrorMessage(repoResult.exceptionOrNull()?.message),
replaceKey = ADDRESS_TYPE_TOAST_KEY,
)
}
}
Expand Down
14 changes: 12 additions & 2 deletions app/src/main/java/to/bitkit/ui/shared/toast/ToastEventBus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,20 @@ object ToastEventBus {
autoHide: Boolean = true,
visibilityTime: Long = Toast.VISIBILITY_TIME_DEFAULT,
testTag: String? = null,
replacedByNext: Boolean = false,
replaceKey: String? = null,
interruptible: Boolean = true,
) {
_events.emit(
Toast(type, title, description, autoHide, visibilityTime, testTag, replacedByNext = replacedByNext)
Toast(
type = type,
title = title,
description = description,
autoHide = autoHide,
visibilityTime = visibilityTime,
testTag = testTag,
interruptible = interruptible,
replaceKey = replaceKey,
)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,18 @@ class ToastQueueManager(private val scope: CoroutineScope) {

/**
* Add toast to queue. If queue is full, drops oldest. A shown or queued loading toast returns after the new one,
* and a shown toast that is not interruptible stays until it hides. A queued progress toast is replaced by the new
* one, as it would have been on screen.
* and a shown toast that is not interruptible stays until it hides. A queued toast is replaced by a new one with
* the same key, as it would have been on screen. A toast that is not interruptible and already shown or queued is
* not added again.
*/
fun enqueue(toast: Toast) {
if (!toast.interruptible && (toast == _currentToast.value || toast in _queue.value)) return
val isCurrentInterruptible = _currentToast.value?.interruptible != false
val interruptedLoading = _currentToast.value?.takeIf { it.isLoading && isCurrentInterruptible }
_queue.update { current ->
val (loading, others) = current.partition { it.isLoading }
// Drop oldest when queue full
val waiting = others.filterNot { it.replacedByNext }
val waiting = others.filterNot { it.replaceKey != null && it.replaceKey == toast.replaceKey }
(waiting + toast + loading + listOfNotNull(interruptedLoading)).takeLast(MAX_QUEUE_SIZE)
}
if (isCurrentInterruptible) dismissCurrentToast()
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9355,6 +9355,7 @@ class AppViewModel @Inject constructor(
autoHide: Boolean = true,
visibilityTime: Long = Toast.VISIBILITY_TIME_DEFAULT,
testTag: String? = null,
replaceKey: String? = null,
) {
toastManager.enqueue(
Toast(
Expand All @@ -9364,6 +9365,7 @@ class AppViewModel @Inject constructor(
autoHide = autoHide,
visibilityTime = visibilityTime,
testTag = testTag,
replaceKey = replaceKey,
)
)
}
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/to/bitkit/viewmodels/WalletViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -598,13 +598,15 @@ class WalletViewModel @Inject constructor(
swaps.restored.forEach { swapId ->
launch {
pubkySwapsRouteRepo.awaitClaim(swapId).onSuccess {
// The claim also opens the received sheet above the toasts: stay until dismissed so the
// user still sees why the money came back once that sheet closes.
// The claim also opens the received sheet above the toasts: stay until dismissed, with
// later toasts waiting behind, so the user still sees why the money came back once that
// sheet closes.
ToastEventBus.send(
type = Toast.ToastType.INFO,
title = context.getString(R.string.wallet__swap_route_restored_title),
description = context.getString(R.string.wallet__swap_route_restored_text),
autoHide = false,
interruptible = false,
Comment thread
ovitrif marked this conversation as resolved.
Resolved
)
}
}
Expand Down
11 changes: 8 additions & 3 deletions app/src/test/java/to/bitkit/ui/WalletViewModelTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,15 @@ class WalletViewModelTest : BaseUnitTest() {
)
lightningState.value = lightningState.value.copy(nodeLifecycleState = NodeLifecycleState.Running)

sut.restoreWallet("mnemonic", "passphrase")
walletState.value = walletState.value.copy(walletExists = true)
advanceUntilIdle()
ToastEventBus.events.test {
sut.restoreWallet("mnemonic", "passphrase")
walletState.value = walletState.value.copy(walletExists = true)
advanceUntilIdle()

val toast = awaitItem()
assertFalse(toast.autoHide)
assertFalse(toast.interruptible)
}
assertEquals(RestoreState.Completed(emptySet()), sut.restoreState.value)
verify(pubkySwapsRouteRepo).restoredSwaps()
verify(pubkySwapsRouteRepo).awaitClaim(eq("swap"), any(), anyOrNull())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,14 @@ class ToastQueueManagerTest : BaseUnitTest() {
type = Toast.ToastType.INFO,
title = "Applying changes",
autoHide = false,
replacedByNext = true,
replaceKey = "AddressType",
)
val resultToast = Toast(
type = Toast.ToastType.SUCCESS,
title = "Settings updated",
autoHide = true,
replaceKey = "AddressType",
)
val resultToast = Toast(type = Toast.ToastType.SUCCESS, title = "Settings updated", autoHide = true)
sut.enqueue(updateToast)
sut.enqueue(progressToast)
sut.enqueue(resultToast)
Expand All @@ -81,9 +86,14 @@ class ToastQueueManagerTest : BaseUnitTest() {
type = Toast.ToastType.INFO,
title = "Applying changes",
autoHide = false,
replacedByNext = true,
replaceKey = "AddressType",
)
val resultToast = Toast(
type = Toast.ToastType.SUCCESS,
title = "Settings updated",
autoHide = true,
replaceKey = "AddressType",
)
val resultToast = Toast(type = Toast.ToastType.SUCCESS, title = "Settings updated", autoHide = true)
sut.enqueue(updateToast)
sut.enqueue(onlineToast)
sut.enqueue(progressToast)
Expand Down Expand Up @@ -116,6 +126,85 @@ class ToastQueueManagerTest : BaseUnitTest() {
assertEquals(onlineToast, sut.currentToast.value)
}

@Test
fun `later toasts wait until a shown toast that is not interruptible is dismissed`() = test {
val sut = ToastQueueManager(backgroundScope)
val returnedToast = Toast(
type = Toast.ToastType.INFO,
title = "Payment returned",
autoHide = false,
interruptible = false,
)
val offlineToast = Toast(
type = Toast.ToastType.WARNING,
title = "Offline",
autoHide = true,
replaceKey = "Connectivity",
)
val onlineToast = Toast(
type = Toast.ToastType.SUCCESS,
title = "Back online",
autoHide = true,
replaceKey = "Connectivity",
)
sut.enqueue(returnedToast)

sut.enqueue(offlineToast)
sut.enqueue(onlineToast)
advanceTimeBy(Toast.VISIBILITY_TIME_DEFAULT * 2)

assertEquals(returnedToast, sut.currentToast.value)
sut.dismissCurrentToast()
assertEquals(onlineToast, sut.currentToast.value)
}

@Test
fun `only the latest connection status waits behind a toast that is not interruptible`() = test {
val sut = ToastQueueManager(backgroundScope)
val returnedToast = Toast(
type = Toast.ToastType.INFO,
title = "Payment returned",
autoHide = false,
interruptible = false,
)
val offlineToast = Toast(
type = Toast.ToastType.WARNING,
title = "Offline",
autoHide = true,
replaceKey = "Connectivity",
)
val onlineToast = offlineToast.copy(type = Toast.ToastType.SUCCESS, title = "Back online")
val resultToast = Toast(type = Toast.ToastType.SUCCESS, title = "Settings updated", autoHide = true)
sut.enqueue(returnedToast)

sut.enqueue(offlineToast)
sut.enqueue(onlineToast)
sut.enqueue(offlineToast.copy(description = "Again"))
sut.enqueue(resultToast)
sut.dismissCurrentToast()

assertEquals(offlineToast.copy(description = "Again"), sut.currentToast.value)
advanceTimeBy(Toast.VISIBILITY_TIME_DEFAULT + 1)
assertEquals(resultToast, sut.currentToast.value)
}

@Test
fun `a toast that is not interruptible is shown once when sent twice`() = test {
val sut = ToastQueueManager(backgroundScope)
val returnedToast = Toast(
type = Toast.ToastType.INFO,
title = "Payment returned",
autoHide = false,
interruptible = false,
)
sut.enqueue(returnedToast)

sut.enqueue(returnedToast.copy())
sut.dismissCurrentToast()

assertNull(sut.currentToast.value)
}

@Test
fun `loading toast returns after a toast that interrupts it`() = test {
val sut = ToastQueueManager(backgroundScope)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1134,12 +1134,12 @@ class AppViewModelSendFlowTest : BaseUnitTest() {
}

@Test
fun `a sent toast reaches the queue with its progress flag`() = test {
fun `a sent toast reaches the queue with its replace key`() = test {
val progressToast = Toast(
type = Toast.ToastType.INFO,
title = "Applying changes",
autoHide = false,
replacedByNext = true,
replaceKey = "AddressType",
)
clearInvocations(toastManager)

Expand Down
1 change: 1 addition & 0 deletions changelog.d/next/2030.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The "Payment returned to Savings" notice now stays on screen until you close it.