Skip to content

Commit

Permalink
Modify observe logic on archive and delete and animation on tablet mode
Browse files Browse the repository at this point in the history
wip
  • Loading branch information
NicolasBourdin88 committed May 6, 2024
1 parent 6a93e12 commit 7d7f785
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 29 deletions.
35 changes: 30 additions & 5 deletions app/src/main/java/com/infomaniak/mail/ui/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ class MainViewModel @Inject constructor(
val reportPhishingTrigger = SingleLiveEvent<Unit>()
val canInstallUpdate = MutableLiveData(false)


// Thread auto advance
val isThreadDeletedOrArchived = MutableLiveData(false)
val threadUidToDeleteOrArchive = MutableLiveData<List<String>>()

val mailboxesLive = mailboxController.getMailboxesAsync(AccountUtils.currentUserId).asLiveData(ioCoroutineContext)

Expand Down Expand Up @@ -444,15 +442,21 @@ class MainViewModel @Inject constructor(

//region Delete
fun deleteMessage(threadUid: String, message: Message) {
if (shouldWeDeleteOrAchieveThread(threadUid))
threadUidToDeleteOrArchive.postValue(listOf(threadUid))

deleteThreadsOrMessage(threadsUids = listOf(threadUid), message = message)
}

fun deleteThread(threadUid: String, isSwipe: Boolean = false) {
deleteThreadsOrMessage(threadsUids = listOf(threadUid), isSwipe = isSwipe)
val threadUidList = listOf(threadUid)
deleteThreadsOrMessage(threadsUids = threadUidList, isSwipe = isSwipe)
threadUidToDeleteOrArchive.postValue(threadUidList)
}

fun deleteThreads(threadsUids: List<String>) {
deleteThreadsOrMessage(threadsUids = threadsUids)
threadUidToDeleteOrArchive.postValue(threadsUids)
}

private fun deleteThreadsOrMessage(
Expand Down Expand Up @@ -503,6 +507,7 @@ class MainViewModel @Inject constructor(
undoDestinationId,
threads.count(),
)

}

private fun showDeleteSnackbar(
Expand Down Expand Up @@ -611,21 +616,29 @@ class MainViewModel @Inject constructor(

//region Archive
fun archiveMessage(threadUid: String, message: Message) {
if (shouldWeDeleteOrAchieveThread(threadUid))
threadUidToDeleteOrArchive.postValue(listOf(threadUid))

archiveThreadsOrMessage(threadsUids = listOf(threadUid), message = message)
}

fun archiveThread(threadUid: String) {
archiveThreadsOrMessage(threadsUids = listOf(threadUid))
val threadUidsList = listOf(threadUid)
if (shouldWeDeleteOrAchieveThread(threadUid)) threadUidToDeleteOrArchive.postValue(threadUidsList)

archiveThreadsOrMessage(threadsUids = threadUidsList)
}

fun archiveThreads(threadsUids: List<String>) {
threadUidToDeleteOrArchive.postValue(threadsUids)
archiveThreadsOrMessage(threadsUids = threadsUids)
}

private fun archiveThreadsOrMessage(
threadsUids: List<String>,
message: Message? = null,
) = viewModelScope.launch(ioCoroutineContext) {

val mailbox = currentMailbox.value!!
val threads = getActionThreads(threadsUids).ifEmpty { return@launch }

Expand All @@ -647,6 +660,7 @@ class MainViewModel @Inject constructor(
destinationFolderId = destinationFolder.id,
callbacks = RefreshCallbacks(::onDownloadStart, ::onDownloadStop),
)

}

showMoveSnackbar(threads, message, messages, apiResponse, destinationFolder)
Expand Down Expand Up @@ -1042,7 +1056,18 @@ class MainViewModel @Inject constructor(

snackbarManager.postValue(appContext.getString(snackbarTitleRes))
}
private fun shouldWeDeleteOrAchieveThread(threadUid: String): Boolean {
var howMuchMessageInFolder = 0
threadController.getThread(threadUid)?.messages?.forEach {
if (it.folderId == currentFolderId)
howMuchMessageInFolder++

if (howMuchMessageInFolder > 1)
return false
}

return true
}
companion object {
private val TAG: String = MainViewModel::class.java.simpleName
private val DEFAULT_SELECTED_FOLDER = FolderRole.INBOX
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.ernestoyaquello.dragdropswiperecyclerview.DragDropSwipeAdapter
import com.ernestoyaquello.dragdropswiperecyclerview.DragDropSwipeRecyclerView
import com.ernestoyaquello.dragdropswiperecyclerview.util.DragDropSwipeDiffCallback
import com.google.android.material.card.MaterialCardView
import com.infomaniak.lib.core.MatomoCore.TrackerAction
import com.infomaniak.lib.core.utils.*
Expand Down Expand Up @@ -161,8 +160,8 @@ class ThreadListAdapter @Inject constructor(
}

override fun onBindViewHolder(holder: ThreadListViewHolder, position: Int, payloads: MutableList<Any>) = runCatchingRealm {

val payload = payloads.firstOrNull()

if (payload !is NotificationType) {
super.onBindViewHolder(holder, position, payloads)
return@runCatchingRealm
Expand Down Expand Up @@ -213,9 +212,9 @@ class ThreadListAdapter @Inject constructor(

selectionCardView.setOnClickListener {
previousThreadClickedPosition?.let { previousThreadClickedPosition ->
if (position > previousThreadClickedPosition) {
localSettings.autoAdvanceIntelligentMode = AutoAdvanceMode.NEXT_THREAD
} else if (position < previousThreadClickedPosition) {
if (position < previousThreadClickedPosition) {
localSettings.autoAdvanceIntelligentMode = AutoAdvanceMode.FOLLOWING_THREAD
} else if (position > previousThreadClickedPosition) {
localSettings.autoAdvanceIntelligentMode = AutoAdvanceMode.PREVIOUS_THREAD
}
}
Expand Down Expand Up @@ -320,23 +319,28 @@ class ThreadListAdapter @Inject constructor(
if (newPosition != null) notifyItemChanged(newPosition, NotificationType.SELECTED_STATE)
}

fun openThreadByPosition(autoAdvanceMode: AutoAdvanceMode) {
val thread: Thread? = openedThreadPosition?.let {
getNextThreadToOpenByPosition(it, autoAdvanceMode)
}
fun openThreadByPosition(autoAdvanceMode: AutoAdvanceMode, threadDeleteUid: List<String>) {
if (threadDeleteUid.contains(openedThreadUid)) {
val thread: Thread? = openedThreadPosition?.let {
println("here start openedThread")
getNextThreadToOpenByPosition(it, autoAdvanceMode)
}

thread?.let {
if (thread.uid != openedThreadUid && !thread.isOnlyOneDraft) selectNewThread(openedThreadPosition, thread.uid)
onThreadClicked?.invoke(it)
thread?.let {
if (thread.uid != openedThreadUid && !thread.isOnlyOneDraft) selectNewThread(openedThreadPosition, thread.uid)
onThreadClicked?.invoke(it)
}
} else {
println("don't contains")
}
}

private fun getNextThreadToOpenByPosition(startingThreadIndex: Int, autoAdvanceMode: AutoAdvanceMode): Thread? {
return when (autoAdvanceMode) {
AutoAdvanceMode.PREVIOUS_THREAD -> getNextThread(startingThreadIndex, direction = PREVIOUS_CHRONOLOGICAL_THREAD)
AutoAdvanceMode.NEXT_THREAD -> getNextThread(startingThreadIndex, direction = NEXT_CHRONOLOGICAL_THREAD)
AutoAdvanceMode.FOLLOWING_THREAD -> getNextThread(startingThreadIndex, direction = NEXT_CHRONOLOGICAL_THREAD)
AutoAdvanceMode.LIST_THREAD -> null
AutoAdvanceMode.LAST_ACTION -> {
AutoAdvanceMode.NATURAL_THREAD -> {
if (localSettings.autoAdvanceIntelligentMode == AutoAdvanceMode.PREVIOUS_THREAD) {
getNextThread(startingThreadIndex, direction = PREVIOUS_CHRONOLOGICAL_THREAD)
} else {
Expand All @@ -347,14 +351,19 @@ class ThreadListAdapter @Inject constructor(
}

private fun getNextThread(startingThreadIndex: Int, direction: Int): Thread? {
println("start")
var currentIndexThread = startingThreadIndex
currentIndexThread += direction
println("second start $currentIndexThread")
while (currentIndexThread >= 0 && currentIndexThread <= dataSet.lastIndex) {
println("here $currentIndexThread")
if (dataSet[currentIndexThread] is Thread) {
openedThreadPosition = currentIndexThread
println("found")
println("folder ${dataSet[currentIndexThread]}")
val thread = dataSet[currentIndexThread] as Thread
return dataSet[currentIndexThread] as Thread
}

currentIndexThread += direction
}
return null
Expand Down Expand Up @@ -612,7 +621,7 @@ class ThreadListAdapter @Inject constructor(
return getItemViewType(position) == DisplayType.THREAD.layout && swipingIsAuthorized
}

override fun createDiffUtil(oldList: List<Any>, newList: List<Any>): DragDropSwipeDiffCallback<Any>? = null
override fun createDiffUtil(oldList: List<Any>, newList: List<Any>) = null

override fun updateList(itemList: List<Thread>, lifecycleScope: LifecycleCoroutineScope) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,8 @@ class ThreadListFragment : TwoPaneFragment(), SwipeRefreshLayout.OnRefreshListen
}
}

mainViewModel.isThreadDeletedOrArchived.observe(viewLifecycleOwner) {
threadListAdapter.openThreadByPosition(localSettings.autoAdvanceMode)
mainViewModel.threadUidToDeleteOrArchive.observe(viewLifecycleOwner) {
threadListAdapter.openThreadByPosition(localSettings.autoAdvanceMode, it)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import com.infomaniak.mail.MatomoMail.ACTION_FORWARD_NAME
import com.infomaniak.mail.MatomoMail.ACTION_REPLY_NAME
import com.infomaniak.mail.MatomoMail.OPEN_ACTION_BOTTOM_SHEET
import com.infomaniak.mail.MatomoMail.OPEN_FROM_DRAFT_NAME
import com.infomaniak.mail.MatomoMail.shouldOptOut
import com.infomaniak.mail.MatomoMail.trackAttachmentActionsEvent
import com.infomaniak.mail.MatomoMail.trackMessageActionsEvent
import com.infomaniak.mail.MatomoMail.trackNewMessageEvent
Expand Down Expand Up @@ -499,13 +500,11 @@ class ThreadFragment : Fragment() {
R.id.quickActionArchive -> {
trackThreadActionsEvent(ACTION_ARCHIVE_NAME, isFromArchive)
mainViewModel.archiveThread(threadUid)
mainViewModel.isThreadDeletedOrArchived.value = true
}
R.id.quickActionDelete -> {
descriptionDialog.deleteWithConfirmationPopup(folderRole, count = 1) {
trackThreadActionsEvent(ACTION_DELETE_NAME)
mainViewModel.deleteThread(threadUid)
mainViewModel.isThreadDeletedOrArchived.value = true
}
}
R.id.quickActionMenu -> {
Expand Down
3 changes: 0 additions & 3 deletions app/src/main/res/navigation/main_navigation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,6 @@
<action
android:id="@+id/action_settings_to_aiEngineSetting"
app:destination="@id/aiEngineSettingFragment" />
<action
android:id="@+id/action_settings_to_externalContentSetting"
app:destination="@id/externalContentSettingFragment" />
<action
android:id="@+id/action_settings_to_threadListDensitySetting"
app:destination="@id/threadListDensitySettingFragment" />
Expand Down

0 comments on commit 7d7f785

Please sign in to comment.