-
Notifications
You must be signed in to change notification settings - Fork 133
Refactor DataCollectionViewModel to Remove Blocking Calls and Add Load State Management #3224
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
Conversation
app/src/main/java/org/groundplatform/android/ui/datacollection/DataCollectionViewModel.kt
Outdated
Show resolved
Hide resolved
app/src/main/java/org/groundplatform/android/ui/datacollection/DataCollectionViewModel.kt
Outdated
Show resolved
Hide resolved
app/src/main/java/org/groundplatform/android/ui/datacollection/DataCollectionViewModel.kt
Outdated
Show resolved
Hide resolved
app/src/main/java/org/groundplatform/android/ui/datacollection/DataCollectionViewModel.kt
Outdated
Show resolved
Hide resolved
@anandwana001 Gentle ping |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #3224 +/- ##
============================================
+ Coverage 67.16% 67.22% +0.05%
- Complexity 1411 1415 +4
============================================
Files 303 304 +1
Lines 7642 7682 +40
Branches 774 782 +8
============================================
+ Hits 5133 5164 +31
- Misses 2076 2080 +4
- Partials 433 438 +5
🚀 New features to boost your workflow:
|
Use cases to verify:
|
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { | ||
var uiStateJob: Job? = null | ||
viewModel.loadState.collect { state -> | ||
binding.progressBar.isVisible = state is LoadState.Loading |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure that this is well tested too.
when (state) { | ||
is LoadState.Error -> { | ||
uiStateJob?.cancel() | ||
} | ||
|
||
lifecycleScope.launch { viewModel.uiState.filterNotNull().collect { updateUI(it) } } | ||
is LoadState.Ready -> { | ||
updateUI( | ||
UiState.TaskListAvailable( | ||
state.tasks, | ||
viewModel.getTaskPosition(viewModel.getCurrentTaskId()), | ||
) | ||
) | ||
uiStateJob?.cancel() | ||
uiStateJob = launch { viewModel.uiState.filterNotNull().collect { updateUI(it) } } | ||
} | ||
else -> { | ||
uiStateJob?.cancel() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please refactor this code into smaller (single-purpose) functions for better readability.
throw e | ||
} | ||
} | ||
val surveyId: String |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check if this can be kept private
viewModelFactory.create(getViewModelClass(task.type)) | ||
} catch (e: Exception) { | ||
Timber.e("ignoring task with invalid type: $task.type") | ||
Timber.e("$e ignoring task with invalid type: $task.type") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Log the entire throwable instead of adding it to the message.
import org.groundplatform.android.model.job.Job | ||
import org.groundplatform.android.model.task.Task | ||
|
||
sealed interface LoadState { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestions:
- Please rename it to be more generic (something like UiState?)
- Consider adding more states to this interface (e.g. collecting data, data submitted) along with validations
runner() | ||
.validateTextIsDisplayed("Unnamed point") | ||
.validateTextIsDisplayed(requireNotNull(JOB.name)) | ||
runner().validateTextIsDisplayed(TASK_1_NAME).validateTextIsDisplayed(requireNotNull(JOB.name)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please verify why this change was required? Does this indicate a regression?
Continuing in #3261 |
Fixes #3216
@shobhitagarwal1612 PTAL?