diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/received/ReceivedScreen.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/received/ReceivedScreen.kt index 6c82f97b0..ea3835177 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/received/ReceivedScreen.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/received/ReceivedScreen.kt @@ -45,12 +45,15 @@ fun ReceivedScreen( ) { val transfers by transfersViewModel.receivedTransfers.collectAsStateWithLifecycle() + val isLoading by remember { derivedStateOf { transfers == null } } - ReceivedScreen( - navigateToDetails = navigateToDetails, - getSelectedTransferUuid = getSelectedTransferUuid, - getTransfers = { transfers }, - ) + if (!isLoading) { + ReceivedScreen( + navigateToDetails = navigateToDetails, + getSelectedTransferUuid = getSelectedTransferUuid, + getTransfers = { transfers!! }, + ) + } } @Composable diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/sent/SentScreen.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/sent/SentScreen.kt index 404f6b286..096d1eb6a 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/sent/SentScreen.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/sent/SentScreen.kt @@ -25,6 +25,8 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.remember import androidx.compose.ui.tooling.preview.PreviewParameter import androidx.hilt.navigation.compose.hiltViewModel +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.compose.LifecycleEventEffect import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.infomaniak.multiplatform_swisstransfer.common.interfaces.ui.TransferUi import com.infomaniak.multiplatform_swisstransfer.common.models.TransferDirection @@ -46,12 +48,19 @@ fun SentScreen( ) { val transfers by transfersViewModel.sentTransfers.collectAsStateWithLifecycle() + val isLoading by remember { derivedStateOf { transfers == null } } - SentScreen( - navigateToDetails = navigateToDetails, - getSelectedTransferUuid = getSelectedTransferUuid, - getTransfers = { transfers }, - ) + LifecycleEventEffect(Lifecycle.Event.ON_RESUME) { + transfersViewModel.fetchPendingTransfers() + } + + if (!isLoading) { + SentScreen( + navigateToDetails = navigateToDetails, + getSelectedTransferUuid = getSelectedTransferUuid, + getTransfers = { transfers!! }, + ) + } } @Composable diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/transfers/TransfersViewModel.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/transfers/TransfersViewModel.kt index 0d8fcfbe0..5686ebe21 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/transfers/TransfersViewModel.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/transfers/TransfersViewModel.kt @@ -23,23 +23,43 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.infomaniak.multiplatform_swisstransfer.common.models.TransferDirection import com.infomaniak.multiplatform_swisstransfer.managers.TransferManager +import com.infomaniak.sentry.SentryLog +import com.infomaniak.swisstransfer.di.IoDispatcher import dagger.hilt.android.lifecycle.HiltViewModel -import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.stateIn +import kotlinx.coroutines.launch import javax.inject.Inject @HiltViewModel -class TransfersViewModel @Inject constructor(transferManager: TransferManager) : ViewModel() { +class TransfersViewModel @Inject constructor( + private val transferManager: TransferManager, + @IoDispatcher private val ioDispatcher: CoroutineDispatcher, +) : ViewModel() { val sentTransfers = transferManager.getTransfers(TransferDirection.SENT) - .flowOn(Dispatchers.IO) - .stateIn(viewModelScope, SharingStarted.Eagerly, initialValue = emptyList()) + .flowOn(ioDispatcher) + .stateIn(viewModelScope, SharingStarted.Eagerly, initialValue = null) val receivedTransfers = transferManager.getTransfers(TransferDirection.RECEIVED) - .flowOn(Dispatchers.IO) - .stateIn(viewModelScope, SharingStarted.Eagerly, initialValue = emptyList()) + .flowOn(ioDispatcher) + .stateIn(viewModelScope, SharingStarted.Eagerly, initialValue = null) val selectedTransferUuids: SnapshotStateMap = mutableStateMapOf() + + fun fetchPendingTransfers() { + viewModelScope.launch(ioDispatcher) { + runCatching { + transferManager.fetchWaitingTransfers() + }.onFailure { + SentryLog.e(TAG, "Failure for fetchWaitingTransfers", it) + } + } + } + + companion object { + private val TAG = TransfersViewModel::class.java.simpleName + } }