Skip to content

Commit ff0c3c0

Browse files
committed
DocumentScreen: Button to start a new document
1 parent 8ed0423 commit ff0c3c0

File tree

5 files changed

+73
-8
lines changed

5 files changed

+73
-8
lines changed

app/src/main/java/org/mydomain/myscan/ImageRepository.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,11 @@ class ImageRepository(appFilesDir: File) {
5252
file.delete()
5353
fileNames.remove(id)
5454
}
55-
}
55+
56+
fun clear() {
57+
fileNames.clear()
58+
scanDir.listFiles()?.forEach {
59+
file -> file.delete()
60+
}
61+
}
62+
}

app/src/main/java/org/mydomain/myscan/MainActivity.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ class MainActivity : ComponentActivity() {
7474
toCameraScreen = { viewModel.navigateTo(Screen.Camera) },
7575
onSavePressed = savePdf(viewModel, context),
7676
onSharePressed = sharePdf(viewModel, context),
77+
onStartNew = {
78+
viewModel.startNewDocument()
79+
viewModel.navigateTo(Screen.Camera) },
7780
onDeleteImage = { id -> viewModel.deletePage(id) }
7881
)
7982
}

app/src/main/java/org/mydomain/myscan/MainViewModel.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ class MainViewModel(
135135
_pageIds.value = imageRepository.imageIds()
136136
}
137137

138+
fun startNewDocument() {
139+
_pageIds.value = listOf()
140+
viewModelScope.launch {
141+
imageRepository.clear()
142+
}
143+
}
144+
138145
fun pageCount(): Int = pageIds.value.size
139146

140147
fun getBitmap(id: String): Bitmap? {

app/src/main/java/org/mydomain/myscan/view/DocumentScreen.kt

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ import androidx.compose.foundation.lazy.itemsIndexed
3636
import androidx.compose.material.icons.Icons
3737
import androidx.compose.material.icons.automirrored.filled.ArrowBack
3838
import androidx.compose.material.icons.filled.Add
39-
import androidx.compose.material.icons.filled.Close
4039
import androidx.compose.material.icons.filled.Download
40+
import androidx.compose.material.icons.filled.RestartAlt
4141
import androidx.compose.material.icons.filled.Share
4242
import androidx.compose.material.icons.outlined.Delete
43+
import androidx.compose.material3.AlertDialog
4344
import androidx.compose.material3.BottomAppBar
4445
import androidx.compose.material3.Button
4546
import androidx.compose.material3.ExperimentalMaterial3Api
@@ -50,13 +51,15 @@ import androidx.compose.material3.MaterialTheme
5051
import androidx.compose.material3.Scaffold
5152
import androidx.compose.material3.SmallFloatingActionButton
5253
import androidx.compose.material3.Text
54+
import androidx.compose.material3.TextButton
5355
import androidx.compose.material3.TopAppBar
5456
import androidx.compose.material3.TopAppBarDefaults
5557
import androidx.compose.runtime.Composable
5658
import androidx.compose.runtime.LaunchedEffect
5759
import androidx.compose.runtime.MutableIntState
5860
import androidx.compose.runtime.MutableState
5961
import androidx.compose.runtime.mutableIntStateOf
62+
import androidx.compose.runtime.mutableStateOf
6063
import androidx.compose.runtime.saveable.rememberSaveable
6164
import androidx.compose.ui.Alignment
6265
import androidx.compose.ui.Modifier
@@ -78,9 +81,11 @@ fun DocumentScreen(
7881
toCameraScreen: () -> Unit,
7982
onSavePressed: () -> Unit,
8083
onSharePressed: () -> Unit,
84+
onStartNew: () -> Unit,
8185
onDeleteImage: (String) -> Unit,
8286
) {
8387
// TODO Check how often images are loaded
88+
var showDialog = rememberSaveable { mutableStateOf(false) }
8489
val currentPageIndex = rememberSaveable { mutableIntStateOf(0) }
8590
if (currentPageIndex.intValue >= pageIds.size) {
8691
currentPageIndex.intValue = pageIds.size - 1
@@ -122,14 +127,19 @@ fun DocumentScreen(
122127
}
123128
},
124129
floatingActionButton = {
125-
FloatingActionButton(onClick = toCameraScreen) {
126-
Icon(Icons.Default.Close, contentDescription = "Close")
130+
FloatingActionButton(onClick = { showDialog.value = true }) {
131+
Icon(Icons.Default.RestartAlt, contentDescription = "Close")
127132
}
128133
}
129134
)
130135
}
131136
}
132-
) { padding -> DocumentPreview(pageIds, imageLoader, currentPageIndex, onDeleteImage, padding) }
137+
) { padding ->
138+
DocumentPreview(pageIds, imageLoader, currentPageIndex, onDeleteImage, padding)
139+
if (showDialog.value) {
140+
NewDocumentDialog(onConfirm = onStartNew, showDialog)
141+
}
142+
}
133143
}
134144

135145
@Composable
@@ -171,13 +181,16 @@ private fun DocumentPreview(
171181
}
172182
SmallFloatingActionButton(
173183
onClick = { onDeleteImage(imageId) },
174-
modifier = Modifier.align(Alignment.TopEnd).padding(4.dp)
184+
modifier = Modifier
185+
.align(Alignment.TopEnd)
186+
.padding(4.dp)
175187
) {
176188
Icon(imageVector = Icons.Outlined.Delete, contentDescription = "Delete page")
177189
}
178190
Text("${currentPageIndex.value + 1} / ${pageIds.size}",
179191
color = MaterialTheme.colorScheme.inverseOnSurface,
180-
modifier = Modifier.align(Alignment.BottomEnd)
192+
modifier = Modifier
193+
.align(Alignment.BottomEnd)
181194
.padding(all = 8.dp)
182195
.background(MaterialTheme.colorScheme.inverseSurface.copy(alpha = 0.5f))
183196
.padding(all = 4.dp)
@@ -238,6 +251,28 @@ private fun PageList(
238251
}
239252
}
240253

254+
@Composable
255+
fun NewDocumentDialog(onConfirm: () -> Unit, showDialog: MutableState<Boolean>) {
256+
AlertDialog(
257+
title = { Text("New document") },
258+
text = { Text("The current document will be lost if you haven't saved it. Do you want to continue?") },
259+
confirmButton = {
260+
TextButton (onClick = {
261+
showDialog.value = false
262+
onConfirm()
263+
}) {
264+
Text("Yes")
265+
}
266+
},
267+
dismissButton = {
268+
TextButton(onClick = { showDialog.value = false }) {
269+
Text("Cancel")
270+
}
271+
},
272+
onDismissRequest = { showDialog.value = false },
273+
)
274+
}
275+
241276
@Composable
242277
@Preview
243278
fun DocumentScreenPreview() {
@@ -253,6 +288,7 @@ fun DocumentScreenPreview() {
253288
toCameraScreen = {},
254289
onSavePressed = {},
255290
onSharePressed = {},
291+
onStartNew = {},
256292
onDeleteImage = { _ -> {} }
257293
)
258294
}

app/src/test/java/org/mydomain/myscan/ImageRepositoryTest.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,16 @@ class ImageRepositoryTest {
7777
assertThat(repo.imageIds()).isEmpty()
7878
assertThat(repo.getContent("x")).isNull()
7979
}
80-
}
80+
81+
@Test
82+
fun `clear should delete pages`() {
83+
val bytes = byteArrayOf(101, 102, 103)
84+
val repo1 = repo()
85+
repo1.add(bytes)
86+
assertThat(repo1.imageIds()).isNotEmpty()
87+
repo1.clear()
88+
assertThat(repo1.imageIds()).isEmpty()
89+
val repo2 = repo()
90+
assertThat(repo2.imageIds()).isEmpty()
91+
}
92+
}

0 commit comments

Comments
 (0)