Skip to content

Commit

Permalink
Make USB import cancellable
Browse files Browse the repository at this point in the history
  • Loading branch information
d4rken committed Mar 1, 2024
1 parent d4852ca commit 98d42dd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
6 changes: 4 additions & 2 deletions app/src/main/java/eu/darken/pgc/common/uix/ViewModel2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import eu.darken.pgc.common.flow.DynamicStateFlow
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -54,11 +55,12 @@ abstract class ViewModel2(
scope: CoroutineScope = viewModelScope,
context: CoroutineContext = getVMContext(),
block: suspend CoroutineScope.() -> Unit
) {
try {
): Job? {
return try {
scope.launch(context = context, block = block)
} catch (e: CancellationException) {
log(TAG, WARN) { "launch()ed coroutine was canceled (scope=$scope): ${e.asLog()}" }
null
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class ImporterFragment : Fragment3(R.layout.importer_fragment) {
usbImportAction.isEnabled = selectedUsbDevice != null
}
}

usbCancelAction.isVisible = false
usbImportAction.setOnClickListener { vm.importUsb(selectedUsbDevice!!) }
}

Expand All @@ -203,6 +203,10 @@ class ImporterFragment : Fragment3(R.layout.importer_fragment) {
"${((usb.current.toDouble() / usb.max.toDouble()) * 100).roundToInt()}%"
usbImportAction.isVisible = false
usbImportDeviceGroup.isVisible = false
usbCancelAction.apply {
isVisible = true
setOnClickListener { vm.cancelImportUsb() }
}
}

is ImporterFragmentVM.UsbImportstate.Result -> {
Expand All @@ -221,6 +225,7 @@ class ImporterFragment : Fragment3(R.layout.importer_fragment) {
isVisible = true
setOnClickListener { vm.importUsb(null) }
}
usbCancelAction.isVisible = false
usbImportDeviceGroup.isVisible = false
}
}
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/eu/darken/pgc/importer/ui/ImporterFragmentVM.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ import eu.darken.pgc.importer.core.IngestIGCPayload
import eu.darken.pgc.importer.core.Ingester
import eu.darken.pgc.importer.core.MassStorageCrawler
import eu.darken.pgc.importer.core.UsbDevicesProvider
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.isActive
import me.jahnen.libaums.core.fs.UsbFile
import me.jahnen.libaums.core.fs.UsbFileInputStream
import okhttp3.internal.closeQuietly
Expand Down Expand Up @@ -157,6 +160,7 @@ class ImporterFragmentVM @Inject constructor(
)
}

private var usbImportJob: Job? = null
fun importUsb(device: UsbDevice?) = launch {
log(TAG) { "importUsb($device)" }
if (device == null) {
Expand Down Expand Up @@ -189,6 +193,10 @@ class ImporterFragmentVM @Inject constructor(
)

igcFiles.forEach { file ->
if (!isActive) {
usbImportState.value = UsbImportstate.Start()
throw CancellationException()
}
log(TAG) { "importUsb(...): Ingesting $file" }
usbImportState.value = (usbImportState.value as UsbImportstate.Progress).copy(
progressMsg = file.absolutePath.toCaString()
Expand Down Expand Up @@ -227,6 +235,10 @@ class ImporterFragmentVM @Inject constructor(
skipped = skipped,
failed = failed
)
}.also { usbImportJob = it }

fun cancelImportUsb() {
usbImportJob?.cancel()
}

data class ImporterState(
Expand Down
20 changes: 18 additions & 2 deletions app/src/main/res/layout/importer_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,8 @@
android:id="@+id/usb_import_device_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="16dp"
android:layout_marginTop="16dp"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@id/usb_import_action"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/usb_import_secondary">
Expand All @@ -293,10 +292,27 @@

</RadioGroup>

<com.google.android.material.button.MaterialButton
android:id="@+id/usb_cancel_action"
style="@style/Widget.Material3.Button.TextButton"
android:layout_width="wrap_content"
android:layout_marginTop="16dp"
android:layout_height="wrap_content"
android:visibility="gone"
tools:visibility="visible"
android:text="@string/general_cancel_action"
app:layout_constraintTop_toBottomOf="@id/usb_import_device_group"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<com.google.android.material.button.MaterialButton
android:id="@+id/usb_import_action"
style="@style/Widget.Material3.Button"
android:layout_width="wrap_content"
android:layout_marginTop="16dp"
android:layout_height="wrap_content"
android:visibility="gone"
tools:visibility="visible"
android:text="@string/importer_usb_import_start_action"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
Expand Down

0 comments on commit 98d42dd

Please sign in to comment.