-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathIsOnlineTracker.kt
More file actions
51 lines (44 loc) · 1.72 KB
/
IsOnlineTracker.kt
File metadata and controls
51 lines (44 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package to.bitkit.ui.components
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import to.bitkit.R
import to.bitkit.models.Toast
import to.bitkit.repositories.ConnectivityState
import to.bitkit.viewmodels.AppViewModel
@Composable
fun IsOnlineTracker(
app: AppViewModel,
) {
val context = LocalContext.current
val connectivityState by app.isOnline.collectAsStateWithLifecycle(initialValue = ConnectivityState.CONNECTED)
val (isFirstEmission, setIsFirstEmission) = remember { mutableStateOf(true) }
LaunchedEffect(connectivityState) {
// Skip the first emission to prevent toast on startup
if (isFirstEmission) {
setIsFirstEmission(false)
return@LaunchedEffect
}
when (connectivityState) {
ConnectivityState.CONNECTED -> {
app.toast(
type = Toast.ToastType.SUCCESS,
title = context.getString(R.string.other__connection_back_title),
description = context.getString(R.string.other__connection_back_msg),
)
}
ConnectivityState.DISCONNECTED -> {
app.toast(
type = Toast.ToastType.WARNING,
title = context.getString(R.string.other__connection_issue),
description = context.getString(R.string.other__connection_issue_explain),
)
}
else -> Unit
}
}
}