-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from Infomaniak/file-picker-transfer
Add basic file picker logic backbone to new transfers
- Loading branch information
Showing
7 changed files
with
387 additions
and
23 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/NewTransferViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Infomaniak SwissTransfer - Android | ||
* Copyright (C) 2024 Infomaniak Network SA | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.infomaniak.swisstransfer.ui.screen.newtransfer | ||
|
||
import android.net.Uri | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.SharedFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class NewTransferViewModel @Inject constructor(private val transferFilesManager: TransferFilesManager) : ViewModel() { | ||
|
||
private val _transferFiles = MutableStateFlow<List<TransferFile>>(emptyList()) | ||
val transferFiles: StateFlow<List<TransferFile>> = _transferFiles | ||
|
||
private val _failedTransferFileCount = MutableSharedFlow<Int>() | ||
val failedTransferFileCount: SharedFlow<Int> = _failedTransferFileCount | ||
|
||
fun addFiles(uris: List<Uri>) { | ||
viewModelScope.launch { | ||
val alreadyUsedFileNames = buildSet { transferFiles.value.forEach { add(it.fileName) } } | ||
|
||
val newTransferFiles = transferFilesManager.getTransferFiles(uris, alreadyUsedFileNames) | ||
|
||
_transferFiles.value += newTransferFiles | ||
_failedTransferFileCount.emit(uris.count() - newTransferFiles.count()) | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/TransferFile.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Infomaniak SwissTransfer - Android | ||
* Copyright (C) 2024 Infomaniak Network SA | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.infomaniak.swisstransfer.ui.screen.newtransfer | ||
|
||
import android.net.Uri | ||
|
||
data class TransferFile(val fileName: String, val size: Long, val uri: Uri) |
73 changes: 73 additions & 0 deletions
73
app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/TransferFilesManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Infomaniak SwissTransfer - Android | ||
* Copyright (C) 2024 Infomaniak Network SA | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.infomaniak.swisstransfer.ui.screen.newtransfer | ||
|
||
import android.content.ContentResolver | ||
import android.content.Context | ||
import android.database.Cursor | ||
import android.net.Uri | ||
import android.provider.OpenableColumns | ||
import com.infomaniak.swisstransfer.ui.utils.FileNameUtils | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class TransferFilesManager @Inject constructor(@ApplicationContext private val appContext: Context) { | ||
fun getTransferFiles(uris: List<Uri>, alreadyUsedFileNames: Set<String>): MutableSet<TransferFile> { | ||
val currentUsedFileNames = alreadyUsedFileNames.toMutableSet() | ||
val transferFiles = mutableSetOf<TransferFile>() | ||
|
||
uris.forEach { uri -> | ||
getTransferFile(uri, currentUsedFileNames)?.let { transferFile -> | ||
currentUsedFileNames += transferFile.fileName | ||
transferFiles += transferFile | ||
} | ||
} | ||
|
||
return transferFiles | ||
} | ||
|
||
private fun getTransferFile(uri: Uri, alreadyUsedFileNames: Set<String>): TransferFile? { | ||
val contentResolver: ContentResolver = appContext.contentResolver | ||
val cursor: Cursor? = contentResolver.query(uri, null, null, null, null) | ||
|
||
return cursor?.getFileNameAndSize()?.let { (name, size) -> | ||
val uniqueName = FileNameUtils.postfixExistingFileNames(name, alreadyUsedFileNames) | ||
TransferFile(uniqueName, size, uri) | ||
} | ||
} | ||
|
||
private fun Cursor.getFileNameAndSize(): Pair<String, Long>? = use { | ||
if (it.moveToFirst()) { | ||
val displayNameColumnIndex = it.getColumnIndexOrNull(OpenableColumns.DISPLAY_NAME) ?: return null | ||
val fileName = it.getString(displayNameColumnIndex) | ||
|
||
val fileSizeColumnIndex = it.getColumnIndexOrNull(OpenableColumns.SIZE) ?: return null | ||
val fileSize = it.getLong(fileSizeColumnIndex) | ||
|
||
fileName to fileSize | ||
} else { | ||
null | ||
} | ||
} | ||
|
||
private fun Cursor.getColumnIndexOrNull(column: String): Int? { | ||
return getColumnIndex(column).takeIf { it != -1 } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
app/src/main/java/com/infomaniak/swisstransfer/ui/utils/FileNameUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Infomaniak SwissTransfer - Android | ||
* Copyright (C) 2024 Infomaniak Network SA | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.infomaniak.swisstransfer.ui.utils | ||
|
||
object FileNameUtils { | ||
|
||
fun postfixExistingFileNames(fileName: String, existingFileNames: Set<String>): String { | ||
return if (fileName in existingFileNames) { | ||
val postfixedFileName = PostfixedFileName.fromFileName(fileName) | ||
|
||
while (postfixedFileName.fullName() in existingFileNames) { | ||
postfixedFileName.incrementPostfix() | ||
} | ||
|
||
postfixedFileName.fullName() | ||
} else { | ||
fileName | ||
} | ||
} | ||
|
||
private data class PostfixedFileName( | ||
private val start: String, | ||
private var postfixNumber: Int, | ||
private val end: String, | ||
private val extension: String, | ||
) { | ||
fun incrementPostfix() { | ||
postfixNumber += 1 | ||
} | ||
|
||
fun fullName(): String = "$start$postfixNumber$end$extension" | ||
|
||
companion object { | ||
fun fromFileName(fileName: String): PostfixedFileName { | ||
val (name, ext) = splitNameAndExtension(fileName) | ||
|
||
return PostfixedFileName( | ||
start = "$name(", | ||
postfixNumber = 1, | ||
end = ")", | ||
extension = ext, | ||
) | ||
} | ||
|
||
private fun splitNameAndExtension(fileName: String): Pair<String, String> { | ||
val dotIndex = fileName.lastIndexOf('.') | ||
|
||
// If there's no dot or it's the first/last character, return the whole name and an empty extension | ||
return if (dotIndex == -1) { | ||
fileName to "" | ||
} else { | ||
fileName.substring(0, dotIndex) to fileName.substring(dotIndex) | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.