Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn FileUiItem from interface to data class to make future replacement easier #89

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ import com.infomaniak.swisstransfer.ui.theme.Margin
import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme
import com.infomaniak.swisstransfer.ui.utils.PreviewAllWindows

// TODO: Get the interface from the shared kmp code
interface FileUiItem {
val uid: String
val fileName: String
val fileSizeInBytes: Long
val mimeType: String?
val uri: String
}
// TODO: Get the data class from the shared kmp code
data class FileUi(
val uid: String,
val fileName: String,
val fileSizeInBytes: Long,
val mimeType: String?,
val uri: String,
)

@Composable
fun FileItem(
file: FileUiItem,
file: FileUi,
isRemoveButtonVisible: Boolean,
isCheckboxVisible: Boolean,
isChecked: () -> Boolean = { false },
Expand Down Expand Up @@ -131,7 +131,7 @@ private fun FileItemContent(

@PreviewAllWindows
@Composable
private fun FileItemPreview(@PreviewParameter(FileUiListPreviewParameter::class) files: List<FileUiItem>) {
private fun FileItemPreview(@PreviewParameter(FileUiListPreviewParameter::class) files: List<FileUi>) {
SwissTransferTheme {
Surface {
Column(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import com.infomaniak.swisstransfer.ui.utils.PreviewAllWindows
@Composable
fun FileItemList(
modifier: Modifier = Modifier,
files: List<FileUiItem>,
files: List<FileUi>,
isRemoveButtonVisible: Boolean,
isCheckboxVisible: Boolean,
isUidChecked: (String) -> Boolean,
Expand Down Expand Up @@ -61,7 +61,7 @@ fun FileItemList(

@PreviewAllWindows
@Composable
private fun FileItemListPreview(@PreviewParameter(FileUiListPreviewParameter::class) files: List<FileUiItem>) {
private fun FileItemListPreview(@PreviewParameter(FileUiListPreviewParameter::class) files: List<FileUi>) {
SwissTransferTheme {
FileItemList(
files = files,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import com.infomaniak.swisstransfer.ui.utils.hasPreview

@Composable
fun FilePreview(
file: FileUiItem,
file: FileUi,
circleColor: Color,
circleSize: Dp,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme
@Composable
fun SmallFileItem(
modifier: Modifier = Modifier,
file: FileUiItem,
file: FileUi,
smallFileTileSize: SmallFileTileSize,
onRemove: (() -> Unit)? = null,
) {
Expand Down Expand Up @@ -67,7 +67,7 @@ enum class SmallFileTileSize(val size: Dp, val shape: Shape) {
@Preview(name = "Light")
@Preview(name = "Dark", uiMode = Configuration.UI_MODE_NIGHT_YES or Configuration.UI_MODE_TYPE_NORMAL)
@Composable
private fun SmallFileItemPreview(@PreviewParameter(FileUiListPreviewParameter::class) files: List<FileUiItem>) {
private fun SmallFileItemPreview(@PreviewParameter(FileUiListPreviewParameter::class) files: List<FileUi>) {
SwissTransferTheme {
Surface(color = SwissTransferTheme.materialColors.surfaceContainerHighest) {
Column(Modifier.padding(16.dp)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,32 @@
package com.infomaniak.swisstransfer.ui.previewparameter

import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import com.infomaniak.swisstransfer.ui.components.FileUiItem
import com.infomaniak.swisstransfer.ui.components.FileUi

class FileUiListPreviewParameter : PreviewParameterProvider<List<FileUiItem>> {
override val values: Sequence<List<FileUiItem>> = sequenceOf(
class FileUiListPreviewParameter : PreviewParameterProvider<List<FileUi>> {
override val values: Sequence<List<FileUi>> = sequenceOf(
listOf(
object : FileUiItem { // Non-image file
override val fileName: String = "How to not get fired.pdf"
override val uid: String = fileName
override val fileSizeInBytes: Long = 10302130
override val mimeType: String? = null
override val uri: String = ""
},
object : FileUiItem { // Image file
override val fileName: String = "Opening images tutorial.png"
override val uid: String = fileName
override val fileSizeInBytes: Long = 456782
override val mimeType: String? = null
override val uri: String = "https://picsum.photos/200/300"
},
object : FileUiItem {
override val fileName: String = "The 5 step guide to turning it off and on again.docx"
override val uid: String = fileName
override val fileSizeInBytes: Long = 89723143
override val mimeType: String? = null
override val uri: String = ""
},
FileUi(
fileName = "How to not get fired.pdf",
uid = "How to not get fired.pdf",
fileSizeInBytes = 10302130,
mimeType = null,
uri = "",
),
FileUi(
fileName = "Opening images tutorial.png",
uid = "Opening images tutorial.png",
fileSizeInBytes = 456782,
mimeType = null,
uri = "https://picsum.photos/200/300",
),
FileUi(
fileName = "The 5 step guide to turning it off and on again.docx",
uid = "The 5 step guide to turning it off and on again.docx",
fileSizeInBytes = 89723143,
mimeType = null,
uri = "",
),
)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.infomaniak.swisstransfer.R
import com.infomaniak.swisstransfer.ui.components.EmptyState
import com.infomaniak.swisstransfer.ui.components.FileItemList
import com.infomaniak.swisstransfer.ui.components.FileUiItem
import com.infomaniak.swisstransfer.ui.components.FileUi
import com.infomaniak.swisstransfer.ui.images.AppImages.AppIllus
import com.infomaniak.swisstransfer.ui.images.illus.MascotSearching
import com.infomaniak.swisstransfer.ui.screen.main.components.BrandTopAppBarScaffold
Expand Down Expand Up @@ -71,25 +71,29 @@ private fun ReceivedScreen(
description = R.string.noTransferReceivedDescription,
)
} else {
val files = listOf(object : FileUiItem {
override val fileName: String = "The 5-Step Guide to Not Breaking Your Code.txt"
override val uid: String = fileName
override val fileSizeInBytes: Long = 57689032
override val mimeType: String? = null
override val uri: String = ""
}, object : FileUiItem {
override val fileName: String = "Introduction to Turning It Off and On Again.pptx"
override val uid: String = fileName
override val fileSizeInBytes: Long = 89723143
override val mimeType: String? = null
override val uri: String = ""
}, object : FileUiItem {
override val fileName: String = "Learning to Copy and Paste: A Complete Guide.docx"
override val uid: String = fileName
override val fileSizeInBytes: Long = 237866728
override val mimeType: String? = null
override val uri: String = ""
})
val files = listOf(
FileUi(
fileName = "The 5-Step Guide to Not Breaking Your Code.txt",
uid = "The 5-Step Guide to Not Breaking Your Code.txt",
fileSizeInBytes = 57689032,
mimeType = null,
uri = "",
),
FileUi(
fileName = "Introduction to Turning It Off and On Again.pptx",
uid = "Introduction to Turning It Off and On Again.pptx",
fileSizeInBytes = 89723143,
mimeType = null,
uri = "",
),
FileUi(
fileName = "Learning to Copy and Paste: A Complete Guide.docx",
uid = "Learning to Copy and Paste: A Complete Guide.docx",
fileSizeInBytes = 237866728,
mimeType = null,
uri = "",
),
)
FileItemList(
modifier = Modifier.padding(Margin.Medium),
files = files,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,36 @@ import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import com.infomaniak.swisstransfer.ui.components.FileItemList
import com.infomaniak.swisstransfer.ui.components.FileUiItem
import com.infomaniak.swisstransfer.ui.components.FileUi
import com.infomaniak.swisstransfer.ui.theme.Margin
import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme
import com.infomaniak.swisstransfer.ui.utils.PreviewAllWindows

@Composable
fun SentListScreen(transfers: List<Any>) {
val transfers = listOf(object : FileUiItem {
override val fileName: String = "The 5-Step Guide to Not Breaking Your Code.txt"
override val uid: String = fileName
override val fileSizeInBytes: Long = 57689032
override val mimeType: String? = null
override val uri: String = ""
}, object : FileUiItem {
override val fileName: String = "Introduction to Turning It Off and On Again.pptx"
override val uid: String = fileName
override val fileSizeInBytes: Long = 89723143
override val mimeType: String? = null
override val uri: String = ""
}, object : FileUiItem {
override val fileName: String = "Learning to Copy and Paste: A Complete Guide.docx"
override val uid: String = fileName
override val fileSizeInBytes: Long = 237866728
override val mimeType: String? = null
override val uri: String = ""
})
val transfers = listOf(
FileUi(
fileName = "The 5-Step Guide to Not Breaking Your Code.txt",
uid = "The 5-Step Guide to Not Breaking Your Code.txt",
fileSizeInBytes = 57689032,
mimeType = null,
uri = "",
),
FileUi(
fileName = "Introduction to Turning It Off and On Again.pptx",
uid = "Introduction to Turning It Off and On Again.pptx",
fileSizeInBytes = 89723143,
mimeType = null,
uri = "",
),
FileUi(
fileName = "Learning to Copy and Paste: A Complete Guide.docx",
uid = "Learning to Copy and Paste: A Complete Guide.docx",
fileSizeInBytes = 237866728,
mimeType = null,
uri = "",
),
)
FileItemList(
modifier = Modifier.padding(Margin.Medium),
files = transfers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package com.infomaniak.swisstransfer.ui.screen.newtransfer
import android.net.Uri
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.infomaniak.swisstransfer.ui.components.FileUiItem
import com.infomaniak.swisstransfer.ui.components.FileUi
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -32,8 +32,8 @@ import javax.inject.Inject
@HiltViewModel
class NewTransferViewModel @Inject constructor(private val transferFilesManager: TransferFilesManager) : ViewModel() {

private val _files = MutableStateFlow<List<FileUiItem>>(emptyList())
val files: StateFlow<List<FileUiItem>> = _files
private val _files = MutableStateFlow<List<FileUi>>(emptyList())
val files: StateFlow<List<FileUi>> = _files

private val _failedFileCount = MutableSharedFlow<Int>()
val failedFileCount: SharedFlow<Int> = _failedFileCount
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ import android.content.Context
import android.database.Cursor
import android.net.Uri
import android.provider.OpenableColumns
import com.infomaniak.swisstransfer.ui.components.FileUiItem
import com.infomaniak.swisstransfer.ui.components.FileUi
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 getFiles(uris: List<Uri>, alreadyUsedFileNames: Set<String>): MutableSet<FileUiItem> {
fun getFiles(uris: List<Uri>, alreadyUsedFileNames: Set<String>): MutableSet<FileUi> {
val currentUsedFileNames = alreadyUsedFileNames.toMutableSet()
val files = mutableSetOf<FileUiItem>()
val files = mutableSetOf<FileUi>()

uris.forEach { uri ->
getFile(uri, currentUsedFileNames)?.let { file ->
Expand All @@ -44,19 +44,13 @@ class TransferFilesManager @Inject constructor(@ApplicationContext private val a
return files
}

private fun getFile(uri: Uri, alreadyUsedFileNames: Set<String>): FileUiItem? {
private fun getFile(uri: Uri, alreadyUsedFileNames: Set<String>): FileUi? {
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)
object: FileUiItem {
override val fileName: String = uniqueName
override val uid: String = fileName
override val fileSizeInBytes: Long = size
override val mimeType: String? = null
override val uri: String = uri.toString()
}
FileUi(fileName = uniqueName, uid = uniqueName, fileSizeInBytes = size, mimeType = null, uri = uri.toString())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fun ImportFilesScreen(

@Composable
private fun ImportFilesScreen(
files: () -> List<FileUiItem>,
files: () -> List<FileUi>,
removeFileByUid: (uid: String) -> Unit,
addFiles: (List<Uri>) -> Unit,
closeActivity: () -> Unit,
Expand Down Expand Up @@ -111,7 +111,7 @@ private fun ImportFilesScreen(

@PreviewAllWindows
@Composable
private fun ImportFilesScreenPreview(@PreviewParameter(FileUiListPreviewParameter::class) files: List<FileUiItem>) {
private fun ImportFilesScreenPreview(@PreviewParameter(FileUiListPreviewParameter::class) files: List<FileUi>) {
SwissTransferTheme {
ImportFilesScreen({ files }, {}, {}, closeActivity = {})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import kotlinx.parcelize.Parcelize
@Composable
fun ImportedFilesCard(
modifier: Modifier = Modifier,
files: () -> List<FileUiItem>,
files: () -> List<FileUi>,
humanReadableSize: () -> String,
showUploadSourceChoiceBottomSheet: () -> Unit,
removeFileByUid: (uid: String) -> Unit,
Expand Down Expand Up @@ -140,7 +140,7 @@ private data class TransferLazyRowKey(
@Preview(name = "Light")
@Preview(name = "Dark", uiMode = Configuration.UI_MODE_NIGHT_YES or Configuration.UI_MODE_TYPE_NORMAL)
@Composable
private fun ImportededFilesCardPreview(@PreviewParameter(FileUiListPreviewParameter::class) files: List<FileUiItem>) {
private fun ImportedFilesCardPreview(@PreviewParameter(FileUiListPreviewParameter::class) files: List<FileUi>) {
SwissTransferTheme {
ImportedFilesCard(
modifier = Modifier.padding(Margin.Medium),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
package com.infomaniak.swisstransfer.ui.utils

import com.infomaniak.library.filetypes.FileType
import com.infomaniak.swisstransfer.ui.components.FileUiItem
import com.infomaniak.swisstransfer.ui.components.FileUi

val FileUiItem.fileType: FileType get() = mimeType?.let { FileType.guessFromMimeType(it) } ?: FileType.guessFromFileName(fileName)
val FileUi.fileType: FileType get() = mimeType?.let { FileType.guessFromMimeType(it) } ?: FileType.guessFromFileName(fileName)

val FileUiItem.hasPreview: Boolean get() = fileType == FileType.IMAGE
val FileUi.hasPreview: Boolean get() = fileType == FileType.IMAGE
Loading