Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 423423da authored by mitulsheth's avatar mitulsheth
Browse files

feat: Murena Workspace Login Part 1 of 2

parent 6a72d2fa
Loading
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
@@ -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) {
@@ -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()) {
@@ -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 {