From cf406d5860ef68d46573fc2b995d0ac6e50304f0 Mon Sep 17 00:00:00 2001 From: William Brawner Date: Sun, 11 Jul 2021 13:42:33 -0600 Subject: [PATCH] Add ability to remove pages that failed to scan --- .../java/com/wbrawner/skerge/MainActivity.kt | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/wbrawner/skerge/MainActivity.kt b/app/src/main/java/com/wbrawner/skerge/MainActivity.kt index 5540bc7..aff727f 100644 --- a/app/src/main/java/com/wbrawner/skerge/MainActivity.kt +++ b/app/src/main/java/com/wbrawner/skerge/MainActivity.kt @@ -38,6 +38,7 @@ class MainActivity : ComponentActivity() { ScanScreen( addButtonClicked = { viewModel.requestScan(scansDirectory) }, shareButtonClicked = { sharePdf(viewModel.pages.replayCache.first()) }, + removePage = viewModel::removePage, pagesFlow = viewModel.pages ) } @@ -61,6 +62,7 @@ class MainActivity : ComponentActivity() { fun ScanScreen( addButtonClicked: () -> Unit, shareButtonClicked: () -> Unit, + removePage: (Page) -> Unit, pagesFlow: SharedFlow> ) { val pages = pagesFlow.collectAsState(initial = emptyList()) @@ -87,7 +89,7 @@ fun ScanScreen( if (pages.value.isEmpty()) { EmptyDocumentView() } else { - PageList(pages.value) + PageList(pages.value, removePage) } } } @@ -111,7 +113,7 @@ fun EmptyDocumentView() { } @Composable -fun PageList(pages: List) { +fun PageList(pages: List, removePage: (Page) -> Unit) { LazyColumn(modifier = Modifier.fillMaxSize()) { itemsIndexed(items = pages) { index, page -> val topPadding = if (index == 0) 16.dp else 8.dp @@ -122,14 +124,14 @@ fun PageList(pages: List) { .padding(start = 16.dp, end = 16.dp, top = topPadding, bottom = bottomPadding) .aspectRatio(8.5f / 11), ) { - PagePreview(page = page) + PagePreview(page = page, removePage) } } } } @Composable -fun PagePreview(page: Page) { +fun PagePreview(page: Page, removePage: (Page) -> Unit) { val (pageBitmap, setPageBitmap) = remember { mutableStateOf(null) } LaunchedEffect(page.file) { setPageBitmap(page.loadBitmap().first) @@ -140,7 +142,21 @@ fun PagePreview(page: Page) { bitmap = it.asImageBitmap(), contentDescription = null ) - } ?: LoadingPage() + } + ?: page.error?.let { + Column( + modifier = Modifier.fillMaxSize(), + verticalArrangement = Arrangement.Center, + horizontalAlignment = Alignment.CenterHorizontally + ) { + Text(it.localizedMessage ?: "An unknown error has occurred") + TextButton( + onClick = { removePage(page) } + ) { + Text("Remove page") + } + } + } ?: LoadingPage() } @Composable @@ -157,7 +173,7 @@ fun LoadingPage() { @Composable fun DefaultPreview() { SkergeTheme { - ScanScreen({}, {}, MutableSharedFlow()) + ScanScreen({}, {}, {}, MutableSharedFlow()) } } @@ -165,6 +181,14 @@ fun DefaultPreview() { @Composable fun LoadingPagePreview() { SkergeTheme { - LoadingPage() + PagePreview(Page()) {} + } +} + +@Preview(showBackground = true) +@Composable +fun ErrorPagePreview() { + SkergeTheme { + PagePreview(Page(error = Exception("Error message here"))) {} } } \ No newline at end of file