Skip to content

Commit

Permalink
Fetch last sync token: separate method
Browse files Browse the repository at this point in the history
  • Loading branch information
rfc2822 committed Jan 28, 2025
1 parent 1a896c4 commit 68f3529
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 25 deletions.
47 changes: 31 additions & 16 deletions app/src/main/kotlin/at/bitfire/davdroid/push/UnifiedPushReceiver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

package at.bitfire.davdroid.push

import android.accounts.Account
import android.content.Context
import at.bitfire.davdroid.db.Collection.Companion.TYPE_ADDRESSBOOK
import at.bitfire.davdroid.db.SyncState
import at.bitfire.davdroid.repository.AccountRepository
import at.bitfire.davdroid.repository.DavCollectionRepository
import at.bitfire.davdroid.repository.DavServiceRepository
import at.bitfire.davdroid.repository.PreferenceRepository
import at.bitfire.davdroid.resource.LocalAddressBookStore
import at.bitfire.davdroid.resource.LocalCalendarStore
import at.bitfire.davdroid.resource.LocalDataStore
import at.bitfire.davdroid.sync.SyncDataType
import at.bitfire.davdroid.sync.TasksAppManager
Expand All @@ -33,7 +36,13 @@ class UnifiedPushReceiver: MessagingReceiver() {

@Inject
lateinit var collectionRepository: DavCollectionRepository


@Inject
lateinit var localAddressBookStore: Lazy<LocalAddressBookStore>

@Inject
lateinit var localCalendarStore: Lazy<LocalCalendarStore>

@Inject
lateinit var logger: Logger

Expand All @@ -50,10 +59,10 @@ class UnifiedPushReceiver: MessagingReceiver() {
lateinit var pushRegistrationWorkerManager: PushRegistrationWorkerManager

@Inject
lateinit var tasksAppManager: Lazy<TasksAppManager>
lateinit var syncWorkerManager: SyncWorkerManager

@Inject
lateinit var syncWorkerManager: SyncWorkerManager
lateinit var tasksAppManager: Lazy<TasksAppManager>


override fun onNewEndpoint(context: Context, endpoint: String, instance: String) {
Expand Down Expand Up @@ -103,19 +112,11 @@ class UnifiedPushReceiver: MessagingReceiver() {
// Schedule sync for all the types identified
val account = accountRepository.fromName(service.accountName)
for (syncDataType in syncDataTypes) {
// TODO: get LocalDataStore according to sync data type (i.e. LocalAddressBookStore if authority is contacts, etc.)
val dataStore: LocalDataStore<*>? = TODO()

val localCollection = dataStore?.getByLocalId(collection.id)
val lastSyncedSyncToken = localCollection?.lastSyncState?.value.takeIf {
localCollection?.lastSyncState?.type == SyncState.Type.SYNC_TOKEN
}
if (lastSyncedSyncToken == pushMessage.syncToken) {
logger.fine("Collection $collection is already up-to-date, ignoring push message")
continue
}

syncWorkerManager.enqueueOneTime(account, syncDataType, fromPush = true)
val localSyncToken = lastSyncToken(account, syncDataType, collection.id)
if (localSyncToken == pushMessage.syncToken)
logger.fine("Collection $collection already up-to-date ($localSyncToken), ignoring push message")
else
syncWorkerManager.enqueueOneTime(account, syncDataType, fromPush = true)
}
}
}
Expand All @@ -129,4 +130,18 @@ class UnifiedPushReceiver: MessagingReceiver() {
}
}

fun lastSyncToken(account: Account, dataType: SyncDataType, collectionId: Long): String? {
val dataStore: LocalDataStore<*>? = when (dataType) {
SyncDataType.CONTACTS -> localAddressBookStore.get()
SyncDataType.EVENTS -> localCalendarStore.get()
SyncDataType.TASKS -> tasksAppManager.get().getDataStore()
}

val provider = TODO("Add LocalDataStore.acquireProvider()")
val localCollection = dataStore?.getByLocalId(account, provider, collectionId)
return localCollection?.lastSyncState?.value.takeIf {
localCollection?.lastSyncState?.type == SyncState.Type.SYNC_TOKEN
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ class LocalAddressBookStore @Inject constructor(
}
}

override fun getByLocalId(account: Account, provider: ContentProviderClient, id: Long): LocalAddressBook? {
val accountManager = AccountManager.get(context)
return accountManager.getAccountsByType(context.getString(R.string.account_type_address_book))
.filter { addressBookAccount ->
accountManager.getUserData(addressBookAccount, LocalAddressBook.USER_DATA_COLLECTION_ID).toLongOrNull() == id
}
.map { addressBookAccount ->
localAddressBookFactory.create(account, addressBookAccount, provider)
}.firstOrNull()
}


override fun update(provider: ContentProviderClient, localCollection: LocalAddressBook, fromCollection: Collection) {
var currentAccount = localCollection.addressBookAccount
Expand Down
20 changes: 11 additions & 9 deletions app/src/main/kotlin/at/bitfire/davdroid/resource/LocalDataStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ interface LocalDataStore<T: LocalCollection<*>> {
/**
* Retrieves the local collection with the given local (database) ID.
*
* @param id the local (database) ID of the collection ([Collection.id])
* @param account the account that the data store is associated with
* @param provider the content provider client
* @param id the local (database) ID of the collection ([Collection.id])
*
* @return the local collection, or `null` if none was found
*/
fun getByLocalId(id: Long): T?
fun getByLocalId(account: Account, provider: ContentProviderClient, id: Long): T?

/**
* Updates the local collection with the data from the given (remote) collection info.
Expand All @@ -53,13 +55,6 @@ interface LocalDataStore<T: LocalCollection<*>> {
*/
fun update(provider: ContentProviderClient, localCollection: T, fromCollection: Collection)

/**
* Deletes the local collection.
*
* @param localCollection the local collection to delete
*/
fun delete(localCollection: T)

/**
* Changes the account assigned to the containing data to another one.
*
Expand All @@ -68,4 +63,11 @@ interface LocalDataStore<T: LocalCollection<*>> {
*/
fun updateAccount(oldAccount: Account, newAccount: Account)

/**
* Deletes the local collection.
*
* @param localCollection the local collection to delete
*/
fun delete(localCollection: T)

}

0 comments on commit 68f3529

Please sign in to comment.