@@ -18,27 +18,26 @@ import android.graphics.Bitmap
1818import android.graphics.BitmapFactory
1919import androidx.compose.foundation.Image
2020import androidx.compose.foundation.background
21+ import androidx.compose.foundation.clickable
2122import androidx.compose.foundation.layout.Arrangement
2223import androidx.compose.foundation.layout.Box
2324import androidx.compose.foundation.layout.Column
2425import androidx.compose.foundation.layout.PaddingValues
25- import androidx.compose.foundation.layout.Row
2626import androidx.compose.foundation.layout.Spacer
2727import androidx.compose.foundation.layout.fillMaxSize
2828import androidx.compose.foundation.layout.fillMaxWidth
2929import androidx.compose.foundation.layout.height
3030import androidx.compose.foundation.layout.padding
3131import androidx.compose.foundation.layout.size
3232import androidx.compose.foundation.layout.width
33- import androidx.compose.foundation.lazy.LazyColumn
3433import androidx.compose.foundation.lazy.LazyRow
35- import androidx.compose.foundation.lazy.items
34+ import androidx.compose.foundation.lazy.itemsIndexed
3635import androidx.compose.material.icons.Icons
3736import androidx.compose.material.icons.automirrored.filled.ArrowBack
3837import androidx.compose.material.icons.filled.Add
39- import androidx.compose.material.icons.filled.Clear
4038import androidx.compose.material.icons.filled.Close
4139import androidx.compose.material.icons.filled.Share
40+ import androidx.compose.material.icons.outlined.Delete
4241import androidx.compose.material3.BottomAppBar
4342import androidx.compose.material3.Button
4443import androidx.compose.material3.ExperimentalMaterial3Api
@@ -52,9 +51,12 @@ import androidx.compose.material3.Text
5251import androidx.compose.material3.TopAppBar
5352import androidx.compose.material3.TopAppBarDefaults
5453import androidx.compose.runtime.Composable
54+ import androidx.compose.runtime.MutableIntState
55+ import androidx.compose.runtime.MutableState
56+ import androidx.compose.runtime.mutableIntStateOf
57+ import androidx.compose.runtime.saveable.rememberSaveable
5558import androidx.compose.ui.Alignment
5659import androidx.compose.ui.Modifier
57- import androidx.compose.ui.graphics.Color
5860import androidx.compose.ui.graphics.asImageBitmap
5961import androidx.compose.ui.platform.LocalContext
6062import androidx.compose.ui.tooling.preview.Preview
@@ -65,11 +67,19 @@ import androidx.compose.ui.unit.dp
6567fun DocumentScreen (
6668 pageIds : List <String >,
6769 imageLoader : (String ) -> Bitmap ,
68- onBackPressed : () -> Unit ,
70+ toCameraScreen : () -> Unit ,
6971 onSavePressed : () -> Unit ,
7072 onSharePressed : () -> Unit ,
7173 onDeleteImage : (String ) -> Unit ,
7274) {
75+ val currentPageIndex = rememberSaveable { mutableIntStateOf(0 ) }
76+ if (currentPageIndex.intValue >= pageIds.size) {
77+ currentPageIndex.intValue = pageIds.size - 1
78+ }
79+ if (currentPageIndex.intValue < 0 ) {
80+ toCameraScreen()
81+ return
82+ }
7383 Scaffold (
7484 topBar = {
7585 TopAppBar (
@@ -79,15 +89,15 @@ fun DocumentScreen(
7989 ),
8090 title = { Text (" Finalize document" ) },
8191 navigationIcon = {
82- IconButton (onClick = onBackPressed ) {
92+ IconButton (onClick = toCameraScreen ) {
8393 Icon (Icons .AutoMirrored .Filled .ArrowBack , contentDescription = " Back" )
8494 }
8595 },
8696 )
8797 },
8898 bottomBar = {
8999 Column {
90- PageList (pageIds, imageLoader)
100+ PageList (pageIds, imageLoader, currentPageIndex, toCameraScreen )
91101 BottomAppBar (
92102 actions = {
93103 Button (onClick = onSharePressed) {
@@ -101,42 +111,49 @@ fun DocumentScreen(
101111 }
102112 },
103113 floatingActionButton = {
104- FloatingActionButton (onClick = onBackPressed ) {
114+ FloatingActionButton (onClick = toCameraScreen ) {
105115 Icon (Icons .Default .Close , contentDescription = " Close" )
106116 }
107117 }
108118 )
109119 }
110120 }
111- ) { padding -> DocumentPreview (pageIds, padding, imageLoader , onDeleteImage) }
121+ ) { padding -> DocumentPreview (pageIds, imageLoader, currentPageIndex , onDeleteImage, padding ) }
112122}
113123
114124@Composable
115125private fun DocumentPreview (
116126 pageIds : List <String >,
117- padding : PaddingValues ,
118127 imageLoader : (String ) -> Bitmap ,
128+ currentPageIndex : MutableIntState ,
119129 onDeleteImage : (String ) -> Unit ,
130+ padding : PaddingValues ,
120131) {
132+ val imageId = pageIds[currentPageIndex.intValue]
121133 Column (
122134 modifier = Modifier
123135 .fillMaxSize()
124136 .background(MaterialTheme .colorScheme.surfaceContainer)
125137 .padding(padding)
126138 ) {
127-
128139 Box (
129140 modifier = Modifier .fillMaxSize()
130141 ) {
131142 // TODO Make it possible to zoom on the image
132143 Image (
133- bitmap = imageLoader(pageIds[ 0 ] ).asImageBitmap(),
144+ bitmap = imageLoader(imageId ).asImageBitmap(),
134145 contentDescription = null ,
135146 modifier = Modifier
136147 .padding(4 .dp)
137148 .align(Alignment .Center )
138149 )
139- Text (" 1 / ${pageIds.size} " ,
150+ IconButton (
151+ onClick = { onDeleteImage(imageId) },
152+ modifier = Modifier .align(Alignment .TopEnd )
153+ ) {
154+ Icon (imageVector = Icons .Outlined .Delete , contentDescription = " Delete page" )
155+ }
156+ Text (" ${currentPageIndex.value + 1 } / ${pageIds.size} " ,
140157 color = MaterialTheme .colorScheme.inverseOnSurface,
141158 modifier = Modifier .align(Alignment .BottomEnd )
142159 .padding(all = 8 .dp)
@@ -150,7 +167,9 @@ private fun DocumentPreview(
150167@Composable
151168private fun PageList (
152169 pageIds : List <String >,
153- imageLoader : (String ) -> Bitmap
170+ imageLoader : (String ) -> Bitmap ,
171+ currentPageIndex : MutableState <Int >,
172+ toCameraScreen : () -> Unit
154173) {
155174 Box {
156175 LazyRow (
@@ -161,7 +180,7 @@ private fun PageList(
161180 .background(MaterialTheme .colorScheme.secondaryContainer),
162181 horizontalArrangement = Arrangement .spacedBy(8 .dp)
163182 ) {
164- items (pageIds) { id ->
183+ itemsIndexed (pageIds) { index, id ->
165184 // TODO Use small images rather than big ones
166185 val bitmap = imageLoader(id).asImageBitmap()
167186 Image (
@@ -170,11 +189,12 @@ private fun PageList(
170189 modifier = Modifier
171190 .height(120 .dp)
172191 .padding(4 .dp)
192+ .clickable { currentPageIndex.value = index }
173193 )
174194 }
175195 }
176196 SmallFloatingActionButton (
177- onClick = {} ,
197+ onClick = toCameraScreen ,
178198 modifier = Modifier
179199 .align(Alignment .CenterEnd )
180200 .padding(8 .dp)
@@ -195,7 +215,7 @@ fun DocumentScreenPreview() {
195215 BitmapFactory .decodeStream(input)
196216 }
197217 },
198- onBackPressed = {},
218+ toCameraScreen = {},
199219 onSavePressed = {},
200220 onSharePressed = {},
201221 onDeleteImage = { _ -> {} }
0 commit comments