diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/MainApplication.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/MainApplication.kt index 27f4dd238..7997cd98f 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/MainApplication.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/MainApplication.kt @@ -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 @@ -41,6 +45,9 @@ class MainApplication : Application(), Configuration.Provider { @Inject lateinit var accountUtils: AccountUtils + @Inject + lateinit var transferManager: TransferManager + @Inject lateinit var globalCoroutineScope: CoroutineScope @@ -55,6 +62,7 @@ class MainApplication : Application(), Configuration.Provider { globalCoroutineScope.launch { accountUtils.init() + collectTransfers() } SentryAndroid.init(this) { options: SentryAndroidOptions -> @@ -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) { + deleteTransfers(transfers.getExpiredTransfers(since = 15)) + } + + private suspend fun deleteTransfers(transfers: List) { + transfers.forEach { transferManager.deleteTransfer(it.uuid) } + } } diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/utils/TransferUiExt.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/utils/TransferUiExt.kt index 5ef5d1457..1ed1d4046 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/utils/TransferUiExt.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/utils/TransferUiExt.kt @@ -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.getExpiredTransfers(since: Int): List = filter { it.expiresInDays < -since }