Skip to content

Commit

Permalink
Merge pull request #1750 from Infomaniak/fix-wrong-selected-mailbox
Browse files Browse the repository at this point in the history
Fix wrong selected mailbox
  • Loading branch information
LunarX authored Mar 12, 2024
2 parents 072cb25 + 4e10707 commit 6e6971f
Showing 1 changed file with 27 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Infomaniak Mail - Android
* Copyright (C) 2022-2023 Infomaniak Network SA
* Copyright (C) 2022-2024 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -18,6 +18,7 @@
package com.infomaniak.mail.ui.main.menu

import android.view.LayoutInflater
import android.view.View.OnClickListener
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView.Adapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
Expand Down Expand Up @@ -73,6 +74,21 @@ class MailboxesAdapter(
}
}.getOrDefault(Unit)

override fun getItemViewType(position: Int): Int = runCatchingRealm {
return when {
!mailboxes[position].isValid -> DisplayType.INVALID_MAILBOX.layout
isInMenuDrawer -> DisplayType.MENU_DRAWER_MAILBOX.layout
else -> DisplayType.SIMPLE_MAILBOX.layout
}
}.getOrDefault(super.getItemViewType(position))

override fun getItemCount(): Int = runCatchingRealm { mailboxes.count() }.getOrDefault(0)

fun setMailboxes(newMailboxes: List<Mailbox>) {
mailboxes = newMailboxes
notifyDataSetChanged()
}

private fun ItemSelectableMailboxBinding.displaySimpleMailbox(mailbox: Mailbox, isCurrentMailbox: Boolean) = with(root) {
displayValidMailbox(mailbox, isCurrentMailbox) { context.trackAccountEvent(SWITCH_MAILBOX_NAME) }
setSelectedState(isCurrentMailbox)
Expand All @@ -92,12 +108,18 @@ class MailboxesAdapter(
) {
text = mailbox.email

if (!isCurrentMailbox) {
setOnClickListener {
trackerCallback()
onValidMailboxClicked?.invoke(mailbox.mailboxId)
fun getClickListener(): OnClickListener? {
return if (isCurrentMailbox) {
null
} else {
OnClickListener {
trackerCallback()
onValidMailboxClicked?.invoke(mailbox.mailboxId)
}
}
}

setOnClickListener(getClickListener())
}

private fun ItemInvalidMailboxBinding.displayInvalidMailbox(mailbox: Mailbox) = with(root) {
Expand All @@ -117,21 +139,6 @@ class MailboxesAdapter(
)
}

override fun getItemViewType(position: Int): Int = runCatchingRealm {
return when {
!mailboxes[position].isValid -> DisplayType.INVALID_MAILBOX.layout
isInMenuDrawer -> DisplayType.MENU_DRAWER_MAILBOX.layout
else -> DisplayType.SIMPLE_MAILBOX.layout
}
}.getOrDefault(super.getItemViewType(position))

override fun getItemCount(): Int = runCatchingRealm { mailboxes.count() }.getOrDefault(0)

fun setMailboxes(newMailboxes: List<Mailbox>) {
mailboxes = newMailboxes
notifyDataSetChanged()
}

private enum class DisplayType(val layout: Int) {
INVALID_MAILBOX(R.layout.item_invalid_mailbox),
MENU_DRAWER_MAILBOX(R.layout.item_mailbox_menu_drawer),
Expand Down

0 comments on commit 6e6971f

Please sign in to comment.