Loading cardinal-android/app/src/main/java/earth/maps/cardinal/data/sync/FavoritesLocalDataSource.kt +23 −0 Original line number Diff line number Diff line Loading @@ -86,25 +86,38 @@ class FavoritesLocalDataSource internal constructor( val rootList = ensureRootList(payload) val existingLists = savedListDao.getAllLists() .associateBy { it.id } val remoteListIds = mutableSetOf<String>() payload.lists.forEach { remote -> if (remote.isRoot && remote.id != rootList.id) return@forEach remoteListIds += remote.id val existing = existingLists[remote.id] if (existing == null || remote.updatedAt > existing.updatedAt) { savedListDao.upsertList(remote.toSavedList()) } } existingLists.values .filterNot { it.id == rootList.id } .filterNot { it.id in remoteListIds } .filter { it.updatedAt <= payload.syncedAt } .forEach { savedListDao.deleteList(it) } return rootList } private suspend fun mergePlaces(payload: FavoritesSyncPayloadDto) { val existingPlaces = savedPlaceDao.getAllPlaces() .associateBy { it.id } val remotePlaceIds = mutableSetOf<String>() payload.places.forEach { remote -> remotePlaceIds += remote.id val existing = existingPlaces[remote.id] if (existing == null || remote.updatedAt > existing.updatedAt) { savedPlaceDao.upsertPlace(remote.toSavedPlace()) } } existingPlaces.values .filterNot { it.id in remotePlaceIds } .filter { it.updatedAt <= payload.syncedAt } .forEach { savedPlaceDao.deletePlace(it) } } private suspend fun mergeListItems(payload: FavoritesSyncPayloadDto, rootList: SavedList) { Loading @@ -117,9 +130,12 @@ class FavoritesLocalDataSource internal constructor( .groupBy { ListItemIdentity(itemId = it.itemId, itemType = it.itemType) } .toMutableMap() val remoteIdentities = mutableSetOf<ListItemIdentity>() payload.listItems.forEach { remote -> remote.toListItem(mappingPolicy)?.let { remoteItem -> val identity = remoteItem.identity() remoteIdentities += identity val localItems = localItemsByIdentity[identity].orEmpty() if (shouldApplyRemoteListItem(remoteItem, localItems)) { if (localItems.isNotEmpty()) { Loading @@ -130,6 +146,13 @@ class FavoritesLocalDataSource internal constructor( } } } localItemsByIdentity.entries .filterNot { it.key in remoteIdentities } .forEach { (_, items) -> items.filter { it.placementUpdatedAt <= payload.syncedAt } .forEach { listItemDao.deleteItem(it) } } } private fun shouldApplyRemoteListItem(remoteItem: ListItem, localItems: List<ListItem>): Boolean { Loading Loading
cardinal-android/app/src/main/java/earth/maps/cardinal/data/sync/FavoritesLocalDataSource.kt +23 −0 Original line number Diff line number Diff line Loading @@ -86,25 +86,38 @@ class FavoritesLocalDataSource internal constructor( val rootList = ensureRootList(payload) val existingLists = savedListDao.getAllLists() .associateBy { it.id } val remoteListIds = mutableSetOf<String>() payload.lists.forEach { remote -> if (remote.isRoot && remote.id != rootList.id) return@forEach remoteListIds += remote.id val existing = existingLists[remote.id] if (existing == null || remote.updatedAt > existing.updatedAt) { savedListDao.upsertList(remote.toSavedList()) } } existingLists.values .filterNot { it.id == rootList.id } .filterNot { it.id in remoteListIds } .filter { it.updatedAt <= payload.syncedAt } .forEach { savedListDao.deleteList(it) } return rootList } private suspend fun mergePlaces(payload: FavoritesSyncPayloadDto) { val existingPlaces = savedPlaceDao.getAllPlaces() .associateBy { it.id } val remotePlaceIds = mutableSetOf<String>() payload.places.forEach { remote -> remotePlaceIds += remote.id val existing = existingPlaces[remote.id] if (existing == null || remote.updatedAt > existing.updatedAt) { savedPlaceDao.upsertPlace(remote.toSavedPlace()) } } existingPlaces.values .filterNot { it.id in remotePlaceIds } .filter { it.updatedAt <= payload.syncedAt } .forEach { savedPlaceDao.deletePlace(it) } } private suspend fun mergeListItems(payload: FavoritesSyncPayloadDto, rootList: SavedList) { Loading @@ -117,9 +130,12 @@ class FavoritesLocalDataSource internal constructor( .groupBy { ListItemIdentity(itemId = it.itemId, itemType = it.itemType) } .toMutableMap() val remoteIdentities = mutableSetOf<ListItemIdentity>() payload.listItems.forEach { remote -> remote.toListItem(mappingPolicy)?.let { remoteItem -> val identity = remoteItem.identity() remoteIdentities += identity val localItems = localItemsByIdentity[identity].orEmpty() if (shouldApplyRemoteListItem(remoteItem, localItems)) { if (localItems.isNotEmpty()) { Loading @@ -130,6 +146,13 @@ class FavoritesLocalDataSource internal constructor( } } } localItemsByIdentity.entries .filterNot { it.key in remoteIdentities } .forEach { (_, items) -> items.filter { it.placementUpdatedAt <= payload.syncedAt } .forEach { listItemDao.deleteItem(it) } } } private fun shouldApplyRemoteListItem(remoteItem: ListItem, localItems: List<ListItem>): Boolean { Loading