diff --git a/auth/src/main/java/org/futo/circles/auth/feature/workspace/ConfigureWorkspaceViewModel.kt b/auth/src/main/java/org/futo/circles/auth/feature/workspace/ConfigureWorkspaceViewModel.kt index ad610e261e7b78e7667c165dbface0006bf3c6e1..16b537a664b68c8883d1c6622c6e073bb496d767 100644 --- a/auth/src/main/java/org/futo/circles/auth/feature/workspace/ConfigureWorkspaceViewModel.kt +++ b/auth/src/main/java/org/futo/circles/auth/feature/workspace/ConfigureWorkspaceViewModel.kt @@ -1,6 +1,7 @@ package org.futo.circles.auth.feature.workspace import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.ViewModel import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.Dispatchers @@ -18,12 +19,50 @@ import javax.inject.Inject @HiltViewModel class ConfigureWorkspaceViewModel @Inject constructor( + savedStateHandle: SavedStateHandle, private val workspaceDataSource: ConfigureWorkspaceDataSource, workspaceTasksProvider: WorkspaceTasksProvider ) : 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 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 { val tasks = tasksLiveData.value?.toMutableList() ?: mutableListOf() diff --git a/auth/src/main/java/org/futo/circles/auth/feature/workspace/data_source/WorkspaceTasksProvider.kt b/auth/src/main/java/org/futo/circles/auth/feature/workspace/data_source/WorkspaceTasksProvider.kt index d69960e0e3ebf34237ed7c670b855157987be57b..928e884f90a329e29ef27188600c0f751a27b934 100644 --- a/auth/src/main/java/org/futo/circles/auth/feature/workspace/data_source/WorkspaceTasksProvider.kt +++ b/auth/src/main/java/org/futo/circles/auth/feature/workspace/data_source/WorkspaceTasksProvider.kt @@ -21,7 +21,7 @@ class WorkspaceTasksProvider @Inject constructor() { addAll(getOptionalTasks()) } - private fun getMandatoryTasks() = listOf( + fun getMandatoryTasks() = listOf( MandatoryWorkspaceTask(RootSpace(), R.string.root_space_description), MandatoryWorkspaceTask(CirclesSpace(), R.string.circles_space_description), MandatoryWorkspaceTask(GroupsSpace(), R.string.groups_space_description),