1
1
package com.getcode.ui.components
2
2
3
- import androidx.compose.animation.*
3
+ import androidx.compose.animation.AnimatedVisibility
4
4
import androidx.compose.animation.core.MutableTransitionState
5
5
import androidx.compose.animation.core.tween
6
+ import androidx.compose.animation.slideInVertically
7
+ import androidx.compose.animation.slideOutVertically
6
8
import androidx.compose.foundation.interaction.MutableInteractionSource
7
9
import androidx.compose.foundation.layout.Box
8
10
import androidx.compose.foundation.layout.fillMaxSize
9
- import androidx.compose.runtime.*
10
- import androidx.compose.runtime.livedata.observeAsState
11
+ import androidx.compose.runtime.Composable
12
+ import androidx.compose.runtime.LaunchedEffect
13
+ import androidx.compose.runtime.collectAsState
14
+ import androidx.compose.runtime.getValue
15
+ import androidx.compose.runtime.mutableLongStateOf
16
+ import androidx.compose.runtime.remember
17
+ import androidx.compose.runtime.rememberCoroutineScope
18
+ import androidx.compose.runtime.setValue
11
19
import androidx.compose.ui.Modifier
12
20
import com.getcode.CodeAppState
13
21
import com.getcode.manager.BottomBarManager
14
22
import com.getcode.ui.utils.rememberedClickable
15
- import java.util.*
16
- import kotlin.concurrent.timerTask
23
+ import kotlinx.coroutines.delay
24
+ import kotlinx.coroutines.launch
17
25
18
26
@Composable
19
27
fun BottomBarContainer (appState : CodeAppState ) {
28
+ val scope = rememberCoroutineScope()
20
29
val bottomBarMessage by appState.bottomBarMessage.collectAsState()
21
- val bottomBarVisibleState = remember { MutableTransitionState (false ) }
30
+ val bottomBarVisibleState = remember(bottomBarMessage?.id) { MutableTransitionState (false ) }
22
31
var bottomBarMessageDismissId by remember { mutableLongStateOf(0L ) }
23
- val onClose: (bottomBarActionType: BottomBarManager .BottomBarActionType ? ) -> Unit = {
32
+ val onClose: suspend (bottomBarActionType: BottomBarManager .BottomBarActionType ? ) -> Unit = {
24
33
bottomBarMessageDismissId = bottomBarMessage?.id ? : 0
25
34
bottomBarVisibleState.targetState = false
26
-
27
35
bottomBarMessage?.onClose?.invoke(it)
28
36
29
- Timer ().schedule(timerTask {
30
- BottomBarManager .setMessageShown(bottomBarMessageDismissId)
31
- }, 100 )
32
- }
33
- val onBackPressed = {
34
- if (bottomBarMessage?.isDismissible == true ) onClose(null )
37
+ delay(100 )
38
+ BottomBarManager .setMessageShown(bottomBarMessageDismissId)
35
39
}
36
40
37
- if (! bottomBarVisibleState.targetState && ! bottomBarVisibleState.currentState) {
38
- Timer ().schedule(timerTask {
41
+ // handle changes in visible state
42
+ LaunchedEffect (bottomBarVisibleState) {
43
+ if (! bottomBarVisibleState.targetState && ! bottomBarVisibleState.currentState) {
44
+ delay(50 )
39
45
bottomBarVisibleState.targetState = bottomBarMessage != null
40
- }, 50 )
41
46
42
- if (bottomBarMessageDismissId == bottomBarMessage?.id) {
47
+ if (bottomBarMessageDismissId == bottomBarMessage?.id) {
48
+ bottomBarMessageDismissId = 0
49
+ }
50
+ }
51
+ }
52
+
53
+ // handle provided timeout duration; triggering onClose with no action
54
+ LaunchedEffect (bottomBarMessage) {
55
+ bottomBarMessage?.timeoutSeconds?.let {
56
+ delay(it * 1000L )
43
57
onClose(null )
44
- bottomBarMessageDismissId = 0
45
58
}
46
59
}
47
60
61
+ // add transparent touch handler if dismissible
48
62
if (bottomBarVisibleState.targetState && bottomBarMessage != null ) {
49
- Box (
50
- modifier = Modifier
51
- .fillMaxSize()
52
- .rememberedClickable(indication = null ,
53
- interactionSource = remember { MutableInteractionSource () }) {
54
- if (bottomBarMessage?.isDismissible == true ) onClose(null )
55
- }
56
- )
63
+ bottomBarMessage?.let {
64
+ if (it.isDismissible) {
65
+ Box (
66
+ modifier = Modifier
67
+ .fillMaxSize()
68
+ .rememberedClickable(indication = null ,
69
+ interactionSource = remember { MutableInteractionSource () }
70
+ ) {
71
+ scope.launch { onClose(null ) }
72
+ }
73
+ )
74
+ }
75
+ }
76
+
57
77
}
58
78
59
79
AnimatedVisibility (
@@ -68,14 +88,9 @@ fun BottomBarContainer(appState: CodeAppState) {
68
88
animationSpec = tween(300 )
69
89
),
70
90
) {
71
- BottomBarView (bottomBarMessage = bottomBarMessage, onClose, onBackPressed)
72
- }
73
-
74
- LaunchedEffect (bottomBarMessage) {
75
- bottomBarMessage?.timeoutSeconds?.let { timeout ->
76
- Timer ().schedule(timerTask {
77
- onClose(null )
78
- }, timeout.toLong() * 1000 )
91
+ val closeWith: (BottomBarManager .BottomBarActionType ? ) -> Unit = { type ->
92
+ scope.launch { onClose(type) }
79
93
}
94
+ BottomBarView (bottomBarMessage = bottomBarMessage, closeWith, onBackPressed = { closeWith(null )})
80
95
}
81
96
}
0 commit comments