1
+ package com.smarttoolfactory.tutorial1_1basics.chapter9_animation
2
+
3
+ import androidx.compose.animation.core.Animatable
4
+ import androidx.compose.animation.core.LinearEasing
5
+ import androidx.compose.animation.core.tween
6
+ import androidx.compose.foundation.MutatorMutex
7
+ import androidx.compose.foundation.layout.Column
8
+ import androidx.compose.foundation.layout.fillMaxSize
9
+ import androidx.compose.foundation.layout.fillMaxWidth
10
+ import androidx.compose.foundation.layout.padding
11
+ import androidx.compose.material3.Button
12
+ import androidx.compose.material3.Text
13
+ import androidx.compose.runtime.Composable
14
+ import androidx.compose.runtime.remember
15
+ import androidx.compose.runtime.rememberCoroutineScope
16
+ import androidx.compose.ui.Modifier
17
+ import androidx.compose.ui.tooling.preview.Preview
18
+ import androidx.compose.ui.unit.dp
19
+ import androidx.compose.ui.unit.sp
20
+ import kotlinx.coroutines.CancellationException
21
+ import kotlinx.coroutines.launch
22
+ import kotlinx.coroutines.sync.Mutex
23
+ import kotlinx.coroutines.sync.withLock
24
+
25
+ @Preview
26
+ @Composable
27
+ fun CoroutinesTest () {
28
+
29
+ val mutex = remember {
30
+ Mutex ()
31
+ }
32
+
33
+ val mutatorMutex = remember {
34
+ MutatorMutex ()
35
+ }
36
+
37
+
38
+ val scope = rememberCoroutineScope()
39
+
40
+ val animatable = remember {
41
+ Animatable (0f )
42
+ }
43
+
44
+ Column (
45
+ modifier = Modifier .fillMaxSize().padding(24 .dp)
46
+ ) {
47
+
48
+ Text (
49
+ " Value: ${animatable.value.toInt()} " , fontSize = 26 .sp
50
+ )
51
+
52
+ Button (
53
+ modifier = Modifier .fillMaxWidth(),
54
+ onClick = {
55
+ scope.launch {
56
+ mutex.withLock {
57
+ animatable.snapTo(0f )
58
+ try {
59
+ animatable.animateTo(
60
+ targetValue = 100f ,
61
+ animationSpec = tween(5000 , easing = LinearEasing )
62
+ )
63
+ } catch (e: CancellationException ) {
64
+ println (" Exception: ${e.message} " )
65
+ }
66
+ }
67
+ }
68
+ }
69
+ ) {
70
+ Text (" Start with Mutex" )
71
+ }
72
+
73
+ Button (
74
+ modifier = Modifier .fillMaxWidth(),
75
+ onClick = {
76
+ scope.launch {
77
+ animatable.snapTo(0f )
78
+ try {
79
+ animatable.animateTo(
80
+ targetValue = 100f ,
81
+ animationSpec = tween(5000 , easing = LinearEasing )
82
+ )
83
+ } catch (e: CancellationException ) {
84
+ println (" Exception: $e " )
85
+ }
86
+ }
87
+ }
88
+ ) {
89
+ Text (" Start" )
90
+ }
91
+
92
+ Button (
93
+ modifier = Modifier .fillMaxWidth(),
94
+ onClick = {
95
+
96
+ scope.launch {
97
+ try {
98
+ mutatorMutex.mutate {
99
+ animatable.snapTo(0f )
100
+ try {
101
+ animatable.animateTo(
102
+ targetValue = 100f ,
103
+ animationSpec = tween(5000 , easing = LinearEasing )
104
+ )
105
+ } catch (e: CancellationException ) {
106
+ println (" Exception: $e " )
107
+ }
108
+ }
109
+ }catch (e: Exception ){
110
+ println (" MutatorMutexException: $e " )
111
+ }
112
+ }
113
+ }
114
+ ) {
115
+ Text (" Start with Mutex" )
116
+ }
117
+
118
+ }
119
+ }
0 commit comments