Skip to content

Commit

Permalink
feat(TransferType): Keep transfer type in the DB when at user's choice
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianDevel committed Nov 13, 2024
1 parent 6d1c9c2 commit 44070f8
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package com.infomaniak.swisstransfer.ui.navigation

import android.os.Bundle
import androidx.navigation.NavBackStackEntry
import com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.components.TransferType
import com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.components.TransferTypeUi
import kotlinx.serialization.Serializable
import kotlin.reflect.KClass
import kotlin.reflect.full.primaryConstructor
Expand Down Expand Up @@ -60,9 +60,9 @@ sealed class NewTransferNavigation : NavigationDestination() {
data object ValidateUserEmailDestination : NewTransferNavigation()

@Serializable
data class UploadProgressDestination(val transferType: TransferType, val totalSize: Long) : NewTransferNavigation()
data class UploadProgressDestination(val transferType: TransferTypeUi, val totalSize: Long) : NewTransferNavigation()
@Serializable
data class UploadSuccessDestination(val transferType: TransferType, val transferUrl: String) : NewTransferNavigation()
data class UploadSuccessDestination(val transferType: TransferTypeUi, val transferUrl: String) : NewTransferNavigation()
@Serializable
data object UploadErrorDestination : NewTransferNavigation()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ import com.infomaniak.swisstransfer.ui.screen.main.settings.components.SettingOp
import com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.PasswordTransferOption
import com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.TransferOptionState
import com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.TransferOptionsCallbacks
import com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.components.TransferType
import com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.components.TransferTypeUi
import com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.components.TransferTypeUi.Companion.toTransferTypeUi
import com.infomaniak.swisstransfer.ui.utils.GetSetCallbacks
import com.infomaniak.swisstransfer.workers.UploadWorker
import dagger.hilt.android.lifecycle.HiltViewModel
Expand Down Expand Up @@ -155,10 +156,11 @@ class ImportFilesViewModel @Inject constructor(
}

//region Transfer Type
val selectedTransferType = savedStateHandle.getStateFlow(SELECTED_TRANSFER_TYPE, TransferType.LINK)
val selectedTransferType = savedStateHandle.getStateFlow(SELECTED_TRANSFER_TYPE, TransferTypeUi.QR_CODE)

fun selectTransferType(type: TransferType) {
fun selectTransferType(type: TransferTypeUi) {
savedStateHandle[SELECTED_TRANSFER_TYPE] = type
viewModelScope.launch(ioDispatcher) { appSettingsManager.setLastTransferType(type.dbValue) }
}
//endregion

Expand Down Expand Up @@ -205,6 +207,7 @@ class ImportFilesViewModel @Inject constructor(
selectTransferValidityPeriod(it.validityPeriod.toTransferOption())
selectTransferDownloadLimit(it.downloadLimit.toTransferOption())
selectTransferLanguage(it.emailLanguage.toTransferOption())
selectTransferType(it.lastTransferType.toTransferTypeUi())
} ?: run {
SentryLog.e(TAG, "AppSettings is null in ImportFilesScreen, it should not happened")
selectTransferValidityPeriod(ValidityPeriodOption.THIRTY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private const val TOTAL_FILE_SIZE: Long = 50_000_000_000L
fun ImportFilesScreen(
importFilesViewModel: ImportFilesViewModel = hiltViewModel<ImportFilesViewModel>(),
closeActivity: () -> Unit,
navigateToUploadProgress: (transferType: TransferType, totalSize: Long) -> Unit,
navigateToUploadProgress: (transferType: TransferTypeUi, totalSize: Long) -> Unit,
) {
val files by importFilesViewModel.importedFilesDebounced.collectAsStateWithLifecycle()
val filesToImportCount by importFilesViewModel.filesToImportCount.collectAsStateWithLifecycle()
Expand Down Expand Up @@ -120,8 +120,8 @@ fun ImportFilesScreen(
@Composable
private fun HandleSendActionResult(
getSendActionResult: () -> SendActionResult?,
transferType: () -> TransferType,
navigateToUploadProgress: (transferType: TransferType, totalSize: Long) -> Unit,
transferType: () -> TransferTypeUi,
navigateToUploadProgress: (transferType: TransferTypeUi, totalSize: Long) -> Unit,
) {
LaunchedEffect(getSendActionResult()) {
when (val actionResult = getSendActionResult()) {
Expand All @@ -137,7 +137,7 @@ private fun ImportFilesScreen(
files: () -> List<FileUi>,
filesToImportCount: () -> Int,
currentSessionFilesCount: () -> Int,
selectedTransferType: GetSetCallbacks<TransferType>,
selectedTransferType: GetSetCallbacks<TransferTypeUi>,
transferOptionsCallbacks: TransferOptionsCallbacks,
removeFileByUid: (uid: String) -> Unit,
addFiles: (List<Uri>) -> Unit,
Expand Down Expand Up @@ -213,7 +213,7 @@ private fun ImportFilesScreen(
}

@Composable
private fun ColumnScope.ImportTextFields(selectedTransferType: () -> TransferType) {
private fun ColumnScope.ImportTextFields(selectedTransferType: () -> TransferTypeUi) {

EmailAddressesTextFields(selectedTransferType)

Expand All @@ -226,9 +226,9 @@ private fun ColumnScope.ImportTextFields(selectedTransferType: () -> TransferTyp
}

@Composable
private fun ColumnScope.EmailAddressesTextFields(selectedTransferType: () -> TransferType) {
private fun ColumnScope.EmailAddressesTextFields(selectedTransferType: () -> TransferTypeUi) {

val shouldShowEmailAddressesFields by remember { derivedStateOf { selectedTransferType() == TransferType.MAIL } }
val shouldShowEmailAddressesFields by remember { derivedStateOf { selectedTransferType() == TransferTypeUi.MAIL } }

AnimatedVisibility(visible = shouldShowEmailAddressesFields) {
Column {
Expand Down Expand Up @@ -375,7 +375,7 @@ private fun Preview(@PreviewParameter(FileUiListPreviewParameter::class) files:
files = { files },
filesToImportCount = { 0 },
currentSessionFilesCount = { 0 },
selectedTransferType = GetSetCallbacks(get = { TransferType.QR_CODE }, set = {}),
selectedTransferType = GetSetCallbacks(get = { TransferTypeUi.QR_CODE }, set = {}),
transferOptionsCallbacks = transferOptionsCallbacks,
removeFileByUid = {},
addFiles = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import com.infomaniak.swisstransfer.ui.utils.PreviewLightAndDark

@Composable
fun TransferTypeButton(
transferType: TransferType,
transferType: TransferTypeUi,
isActive: () -> Boolean,
onClick: () -> Unit,
) {
Expand Down Expand Up @@ -63,7 +63,7 @@ private fun TransferTypeButtonPreview() {
SwissTransferTheme {
Surface {
Column {
TransferType.entries.forEach { entry ->
TransferTypeUi.entries.forEach { entry ->
TransferTypeButton(
transferType = entry,
isActive = { entry.ordinal == 0 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import com.infomaniak.multiplatform_swisstransfer.common.models.TransferType
import com.infomaniak.swisstransfer.R
import com.infomaniak.swisstransfer.ui.images.AppImages.AppIcons
import com.infomaniak.swisstransfer.ui.images.icons.Chain
Expand All @@ -39,12 +40,12 @@ import com.infomaniak.swisstransfer.ui.utils.GetSetCallbacks
import com.infomaniak.swisstransfer.ui.utils.PreviewLightAndDark

@Composable
fun TransferTypeButtons(transferType: GetSetCallbacks<TransferType>) {
fun TransferTypeButtons(transferType: GetSetCallbacks<TransferTypeUi>) {
Row(
modifier = Modifier.horizontalScroll(rememberScrollState()),
horizontalArrangement = Arrangement.spacedBy(Margin.Mini),
) {
for (transferTypeEntry in TransferType.entries) {
for (transferTypeEntry in TransferTypeUi.entries) {
TransferTypeButton(
transferType = transferTypeEntry,
isActive = { transferTypeEntry == transferType.get() },
Expand All @@ -54,44 +55,58 @@ fun TransferTypeButtons(transferType: GetSetCallbacks<TransferType>) {
}
}

enum class TransferType(
enum class TransferTypeUi(
val buttonIcon: ImageVector,
@StringRes val buttonTextRes: Int,
@StringRes val titleRes: Int,
@StringRes @PluralsRes val descriptionRes: Int?,
val dbValue: TransferType,
) {
LINK(
buttonIcon = AppIcons.Chain,
buttonTextRes = R.string.transferTypeLink,
titleRes = R.string.uploadSuccessLinkTitle,
descriptionRes = R.string.uploadSuccessLinkDescription,
),
QR_CODE(
buttonIcon = AppIcons.QrCode,
buttonTextRes = R.string.transferTypeQrCode,
titleRes = R.string.uploadSuccessQrTitle,
descriptionRes = null,
dbValue = TransferType.QR_CODE,
),
LINK(
buttonIcon = AppIcons.Chain,
buttonTextRes = R.string.transferTypeLink,
titleRes = R.string.uploadSuccessLinkTitle,
descriptionRes = R.string.uploadSuccessLinkDescription,
dbValue = TransferType.LINK,
),
PROXIMITY(
buttonIcon = AppIcons.WifiWave,
buttonTextRes = R.string.transferTypeProximity,
titleRes = R.string.uploadSuccessLinkTitle,
descriptionRes = R.string.uploadSuccessLinkDescription,
dbValue = TransferType.PROXIMITY,
),
MAIL(
buttonIcon = AppIcons.Envelope,
buttonTextRes = R.string.transferTypeEmail,
titleRes = R.string.uploadSuccessEmailTitle,
descriptionRes = R.plurals.uploadSuccessEmailDescription,
),
dbValue = TransferType.MAIL,
);

companion object {
fun TransferType.toTransferTypeUi() = when(this) {
TransferType.LINK -> LINK
TransferType.QR_CODE -> QR_CODE
TransferType.PROXIMITY -> PROXIMITY
TransferType.MAIL -> MAIL
}
}
}

@PreviewLightAndDark
@Composable
private fun TransferTypeButtonsPreview() {
SwissTransferTheme {
Surface {
TransferTypeButtons(GetSetCallbacks(get = { TransferType.QR_CODE }, set = {}))
TransferTypeButtons(GetSetCallbacks(get = { TransferTypeUi.QR_CODE }, set = {}))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import com.infomaniak.swisstransfer.ui.components.*
import com.infomaniak.swisstransfer.ui.images.AppImages.AppIllus
import com.infomaniak.swisstransfer.ui.images.illus.beers.Beers
import com.infomaniak.swisstransfer.ui.previewparameter.emailsPreviewData
import com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.components.TransferType
import com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.components.TransferTypeUi
import com.infomaniak.swisstransfer.ui.theme.Margin
import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme
import com.infomaniak.swisstransfer.ui.utils.PreviewAllWindows
Expand Down Expand Up @@ -62,8 +62,8 @@ fun UploadSuccessEmailScreen(

IllustratedMessageBlock(
icon = AppIllus.Beers.image(),
title = TransferType.MAIL.titleRes,
description = pluralStringResource(TransferType.MAIL.descriptionRes!!, emails.count()),
title = TransferTypeUi.MAIL.titleRes,
description = pluralStringResource(TransferTypeUi.MAIL.descriptionRes!!, emails.count()),
)

Spacer(modifier = Modifier.height(Margin.Medium))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ import com.infomaniak.swisstransfer.R
import com.infomaniak.swisstransfer.ui.components.*
import com.infomaniak.swisstransfer.ui.images.AppImages.AppIllus
import com.infomaniak.swisstransfer.ui.images.illus.beers.Beers
import com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.components.TransferType
import com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.components.TransferTypeUi
import com.infomaniak.swisstransfer.ui.screen.newtransfer.upload.components.ShareAndCopyButtons
import com.infomaniak.swisstransfer.ui.theme.Dimens
import com.infomaniak.swisstransfer.ui.theme.Margin
import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme
import com.infomaniak.swisstransfer.ui.utils.PreviewAllWindows

@Composable
fun UploadSuccessQrScreen(transferType: TransferType, transferUrl: String, closeActivity: () -> Unit) {
fun UploadSuccessQrScreen(transferType: TransferTypeUi, transferUrl: String, closeActivity: () -> Unit) {
val snackbarHostState = remember { SnackbarHostState() }

BottomStickyButtonScaffold(
Expand Down Expand Up @@ -70,7 +70,7 @@ fun UploadSuccessQrScreen(transferType: TransferType, transferUrl: String, close
}

@Composable
private fun ColumnScope.SuccessMessage(transferType: TransferType, transferUrl: String) {
private fun ColumnScope.SuccessMessage(transferType: TransferTypeUi, transferUrl: String) {
Column(
modifier = Modifier
.fillMaxWidth()
Expand Down Expand Up @@ -115,7 +115,7 @@ private fun UploadSuccessQrScreenPreview() {
SwissTransferTheme {
Surface {
UploadSuccessQrScreen(
transferType = TransferType.LINK,
transferType = TransferTypeUi.QR_CODE,
transferUrl = "https://chk.me/83azQOl",
closeActivity = {}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ package com.infomaniak.swisstransfer.ui.screen.newtransfer.upload
import androidx.activity.compose.BackHandler
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.components.TransferType
import com.infomaniak.swisstransfer.ui.screen.newtransfer.importfiles.components.TransferTypeUi
import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme
import com.infomaniak.swisstransfer.ui.utils.PreviewAllWindows

@Composable
fun UploadSuccessScreen(
transferType: TransferType,
transferType: TransferTypeUi,
transferUrl: String,
closeActivity: () -> Unit,
) {
BackHandler(onBack = closeActivity)

if (transferType == TransferType.MAIL) {
if (transferType == TransferTypeUi.MAIL) {
UploadSuccessEmailScreen(closeActivity = closeActivity)
} else {
UploadSuccessQrScreen(transferType, transferUrl, closeActivity)
Expand All @@ -45,7 +45,7 @@ private fun UploadSuccessScreenPreview() {
SwissTransferTheme {
Surface {
UploadSuccessScreen(
transferType = TransferType.QR_CODE,
transferType = TransferTypeUi.QR_CODE,
transferUrl = "https://chk.me/83azQOl",
closeActivity = {}
)
Expand Down

0 comments on commit 44070f8

Please sign in to comment.