Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Avoid to use a nullable realm in TransferController #86

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading