Skip to content

Commit

Permalink
feat: Auto-delete old expired Transfers
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinBoulongne committed Nov 29, 2024
1 parent 1d5d9bf commit 8fa203a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ package com.infomaniak.swisstransfer.ui
import android.app.Application
import androidx.hilt.work.HiltWorkerFactory
import androidx.work.Configuration
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.ui.TransferUi
import com.infomaniak.multiplatform_swisstransfer.common.models.TransferDirection
import com.infomaniak.multiplatform_swisstransfer.managers.AccountManager
import com.infomaniak.multiplatform_swisstransfer.managers.TransferManager
import com.infomaniak.swisstransfer.BuildConfig
import com.infomaniak.swisstransfer.ui.utils.AccountUtils
import com.infomaniak.swisstransfer.ui.utils.getExpiredTransfers
import dagger.hilt.android.HiltAndroidApp
import io.sentry.SentryEvent
import io.sentry.SentryOptions
Expand All @@ -41,6 +45,9 @@ class MainApplication : Application(), Configuration.Provider {
@Inject
lateinit var accountUtils: AccountUtils

@Inject
lateinit var transferManager: TransferManager

@Inject
lateinit var globalCoroutineScope: CoroutineScope

Expand All @@ -55,6 +62,7 @@ class MainApplication : Application(), Configuration.Provider {

globalCoroutineScope.launch {
accountUtils.init()
collectTransfers()
}

SentryAndroid.init(this) { options: SentryAndroidOptions ->
Expand All @@ -65,4 +73,17 @@ class MainApplication : Application(), Configuration.Provider {
}
}
}

private suspend fun collectTransfers() {
transferManager.getTransfers(TransferDirection.SENT).collect(::deleteExpiredTransfers)
transferManager.getTransfers(TransferDirection.RECEIVED).collect(::deleteExpiredTransfers)
}

private suspend fun deleteExpiredTransfers(transfers: List<TransferUi>) {
deleteTransfers(transfers.getExpiredTransfers(since = 15))
}

private suspend fun deleteTransfers(transfers: List<TransferUi>) {
transfers.forEach { transferManager.deleteTransfer(it.uuid) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@ package com.infomaniak.swisstransfer.ui.utils
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.ui.TransferUi

val TransferUi.isExpired: Boolean get() = expiresInDays < 0 || downloadLeft <= 0

/**
* Get Transfers that are expired since a certain amount of days.
*
* @param since The amount of days since this Transfer is expired.
*/
fun List<TransferUi>.getExpiredTransfers(since: Int): List<TransferUi> = filter { it.expiresInDays < -since }

0 comments on commit 8fa203a

Please sign in to comment.