Skip to content

Commit 5fd08b2

Browse files
committed
add can flows
1 parent b8f5d57 commit 5fd08b2

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

UndoRedo/src/main/java/com/abada/undoredo/UndoRedoManager.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.abada.undoredo
22

33
import androidx.lifecycle.SavedStateHandle
44
import kotlinx.coroutines.flow.MutableStateFlow
5+
import kotlinx.coroutines.flow.StateFlow
56

67
class UndoRedoManager(
78
private val initialStates: Map<String, Any>,
@@ -10,7 +11,18 @@ class UndoRedoManager(
1011
) {
1112

1213
private val stack = MutableStateFlow(listOf<UndoRedoItem>())
14+
15+
private val _canUndoFlow = MutableStateFlow(false)
16+
val canUndoFlow: StateFlow<Boolean> = _canUndoFlow
17+
18+
private val _canRedoFlow = MutableStateFlow(false)
19+
val canRedoFlow: StateFlow<Boolean> = _canRedoFlow
1320
private var index: Int = -1
21+
set(value) {
22+
field = value
23+
_canRedoFlow.value = (field + 1) in stack.value.indices
24+
_canUndoFlow.value = (field) >= 0
25+
}
1426

1527
init {
1628
if (maxSize < 5)
@@ -19,14 +31,14 @@ class UndoRedoManager(
1931
}
2032

2133
fun undo() {
22-
index.takeIf { it >= 0 }?.let {
34+
index.takeIf { canUndoFlow.value }?.let {
2335
val undoRedoItem = stack.value[it]
2436
savedStateHandle[undoRedoItem.key] = undoRedoItem.prevValue
2537
index = (it - 1)
2638
}
2739
}
2840

29-
fun redo() = index.let {
41+
fun redo() = index.takeIf { canRedoFlow.value }?.let {
3042
if (it + 1 in stack.value.indices) {
3143
index = it + 1
3244
val currentState = stack.value[it + 1]

app/src/main/java/com/abada/undoredomanager/MainActivity.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class MainActivity : ComponentActivity() {
3333
// A surface container using the 'background' color from the theme
3434
val text1 by viewModel.text1.collectAsState("")
3535
val text2 by viewModel.text2.collectAsState("")
36+
37+
val canUndo by viewModel.canUndo.collectAsState()
38+
val canRedo by viewModel.canRedo.collectAsState()
3639
Surface(
3740
modifier = Modifier.fillMaxSize(),
3841
color = MaterialTheme.colorScheme.background
@@ -43,10 +46,10 @@ class MainActivity : ComponentActivity() {
4346
) {
4447
TextField(value = text1, onValueChange = viewModel::onText1Changed)
4548
TextField(value = text2, onValueChange = viewModel::onText2Changed)
46-
Button(onClick = viewModel::undo) {
49+
Button(onClick = viewModel::undo, enabled = canUndo) {
4750
Text("Undo")
4851
}
49-
Button(onClick = viewModel::redo) {
52+
Button(onClick = viewModel::redo, enabled = canRedo) {
5053
Text("redo")
5154
}
5255
}
@@ -67,7 +70,8 @@ class MyViewModel(
6770
savedStateHandle
6871
)
6972

70-
73+
val canUndo = undoRedoManager.canUndoFlow
74+
val canRedo = undoRedoManager.canRedoFlow
7175
val text1 = savedStateHandle.getStateFlow("text1", "")
7276
val text2 = savedStateHandle.getStateFlow("text2", "")
7377
fun onText1Changed(text: String) {

0 commit comments

Comments
 (0)