Commit 1a52794b authored by Ricki Hirner's avatar Ricki Hirner 🐑

Keep "force read only" flag when refreshing collections

parent 5f9f5e27
Pipeline #85493905 passed with stages
in 7 minutes and 26 seconds
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
......@@ -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)
......
......@@ -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() {
......@@ -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()) {
......@@ -344,10 +339,6 @@ class DavService: android.app.Service() {
throw e
}
}
// restore selections
for (url in selectedCollections)
collections[url]?.let { it.sync = true }
}
saveResults()
......
......@@ -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 ->
......@@ -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)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment