weather: deduplicate saved locations on exact coordinates
- Commit
065aa0450459439fcf62766b690d20bfa515ff03- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-08 02:47 UTC (28 days ago)
- Parents
ea20892
Tapping a search result calls WeatherViewModel.addLocation, which inserted unconditionally: SavedLocation has no uniqueness constraint and @Insert(onConflict = REPLACE) only fires on primary-key conflict, so every tap minted a fresh row (autoGenerate id). Re-adding a city โ or double-tapping a result โ listed it twice in the home pager and drawer and made refreshAll fetch it twice. addLocation now skips the insert when a pinned row already carries the same coordinates. The same search result tapped twice passes bit-identical doubles, so exact equality only matches true duplicates and can never block two distinct nearby places; the live-GPS row is excluded so pinning the city you're standing in still works. A Mutex serializes the check-then-insert so two rapid taps can't both slip past the duplicate check before either insert lands (Room suspends between the read and the write). User-visible effect: a city can appear in the saved list only once; re-tapping it is a no-op instead of a duplicate entry. Verified with :weather:testDevUnitTest (BUILD SUCCESSFUL): new ViewModelAddLocationTest pins duplicateAddKeepsSingleRow, distinctNearbyPlacesBothKept, and pinningCurrentCityStillAdds.
Diff
weather/src/main/java/com/vayunmathur/weather/platform/WeatherViewModel.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/platform/WeatherViewModel.kt b/weather/src/main/java/com/vayunmathur/weather/platform/WeatherViewModel.ktindex 59714b5a75..a8b8041cab 100644--- a/weather/src/main/java/com/vayunmathur/weather/platform/WeatherViewModel.kt+++ b/weather/src/main/java/com/vayunmathur/weather/platform/WeatherViewModel.kt@@ -26,6 +26,8 @@ import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch+import kotlinx.coroutines.sync.Mutex+import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.withTimeoutOrNull data class ForecastUiState(@@ -50,6 +52,10 @@ class WeatherViewModel( private val inFlight = mutableSetOf<Long>() + // Serializes addLocation's check-then-insert so two rapid taps on the same+ // search result can't both slip past the duplicate check.+ private val addMutex = Mutex()+ private val _selected = MutableStateFlow<SelectedDateOrTime?>(null) val selectedDateOrTime: StateFlow<SelectedDateOrTime?> = _selected.asStateFlow() @@ -220,17 +226,27 @@ class WeatherViewModel( fun addLocation(name: String, country: String, latitude: Double, longitude: Double) { viewModelScope.launch {- val existing = repository.getLocations()- repository.insertLocation(- SavedLocation(- name = name,- country = country,- latitude = latitude,- longitude = longitude,- displayOrder = existing.size,- isCurrent = false,+ addMutex.withLock {+ val existing = repository.getLocations()+ // The same city tapped twice (or re-added after a new search)+ // arrives with bit-identical coordinates, so exact equality+ // only ever matches true duplicates โ never two distinct+ // nearby places. The live-GPS row is excluded: pinning the+ // city you're standing in is deliberate, not a duplicate.+ if (existing.any { !it.isCurrent && it.latitude == latitude && it.longitude == longitude }) {+ return@withLock+ }+ repository.insertLocation(+ SavedLocation(+ name = name,+ country = country,+ latitude = latitude,+ longitude = longitude,+ displayOrder = existing.size,+ isCurrent = false,+ ) )- )+ } } }
weather/src/test/java/com/vayunmathur/weather/platform/ViewModelFetchTest.kt
diff --git a/weather/src/test/java/com/vayunmathur/weather/platform/ViewModelFetchTest.kt b/weather/src/test/java/com/vayunmathur/weather/platform/ViewModelFetchTest.ktindex 1a3f9e9ca2..4ccbf9a804 100644--- a/weather/src/test/java/com/vayunmathur/weather/platform/ViewModelFetchTest.kt+++ b/weather/src/test/java/com/vayunmathur/weather/platform/ViewModelFetchTest.kt@@ -161,3 +161,50 @@ class ViewModelDeviceFixTest { assertTrue(awaitError(22L), "state=${vm.forecasts.value}") } }++@RunWith(AndroidJUnit4::class)+class ViewModelAddLocationTest {++ private lateinit var app: Application+ private lateinit var repo: FakeRepository+ private lateinit var vm: WeatherViewModel++ @BeforeTest fun setUp() {+ app = ApplicationProvider.getApplicationContext()+ runCatching {+ androidx.work.WorkManager.initialize(app, androidx.work.Configuration.Builder().build())+ }+ repo = FakeRepository(app)+ vm = WeatherViewModel(app, repo)+ }++ private fun drainMain() {+ repeat(30) {+ Shadows.shadowOf(Looper.getMainLooper()).idle()+ Thread.sleep(20)+ }+ }++ @Test fun duplicateAddKeepsSingleRow() {+ vm.addLocation("Berlin", "Germany", 52.52006, 13.40495)+ vm.addLocation("Berlin", "Germany", 52.52006, 13.40495)+ drainMain()+ assertEquals(1, repo.rows.value.size, "rows=${repo.rows.value}")+ }++ @Test fun distinctNearbyPlacesBothKept() {+ vm.addLocation("Home", "Germany", 52.52006, 13.40495)+ vm.addLocation("Office", "Germany", 52.52007, 13.40496)+ drainMain()+ assertEquals(2, repo.rows.value.size, "rows=${repo.rows.value}")+ }++ @Test fun pinningCurrentCityStillAdds() {+ repo.rows.value = listOf(+ SavedLocation(id = 50L, name = "GPS", latitude = 52.52, longitude = 13.40, isCurrent = true),+ )+ vm.addLocation("Berlin", "Germany", 52.52, 13.40)+ drainMain()+ assertEquals(2, repo.rows.value.size, "rows=${repo.rows.value}")+ }+}