Skip to content

Commit

Permalink
refactor: Clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinBoulongne committed Jan 30, 2025
1 parent 5e55fdc commit f550fe9
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ class FolderController @Inject constructor(
//region Get data
fun getMenuDrawerDefaultFoldersAsync(): Flow<ResultsChange<Folder>> {
return getFoldersQuery(
mailboxContentRealm(),
realm = mailboxContentRealm(),
withoutTypes = listOf(FoldersType.CUSTOM),
withoutChildren = true,
).asFlow()
}

fun getMenuDrawerCustomFoldersAsync(): Flow<ResultsChange<Folder>> {
return getFoldersQuery(
mailboxContentRealm(),
realm = mailboxContentRealm(),
withoutTypes = listOf(FoldersType.DEFAULT),
withoutChildren = true,
).asFlow()
Expand All @@ -67,7 +67,7 @@ class FolderController @Inject constructor(

fun getMoveFolders(): RealmResults<Folder> {
return getFoldersQuery(
mailboxContentRealm(),
realm = mailboxContentRealm(),
withoutTypes = listOf(FoldersType.SCHEDULED_DRAFTS, FoldersType.DRAFT),
withoutChildren = true,
).find()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class Message : RealmObject {
// ------------- !IMPORTANT! -------------
// Every field that is added in this Transient region should be declared in 'initLocalValue()' too
// to avoid loosing data when updating from API.
// If the Field is a "heavy data" (i.e. an embedded object), it should also be added in 'keepHeavyData()'
// If the Field is a "heavy data" (i.e. an embedded object), it should also be added in 'keepHeavyData()'.

@Transient
@PersistedName("isFullyDownloaded")
Expand Down
24 changes: 10 additions & 14 deletions app/src/main/java/com/infomaniak/mail/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,16 @@ class MainActivity : BaseActivity() {

private val showSendingSnackbarTimer: CountDownTimer by lazy {
Utils.createRefreshTimer(milliseconds = 1_000L) {
snackbarManager.setValue(
getString(if (draftAction == DraftAction.SCHEDULE) R.string.snackbarScheduling else R.string.snackbarEmailSending)
)
val resId = if (draftAction == DraftAction.SCHEDULE) R.string.snackbarScheduling else R.string.snackbarEmailSending
snackbarManager.setValue(getString(resId))
}
}

private val newMessageActivityResultLauncher = registerForActivityResult(StartActivityForResult()) { result ->
draftAction = result.data?.getStringExtra(DRAFT_ACTION_KEY)?.let(DraftAction::valueOf)

if (draftAction == DraftAction.SEND) {
showEasterXMas()
showSendingSnackbarTimer.start()
} else if (draftAction == DraftAction.SCHEDULE) {
showSendingSnackbarTimer.start()
}
if (draftAction == DraftAction.SEND) showEasterXMas()
if (draftAction == DraftAction.SEND || draftAction == DraftAction.SCHEDULE) showSendingSnackbarTimer.start()
}

private val syncAutoConfigActivityResultLauncher = registerForActivityResult(StartActivityForResult()) { result ->
Expand Down Expand Up @@ -290,13 +285,14 @@ class MainActivity : BaseActivity() {
showSavedDraftSnackbar(associatedMailboxUuid, remoteDraftUuid)
}
}
DraftAction.SEND -> showSentDraftSnackbar()
DraftAction.SEND -> {
showSentDraftSnackbar()
}
DraftAction.SCHEDULE -> {
val scheduleDate = getLongOrNull(DraftsActionsWorker.SCHEDULE_DATE_KEY)
val scheduleAction = getString(DraftsActionsWorker.SCHEDULE_ACTION_KEY)

if (scheduleDate != null && scheduleAction != null) {
showSentScheduleDraftSnackbar(scheduleDate = Date(scheduleDate), scheduleAction)
showScheduledDraftSnackbar(scheduleDate = Date(scheduleDate), scheduleAction = scheduleAction)
}
}
}
Expand Down Expand Up @@ -330,7 +326,7 @@ class MainActivity : BaseActivity() {
}

// Still display the Snackbar even if it took three times 10 seconds of timeout to succeed
private fun showSentScheduleDraftSnackbar(scheduleDate: Date, scheduleAction: String) {
private fun showScheduledDraftSnackbar(scheduleDate: Date, scheduleAction: String) {
showSendingSnackbarTimer.cancel()

val dateString = mostDetailedDate(
Expand All @@ -342,7 +338,7 @@ class MainActivity : BaseActivity() {
snackbarManager.setValue(
title = String.format(getString(R.string.snackbarScheduleSaved), dateString),
buttonTitle = RCore.string.buttonCancel,
customBehavior = { mainViewModel.deleteScheduleDraft(scheduleAction) },
customBehavior = { mainViewModel.deleteScheduledDraft(scheduleAction) },
)
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/infomaniak/mail/ui/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ class MainViewModel @Inject constructor(
showDraftDeletedSnackbar(apiResponse)
}

fun deleteScheduleDraft(scheduleAction: String) = viewModelScope.launch(ioCoroutineContext) {
fun deleteScheduledDraft(scheduleAction: String) = viewModelScope.launch(ioCoroutineContext) {
val mailbox = currentMailbox.value!!
val apiResponse = ApiRepository.deleteScheduledDraft(scheduleAction)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class MessageAlertView @JvmOverloads constructor(
}
}

fun setDescription(text: String) = with(binding) {
description.text = text
fun setDescription(text: String) {
binding.description.text = text
}

fun onAction1(listener: OnClickListener) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,13 @@ class ThreadAdapter(
val messageDate = message.date.toDate()

if (message.isScheduledDraft) {
scheduleSendIcon.isVisible = true

scheduleAlert.setDescription(
context.getString(
R.string.scheduledEmailHeader,
message.date.toDate().format(FORMAT_DATE_DAY_FULL_MONTH_WITH_TIME),
)
),
)
scheduleSendIcon.isVisible = true
alertsGroup.isVisible = true
scheduleAlert.isVisible = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ abstract class BaseCoroutineWorker(appContext: Context, params: WorkerParameters
companion object {
private const val MAX_RETRIES = 3

fun Data.getLongOrNull(key: String) = getLong(key, 0).run { if (this == 0L) null else this }
fun Data.getLongOrNull(key: String) = getLong(key, 0L).run { if (this == 0L) null else this }
}
}

0 comments on commit f550fe9

Please sign in to comment.