Skip to content

Commit 536d3db

Browse files
authored
Merge pull request #7065 from grzesiek2010/COLLECT-7058
Reduce duplicate drawing of polylines and polygons + fix feature removing in Google Maps
2 parents aac3211 + 280efa3 commit 536d3db

File tree

6 files changed

+33
-4
lines changed

6 files changed

+33
-4
lines changed

androidshared/src/main/java/org/odk/collect/androidshared/livedata/LiveDataUtils.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ private static class CombinedLiveData<T> extends MediatorLiveData<T> {
6262

6363
private final Object[] values;
6464
private final Function<Object[], T> map;
65+
private T lastEmitted;
6566

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

8182
private void update() {
82-
setValue(map.apply(values));
83+
T newValue = map.apply(values);
84+
85+
if (lastEmitted == null || !lastEmitted.equals(newValue)) {
86+
lastEmitted = newValue;
87+
setValue(newValue);
88+
}
8389
}
8490
}
8591

androidshared/src/test/java/org/odk/collect/androidshared/livedata/LiveDataUtilsTest.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,19 @@ class LiveDataUtilsTest {
3434

3535
assertThat(combined.getOrAwaitValue(), equalTo(Pair("one-updated", "two")))
3636
}
37+
38+
@Test
39+
fun `#combine skips emission when result is equal to previous`() {
40+
val one = MutableLiveData("one")
41+
val combined = one.combine(MutableLiveData<String>())
42+
43+
var counter = 0
44+
LiveDataUtils.observe(combined) {
45+
counter++
46+
}
47+
48+
one.value = "one"
49+
50+
assertThat(counter, equalTo(1))
51+
}
3752
}

geo/src/main/java/org/odk/collect/geo/geopoly/GeoPolyFragment.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import androidx.lifecycle.asLiveData
1515
import androidx.lifecycle.viewmodel.viewModelFactory
1616
import com.google.android.material.dialog.MaterialAlertDialogBuilder
1717
import com.google.android.material.snackbar.Snackbar
18-
import org.odk.collect.androidshared.livedata.LiveDataExt.combine
1918
import org.odk.collect.androidshared.ui.DialogFragmentUtils.showIfNotShowing
2019
import org.odk.collect.androidshared.ui.FragmentFactoryBuilder
2120
import org.odk.collect.androidshared.ui.SnackbarUtils
@@ -253,8 +252,7 @@ class GeoPolyFragment @JvmOverloads constructor(
253252
},
254253
displayDismissButton = true
255254
)
256-
val viewData = viewModel.points.asLiveData().combine(viewModel.invalidMessage)
257-
viewData.observe(viewLifecycleOwner) { (points, invalidMessage) ->
255+
viewModel.viewData.observe(viewLifecycleOwner) { (points, invalidMessage) ->
258256
val isValid = invalidMessage == null
259257
if (!isValid) {
260258
snackbar.setText(invalidMessage)

geo/src/main/java/org/odk/collect/geo/geopoly/GeoPolyViewModel.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package org.odk.collect.geo.geopoly
22

33
import androidx.lifecycle.LiveData
44
import androidx.lifecycle.ViewModel
5+
import androidx.lifecycle.asLiveData
56
import androidx.lifecycle.map
67
import kotlinx.coroutines.flow.MutableStateFlow
78
import kotlinx.coroutines.flow.StateFlow
89
import org.odk.collect.androidshared.data.Consumable
10+
import org.odk.collect.androidshared.livedata.LiveDataExt.combine
911
import org.odk.collect.androidshared.livedata.LiveDataExt.withLast
1012
import org.odk.collect.async.Cancellable
1113
import org.odk.collect.async.Scheduler
@@ -53,6 +55,8 @@ class GeoPolyViewModel(
5355
}
5456
}
5557

58+
val viewData = _points.asLiveData().combine(invalidMessage)
59+
5660
private var accuracyThreshold: Int = 0
5761
private var recording: Cancellable? = null
5862

geo/src/test/java/org/odk/collect/geo/geopoly/GeoPolyViewModelTest.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ import org.hamcrest.Matchers.equalTo
77
import org.junit.Rule
88
import org.junit.Test
99
import org.mockito.Mockito.mock
10+
import org.odk.collect.androidtest.MainDispatcherRule
1011
import org.odk.collect.testshared.getOrAwaitValue
1112

1213
class GeoPolyViewModelTest {
1314

1415
@get:Rule
1516
val instantTaskExecutorRule = InstantTaskExecutorRule()
1617

18+
@get:Rule
19+
val mainDispatcherRule = MainDispatcherRule()
20+
1721
@Test
1822
fun `#fixedAlerts is null until after invalid message is non-null`() {
1923
val invalidMessage = MutableLiveData<String?>(null)

google-maps/src/main/java/org/odk/collect/googlemaps/GoogleMapFragment.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,7 @@ public void dispose() {
850850

851851
private void clearPolyline() {
852852
if (polyline != null) {
853+
polyline.setVisible(false);
853854
polyline.remove();
854855
polyline = null;
855856
}
@@ -1024,6 +1025,7 @@ public List<MapPoint> getPoints() {
10241025

10251026
private void clearPolygon() {
10261027
if (polygon != null) {
1028+
polygon.setVisible(false);
10271029
polygon.remove();
10281030
polygon = null;
10291031
}

0 commit comments

Comments
 (0)