Skip to content

Commit

Permalink
Merge pull request #86 from Infomaniak/improve-transferController
Browse files Browse the repository at this point in the history
chore: Avoid to use a nullable realm in TransferController
  • Loading branch information
tevincent authored Nov 13, 2024
2 parents c5309e5 + 68379d9 commit 0375bd9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,32 @@ import io.realm.kotlin.query.Sort
import io.realm.kotlin.query.TRUE_PREDICATE
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.mapLatest
import kotlin.coroutines.cancellation.CancellationException

@OptIn(ExperimentalCoroutinesApi::class)
class TransferController(private val realmProvider: RealmProvider) {

private val realm by lazy { realmProvider.realmTransfers }
private val realm by lazy { realmProvider.realmTransfers!! }

//region Get data
@Throws(RealmException::class)
internal fun getTransfers(transferDirection: TransferDirection? = null): RealmResults<TransferDB>? = runThrowingRealm {
internal fun getTransfers(transferDirection: TransferDirection? = null): RealmResults<TransferDB> = runThrowingRealm {
val sentFilterQuery = when (transferDirection) {
null -> TRUE_PREDICATE
else -> "${TransferDB.transferDirectionPropertyName} == '${transferDirection}'"
}
return realm?.query<TransferDB>(sentFilterQuery)?.sort(TransferDB::createdDateTimestamp.name, Sort.DESCENDING)?.find()
return realm.query<TransferDB>(sentFilterQuery).sort(TransferDB::createdDateTimestamp.name, Sort.DESCENDING).find()
}

@Throws(RealmException::class)
fun getTransfersFlow(transferDirection: TransferDirection): Flow<List<Transfer>> = runThrowingRealm {
return getTransfers(transferDirection)?.asFlow()?.mapLatest { it.list } ?: emptyFlow()
return getTransfers(transferDirection).asFlow().mapLatest { it.list }
}

@Throws(RealmException::class)
fun getTransfer(linkUUID: String): Transfer? = runThrowingRealm {
return realm?.query<TransferDB>("${TransferDB::linkUUID.name} == '$linkUUID'")?.first()?.find()
return realm.query<TransferDB>("${TransferDB::linkUUID.name} == '$linkUUID'").first().find()
}

@Throws(RealmException::class)
Expand All @@ -71,7 +70,7 @@ class TransferController(private val realmProvider: RealmProvider) {
//region Upsert data
@Throws(RealmException::class, CancellationException::class)
suspend fun upsert(transfer: Transfer, transferDirection: TransferDirection) = runThrowingRealm {
realm?.write {
realm.write {
this.copyToRealm(TransferDB(transfer, transferDirection), UpdatePolicy.ALL)
}
}
Expand All @@ -82,7 +81,7 @@ class TransferController(private val realmProvider: RealmProvider) {
uploadSession: UploadSession,
transferStatus: TransferStatus,
) = runThrowingRealm {
realm?.write {
realm.write {
this.copyToRealm(TransferDB(linkUUID, uploadSession, transferStatus), UpdatePolicy.ALL)
}
}
Expand All @@ -91,7 +90,7 @@ class TransferController(private val realmProvider: RealmProvider) {
//region Update data
@Throws(RealmException::class, CancellationException::class)
suspend fun removeData() = runThrowingRealm {
realm?.write { deleteAll() }
realm.write { deleteAll() }
}
//endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class TransferControllerTest {
fun canGetTransfers() = runTest {
addTwoRandomTransfersInDatabase()
val transfers = transferController.getTransfers()
assertEquals(2, transfers?.count(), "The transfer list must contain 2 items")
assertEquals(2, transfers.count(), "The transfer list must contain 2 items")
}

@Test
Expand Down Expand Up @@ -100,7 +100,7 @@ class TransferControllerTest {
fun canRemoveTransfers() = runTest {
transferController.upsert(DummyTransfer.transfer1, TransferDirection.SENT)
transferController.removeData()
assertEquals(0, transferController.getTransfers()?.count(), "The transfers table must be empty")
assertEquals(0, transferController.getTransfers().count(), "The transfers table must be empty")
}

private suspend fun canCreateTransfer(sent: TransferDirection) {
Expand Down

0 comments on commit 0375bd9

Please sign in to comment.