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

View File

@ -20,6 +20,7 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.io.FileNotFoundException import java.io.FileNotFoundException
import javax.inject.Inject 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 // TODO-han.yin: Stub for now. Would need to investigate HuggingFace APIs
fun importFromHuggingFace() {} fun importFromHuggingFace() {}
@ -171,7 +195,7 @@ sealed class ModelManagementState {
sealed class Importation : ModelManagementState() { sealed class Importation : ModelManagementState() {
data class Confirming(val uri: Uri, val fileName: String, val fileSize: Long) : Importation() 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 Success(val model: ModelInfo) : Importation()
data class Error(val message: String) : Importation() data class Error(val message: String) : Importation()
} }