Loading app/src/androidTest/java/at/bitfire/davdroid/model/DaoToolsTest.kt +1 −2 Original line number Diff line number Diff line package at.bitfire.davdroid.model import androidx.room.Room import androidx.room.RoomDatabase import androidx.test.platform.app.InstrumentationRegistry import okhttp3.HttpUrl import org.junit.After Loading Loading @@ -51,7 +50,7 @@ class DaoToolsTest { val created = HomeSet(id=4, serviceId=service.id, url=HttpUrl.get("https://example.com/4")) newItems[created.url] = created DaoTools(homeSetDao).syncAll(oldItems, newItems) { it.url } DaoTools(homeSetDao).syncAll(oldItems, newItems, { it.url }) val afterSync = homeSetDao.getByService(service.id) assertEquals(afterSync.size, 3) Loading app/src/main/java/at/bitfire/davdroid/DavService.kt +6 −15 Original line number Diff line number Diff line Loading @@ -240,16 +240,18 @@ class DavService: android.app.Service() { fun saveHomesets() { DaoTools(homeSetDao).syncAll( homeSetDao.getByService(serviceId), homeSets ) { it.url } homeSets, { it.url }) } @Transaction fun saveCollections() { DaoTools(collectionDao).syncAll( collectionDao.getByService(serviceId), collections ) { it.url } collections, { it.url }) { new, old -> new.forceReadOnly = old.forceReadOnly new.sync = old.sync } } fun saveResults() { Loading @@ -276,13 +278,6 @@ class DavService: android.app.Service() { queryHomeSets(httpClient, principalUrl) } // remember selected collections val selectedCollections = HashSet<HttpUrl>() collections.forEach { (url, collection) -> if (collection.sync) selectedCollections += url } // now refresh homesets and their member collections val itHomeSets = homeSets.iterator() while (itHomeSets.hasNext()) { Loading Loading @@ -344,10 +339,6 @@ class DavService: android.app.Service() { throw e } } // restore selections for (url in selectedCollections) collections[url]?.let { it.sync = true } } saveResults() Loading app/src/main/java/at/bitfire/davdroid/model/DaoTools.kt +12 −1 Original line number Diff line number Diff line Loading @@ -5,7 +5,16 @@ import java.util.logging.Level class DaoTools<T: IdEntity>(dao: SyncableDao<T>): SyncableDao<T> by dao { fun <K> syncAll(allOld: List<T>, allNew: Map<K,T>, selectKey: (T) -> K) { /** * Synchronizes a list of "old" elements with a list of "new" elements so that the list * only contain equal elements. * * @param allOld list of old elements * @param allNew map of new elements (stored in key map) * @param selectKey generates a unique key from the element (will be called on old elements) * @param prepareNew prepares new elements (can be used to take over properties of old elements) */ fun <K> syncAll(allOld: List<T>, allNew: Map<K,T>, selectKey: (T) -> K, prepareNew: (new: T, old: T) -> Unit = { _, _ -> }) { Logger.log.log(Level.FINE, "Syncing tables", arrayOf(allOld, allNew)) val remainingNew = allNew.toMutableMap() allOld.forEach { old -> Loading @@ -14,6 +23,8 @@ class DaoTools<T: IdEntity>(dao: SyncableDao<T>): SyncableDao<T> by dao { if (matchingNew != null) { // keep this old item, but maybe update it matchingNew.id = old.id // identity is proven by key prepareNew(matchingNew, old) if (matchingNew != old) update(matchingNew) Loading Loading
app/src/androidTest/java/at/bitfire/davdroid/model/DaoToolsTest.kt +1 −2 Original line number Diff line number Diff line package at.bitfire.davdroid.model import androidx.room.Room import androidx.room.RoomDatabase import androidx.test.platform.app.InstrumentationRegistry import okhttp3.HttpUrl import org.junit.After Loading Loading @@ -51,7 +50,7 @@ class DaoToolsTest { val created = HomeSet(id=4, serviceId=service.id, url=HttpUrl.get("https://example.com/4")) newItems[created.url] = created DaoTools(homeSetDao).syncAll(oldItems, newItems) { it.url } DaoTools(homeSetDao).syncAll(oldItems, newItems, { it.url }) val afterSync = homeSetDao.getByService(service.id) assertEquals(afterSync.size, 3) Loading
app/src/main/java/at/bitfire/davdroid/DavService.kt +6 −15 Original line number Diff line number Diff line Loading @@ -240,16 +240,18 @@ class DavService: android.app.Service() { fun saveHomesets() { DaoTools(homeSetDao).syncAll( homeSetDao.getByService(serviceId), homeSets ) { it.url } homeSets, { it.url }) } @Transaction fun saveCollections() { DaoTools(collectionDao).syncAll( collectionDao.getByService(serviceId), collections ) { it.url } collections, { it.url }) { new, old -> new.forceReadOnly = old.forceReadOnly new.sync = old.sync } } fun saveResults() { Loading @@ -276,13 +278,6 @@ class DavService: android.app.Service() { queryHomeSets(httpClient, principalUrl) } // remember selected collections val selectedCollections = HashSet<HttpUrl>() collections.forEach { (url, collection) -> if (collection.sync) selectedCollections += url } // now refresh homesets and their member collections val itHomeSets = homeSets.iterator() while (itHomeSets.hasNext()) { Loading Loading @@ -344,10 +339,6 @@ class DavService: android.app.Service() { throw e } } // restore selections for (url in selectedCollections) collections[url]?.let { it.sync = true } } saveResults() Loading
app/src/main/java/at/bitfire/davdroid/model/DaoTools.kt +12 −1 Original line number Diff line number Diff line Loading @@ -5,7 +5,16 @@ import java.util.logging.Level class DaoTools<T: IdEntity>(dao: SyncableDao<T>): SyncableDao<T> by dao { fun <K> syncAll(allOld: List<T>, allNew: Map<K,T>, selectKey: (T) -> K) { /** * Synchronizes a list of "old" elements with a list of "new" elements so that the list * only contain equal elements. * * @param allOld list of old elements * @param allNew map of new elements (stored in key map) * @param selectKey generates a unique key from the element (will be called on old elements) * @param prepareNew prepares new elements (can be used to take over properties of old elements) */ fun <K> syncAll(allOld: List<T>, allNew: Map<K,T>, selectKey: (T) -> K, prepareNew: (new: T, old: T) -> Unit = { _, _ -> }) { Logger.log.log(Level.FINE, "Syncing tables", arrayOf(allOld, allNew)) val remainingNew = allNew.toMutableMap() allOld.forEach { old -> Loading @@ -14,6 +23,8 @@ class DaoTools<T: IdEntity>(dao: SyncableDao<T>): SyncableDao<T> by dao { if (matchingNew != null) { // keep this old item, but maybe update it matchingNew.id = old.id // identity is proven by key prepareNew(matchingNew, old) if (matchingNew != old) update(matchingNew) Loading