Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using activeSurveyFlow to make flow async #2890

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.stateIn
Expand All @@ -82,7 +83,7 @@ internal constructor(
private val submissionRepository: SubmissionRepository,
private val offlineUuidGenerator: OfflineUuidGenerator,
locationOfInterestRepository: LocationOfInterestRepository,
surveyRepository: SurveyRepository,
private val surveyRepository: SurveyRepository,
) : AbstractViewModel() {

private val _uiState: MutableStateFlow<UiState?> = MutableStateFlow(null)
Expand All @@ -97,8 +98,15 @@ internal constructor(
private var shouldLoadFromDraft: Boolean = savedStateHandle[TASK_SHOULD_LOAD_FROM_DRAFT] ?: false
private var draftDeltas: List<ValueDelta>? = null

private val activeSurvey: Survey = requireNotNull(surveyRepository.activeSurvey)
private val job: Job = activeSurvey.getJob(jobId) ?: error("couldn't retrieve job for $jobId")
private val _activeSurvey = MutableStateFlow<Survey?>(null)
val activeSurvey = _activeSurvey.asStateFlow()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean for this to be make public? I don't see a use anywhere else.


init {
viewModelScope.launch { surveyRepository.activeSurveyFlow.collect { _activeSurvey.value = it } }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't activeSurvey as you're defining it here functionally equivalent to surveyRepository.activeSurveyFlow? If so, why not just expose that instead?

}

private val job: Job =
activeSurvey.value?.getJob(jobId) ?: error("couldn't retrieve job for $jobId")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling .value here will grab a snapshot of the current survey at the time this VM is initialized. How is that different from what we're doing already?

private var customLoiName: String?
get() = savedStateHandle[TASK_LOI_NAME_KEY]
set(value) {
Expand Down