UI: update UI ongoing model import's cancellation

This commit is contained in:
Han Yin 2025-04-16 14:28:49 -07:00
parent 9ba74a9d3d
commit 06448a60a5
2 changed files with 40 additions and 12 deletions

View File

@ -255,7 +255,7 @@ fun ModelsManagementScreen(
}
IconButton(
onClick = {/* TODO-han.yin: implement filtering */ }
onClick = {/* TODO-han.yin: implement filtering once metadata ready */ }
) {
Icon(
imageVector = Icons.Default.FilterAlt,
@ -377,9 +377,7 @@ fun ModelsManagementScreen(
state.uri, state.fileName, state.fileSize
)
},
onCancel = {
viewModel.resetManagementState()
}
onCancel = { viewModel.resetManagementState() }
)
}
@ -388,11 +386,10 @@ fun ModelsManagementScreen(
fileName = state.fileName,
fileSize = state.fileSize,
isImporting = true,
isCancelling = state.isCancelling,
progress = state.progress,
onConfirm = {},
onCancel = {
// TODO-han.yin: viewModel.cancelImport()
},
onCancel = { viewModel.cancelOngoingLocalModelImport() },
)
}
@ -467,6 +464,7 @@ fun ImportProgressDialog(
fileName: String,
fileSize: Long,
isImporting: Boolean,
isCancelling: Boolean = false,
progress: Float,
onConfirm: () -> Unit,
onCancel: () -> Unit
@ -528,7 +526,7 @@ fun ImportProgressDialog(
Spacer(modifier = Modifier.height(16.dp))
// Informational text
// Additional information
if (isImporting) {
Text(
text = "This may take several minutes for large models",
@ -536,6 +534,12 @@ fun ImportProgressDialog(
color = MaterialTheme.colorScheme.onSurfaceVariant,
textAlign = TextAlign.Center
)
} else if (isCancelling) {
Text(
text = "Cancelling ongoing import...",
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.error
)
}
}
},
@ -546,9 +550,9 @@ fun ImportProgressDialog(
}
},
dismissButton = {
if (!isImporting || progress < 0.7f) {
TextButton(onClick = onCancel, enabled = !isImporting) {
Text(if (isImporting) "Cancel" else "Back")
if (!isImporting || (progress < 0.7f && !isCancelling)) {
TextButton(onClick = onCancel, enabled = !isCancelling) {
Text("Cancel")
}
}
}

View File

@ -20,6 +20,7 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import java.io.FileNotFoundException
import javax.inject.Inject
@ -113,6 +114,29 @@ class ModelsManagementViewModel @Inject constructor(
}
}
fun cancelOngoingLocalModelImport() = viewModelScope.launch {
viewModelScope.launch {
// First update UI to show we're attempting to cancel
_managementState.update { current ->
if (current is Importation.Importing) {
current.copy(isCancelling = true)
} else {
current
}
}
// Attempt to cancel
when (modelRepository.cancelImport()) {
null, true -> { _managementState.value = ModelManagementState.Idle }
false -> {
_managementState.value = Importation.Error(
message = "Failed to cancel import. Try again later."
)
}
}
}
}
// TODO-han.yin: Stub for now. Would need to investigate HuggingFace APIs
fun importFromHuggingFace() {}
@ -171,7 +195,7 @@ sealed class ModelManagementState {
sealed class Importation : ModelManagementState() {
data class Confirming(val uri: Uri, val fileName: String, val fileSize: Long) : Importation()
data class Importing(val progress: Float = 0f, val fileName: String, val fileSize: Long) : Importation()
data class Importing(val progress: Float = 0f, val fileName: String, val fileSize: Long, val isCancelling: Boolean = false) : Importation()
data class Success(val model: ModelInfo) : Importation()
data class Error(val message: String) : Importation()
}