Skip to content
This repository was archived by the owner on Apr 23, 2026. It is now read-only.

Commit 104653d

Browse files
committed
refactor: extract composables to his own files
1 parent 435c346 commit 104653d

4 files changed

Lines changed: 129 additions & 102 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.eps.todoturtle.nfc.ui
2+
3+
import androidx.compose.animation.AnimatedVisibility
4+
import androidx.compose.animation.core.tween
5+
import androidx.compose.animation.fadeIn
6+
import androidx.compose.animation.fadeOut
7+
import androidx.compose.foundation.layout.Box
8+
import androidx.compose.foundation.layout.ColumnScope
9+
import androidx.compose.material3.Text
10+
import androidx.compose.runtime.Composable
11+
import androidx.compose.runtime.LaunchedEffect
12+
import androidx.compose.runtime.getValue
13+
import androidx.compose.runtime.mutableStateOf
14+
import androidx.compose.runtime.saveable.rememberSaveable
15+
import androidx.compose.runtime.setValue
16+
import androidx.compose.ui.Alignment
17+
import androidx.compose.ui.Modifier
18+
import androidx.compose.ui.text.style.TextAlign
19+
import kotlinx.coroutines.delay
20+
21+
@Composable
22+
fun ColumnScope.AnimatedDisappearingText(
23+
modifier: Modifier = Modifier,
24+
animationDuration: Int = 2000,
25+
timeShowing: Long = 4000,
26+
timeDisappearing: Long = 3000,
27+
text: String,
28+
) {
29+
Box(modifier = modifier) {
30+
var visible by rememberSaveable { mutableStateOf(true) }
31+
this@AnimatedDisappearingText.AnimatedVisibility(
32+
modifier = Modifier.align(Alignment.BottomCenter),
33+
visible = visible,
34+
enter = fadeIn(animationSpec = tween(durationMillis = animationDuration)),
35+
exit = fadeOut(animationSpec = tween(durationMillis = animationDuration)),
36+
) {
37+
Text(textAlign = TextAlign.Center, text = text)
38+
}
39+
LaunchEffect(firstWait = timeShowing, secondWait = timeDisappearing) { visible = !visible }
40+
}
41+
}
42+
43+
@Composable
44+
private fun LaunchEffect(firstWait: Long = 4000, secondWait: Long = 3000, change: () -> Unit) {
45+
LaunchedEffect(
46+
key1 = true,
47+
block = {
48+
while (true) {
49+
delay(firstWait)
50+
change()
51+
delay(secondWait)
52+
change()
53+
}
54+
},
55+
)
56+
}

app/src/main/java/com/eps/todoturtle/nfc/ui/LoadingAnimation.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import androidx.compose.runtime.Composable
1818
import androidx.compose.runtime.LaunchedEffect
1919
import androidx.compose.runtime.getValue
2020
import androidx.compose.runtime.mutableStateOf
21-
import androidx.compose.runtime.remember
21+
import androidx.compose.runtime.saveable.rememberSaveable
2222
import androidx.compose.runtime.setValue
2323
import androidx.compose.ui.Alignment
2424
import androidx.compose.ui.Modifier
@@ -32,7 +32,7 @@ import kotlinx.coroutines.delay
3232

3333
@Composable
3434
fun LoadingAnimation(modifier: Modifier = Modifier) {
35-
var rotateOuter by remember {
35+
var rotateOuter by rememberSaveable {
3636
mutableStateOf(false)
3737
}
3838
val angle by animateFloatAsState(
@@ -89,7 +89,7 @@ fun LoadingAnimation(modifier: Modifier = Modifier) {
8989
}
9090

9191
@Composable
92-
fun SmallArc(angle: Float) {
92+
private fun SmallArc(angle: Float) {
9393
val color = colorScheme.background
9494
Box(Modifier.rotate(angle)) {
9595
Canvas(
@@ -114,7 +114,7 @@ fun SmallArc(angle: Float) {
114114
}
115115

116116
@Composable
117-
fun BigArc(angle: Float) {
117+
private fun BigArc(angle: Float) {
118118
val color = colorScheme.primary
119119
Box(Modifier.rotate(angle)) {
120120
Canvas(
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.eps.todoturtle.nfc.ui
2+
3+
import androidx.compose.runtime.Composable
4+
import androidx.compose.ui.res.stringResource
5+
import com.eps.todoturtle.R
6+
import com.eps.todoturtle.ui.ErrorAlert
7+
8+
@Composable
9+
fun ProgrammingError() {
10+
ErrorAlert(
11+
stringResource(R.string.programming_error_nfc_tag),
12+
stringResource(R.string.programing_error_explain_nfc_tag),
13+
stringResource(R.string.send_error_nfc_tag),
14+
) {}
15+
}
16+
17+
@Composable
18+
fun NfcNotEnabled(action: () -> Unit) {
19+
ErrorAlert(
20+
stringResource(R.string.nfc_not_enabled),
21+
stringResource(R.string.nfc_not_enabled_solution),
22+
stringResource(R.string.enable_nfc),
23+
action,
24+
)
25+
}
26+
27+
@Composable
28+
fun NfcNotSupported(action: () -> Unit) {
29+
ErrorAlert(
30+
stringResource(R.string.nfc_not_supported),
31+
stringResource(R.string.nfc_not_supported_solution),
32+
stringResource(R.string.close),
33+
action,
34+
)
35+
}
36+
37+
@Composable
38+
fun TagNotWriteable(action: () -> Unit) {
39+
ErrorAlert(
40+
stringResource(R.string.nfc_tag_not_supported),
41+
stringResource(R.string.nfc_not_supported_solution),
42+
stringResource(R.string.close),
43+
action,
44+
)
45+
}
46+
47+
@Composable
48+
fun TagLost(action: () -> Unit) {
49+
ErrorAlert(
50+
stringResource(R.string.move_too_fast),
51+
stringResource(R.string.move_too_fast_solution),
52+
stringResource(R.string.retry),
53+
action,
54+
)
55+
}
56+
57+
@Composable
58+
fun UnknownError(action: () -> Unit) {
59+
ErrorAlert(
60+
stringResource(R.string.unknown_error_nfc),
61+
stringResource(R.string.unknown_error_nfc_solution),
62+
stringResource(R.string.retry),
63+
action,
64+
)
65+
}

app/src/main/java/com/eps/todoturtle/nfc/ui/WriteDeviceScreen.kt

Lines changed: 4 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
package com.eps.todoturtle.nfc.ui
22

3-
import androidx.compose.animation.AnimatedVisibility
4-
import androidx.compose.animation.core.tween
5-
import androidx.compose.animation.fadeIn
6-
import androidx.compose.animation.fadeOut
73
import androidx.compose.foundation.layout.Arrangement
8-
import androidx.compose.foundation.layout.Box
94
import androidx.compose.foundation.layout.Column
10-
import androidx.compose.foundation.layout.ColumnScope
115
import androidx.compose.foundation.layout.fillMaxSize
126
import androidx.compose.foundation.layout.padding
13-
import androidx.compose.material3.Text
147
import androidx.compose.runtime.Composable
15-
import androidx.compose.runtime.LaunchedEffect
168
import androidx.compose.runtime.getValue
179
import androidx.compose.runtime.mutableStateOf
1810
import androidx.compose.runtime.saveable.rememberSaveable
@@ -21,16 +13,13 @@ import androidx.compose.ui.Alignment
2113
import androidx.compose.ui.Modifier
2214
import androidx.compose.ui.platform.LocalContext
2315
import androidx.compose.ui.res.stringResource
24-
import androidx.compose.ui.text.style.TextAlign
2516
import androidx.compose.ui.tooling.preview.Preview
2617
import androidx.compose.ui.unit.dp
2718
import com.eps.todoturtle.R
2819
import com.eps.todoturtle.nfc.logic.NfcWriteViewModel
2920
import com.eps.todoturtle.nfc.logic.WriteOperationStatus
3021
import com.eps.todoturtle.shared.logic.extensions.dataStore
31-
import com.eps.todoturtle.ui.ErrorAlert
3222
import com.eps.todoturtle.ui.theme.ToDoTurtleTheme
33-
import kotlinx.coroutines.delay
3423

3524
@Composable
3625
fun WriteDevice(
@@ -77,73 +66,17 @@ fun WriteDevice(
7766
}
7867
}
7968

80-
@Composable
81-
fun ProgrammingError() {
82-
ErrorAlert(
83-
stringResource(R.string.programming_error_nfc_tag),
84-
stringResource(R.string.programing_error_explain_nfc_tag),
85-
stringResource(R.string.send_error_nfc_tag),
86-
) {}
87-
}
88-
89-
@Composable
90-
fun NfcNotEnabled(action: () -> Unit) {
91-
ErrorAlert(
92-
stringResource(R.string.nfc_not_enabled),
93-
stringResource(R.string.nfc_not_enabled_solution),
94-
stringResource(R.string.enable_nfc),
95-
action,
96-
)
97-
}
98-
99-
@Composable
100-
fun NfcNotSupported(action: () -> Unit) {
101-
ErrorAlert(
102-
stringResource(R.string.nfc_not_supported),
103-
stringResource(R.string.nfc_not_supported_solution),
104-
stringResource(R.string.close),
105-
action,
106-
)
107-
}
108-
109-
@Composable
110-
fun TagNotWriteable(action: () -> Unit) {
111-
ErrorAlert(
112-
stringResource(R.string.nfc_tag_not_supported),
113-
stringResource(R.string.nfc_not_supported_solution),
114-
stringResource(R.string.close),
115-
action,
116-
)
117-
}
118-
119-
@Composable
120-
fun TagLost(action: () -> Unit) {
121-
ErrorAlert(
122-
stringResource(R.string.move_too_fast),
123-
stringResource(R.string.move_too_fast_solution),
124-
stringResource(R.string.retry),
125-
action,
126-
)
127-
}
128-
129-
@Composable
130-
fun UnknownError(action: () -> Unit) {
131-
ErrorAlert(
132-
stringResource(R.string.unknown_error_nfc),
133-
stringResource(R.string.unknown_error_nfc_solution),
134-
stringResource(R.string.retry),
135-
action,
136-
)
137-
}
138-
13969
@Composable
14070
fun WaitForNfcTag() {
14171
Column(
14272
verticalArrangement = Arrangement.Center,
14373
horizontalAlignment = Alignment.CenterHorizontally,
14474
modifier = Modifier.fillMaxSize(),
14575
) {
146-
AnimatedDisappearingText(text = stringResource(R.string.approach_nfc_tag), modifier = Modifier.weight(1f))
76+
AnimatedDisappearingText(
77+
text = stringResource(R.string.approach_nfc_tag),
78+
modifier = Modifier.weight(1f),
79+
)
14780
LoadingAnimation(
14881
modifier = Modifier
14982
.padding(16.dp)
@@ -152,33 +85,6 @@ fun WaitForNfcTag() {
15285
}
15386
}
15487

155-
@Composable
156-
fun ColumnScope.AnimatedDisappearingText(text: String, modifier: Modifier = Modifier) {
157-
Box(modifier = modifier) {
158-
var visible by rememberSaveable { mutableStateOf(true) }
159-
val animationDuration = 2000
160-
this@AnimatedDisappearingText.AnimatedVisibility(
161-
modifier = Modifier.align(Alignment.BottomCenter),
162-
visible = visible,
163-
enter = fadeIn(animationSpec = tween(durationMillis = animationDuration)),
164-
exit = fadeOut(animationSpec = tween(durationMillis = animationDuration)),
165-
) {
166-
Text(textAlign = TextAlign.Center, text = text)
167-
}
168-
LaunchedEffect(
169-
key1 = true,
170-
block = {
171-
while (true) {
172-
delay(4000)
173-
visible = !visible
174-
delay(3000)
175-
visible = !visible
176-
}
177-
},
178-
)
179-
}
180-
}
181-
18288
@Preview(showBackground = true)
18389
@Composable
18490
fun WriteDeviceScreenPreview() {

0 commit comments

Comments
 (0)