Skip to content
Snippets Groups Projects
Commit 47a03861 authored by Taras's avatar Taras
Browse files

Implement validate workspace

parent 8878353b
No related branches found
No related tags found
No related merge requests found
package org.futo.circles.auth.feature.workspace package org.futo.circles.auth.feature.workspace
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
...@@ -18,12 +19,50 @@ import javax.inject.Inject ...@@ -18,12 +19,50 @@ import javax.inject.Inject
@HiltViewModel @HiltViewModel
class ConfigureWorkspaceViewModel @Inject constructor( class ConfigureWorkspaceViewModel @Inject constructor(
savedStateHandle: SavedStateHandle,
private val workspaceDataSource: ConfigureWorkspaceDataSource, private val workspaceDataSource: ConfigureWorkspaceDataSource,
workspaceTasksProvider: WorkspaceTasksProvider workspaceTasksProvider: WorkspaceTasksProvider
) : ViewModel() { ) : ViewModel() {
val tasksLiveData = MutableLiveData(workspaceTasksProvider.getFullTasksList()) private val shouldValidate =
savedStateHandle.get<Boolean>(ConfigureWorkspaceFragment.SHOULD_VALIDATE) ?: false
val tasksLiveData = MutableLiveData(
if (shouldValidate) workspaceTasksProvider.getMandatoryTasks()
else workspaceTasksProvider.getFullTasksList()
)
val workspaceResultLiveData = SingleEventLiveData<Response<Unit>>() val workspaceResultLiveData = SingleEventLiveData<Response<Unit>>()
val validateWorkspaceResultLiveData = SingleEventLiveData<Response<Unit>>()
init {
if (shouldValidate) validateWorkspace()
}
private fun validateWorkspace() = launchBg {
val tasks = tasksLiveData.value?.toMutableList() ?: mutableListOf()
var hasError = false
tasks.forEachIndexed { i, item ->
updateTaskStatus(i, TaskStatus.RUNNING)
when (val validationResponse =
createResult { workspaceDataSource.validate(item.room) }) {
is Response.Error -> {
hasError = true
updateTaskStatus(i, TaskStatus.FAILED)
}
is Response.Success -> {
if (validationResponse.data)
updateTaskStatus(i, TaskStatus.SUCCESS)
else {
hasError = true
updateTaskStatus(i, TaskStatus.FAILED)
}
}
}
}
validateWorkspaceResultLiveData.postValue(
if (hasError) Response.Error("") else Response.Success(Unit)
)
}
fun createWorkspace() = launchBg { fun createWorkspace() = launchBg {
val tasks = tasksLiveData.value?.toMutableList() ?: mutableListOf() val tasks = tasksLiveData.value?.toMutableList() ?: mutableListOf()
......
...@@ -21,7 +21,7 @@ class WorkspaceTasksProvider @Inject constructor() { ...@@ -21,7 +21,7 @@ class WorkspaceTasksProvider @Inject constructor() {
addAll(getOptionalTasks()) addAll(getOptionalTasks())
} }
private fun getMandatoryTasks() = listOf( fun getMandatoryTasks() = listOf(
MandatoryWorkspaceTask(RootSpace(), R.string.root_space_description), MandatoryWorkspaceTask(RootSpace(), R.string.root_space_description),
MandatoryWorkspaceTask(CirclesSpace(), R.string.circles_space_description), MandatoryWorkspaceTask(CirclesSpace(), R.string.circles_space_description),
MandatoryWorkspaceTask(GroupsSpace(), R.string.groups_space_description), MandatoryWorkspaceTask(GroupsSpace(), R.string.groups_space_description),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment