Skip to content

Commit e69d23d

Browse files
committed
Simplified initialIndex VS currentIndex
1 parent ffebf85 commit e69d23d

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

app/src/main/kotlin/com/darkrockstudios/app/securecamera/obfuscation/ObfuscatePhotoContent.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fun ObfuscatePhotoContent(
4545
outerScope: CoroutineScope,
4646
paddingValues: PaddingValues
4747
) {
48-
val viewModel: ObfuscatePhotoViewModel = koinViewModel()
48+
val viewModel: ObfuscatePhotoViewModel = koinViewModel(key = photoName)
4949
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
5050

5151
LaunchedEffect(photoName) {
@@ -60,10 +60,8 @@ fun ObfuscatePhotoContent(
6060
uiState.isCreatingRegion ||
6161
uiState.currentRegion != null
6262

63-
// State for discard confirmation dialog
6463
val showDiscardDialog = remember { mutableStateOf(false) }
6564

66-
// Handle system back button
6765
BackHandler(enabled = hasUnsavedChanges) {
6866
showDiscardDialog.value = true
6967
}

app/src/main/kotlin/com/darkrockstudios/app/securecamera/viewphoto/ViewPhotoContent.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,14 @@ fun ViewPhotoContent(
103103
}
104104

105105
if (uiState.photos.isNotEmpty()) {
106-
val listState =
107-
remember(uiState.initialIndex) { LazyListState(firstVisibleItemIndex = uiState.initialIndex) }
106+
val listState = remember { LazyListState(firstVisibleItemIndex = uiState.currentIndex) }
108107

109108
LaunchedEffect(listState) {
110109
snapshotFlow {
111110
listState.firstVisibleItemIndex to
112111
listState.firstVisibleItemScrollOffset
113112
}.collect { (idx, off) ->
114-
if (listState.firstVisibleItemIndex != viewModel.currentIndex) {
113+
if (listState.firstVisibleItemIndex != uiState.currentIndex) {
115114
viewModel.setCurrentPhotoIndex(listState.firstVisibleItemIndex)
116115
}
117116
}

app/src/main/kotlin/com/darkrockstudios/app/securecamera/viewphoto/ViewPhotoViewModel.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ class ViewPhotoViewModel(
2424
private val initialPhotoName: String,
2525
) : BaseViewModel<ViewPhotoUiState>() {
2626

27-
var currentIndex: Int = 0
28-
private set
27+
private var currentIndex: Int
28+
get() = uiState.value.currentIndex
29+
set(value) {
30+
_uiState.update { it.copy(currentIndex = value) }
31+
}
2932

3033
override fun createState() = ViewPhotoUiState()
3134

@@ -43,7 +46,7 @@ class ViewPhotoViewModel(
4346
_uiState.update {
4447
it.copy(
4548
photos = photos,
46-
initialIndex = initialIndex,
49+
currentIndex = initialIndex,
4750
hasPoisonPill = hasPoisonPill,
4851
isDecoy = isDecoy
4952
)
@@ -155,7 +158,7 @@ class ViewPhotoViewModel(
155158

156159
data class ViewPhotoUiState(
157160
val photos: List<PhotoDef> = emptyList(),
158-
val initialIndex: Int = 0,
161+
val currentIndex: Int = 0,
159162
val hasPoisonPill: Boolean = false,
160163
val isDecoy: Boolean = false,
161164
val isDecoyLoading: Boolean = false,

0 commit comments

Comments
 (0)