Skip to content

Commit 9f697f0

Browse files
authored
Repositories: apply new naming scheme (#1423)
* DavHomeSetRepository, DavServiceRepository: apply new naming scheme for (non-)suspending calls * AccountRepository: apply new naming scheme for (non-)suspending calls * Update repository methods to new naming scheme
1 parent ba7f95a commit 9f697f0

22 files changed

+65
-61
lines changed

app/src/androidTest/kotlin/at/bitfire/davdroid/repository/DavHomeSetRepositoryTest.kt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class DavHomeSetRepositoryTest {
3333
fun setUp() {
3434
hiltRule.inject()
3535

36-
serviceId = serviceRepository.insertOrReplace(
36+
serviceId = serviceRepository.insertOrReplaceBlocking(
3737
Service(id=0, accountName="test", type= Service.TYPE_CALDAV, principal = null)
3838
)
3939
}
@@ -43,32 +43,32 @@ class DavHomeSetRepositoryTest {
4343
fun testInsertOrUpdate() {
4444
// should insert new row or update (upsert) existing row - without changing its key!
4545
val entry1 = HomeSet(id=0, serviceId=serviceId, personal=true, url="https://example.com/1".toHttpUrl())
46-
val insertId1 = repository.insertOrUpdateByUrl(entry1)
46+
val insertId1 = repository.insertOrUpdateByUrlBlocking(entry1)
4747
assertEquals(1L, insertId1)
48-
assertEquals(entry1.copy(id = 1L), repository.getById(1L))
48+
assertEquals(entry1.copy(id = 1L), repository.getByIdBlocking(1L))
4949

5050
val updatedEntry1 = HomeSet(id=0, serviceId=serviceId, personal=true, url="https://example.com/1".toHttpUrl(), displayName="Updated Entry")
51-
val updateId1 = repository.insertOrUpdateByUrl(updatedEntry1)
51+
val updateId1 = repository.insertOrUpdateByUrlBlocking(updatedEntry1)
5252
assertEquals(1L, updateId1)
53-
assertEquals(updatedEntry1.copy(id = 1L), repository.getById(1L))
53+
assertEquals(updatedEntry1.copy(id = 1L), repository.getByIdBlocking(1L))
5454

5555
val entry2 = HomeSet(id=0, serviceId=serviceId, personal=true, url= "https://example.com/2".toHttpUrl())
56-
val insertId2 = repository.insertOrUpdateByUrl(entry2)
56+
val insertId2 = repository.insertOrUpdateByUrlBlocking(entry2)
5757
assertEquals(2L, insertId2)
58-
assertEquals(entry2.copy(id = 2L), repository.getById(2L))
58+
assertEquals(entry2.copy(id = 2L), repository.getByIdBlocking(2L))
5959
}
6060

6161
@Test
62-
fun testDelete() {
62+
fun testDeleteBlocking() {
6363
// should delete row with given primary key (id)
6464
val entry1 = HomeSet(id=1, serviceId=serviceId, personal=true, url= "https://example.com/1".toHttpUrl())
6565

66-
val insertId1 = repository.insertOrUpdateByUrl(entry1)
66+
val insertId1 = repository.insertOrUpdateByUrlBlocking(entry1)
6767
assertEquals(1L, insertId1)
68-
assertEquals(entry1, repository.getById(1L))
68+
assertEquals(entry1, repository.getByIdBlocking(1L))
6969

70-
repository.delete(entry1)
71-
assertEquals(null, repository.getById(1L))
70+
repository.deleteBlocking(entry1)
71+
assertEquals(null, repository.getByIdBlocking(1L))
7272
}
7373

7474
}

app/src/androidTest/kotlin/at/bitfire/davdroid/sync/JtxSyncManagerTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class JtxSyncManagerTest {
8383

8484
// Create dummy dependencies
8585
val service = Service(0, account.name, Service.TYPE_CALDAV, null)
86-
val serviceId = serviceRepository.insertOrReplace(service)
86+
val serviceId = serviceRepository.insertOrReplaceBlocking(service)
8787
val dbCollection = Collection(
8888
0,
8989
serviceId,
@@ -106,7 +106,7 @@ class JtxSyncManagerTest {
106106
fun tearDown() {
107107
if (this::localJtxCollection.isInitialized)
108108
localJtxCollectionStore.delete(localJtxCollection)
109-
serviceRepository.deleteAll()
109+
serviceRepository.deleteAllBlocking()
110110
if (this::provider.isInitialized)
111111
provider.closeCompat()
112112
TestAccount.remove(account)

app/src/androidTest/kotlin/at/bitfire/davdroid/ui/CollectionSelectedUseCaseTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ class CollectionSelectedUseCaseTest {
7676
fun setUp() {
7777
hiltRule.inject()
7878

79-
serviceRepository.insertOrReplace(service)
79+
serviceRepository.insertOrReplaceBlocking(service)
8080
collectionRepository.insertOrUpdateByUrl(collection)
8181
}
8282

8383
@After
8484
fun tearDown() {
85-
serviceRepository.deleteAll()
85+
serviceRepository.deleteAllBlocking()
8686
}
8787

8888

app/src/main/kotlin/at/bitfire/davdroid/push/PushMessageHandler.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class PushMessageHandler @Inject constructor(
5353
// Sync all authorities of account that the collection belongs to
5454
// Later: only sync affected collection and authorities
5555
collectionRepository.getSyncableByTopic(topic)?.let { collection ->
56-
serviceRepository.getAsync(collection.serviceId)?.let { service ->
56+
serviceRepository.get(collection.serviceId)?.let { service ->
5757
val syncDataTypes = mutableSetOf<SyncDataType>()
5858
// If the type is an address book, add the contacts type
5959
if (collection.type == TYPE_ADDRESSBOOK)
@@ -78,7 +78,7 @@ class PushMessageHandler @Inject constructor(
7878

7979
} else {
8080
// fallback when no known topic is present (shouldn't happen)
81-
val service = instance.toLongOrNull()?.let { serviceRepository.get(it) }
81+
val service = instance.toLongOrNull()?.let { serviceRepository.getBlocking(it) }
8282
if (service != null) {
8383
logger.warning("Got push message without topic and service, syncing all accounts")
8484
val account = accountRepository.fromName(service.accountName)

app/src/main/kotlin/at/bitfire/davdroid/push/PushRegistrationManager.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class PushRegistrationManager @Inject constructor(
9292
}
9393

9494
private suspend fun updateService(serviceId: Long) {
95-
val service = serviceRepository.getAsync(serviceId) ?: return
95+
val service = serviceRepository.get(serviceId) ?: return
9696

9797
val distributorAvailable = UnifiedPush.getSavedDistributor(context) != null
9898
if (distributorAvailable)
@@ -118,7 +118,7 @@ class PushRegistrationManager @Inject constructor(
118118
* Uses the subscription to subscribe to syncable collections, and then unsubscribes from non-syncable collections.
119119
*/
120120
suspend fun processSubscription(serviceId: Long, endpoint: PushEndpoint) = mutex.withLock {
121-
val service = serviceRepository.getAsync(serviceId) ?: return
121+
val service = serviceRepository.get(serviceId) ?: return
122122

123123
try {
124124
// subscribe to collections which are selected for synchronization
@@ -165,7 +165,7 @@ class PushRegistrationManager @Inject constructor(
165165
* Unsubscribes from all subscribed collections.
166166
*/
167167
suspend fun removeSubscription(serviceId: Long) = mutex.withLock {
168-
val service = serviceRepository.getAsync(serviceId) ?: return
168+
val service = serviceRepository.get(serviceId) ?: return
169169
unsubscribeAll(service)
170170
}
171171

app/src/main/kotlin/at/bitfire/davdroid/repository/AccountRepository.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ import javax.inject.Inject
4444
*/
4545
class AccountRepository @Inject constructor(
4646
private val accountSettingsFactory: AccountSettings.Factory,
47-
private val automaticSyncManager: AutomaticSyncManager,
47+
private val automaticSyncManager: Lazy<AutomaticSyncManager>,
4848
@ApplicationContext private val context: Context,
4949
private val collectionRepository: DavCollectionRepository,
5050
private val homeSetRepository: DavHomeSetRepository,
5151
private val localCalendarStore: Lazy<LocalCalendarStore>,
5252
private val localAddressBookStore: Lazy<LocalAddressBookStore>,
5353
private val logger: Logger,
5454
private val serviceRepository: DavServiceRepository,
55-
private val syncWorkerManager: SyncWorkerManager,
55+
private val syncWorkerManager: Lazy<SyncWorkerManager>,
5656
private val tasksAppManager: Lazy<TasksAppManager>
5757
) {
5858

@@ -70,7 +70,7 @@ class AccountRepository @Inject constructor(
7070
*
7171
* @return account if account creation was successful; null otherwise (for instance because an account with this name already exists)
7272
*/
73-
fun create(accountName: String, credentials: Credentials?, config: DavResourceFinder.Configuration, groupMethod: GroupMethod): Account? {
73+
fun createBlocking(accountName: String, credentials: Credentials?, config: DavResourceFinder.Configuration, groupMethod: GroupMethod): Account? {
7474
val account = fromName(accountName)
7575

7676
// create Android account
@@ -104,7 +104,7 @@ class AccountRepository @Inject constructor(
104104
}
105105

106106
// set up automatic sync (processes inserted services)
107-
automaticSyncManager.updateAutomaticSync(account)
107+
automaticSyncManager.get().updateAutomaticSync(account)
108108

109109
} catch(e: InvalidAccountException) {
110110
logger.log(Level.SEVERE, "Couldn't access account settings", e)
@@ -206,11 +206,11 @@ class AccountRepository @Inject constructor(
206206
}
207207

208208
// account renamed, cancel maybe running synchronization of old account
209-
syncWorkerManager.cancelAllWork(oldAccount)
209+
syncWorkerManager.get().cancelAllWork(oldAccount)
210210

211211
// disable periodic syncs for old account
212212
for (dataType in SyncDataType.entries)
213-
syncWorkerManager.disablePeriodic(oldAccount, dataType)
213+
syncWorkerManager.get().disablePeriodic(oldAccount, dataType)
214214

215215
// update account name references in database
216216
serviceRepository.renameAccount(oldName, newName)
@@ -238,7 +238,7 @@ class AccountRepository @Inject constructor(
238238
}
239239

240240
// update automatic sync
241-
automaticSyncManager.updateAutomaticSync(newAccount)
241+
automaticSyncManager.get().updateAutomaticSync(newAccount)
242242
} finally {
243243
// release AccountsCleanupWorker mutex at the end of this async coroutine
244244
AccountsCleanupWorker.unlockAccountsCleanup()
@@ -251,11 +251,11 @@ class AccountRepository @Inject constructor(
251251
private fun insertService(accountName: String, @ServiceType type: String, info: DavResourceFinder.Configuration.ServiceInfo): Long {
252252
// insert service
253253
val service = Service(0, accountName, type, info.principal)
254-
val serviceId = serviceRepository.insertOrReplace(service)
254+
val serviceId = serviceRepository.insertOrReplaceBlocking(service)
255255

256256
// insert home sets
257257
for (homeSet in info.homeSets)
258-
homeSetRepository.insertOrUpdateByUrl(HomeSet(0, serviceId, true, homeSet))
258+
homeSetRepository.insertOrUpdateByUrlBlocking(HomeSet(0, serviceId, true, homeSet))
259259

260260
// insert collections
261261
for (collection in info.collections.values) {

app/src/main/kotlin/at/bitfire/davdroid/repository/DavCollectionRepository.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class DavCollectionRepository @Inject constructor(
162162

163163
/** Deletes the given collection from the server and the database. */
164164
suspend fun deleteRemote(collection: Collection) {
165-
val service = serviceRepository.get(collection.serviceId) ?: throw IllegalArgumentException("Service not found")
165+
val service = serviceRepository.getBlocking(collection.serviceId) ?: throw IllegalArgumentException("Service not found")
166166
val account = Account(service.accountName, context.getString(R.string.account_type))
167167

168168
httpClientBuilder.get()

app/src/main/kotlin/at/bitfire/davdroid/repository/DavHomeSetRepository.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ class DavHomeSetRepository @Inject constructor(
1515
db: AppDatabase
1616
) {
1717

18-
val dao = db.homeSetDao()
18+
private val dao = db.homeSetDao()
1919

2020
fun getAddressBookHomeSetsFlow(account: Account) =
2121
dao.getBindableByAccountAndServiceTypeFlow(account.name, Service.TYPE_CARDDAV)
2222

2323
fun getBindableByServiceFlow(serviceId: Long) = dao.getBindableByServiceFlow(serviceId)
2424

25-
fun getById(id: Long) = dao.getById(id)
25+
fun getByIdBlocking(id: Long) = dao.getById(id)
2626

27-
fun getByService(serviceId: Long) = dao.getByService(serviceId)
27+
fun getByServiceBlocking(serviceId: Long) = dao.getByService(serviceId)
2828

2929
fun getCalendarHomeSetsFlow(account: Account) =
3030
dao.getBindableByAccountAndServiceTypeFlow(account.name, Service.TYPE_CALDAV)
@@ -38,13 +38,13 @@ class DavHomeSetRepository @Inject constructor(
3838
* @return ID of the row, that has been inserted or updated. -1 If the insert fails due to other reasons.
3939
*/
4040
@Transaction
41-
fun insertOrUpdateByUrl(homeset: HomeSet): Long =
41+
fun insertOrUpdateByUrlBlocking(homeset: HomeSet): Long =
4242
dao.getByUrl(homeset.serviceId, homeset.url.toString())?.let { existingHomeset ->
4343
dao.update(homeset.copy(id = existingHomeset.id))
4444
existingHomeset.id
4545
} ?: dao.insert(homeset)
4646

4747

48-
fun delete(homeSet: HomeSet) = dao.delete(homeSet)
48+
fun deleteBlocking(homeSet: HomeSet) = dao.delete(homeSet)
4949

5050
}

app/src/main/kotlin/at/bitfire/davdroid/repository/DavServiceRepository.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class DavServiceRepository @Inject constructor(
1818

1919
// Read
2020

21-
fun get(id: Long): Service? = dao.get(id)
22-
suspend fun getAsync(id: Long): Service? = dao.getAsync(id)
21+
fun getBlocking(id: Long): Service? = dao.get(id)
22+
suspend fun get(id: Long): Service? = dao.getAsync(id)
2323

2424
suspend fun getAll(): List<Service> = dao.getAll()
2525

@@ -35,7 +35,7 @@ class DavServiceRepository @Inject constructor(
3535

3636
// Create & update
3737

38-
fun insertOrReplace(service: Service) =
38+
fun insertOrReplaceBlocking(service: Service) =
3939
dao.insertOrReplace(service)
4040

4141
suspend fun renameAccount(oldName: String, newName: String) =
@@ -44,7 +44,7 @@ class DavServiceRepository @Inject constructor(
4444

4545
// Delete
4646

47-
fun deleteAll() = dao.deleteAll()
47+
fun deleteAllBlocking() = dao.deleteAll()
4848

4949
suspend fun deleteByAccount(accountName: String) =
5050
dao.deleteByAccount(accountName)

app/src/main/kotlin/at/bitfire/davdroid/repository/DavSyncStatsRepository.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import android.content.pm.PackageManager
99
import at.bitfire.davdroid.db.AppDatabase
1010
import at.bitfire.davdroid.db.SyncStats
1111
import dagger.hilt.android.qualifiers.ApplicationContext
12+
import kotlinx.coroutines.flow.Flow
13+
import kotlinx.coroutines.flow.map
1214
import java.text.Collator
1315
import java.util.logging.Logger
1416
import javax.inject.Inject
15-
import kotlinx.coroutines.flow.Flow
16-
import kotlinx.coroutines.flow.map
1717

1818
class DavSyncStatsRepository @Inject constructor(
1919
@ApplicationContext val context: Context,
@@ -40,7 +40,7 @@ class DavSyncStatsRepository @Inject constructor(
4040
}
4141
}
4242

43-
fun logSyncTime(collectionId: Long, authority: String, lastSync: Long = System.currentTimeMillis()) {
43+
fun logSyncTimeBlocking(collectionId: Long, authority: String, lastSync: Long = System.currentTimeMillis()) {
4444
dao.insertOrReplace(SyncStats(
4545
id = 0,
4646
collectionId = collectionId,

0 commit comments

Comments
 (0)