diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/NewTransferViewModel.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/NewTransferViewModel.kt index 4f69e635f..b4cb6bb7e 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/NewTransferViewModel.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/NewTransferViewModel.kt @@ -26,45 +26,63 @@ import android.provider.OpenableColumns import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.viewModelScope import com.infomaniak.swisstransfer.ui.MainApplication +import com.infomaniak.swisstransfer.ui.utils.FileNameUtils.postfixExistingFileNames +import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.* +import kotlinx.coroutines.launch +@OptIn(ExperimentalCoroutinesApi::class) class NewTransferViewModel(application: Application) : AndroidViewModel(application) { private val context get() = getApplication() as Context - private val _fileUris = MutableStateFlow>(emptyList()) + // TODO: replay = 1 not needed for now because addFiles() cannot be called before we start listening to the sharedflow in the + // viewmodel + private val addedFilesUri = MutableSharedFlow>(replay = 1) + private val allTransferFiles = MutableStateFlow>(emptyList()) - private val _transferFiles: Flow> = _fileUris - .map { uris -> uris.map { getFileNameAndSize(it) } } - - val transferFiles: StateFlow> = _transferFiles - .map { files -> files.filter { file -> file.isSuccessfullyImported } } + val successfulTransferFiles: StateFlow> = allTransferFiles + .mapLatest { files -> files.filter { file -> file.isSuccessfullyImported } } .stateIn( scope = viewModelScope, - started = SharingStarted.Lazily, + started = SharingStarted.Eagerly, initialValue = emptyList() ) - private val alreadyUsedFileNames: StateFlow> = transferFiles - .map { files -> + private val alreadyUsedFileNames: StateFlow> = successfulTransferFiles + .mapLatest { files -> buildSet { files.forEach { add(it.metadata.fileName) } } } .stateIn( scope = viewModelScope, - started = SharingStarted.Lazily, + started = SharingStarted.Eagerly, initialValue = emptySet(), ) - val failedTransferFileCount: StateFlow = _transferFiles.map { files -> files.count { !it.isSuccessfullyImported } } - .stateIn( - scope = viewModelScope, - started = SharingStarted.Lazily, - initialValue = 0 - ) + val failedTransferFileCount: StateFlow = + allTransferFiles.mapLatest { files -> files.count { !it.isSuccessfullyImported } } + .stateIn( + scope = viewModelScope, + started = SharingStarted.Lazily, // TODO: Decide of the SharingStarted strategy once this value is used + initialValue = 0 + ) + + init { + viewModelScope.launch { + addedFilesUri.collect { uris -> + uris.forEach { uri -> + val transferFile = getFileNameAndSize(uri) + allTransferFiles.value += transferFile + } + } + } + } fun addFiles(uris: List) { - _fileUris.value += uris + viewModelScope.launch { + addedFilesUri.emit(uris) + } } private fun getFileNameAndSize(uri: Uri): TransferFile { @@ -83,7 +101,8 @@ class NewTransferViewModel(application: Application) : AndroidViewModel(applicat val fileSizeColumnIndex = it.getColumnIndexOrNull(OpenableColumns.SIZE) ?: return null val fileSize = it.getLong(fileSizeColumnIndex) - return TransferFile.Metadata(fileName, fileSize) + val customName = postfixExistingFileNames(fileName, alreadyUsedFileNames.value) + return TransferFile.Metadata(customName, fileSize) } else { return null } diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/ImportFilesScreen.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/ImportFilesScreen.kt index 16252bf1f..7b14b52a6 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/ImportFilesScreen.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/importfiles/ImportFilesScreen.kt @@ -43,7 +43,7 @@ fun ImportFilesScreen( navigateToTransferTypeScreen: () -> Unit, closeActivity: () -> Unit, ) { - val transferFiles by newTransferViewModel.transferFiles.collectAsState() + val transferFiles by newTransferViewModel.successfulTransferFiles.collectAsState() ImportFilesScreen({ transferFiles }, newTransferViewModel::addFiles, closeActivity, navigateToTransferTypeScreen) } @@ -91,7 +91,7 @@ private fun ImportFilesScreen( content = { if (transferFiles().isNotEmpty()) { LazyColumn { - items(items = transferFiles(), key = { it.uri }) { file -> + items(items = transferFiles(), key = { it.metadata.fileName }) { file -> Text(text = "${file.metadata.fileName} - ${file.metadata.size}") } }