Skip to content

Commit

Permalink
chore: Rename several symbols from RealmProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisCAD committed Dec 17, 2024
1 parent 9caf3fd commit 5772e3a
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class AccountManager internal constructor(
@Throws(RealmException::class, CancellationException::class)
suspend fun loadUser(userId: Int) {
appSettingsController.initAppSettings(emailLanguageUtils.getEmailLanguageFromLocal())
realmProvider.openRealmTransfers(userId)
realmProvider.openTransfersDb(userId)
}

/**
Expand All @@ -60,6 +60,6 @@ class AccountManager internal constructor(
uploadController.removeData()
transferController.removeData()

realmProvider.closeAllRealms()
realmProvider.closeAllDatabases()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,45 +36,43 @@ import kotlinx.coroutines.flow.flow

class RealmProvider(private val loadDataInMemory: Boolean = false) {

val realmAppSettings by lazy { Realm.open(realmAppSettingsConfiguration) }
val realmUploads by lazy { Realm.open(realmUploadDBConfiguration) }
val appSettings by lazy { Realm.open(realmAppSettingsConfiguration) }
val uploads by lazy { Realm.open(realmUploadDBConfiguration) }
private val transfersAsync = CompletableDeferred<Realm>()
private suspend fun transfers(): Realm = transfersAsync.await()

suspend fun realmTransfers(): Realm = realmTransfersAsync.await()

private val realmTransfersAsync = CompletableDeferred<Realm>()

fun openRealmTransfers(userId: Int) {
realmTransfersAsync.complete(Realm.open(realmTransfersConfiguration(userId)))
fun openTransfersDb(userId: Int) {
transfersAsync.complete(Realm.open(realmTransfersConfiguration(userId)))
}

suspend inline fun <T> withRealm(block: (Realm) -> T): T {
internal suspend inline fun <T> withTransfersDb(block: (Realm) -> T): T {
runThrowingRealm {
return block(realmTransfers())
return block(transfers())
}
}

fun <T> flowWithRealm(block: suspend (Realm) -> Flow<T>): Flow<T> = flow {
internal fun <T> flowWithTransfersDb(block: suspend (Realm) -> Flow<T>): Flow<T> = flow {
runThrowingRealm {
emitAll(block(realmTransfers()))
emitAll(block(transfers()))
}
}

fun closeRealmAppSettings() {
realmAppSettings.close()
fun closeAppSettingsDb() {
appSettings.close()
}

fun closeRealmUploads() {
realmUploads.close()
fun closeUploadsDb() {
uploads.close()
}

suspend fun closeRealmTransfers() {
realmTransfersAsync.await().close()
suspend fun closeTransfersDb() {
transfersAsync.await().close()
}

suspend fun closeAllRealms() {
closeRealmAppSettings()
closeRealmUploads()
closeRealmTransfers()
suspend fun closeAllDatabases() {
closeAppSettingsDb()
closeUploadsDb()
closeTransfersDb()
}

private val realmAppSettingsConfiguration = RealmConfiguration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import kotlin.coroutines.cancellation.CancellationException
@OptIn(ExperimentalCoroutinesApi::class)
class AppSettingsController(private val realmProvider: RealmProvider) {

private val realm by lazy { realmProvider.realmAppSettings }
private val realm by lazy { realmProvider.appSettings }

private val appSettingsQuery get() = realm.query<AppSettingsDB>().first()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import io.realm.kotlin.ext.query
import kotlin.coroutines.cancellation.CancellationException

class EmailTokensController(private val realmProvider: RealmProvider) {
private val realm by lazy { realmProvider.realmAppSettings }
private val realm by lazy { realmProvider.appSettings }

//region Get data
@Throws(RealmException::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import kotlinx.coroutines.flow.mapLatest
class FileController(private val realmProvider: RealmProvider) {

@Throws(RealmException::class)
fun getFilesFromTransfer(folderUuid: String): Flow<List<File>> = realmProvider.flowWithRealm { realm ->
fun getFilesFromTransfer(folderUuid: String): Flow<List<File>> = realmProvider.flowWithTransfersDb { realm ->
val query = "${FileDB::folder.name}.uuid == '$folderUuid'"
realm.query<FileDB>(query).asFlow().mapLatest { it.list }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class TransferController(private val realmProvider: RealmProvider) {
@Throws(RealmException::class, CancellationException::class)
internal suspend fun getTransfers(
transferDirection: TransferDirection? = null
): RealmResults<TransferDB> = realmProvider.withRealm { realm ->
): RealmResults<TransferDB> = realmProvider.withTransfersDb { realm ->
val directionFilterQuery = when (transferDirection) {
null -> TRUE_PREDICATE
else -> "${TransferDB.transferDirectionPropertyName} == '${transferDirection}'"
Expand All @@ -55,30 +55,30 @@ class TransferController(private val realmProvider: RealmProvider) {
}

@Throws(RealmException::class)
fun getTransfersFlow(transferDirection: TransferDirection): Flow<List<Transfer>> = realmProvider.flowWithRealm {
fun getTransfersFlow(transferDirection: TransferDirection): Flow<List<Transfer>> = realmProvider.flowWithTransfersDb {
getTransfers(transferDirection).asFlow().mapLatest { it.list }
}

@Throws(RealmException::class)
fun getTransferFlow(linkUUID: String): Flow<Transfer?> = realmProvider.flowWithRealm { realm ->
fun getTransferFlow(linkUUID: String): Flow<Transfer?> = realmProvider.flowWithTransfersDb { realm ->
getTransferQuery(realm, linkUUID).asFlow().mapLatest { it.obj }
}

@Throws(RealmException::class, CancellationException::class)
suspend fun getTransfer(linkUUID: String): Transfer? = realmProvider.withRealm { realm ->
suspend fun getTransfer(linkUUID: String): Transfer? = realmProvider.withTransfersDb { realm ->
return getTransferQuery(realm, linkUUID).find()
}

@Throws(RealmException::class, CancellationException::class)
suspend fun getNotReadyTransfers(): List<Transfer> = realmProvider.withRealm { realm ->
suspend fun getNotReadyTransfers(): List<Transfer> = realmProvider.withTransfersDb { realm ->
val query = "${TransferDB.transferStatusPropertyName} != '${TransferStatus.READY.name}'"
return realm.query<TransferDB>(query).find()
}
//endregion

//region Upsert data
@Throws(RealmException::class, CancellationException::class, TransferWithoutFilesException::class)
suspend fun upsert(transfer: Transfer, transferDirection: TransferDirection, password: String?) = realmProvider.withRealm { realm ->
suspend fun upsert(transfer: Transfer, transferDirection: TransferDirection, password: String?) = realmProvider.withTransfersDb { realm ->
realm.write {
val transferDB = TransferDB(transfer, transferDirection, password)
transferDB.container?.files?.let { transferFiles ->
Expand All @@ -93,7 +93,7 @@ class TransferController(private val realmProvider: RealmProvider) {
linkUUID: String,
uploadSession: UploadSession,
transferStatus: TransferStatus,
) = realmProvider.withRealm { realm ->
) = realmProvider.withTransfersDb { realm ->
val transferDB = TransferDB(linkUUID, uploadSession, transferStatus).apply {
container?.files?.let { files ->
FileUtils.getFileDBTree(containerUUID, files)
Expand All @@ -108,20 +108,20 @@ class TransferController(private val realmProvider: RealmProvider) {

//region Update data
@Throws(RealmException::class, CancellationException::class)
suspend fun deleteTransfer(transferUUID: String) = realmProvider.withRealm { realm ->
suspend fun deleteTransfer(transferUUID: String) = realmProvider.withTransfersDb { realm ->
realm.write {
val transferToDelete = query<TransferDB>("${TransferDB::linkUUID.name} == '$transferUUID'").first()
delete(transferToDelete)
}
}

@Throws(RealmException::class, CancellationException::class)
suspend fun deleteExpiredTransfers() = realmProvider.withRealm { realm ->
suspend fun deleteExpiredTransfers() = realmProvider.withTransfersDb { realm ->
realm.write { delete(getExpiredTransfersQuery(realm = this)) }
}

@Throws(RealmException::class, CancellationException::class)
suspend fun removeData() = realmProvider.withRealm { realm ->
suspend fun removeData() = realmProvider.withTransfersDb { realm ->
realm.write { deleteAll() }
}
//endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import kotlin.coroutines.cancellation.CancellationException

class UploadController(private val realmProvider: RealmProvider) {

private val realm by lazy { realmProvider.realmUploads }
private val realm by lazy { realmProvider.uploads }

//region Queries
private fun getUploadsQuery() = realm.query<UploadSessionDB>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ class TransferControllerTest {

@BeforeTest
fun setup() {
realmProvider = RealmProvider(loadDataInMemory = true).apply { openRealmTransfers(userId = 0) }
realmProvider = RealmProvider(loadDataInMemory = true).apply { openTransfersDb(userId = 0) }
transferController = TransferController(realmProvider)
}

@AfterTest
fun tearDown() = runTest {
transferController.removeData()
realmProvider.closeRealmTransfers()
realmProvider.closeTransfersDb()
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class UploadControllerTest {
@AfterTest
fun tearDown() = runTest {
uploadController.removeData()
realmProvider.closeRealmUploads()
realmProvider.closeUploadsDb()
}

@Test
Expand Down

0 comments on commit 5772e3a

Please sign in to comment.