Skip to content

Commit

Permalink
refactor: Various renaming to better differentiate between scheduled …
Browse files Browse the repository at this point in the history
…Message & scheduled Draft
  • Loading branch information
KevinBoulongne committed Feb 6, 2025
1 parent 7da7aa3 commit 9c500f4
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class LocalSettings private constructor(context: Context) : SharedValues {
var autoAdvanceNaturalThread by sharedValue("autoAdvanceNaturalThreadKey", AutoAdvanceMode.FOLLOWING_THREAD)
var showWebViewOutdated by sharedValue("showWebViewOutdatedKey", true)
var accessTokenApiCallRecord by sharedValue<ApiCallRecord>("accessTokenApiCallRecordKey", null)
var lastSelectedScheduleEpoch by sharedValue<Long>("lastSelectedSchedule", null)
var lastSelectedScheduleEpoch by sharedValue<Long>("lastSelectedScheduleEpochKey", null)

fun removeSettings() = sharedPreferences.transaction { clear() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ private fun MigrationContext.keepDefaultValuesAfterNineteenthMigration() {
set(propertyName = "isDisplayed", value = true)
}
}

enumerate(className = "Message") { oldObject: DynamicRealmObject, newObject: DynamicMutableRealmObject? ->
newObject?.apply {
// Rename property without losing its previous value
set(propertyName = "isScheduledMessage", value = oldObject.getValue<Boolean>(fieldName = "isScheduled"))
}
}
}
}
//endregion
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ class MessageController @Inject constructor(private val mailboxContentRealm: Rea

fun getMovableMessages(thread: Thread): List<Message> {
val byFolderId = "${Message::folderId.name} == '${thread.folderId}'"
return getMessagesAndDuplicates(thread, "$byFolderId AND $isNotScheduled")
return getMessagesAndDuplicates(thread, "$byFolderId AND $isNotScheduledMessage")
}

fun getUnscheduledMessages(thread: Thread): List<Message> {
return getMessagesAndDuplicates(thread, isNotScheduled)
return getMessagesAndDuplicates(thread, isNotScheduledMessage)
}

private fun getMessagesAndDuplicates(thread: Thread, query: String): List<Message> {
Expand Down Expand Up @@ -152,7 +152,7 @@ class MessageController @Inject constructor(private val mailboxContentRealm: Rea

companion object {
private val isNotDraft = "${Message::isDraft.name} == false"
private val isNotScheduled = "${Message::isScheduled.name} == false"
private val isNotScheduledMessage = "${Message::isScheduledMessage.name} == false"

//region Queries
private fun getMessagesQuery(messageUid: String, realm: TypedRealm): RealmQuery<Message> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ package com.infomaniak.mail.data.models
enum class FeatureFlag(val apiName: String) {
AI("ai-mail-composer"),
BIMI("bimi"),
SCHEDULE_SEND_DRAFT("schedule-send-draft"),
SCHEDULE_DRAFTS("schedule-send-draft"),
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ import kotlinx.serialization.Serializable
@Serializable
data class SendDraftResult(
@SerialName("etop")
val etopScheduledDate: String,
val scheduledMessageEtop: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ data class ActivitiesResult(
@SerialName("forwarded")
val isForwarded: Boolean,
@SerialName("scheduled")
val isScheduled: Boolean,
val isScheduledMessage: Boolean,
@SerialName("seen")
val isSeen: Boolean,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class Message : RealmObject {
// Boolean used to know if this Message is currently being sent, but can
// still be cancelled during 10~30 sec, depending on user configuration
@SerialName("scheduled")
var isScheduled: Boolean = false
var isScheduledMessage: Boolean = false
var preview: String = ""
var size: Int = 0
@SerialName("has_unsubscribe_link")
Expand Down Expand Up @@ -325,7 +325,7 @@ class Message : RealmObject {
isFavorite = flags.isFavorite
isAnswered = flags.isAnswered
isForwarded = flags.isForwarded
isScheduled = flags.isScheduled
isScheduledMessage = flags.isScheduledMessage
}

fun shouldBeExpanded(index: Int, lastIndex: Int) = !isDraft && (!isSeen || index == lastIndex)
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/infomaniak/mail/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ class MainActivity : BaseActivity() {
val userId = getInt(DraftsActionsWorker.RESULT_USER_ID_KEY, 0)
if (userId != AccountUtils.currentUserId) return

getLong(DraftsActionsWorker.BIGGEST_ETOP_SCHEDULED_DATE_KEY, 0).takeIf { it > 0 }?.let { etopScheduledDate ->
mainViewModel.refreshDraftFolderWhenDraftArrives(etopScheduledDate)
getLong(DraftsActionsWorker.BIGGEST_SCHEDULED_MESSAGE_ETOP_KEY, 0).takeIf { it > 0 }?.let { scheduledMessageEtop ->
mainViewModel.refreshDraftFolderWhenDraftArrives(scheduledMessageEtop)
}
}

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/infomaniak/mail/ui/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1208,13 +1208,13 @@ class MainViewModel @Inject constructor(
selectedThreadsLiveData.value = selectedThreads
}

fun refreshDraftFolderWhenDraftArrives(etopScheduledDate: Long) = viewModelScope.launch(ioCoroutineContext) {
fun refreshDraftFolderWhenDraftArrives(scheduledMessageEtop: Long) = viewModelScope.launch(ioCoroutineContext) {
val folder = folderController.getFolder(FolderRole.DRAFT)

if (folder?.cursor != null) {

val timeNow = Date().time
val delay = REFRESH_DELAY + max(etopScheduledDate - timeNow, 0L)
val delay = REFRESH_DELAY + max(scheduledMessageEtop - timeNow, 0L)
delay(min(delay, MAX_REFRESH_DELAY))

refreshController.refreshThreads(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ class ScheduleSendBottomSheetDialog @Inject constructor() : ActionsBottomSheetDi
if (navigationArgs.isAlreadyScheduled) {
if (draftResource != null && lastSelectedScheduleEpoch != 0L) {
trackScheduleSendEvent(matomoName)
setBackNavigationResult(SCHEDULE_SEND_RESULT, lastSelectedScheduleEpoch)
setBackNavigationResult(SCHEDULE_DRAFT_RESULT, lastSelectedScheduleEpoch)
}
} else {
if (lastSelectedScheduleEpoch != 0L) {
trackScheduleSendEvent(matomoName)
setBackNavigationResult(SCHEDULE_SEND_RESULT, lastSelectedScheduleEpoch)
setBackNavigationResult(SCHEDULE_DRAFT_RESULT, lastSelectedScheduleEpoch)
}
}
}
Expand All @@ -126,12 +126,12 @@ class ScheduleSendBottomSheetDialog @Inject constructor() : ActionsBottomSheetDi
setIconResource(schedule.scheduleIconRes)
setOnClickListener {
trackScheduleSendEvent(schedule.matomoValue)
setBackNavigationResult(SCHEDULE_SEND_RESULT, schedule.date().time)
setBackNavigationResult(SCHEDULE_DRAFT_RESULT, schedule.date().time)
}
}

companion object {
const val SCHEDULE_SEND_RESULT = "schedule_send_result"
const val SCHEDULE_DRAFT_RESULT = "schedule_draft_result"
const val OPEN_DATE_AND_TIME_SCHEDULE_DIALOG = "open_date_and_time_schedule_dialog"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ import com.infomaniak.mail.databinding.FragmentThreadBinding
import com.infomaniak.mail.ui.MainViewModel
import com.infomaniak.mail.ui.alertDialogs.*
import com.infomaniak.mail.ui.bottomSheetDialogs.ScheduleSendBottomSheetDialog.Companion.OPEN_DATE_AND_TIME_SCHEDULE_DIALOG
import com.infomaniak.mail.ui.bottomSheetDialogs.ScheduleSendBottomSheetDialog.Companion.SCHEDULE_SEND_RESULT
import com.infomaniak.mail.ui.bottomSheetDialogs.ScheduleSendBottomSheetDialog.Companion.SCHEDULE_DRAFT_RESULT
import com.infomaniak.mail.ui.bottomSheetDialogs.ScheduleSendBottomSheetDialogArgs
import com.infomaniak.mail.ui.main.SnackbarManager
import com.infomaniak.mail.ui.main.folder.TwoPaneFragment
Expand Down Expand Up @@ -561,7 +561,7 @@ class ThreadFragment : Fragment() {
)
}

getBackNavigationResult(SCHEDULE_SEND_RESULT) { selectedScheduleEpoch: Long ->
getBackNavigationResult(SCHEDULE_DRAFT_RESULT) { selectedScheduleEpoch: Long ->
mainViewModel.rescheduleDraft(Date(selectedScheduleEpoch))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class NewMessageActivity : BaseActivity() {
private fun startWorker() {
draftsActionsWorkerScheduler.scheduleWork(
draftLocalUuid = newMessageViewModel.draftLocalUuid(),
scheduleDate = newMessageViewModel.scheduleDate,
scheduleDate = newMessageViewModel.scheduledDraftDate,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ import com.infomaniak.mail.ui.alertDialogs.DescriptionAlertDialog
import com.infomaniak.mail.ui.alertDialogs.InformationAlertDialog
import com.infomaniak.mail.ui.alertDialogs.SelectDateAndTimeForScheduledDraftDialog
import com.infomaniak.mail.ui.bottomSheetDialogs.ScheduleSendBottomSheetDialog.Companion.OPEN_DATE_AND_TIME_SCHEDULE_DIALOG
import com.infomaniak.mail.ui.bottomSheetDialogs.ScheduleSendBottomSheetDialog.Companion.SCHEDULE_SEND_RESULT
import com.infomaniak.mail.ui.bottomSheetDialogs.ScheduleSendBottomSheetDialog.Companion.SCHEDULE_DRAFT_RESULT
import com.infomaniak.mail.ui.bottomSheetDialogs.ScheduleSendBottomSheetDialogArgs
import com.infomaniak.mail.ui.main.SnackbarManager
import com.infomaniak.mail.ui.main.thread.AttachmentAdapter
Expand Down Expand Up @@ -242,7 +242,7 @@ class NewMessageFragment : Fragment() {
)
}

getBackNavigationResult(SCHEDULE_SEND_RESULT) { selectedScheduleEpoch: Long ->
getBackNavigationResult(SCHEDULE_DRAFT_RESULT) { selectedScheduleEpoch: Long ->
newMessageViewModel.setScheduleDate(Date(selectedScheduleEpoch))
tryToSendEmail(scheduled = true)
}
Expand Down Expand Up @@ -698,7 +698,7 @@ class NewMessageFragment : Fragment() {

private fun observeScheduledDraftsFeatureFlagUpdates() {
newMessageViewModel.currentMailboxLive.observeNotNull(viewLifecycleOwner) { mailbox ->
val isScheduledDraftsEnabled = mailbox.featureFlags.contains(FeatureFlag.SCHEDULE_SEND_DRAFT)
val isScheduledDraftsEnabled = mailbox.featureFlags.contains(FeatureFlag.SCHEDULE_DRAFTS)
binding.scheduleSendButton.isVisible = isScheduledDraftsEnabled
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class NewMessageViewModel @Inject constructor(
var draftAction = DraftAction.SAVE
var signaturesCount = 0
private var isNewMessage = false
var scheduleDate: Date? = null
var scheduledDraftDate: Date? = null

private var snapshot: DraftSnapshot? = null

Expand All @@ -154,7 +154,6 @@ class NewMessageViewModel @Inject constructor(
val editorAction = SingleLiveEvent<Pair<EditorAction, Boolean?>>()
// Needs to trigger every time the Fragment is recreated
val initResult = MutableLiveData<InitResult>()
val scheduleMessageTrigger = SingleLiveEvent<Unit>()

private val _isShimmering = MutableStateFlow(true)
val isShimmering: StateFlow<Boolean> = _isShimmering
Expand Down Expand Up @@ -196,7 +195,6 @@ class NewMessageViewModel @Inject constructor(
fun draftLocalUuid() = draftLocalUuid
fun draftMode() = draftMode
fun shouldLoadDistantResources() = shouldLoadDistantResources
fun triggerScheduleMessage() = scheduleMessageTrigger.postValue(Unit)

fun initDraftAndViewModel(intent: Intent): LiveData<Draft?> = liveData(ioCoroutineContext) {

Expand Down Expand Up @@ -850,21 +848,21 @@ class NewMessageViewModel @Inject constructor(
}.onFailure(Sentry::captureException)
}

fun setScheduleDate(scheduleDate: Date) = viewModelScope.launch(ioDispatcher) {
fun setScheduleDate(date: Date) = viewModelScope.launch(ioDispatcher) {
val localUuid = draftLocalUuid ?: return@launch
this@NewMessageViewModel.scheduleDate = scheduleDate
scheduledDraftDate = date
draftAction = DraftAction.SCHEDULE

mailboxContentRealm().write {
DraftController.getDraft(localUuid, realm = this)?.also { draft ->
draft.scheduleDate = this@NewMessageViewModel.scheduleDate?.format(FORMAT_SCHEDULE_MAIL)
draft.scheduleDate = scheduledDraftDate?.format(FORMAT_SCHEDULE_MAIL)
}
}
}

fun resetScheduledDate() = viewModelScope.launch(ioDispatcher) {
val localUuid = draftLocalUuid ?: return@launch
scheduleDate = null
scheduledDraftDate = null
draftAction = DraftAction.SAVE

mailboxContentRealm().write {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ private tailrec fun formatFolderWithAllChildren(
/*
* There are two types of folders:
* - user's folders (with or without a role)
* - hidden IK folders (scheduled drafts, snoozed, etc…)
* - hidden IK folders (ScheduledDrafts, Snoozed, etc…)
*
* We want to display the user's folders, and also the IK folders for which we handle the role.
* IK folders where we don't handle the role are dismissed.
Expand Down
Loading

0 comments on commit 9c500f4

Please sign in to comment.