Skip to content

Commit

Permalink
Move each MenuDrawerAdapter's items binding to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinBoulongne committed Jul 4, 2024
1 parent e49d41c commit 9c9dbcb
Show file tree
Hide file tree
Showing 11 changed files with 720 additions and 261 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ class Folder : RealmObject, Cloneable {
const val DEFAULT_IS_HISTORY_COMPLETE = false

const val INBOX_FOLDER_ID = "eJzz9HPyjwAABGYBgQ--"
const val MAX_SUB_FOLDERS_INDENT = 2
private const val CUSTOM_FOLDER_ROLE_ORDER = 0
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Infomaniak Mail - Android
* Copyright (C) 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.mail.ui.main.menuDrawer.items

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.infomaniak.mail.R
import com.infomaniak.mail.databinding.ItemMenuDrawerDividerBinding

object DividerItem {

@Suppress("MayBeConstant")
val viewType = R.layout.item_menu_drawer_divider

fun binding(inflater: LayoutInflater, parent: ViewGroup): ViewBinding {
return ItemMenuDrawerDividerBinding.inflate(inflater, parent, false)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Infomaniak Mail - Android
* Copyright (C) 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.mail.ui.main.menuDrawer.items

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.infomaniak.mail.R
import com.infomaniak.mail.databinding.ItemMenuDrawerEmptyCustomFoldersBinding

object EmptyFoldersItem {

@Suppress("MayBeConstant")
val viewType = R.layout.item_menu_drawer_empty_custom_folders

fun binding(inflater: LayoutInflater, parent: ViewGroup): ViewBinding {
return ItemMenuDrawerEmptyCustomFoldersBinding.inflate(inflater, parent, false)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/*
* Infomaniak Mail - Android
* Copyright (C) 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.mail.ui.main.menuDrawer.items

import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.DrawableRes
import androidx.appcompat.content.res.AppCompatResources
import androidx.viewbinding.ViewBinding
import com.infomaniak.mail.MatomoMail.trackMenuDrawerEvent
import com.infomaniak.mail.R
import com.infomaniak.mail.data.models.Folder
import com.infomaniak.mail.databinding.ItemMenuDrawerFolderBinding
import com.infomaniak.mail.utils.UnreadDisplay
import com.infomaniak.mail.views.itemViews.*
import kotlin.math.min

object FolderItem {

@Suppress("MayBeConstant")
val viewType = R.layout.item_menu_drawer_folder

fun binding(inflater: LayoutInflater, parent: ViewGroup): ViewBinding {
return ItemMenuDrawerFolderBinding.inflate(inflater, parent, false)
}

fun display(
item: Any,
binding: ViewBinding,
currentFolderId: String?,
hasCollapsableDefaultFolder: Boolean,
hasCollapsableCustomFolder: Boolean,
onFolderClicked: (folderId: String) -> Unit,
onCollapseChildrenClicked: (folderId: String, shouldCollapse: Boolean) -> Unit,
) {
item as Folder
binding as ItemMenuDrawerFolderBinding

Log.d("Bind", "Bind Folder : ${item.name}")
binding.displayFolder(
item,
currentFolderId,
hasCollapsableDefaultFolder,
hasCollapsableCustomFolder,
onFolderClicked,
onCollapseChildrenClicked,
)
}

fun displayWithPayload(
item: Any,
binding: ViewBinding,
currentFolderId: String?,
hasCollapsableDefaultFolder: Boolean,
hasCollapsableCustomFolder: Boolean,
onFolderClicked: (folderId: String) -> Unit,
onCollapseChildrenClicked: (folderId: String, shouldCollapse: Boolean) -> Unit,
) {
item as Folder
binding as ItemMenuDrawerFolderBinding

Log.d("Bind", "Bind Custom folders because of collapse change = ${item.name}")
binding.displayFolder(
item,
currentFolderId,
hasCollapsableDefaultFolder,
hasCollapsableCustomFolder,
onFolderClicked,
onCollapseChildrenClicked,
)
}

private fun ItemMenuDrawerFolderBinding.displayFolder(
folder: Folder,
currentFolderId: String?,
hasCollapsableDefaultFolder: Boolean,
hasCollapsableCustomFolder: Boolean,
onFolderClicked: (folderId: String) -> Unit,
onCollapseChildrenClicked: (folderId: String, shouldCollapse: Boolean) -> Unit,
) = with(root) {

val unread = when (folder.role) {
Folder.FolderRole.DRAFT -> UnreadDisplay(folder.threads.count())
Folder.FolderRole.SENT, Folder.FolderRole.TRASH -> UnreadDisplay(0)
else -> folder.unreadCountDisplay
}

folder.role?.let {
setFolderUi(
folder = folder,
iconId = it.folderIconRes,
unread = unread,
currentFolderId = currentFolderId,
hasCollapsableDefaultFolder = hasCollapsableDefaultFolder,
hasCollapsableCustomFolder = hasCollapsableCustomFolder,
onFolderClicked = onFolderClicked,
onCollapseChildrenClicked = onCollapseChildrenClicked,
trackerName = it.matomoValue,
)
} ?: run {
val indentLevel = folder.path.split(folder.separator).size - 1
setFolderUi(
folder = folder,
iconId = if (folder.isFavorite) R.drawable.ic_folder_star else R.drawable.ic_folder,
unread = unread,
currentFolderId = currentFolderId,
hasCollapsableDefaultFolder = hasCollapsableDefaultFolder,
hasCollapsableCustomFolder = hasCollapsableCustomFolder,
onFolderClicked = onFolderClicked,
onCollapseChildrenClicked = onCollapseChildrenClicked,
trackerName = "customFolder",
trackerValue = indentLevel.toFloat(),
folderIndent = min(indentLevel, Folder.MAX_SUB_FOLDERS_INDENT),
)
}
}

private fun SelectableItemView.setFolderUi(
folder: Folder,
@DrawableRes iconId: Int,
unread: UnreadDisplay?,
currentFolderId: String?,
hasCollapsableDefaultFolder: Boolean,
hasCollapsableCustomFolder: Boolean,
onFolderClicked: (folderId: String) -> Unit,
onCollapseChildrenClicked: (folderId: String, shouldCollapse: Boolean) -> Unit,
trackerName: String,
trackerValue: Float? = null,
folderIndent: Int = 0,
) {
val folderName = folder.getLocalizedName(context)

text = folderName
icon = AppCompatResources.getDrawable(context, iconId)
setSelectedState(folder.id == currentFolderId)

when (this) {
is SelectableFolderItemView -> setIndent(folderIndent)
is UnreadFolderItemView -> {
initOnCollapsableClickListener { onCollapseChildrenClicked(folder.id, isCollapsed) }
isPastilleDisplayed = unread?.shouldDisplayPastille ?: false
unreadCount = unread?.count ?: 0
isCollapsed = folder.isCollapsed
canBeCollapsed = folder.canBeCollapsed
val hasCollapsableFolder = if (folder.role == null) hasCollapsableCustomFolder else hasCollapsableDefaultFolder
setIndent(
indent = folderIndent,
hasCollapsableFolder = hasCollapsableFolder,
canBeCollapsed = canBeCollapsed,
)
setCollapsingButtonContentDescription(folderName)
}
is SelectableMailboxItemView, is UnreadItemView -> {
error("`${this::class.simpleName}` cannot exists here. Only Folder classes are allowed")
}
}

setOnClickListener {
context.trackMenuDrawerEvent(trackerName, value = trackerValue)
onFolderClicked.invoke(folder.id)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Infomaniak Mail - Android
* Copyright (C) 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.mail.ui.main.menuDrawer.items

import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.infomaniak.mail.R
import com.infomaniak.mail.databinding.ItemMenuDrawerCustomFoldersHeaderBinding

object FoldersHeaderItem {

@Suppress("MayBeConstant")
val viewType = R.layout.view_menu_drawer_dropdown

fun binding(inflater: LayoutInflater, parent: ViewGroup): ViewBinding {
return ItemMenuDrawerCustomFoldersHeaderBinding.inflate(inflater, parent, false)
}

fun display(
binding: ViewBinding,
onCustomFoldersHeaderClicked: (Boolean) -> Unit,
onCreateFolderClicked: () -> Unit,
) {
binding as ItemMenuDrawerCustomFoldersHeaderBinding

Log.d("Bind", "Bind Custom Folders header")
binding.displayCustomFoldersHeader(onCustomFoldersHeaderClicked, onCreateFolderClicked)
}

private fun ItemMenuDrawerCustomFoldersHeaderBinding.displayCustomFoldersHeader(
onCustomFoldersHeaderClicked: (Boolean) -> Unit,
onCreateFolderClicked: () -> Unit,
) = with(root) {
setOnClickListener { onCustomFoldersHeaderClicked(isCollapsed) }
setOnActionClickListener { onCreateFolderClicked() }
}
}
Loading

0 comments on commit 9c9dbcb

Please sign in to comment.