Skip to content

Commit 8c33156

Browse files
committed
fix: implement queue manager
1 parent e278aaa commit 8c33156

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed

app/src/main/java/to/bitkit/ui/MainActivity.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,13 @@ class MainActivity : FragmentActivity() {
166166
}
167167
}
168168

169+
val currentToast by appViewModel.currentToast.collectAsStateWithLifecycle()
169170
ToastOverlay(
170-
toast = appViewModel.currentToast,
171+
toast = currentToast,
171172
hazeState = hazeState,
172-
onDismiss = {
173-
appViewModel.hideToast()
174-
}
173+
onDismiss = { appViewModel.hideToast() },
174+
onDragStart = { appViewModel.pauseToast() },
175+
onDragEnd = { appViewModel.resumeToast() }
175176
)
176177

177178
val transactionSheetDetails by appViewModel.transactionSheet.collectAsStateWithLifecycle()

app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import kotlinx.coroutines.delay
3737
import kotlinx.coroutines.flow.MutableSharedFlow
3838
import kotlinx.coroutines.flow.MutableStateFlow
3939
import kotlinx.coroutines.flow.SharingStarted
40+
import kotlinx.coroutines.flow.StateFlow
4041
import kotlinx.coroutines.flow.asSharedFlow
4142
import kotlinx.coroutines.flow.asStateFlow
4243
import kotlinx.coroutines.flow.first
@@ -96,6 +97,7 @@ import to.bitkit.repositories.WalletRepo
9697
import to.bitkit.services.AppUpdaterService
9798
import to.bitkit.ui.Routes
9899
import to.bitkit.ui.components.Sheet
100+
import to.bitkit.ui.shared.toast.ToastQueueManager
99101
import to.bitkit.ui.components.TimedSheetType
100102
import to.bitkit.ui.shared.toast.ToastEventBus
101103
import to.bitkit.ui.sheets.SendRoute
@@ -1518,8 +1520,8 @@ class AppViewModel @Inject constructor(
15181520
// endregion
15191521

15201522
// region Toasts
1521-
var currentToast by mutableStateOf<Toast?>(null)
1522-
private set
1523+
private val toastManager = ToastQueueManager(viewModelScope)
1524+
val currentToast: StateFlow<Toast?> = toastManager.currentToast
15231525

15241526
fun toast(
15251527
type: Toast.ToastType,
@@ -1529,20 +1531,16 @@ class AppViewModel @Inject constructor(
15291531
visibilityTime: Long = Toast.VISIBILITY_TIME_DEFAULT,
15301532
testTag: String? = null,
15311533
) {
1532-
currentToast = Toast(
1533-
type = type,
1534-
title = title,
1535-
description = description,
1536-
autoHide = autoHide,
1537-
visibilityTime = visibilityTime,
1538-
testTag = testTag,
1534+
toastManager.enqueue(
1535+
Toast(
1536+
type = type,
1537+
title = title,
1538+
description = description,
1539+
autoHide = autoHide,
1540+
visibilityTime = visibilityTime,
1541+
testTag = testTag,
1542+
)
15391543
)
1540-
if (autoHide) {
1541-
viewModelScope.launch {
1542-
delay(visibilityTime)
1543-
currentToast = null
1544-
}
1545-
}
15461544
}
15471545

15481546
fun toast(error: Throwable) {
@@ -1560,7 +1558,19 @@ class AppViewModel @Inject constructor(
15601558
}
15611559

15621560
fun hideToast() {
1563-
currentToast = null
1561+
toastManager.dismissCurrentToast()
1562+
}
1563+
1564+
fun pauseToast() {
1565+
toastManager.pauseCurrentToast()
1566+
}
1567+
1568+
fun resumeToast() {
1569+
toastManager.resumeCurrentToast()
1570+
}
1571+
1572+
fun clearToastQueue() {
1573+
toastManager.clear()
15641574
}
15651575
// endregion
15661576

0 commit comments

Comments
 (0)