Fix crash when no internet connection

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-03-13 10:12:30 +01:00
parent ffe19232dc
commit a959c15354
No known key found for this signature in database
GPG key ID: 4E577DC593B59BDF
2 changed files with 49 additions and 13 deletions

View file

@ -36,6 +36,9 @@ import kotlinx.coroutines.launch
class AssistantViewModel(private val repository: AssistantRepositoryType) : ViewModel() {
private val _errorMessage = MutableStateFlow<String?>(null)
val errorMessage: StateFlow<String?> = _errorMessage
private val _selectedTaskType = MutableStateFlow<TaskType?>(null)
val selectedTaskType: StateFlow<TaskType?> = _selectedTaskType
@ -85,30 +88,43 @@ class AssistantViewModel(private val repository: AssistantRepositoryType) : View
viewModelScope.launch(Dispatchers.IO) {
val allTaskType = MainApp.getAppContext().getString(R.string.assistant_screen_all_task_type)
val result = arrayListOf(TaskType(null, allTaskType, null))
val taskTypes = repository.getTaskTypes().resultData.types
result.addAll(taskTypes)
val taskTypesResult = repository.getTaskTypes()
_taskTypes.update {
result.toList()
}
if (taskTypesResult.isSuccess) {
result.addAll(taskTypesResult.resultData.types)
_taskTypes.update {
result.toList()
}
_selectedTaskType.update {
result.first()
_selectedTaskType.update {
result.first()
}
} else {
_errorMessage.update {
taskTypesResult.message
}
}
}
}
fun getTaskList(appId: String = "assistant", onCompleted: () -> Unit = {}) {
viewModelScope.launch(Dispatchers.IO) {
_taskList = repository.getTaskList(appId).resultData.tasks
val result = repository.getTaskList(appId)
if (result.isSuccess) {
_taskList = result.resultData.tasks
filterTaskList(_selectedTaskType.value?.id)
filterTaskList(_selectedTaskType.value?.id)
_loading.update {
false
_loading.update {
false
}
onCompleted()
} else {
_errorMessage.update {
result.message
}
}
onCompleted()
}
}
@ -126,6 +142,12 @@ class AssistantViewModel(private val repository: AssistantRepositoryType) : View
}
}
fun resetErrorState() {
_errorMessage.update {
null
}
}
fun resetTaskDeletionState() {
_isTaskDeleted.update {
null

View file

@ -71,6 +71,7 @@ import kotlinx.coroutines.delay
@Composable
fun AssistantScreen(viewModel: AssistantViewModel, activity: Activity) {
val loading by viewModel.loading.collectAsState()
val errorMessage by viewModel.errorMessage.collectAsState()
val selectedTaskType by viewModel.selectedTaskType.collectAsState()
val filteredTaskList by viewModel.filteredTaskList.collectAsState()
val isTaskCreated by viewModel.isTaskCreated.collectAsState()
@ -133,6 +134,7 @@ fun AssistantScreen(viewModel: AssistantViewModel, activity: Activity) {
}
}
CheckErrorMessage(errorMessage, activity, viewModel)
CheckTaskAdd(isTaskCreated, activity, viewModel)
CheckTaskDeletion(isTaskDeleted, activity, viewModel)
@ -182,6 +184,18 @@ private fun CheckTaskAdd(isTaskCreated: Boolean?, activity: Activity, viewModel:
}
}
@Composable
private fun CheckErrorMessage(errorMessage: String?, activity: Activity, viewModel: AssistantViewModel) {
errorMessage?.let {
DisplayUtils.showSnackMessage(
activity,
errorMessage
)
viewModel.resetErrorState()
}
}
@Composable
private fun CheckTaskDeletion(isTaskDeleted: Boolean?, activity: Activity, viewModel: AssistantViewModel) {
isTaskDeleted?.let {