Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -62,6 +62,7 @@ private static class CombinedLiveData<T> extends MediatorLiveData<T> {

private final Object[] values;
private final Function<Object[], T> map;
private T lastEmitted;

CombinedLiveData(LiveData<?>[] sources, Function<Object[], T> map) {
this.map = map;
Expand All @@ -79,7 +80,12 @@ private static class CombinedLiveData<T> extends MediatorLiveData<T> {
}

private void update() {
setValue(map.apply(values));
T newValue = map.apply(values);

if (lastEmitted == null || !lastEmitted.equals(newValue)) {
lastEmitted = newValue;
setValue(newValue);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,19 @@ class LiveDataUtilsTest {

assertThat(combined.getOrAwaitValue(), equalTo(Pair("one-updated", "two")))
}

@Test
fun `#combine skips emission when result is equal to previous`() {
val one = MutableLiveData("one")
val combined = one.combine(MutableLiveData<String>())

var counter = 0
LiveDataUtils.observe(combined) {
counter++
}

one.value = "one"

assertThat(counter, equalTo(1))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import androidx.lifecycle.asLiveData
import androidx.lifecycle.viewmodel.viewModelFactory
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.snackbar.Snackbar
import org.odk.collect.androidshared.livedata.LiveDataExt.combine
import org.odk.collect.androidshared.ui.DialogFragmentUtils.showIfNotShowing
import org.odk.collect.androidshared.ui.FragmentFactoryBuilder
import org.odk.collect.androidshared.ui.SnackbarUtils
Expand Down Expand Up @@ -253,8 +252,7 @@ class GeoPolyFragment @JvmOverloads constructor(
},
displayDismissButton = true
)
val viewData = viewModel.points.asLiveData().combine(viewModel.invalidMessage)
viewData.observe(viewLifecycleOwner) { (points, invalidMessage) ->
viewModel.viewData.observe(viewLifecycleOwner) { (points, invalidMessage) ->
val isValid = invalidMessage == null
if (!isValid) {
snackbar.setText(invalidMessage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package org.odk.collect.geo.geopoly

import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.asLiveData
import androidx.lifecycle.map
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import org.odk.collect.androidshared.data.Consumable
import org.odk.collect.androidshared.livedata.LiveDataExt.combine
import org.odk.collect.androidshared.livedata.LiveDataExt.withLast
import org.odk.collect.async.Cancellable
import org.odk.collect.async.Scheduler
Expand Down Expand Up @@ -53,6 +55,8 @@ class GeoPolyViewModel(
}
}

val viewData = _points.asLiveData().combine(invalidMessage)

private var accuracyThreshold: Int = 0
private var recording: Cancellable? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import org.hamcrest.Matchers.equalTo
import org.junit.Rule
import org.junit.Test
import org.mockito.Mockito.mock
import org.odk.collect.androidtest.MainDispatcherRule
import org.odk.collect.testshared.getOrAwaitValue

class GeoPolyViewModelTest {

@get:Rule
val instantTaskExecutorRule = InstantTaskExecutorRule()

@get:Rule
val mainDispatcherRule = MainDispatcherRule()

@Test
fun `#fixedAlerts is null until after invalid message is non-null`() {
val invalidMessage = MutableLiveData<String?>(null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,7 @@ public void dispose() {

private void clearPolyline() {
if (polyline != null) {
polyline.setVisible(false);
polyline.remove();
polyline = null;
}
Expand Down Expand Up @@ -1024,6 +1025,7 @@ public List<MapPoint> getPoints() {

private void clearPolygon() {
if (polygon != null) {
polygon.setVisible(false);
polygon.remove();
polygon = null;
}
Expand Down