Skip to content

Commit

Permalink
Add "export running" dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
Chrisimx committed Dec 24, 2024
1 parent 2e09805 commit 0019809
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import android.content.ClipData
import android.content.ClipboardManager
import android.content.Intent
import android.graphics.BitmapFactory
import android.graphics.Rect
import android.graphics.pdf.PdfDocument
import android.os.Bundle
import android.util.Log
Expand All @@ -37,6 +38,7 @@ import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.pager.HorizontalPager
Expand All @@ -49,6 +51,7 @@ import androidx.compose.material.icons.filled.Settings
import androidx.compose.material.icons.filled.Share
import androidx.compose.material.icons.outlined.Delete
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.Card
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ExtendedFloatingActionButton
Expand Down Expand Up @@ -82,6 +85,7 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Dialog
import androidx.core.content.FileProvider
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewmodel.compose.viewModel
Expand Down Expand Up @@ -112,13 +116,15 @@ data class ScanningActivityData(
val capabilities: ScannerCapabilities,
val scanSettingsMenuOpen: MutableState<Boolean> = mutableStateOf(false),
val scanJobRunning: MutableState<Boolean> = mutableStateOf(false),
val stateExportRunning: MutableState<Boolean> = mutableStateOf(false),
val stateCurrentScans: SnapshotStateList<Pair<String, ScanSettings>> = mutableStateListOf()
) {
fun toImmutable() = ImmutableScanningActivityData(
scanSettingsVM,
capabilities,
scanSettingsMenuOpen,
scanJobRunning,
stateExportRunning,
stateCurrentScans
)
}
Expand All @@ -128,10 +134,12 @@ data class ImmutableScanningActivityData(
val capabilities: ScannerCapabilities,
private val scanSettingsMenuOpenState: State<Boolean>,
private val scanJobRunningState: State<Boolean>,
private val exportRunningState: State<Boolean>,
val currentScansState: SnapshotStateList<Pair<String, ScanSettings>>,
) {
val scanSettingsMenuOpen by scanSettingsMenuOpenState
val scanJobRunning by scanJobRunningState
val exportRunning by exportRunningState
}

class ScanningViewModel(
Expand Down Expand Up @@ -173,6 +181,10 @@ class ScanningViewModel(
}
_scanningActivityData.stateCurrentScans.removeAt(index)
}

fun setExportRunning(running: Boolean) {
_scanningActivityData.stateExportRunning.value = running
}
}


Expand Down Expand Up @@ -381,6 +393,7 @@ class ScanningActivity : ComponentActivity() {
}
IconButton(onClick = {
thread {
scanningViewModel.setExportRunning(true)
PdfDocument().apply {
for (scan in scanningViewModel.scanningActivityData.currentScansState) {
val bitmap =
Expand All @@ -401,7 +414,7 @@ class ScanningActivity : ComponentActivity() {
page.canvas.drawBitmap(
bitmap,
null,
android.graphics.Rect(
Rect(
0,
0,
width72thInches.toInt(),
Expand All @@ -422,6 +435,7 @@ class ScanningActivity : ComponentActivity() {
}.pdf"
)
writeTo(pdfFile.outputStream())
scanningViewModel.setExportRunning(false)
val share = Intent(Intent.ACTION_SEND)
share.type = "application/pdf"
share.putExtra(
Expand Down Expand Up @@ -599,6 +613,29 @@ class ScanningActivity : ComponentActivity() {
)
}
}

if (scanningViewModel.scanningActivityData.exportRunning) {
Dialog(
onDismissRequest = { },
) {
Card(
modifier = Modifier
.fillMaxWidth()
.height(200.dp)
.padding(16.dp),
shape = RoundedCornerShape(16.dp),
) {
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
CircularProgressIndicator()
Text(stringResource(R.string.exporting))
}
}
}
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
<string name="page_x_of_y">Page %1$s of %2$s</string>
<string name="export">Export</string>
<string name="retrieving_page">Retrieving page</string>
<string name="exporting">Exporting...</string>
</resources>

0 comments on commit 0019809

Please sign in to comment.