Skip to content

Commit

Permalink
chore: Order expired transfers to the end of the list
Browse files Browse the repository at this point in the history
  • Loading branch information
tevincent authored and KevinBoulongne committed Jan 16, 2025
1 parent a0001c7 commit 4d23db9
Showing 1 changed file with 56 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,27 @@ import io.realm.kotlin.ext.query
import io.realm.kotlin.ext.toRealmList
import io.realm.kotlin.query.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.map
import kotlinx.datetime.Clock
import kotlin.coroutines.cancellation.CancellationException

class TransferController(private val realmProvider: RealmProvider) {

//region Get data
@Throws(RealmException::class)
fun getTransfersCountFlow(transferDirection: TransferDirection): Flow<Long> = realmProvider.flowWithTransfersDb {
getTransfersCount(realmProvider, transferDirection).asFlow()
}

@Throws(RealmException::class, CancellationException::class)
internal suspend fun getTransfers(
transferDirection: TransferDirection? = null
): RealmResults<TransferDB> = getTransfersQuery(realmProvider, transferDirection).findSuspend()
): List<TransferDB> = getTransfers(realmProvider, transferDirection)

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

@Throws(RealmException::class)
fun getTransfersCountFlow(transferDirection: TransferDirection): Flow<Long> = realmProvider.flowWithTransfersDb {
getTransfersQuery(realmProvider, transferDirection).count().asFlow()
getTransfersFlow(realmProvider, transferDirection)
}

@Throws(RealmException::class)
Expand Down Expand Up @@ -197,15 +198,59 @@ class TransferController(private val realmProvider: RealmProvider) {
}

@Throws(RealmException::class, CancellationException::class)
suspend fun getTransfersQuery(
suspend fun getTransfersCount(
realmProvider: RealmProvider,
transferDirection: TransferDirection?,
): RealmScalarQuery<Long> = realmProvider.withTransfersDb { realm ->
return realm.query<TransferDB>(directionFilterQuery(transferDirection)).count()
}

@Throws(RealmException::class, CancellationException::class)
suspend fun getTransfers(
realmProvider: RealmProvider,
transferDirection: TransferDirection?,
): List<TransferDB> = realmProvider.withTransfersDb { realm ->

val normalTransfers = getNormalTransfersQuery(realm, transferDirection).findSuspend()
val expiredTransfers = getExpiredTransfersQuery(realm, transferDirection).findSuspend()

return@withTransfersDb normalTransfers + expiredTransfers
}

@Throws(RealmException::class, CancellationException::class)
suspend fun getTransfersFlow(
realmProvider: RealmProvider,
transferDirection: TransferDirection?,
): RealmQuery<TransferDB> = realmProvider.withTransfersDb { realm ->
val directionFilterQuery = when (transferDirection) {
): Flow<List<TransferDB>> = realmProvider.withTransfersDb { realm ->

val normalTransfers = getNormalTransfersQuery(realm, transferDirection).asFlow()
val expiredTransfers = getExpiredTransfersQuery(realm, transferDirection).asFlow()

return@withTransfersDb normalTransfers.combine(expiredTransfers) { normal, expired ->
normal.list + expired.list
}
}

private fun getNormalTransfersQuery(realm: Realm, transferDirection: TransferDirection?): RealmQuery<TransferDB> {
val directionFilterQuery = directionFilterQuery(transferDirection)
return realm.query<TransferDB>("$directionFilterQuery AND NOT ($expiredDateQuery OR $downloadCounterQuery)")
.sort(TransferDB::createdDateTimestamp.name, Sort.DESCENDING)
}

private fun getExpiredTransfersQuery(realm: Realm, transferDirection: TransferDirection?): RealmQuery<TransferDB> {
val directionFilterQuery = directionFilterQuery(transferDirection)
return realm.query<TransferDB>("$directionFilterQuery AND ($expiredDateQuery OR $downloadCounterQuery)")
.sort(TransferDB::createdDateTimestamp.name, Sort.DESCENDING)
}

private fun directionFilterQuery(transferDirection: TransferDirection?): String {
return when (transferDirection) {
null -> TRUE_PREDICATE
else -> "${TransferDB.transferDirectionPropertyName} == '${transferDirection}'"
}
return realm.query<TransferDB>(directionFilterQuery).sort(TransferDB::createdDateTimestamp.name, Sort.DESCENDING)
}

private val expiredDateQuery = "${TransferDB::expiredDateTimestamp.name} < ${Clock.System.now().epochSeconds}"
private val downloadCounterQuery = "${TransferDB::downloadCounterCredit.name} <= 0"
}
}

0 comments on commit 4d23db9

Please sign in to comment.