Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use binding in adapter without kotlin Extensions #1104

Merged
merged 4 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,23 @@ import com.google.android.material.chip.Chip
import com.infomaniak.drive.R
import com.infomaniak.drive.data.models.SearchFilter
import com.infomaniak.drive.data.models.SearchFilter.FilterKey
import com.infomaniak.drive.databinding.ItemSearchFilterChipBinding
import com.infomaniak.drive.ui.fileList.SearchFiltersAdapter.SearchFiltersViewHolder
import com.infomaniak.lib.core.views.ViewHolder

class SearchFiltersAdapter(
private val onFilterRemoved: (key: FilterKey, categoryId: Int?) -> Unit
) : RecyclerView.Adapter<ViewHolder>() {
) : RecyclerView.Adapter<SearchFiltersViewHolder>() {

var filters = arrayListOf<SearchFilter>()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_search_filter_chip, parent, false))
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SearchFiltersViewHolder {
return SearchFiltersViewHolder(ItemSearchFilterChipBinding.inflate(LayoutInflater.from(parent.context), parent, false))
}

override fun getItemCount() = filters.size

override fun onBindViewHolder(holder: ViewHolder, position: Int) = with(holder.itemView as Chip) {
override fun onBindViewHolder(holder: SearchFiltersViewHolder, position: Int) = with(holder.binding.root) {
val filter = [email protected][position]
setEndMargin(position)
setIcon(filter)
Expand Down Expand Up @@ -86,4 +89,6 @@ class SearchFiltersAdapter(
filters = newFilters
notifyItemRangeChanged(0, itemCount)
}

class SearchFiltersViewHolder(val binding: ItemSearchFilterChipBinding) : ViewHolder(binding.root)
}
34 changes: 26 additions & 8 deletions app/src/main/java/com/infomaniak/drive/ui/menu/UserAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ package com.infomaniak.drive.ui.menu
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.infomaniak.drive.R
import androidx.viewbinding.ViewBinding
import com.infomaniak.drive.databinding.CardviewUserBinding
import com.infomaniak.drive.databinding.ItemUserBinding
import com.infomaniak.drive.ui.menu.UserAdapter.UserViewHolder
import com.infomaniak.drive.utils.setUserView
import com.infomaniak.lib.core.models.user.User
import com.infomaniak.lib.core.views.ViewHolder
Expand All @@ -29,21 +32,34 @@ class UserAdapter(
private val users: ArrayList<User>,
private val isCardview: Boolean = true,
private val onItemClicked: (user: User) -> Unit
) : RecyclerView.Adapter<ViewHolder>() {
) : RecyclerView.Adapter<UserViewHolder>() {

override fun getItemViewType(position: Int): Int {
return if (isCardview) VIEW_TYPE_CARDVIEW
else VIEW_TYPE_NORMAL
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val layoutId = if (viewType == VIEW_TYPE_CARDVIEW) R.layout.cardview_user else R.layout.item_user
return ViewHolder(LayoutInflater.from(parent.context).inflate(layoutId, parent, false))
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val binding = if (viewType == VIEW_TYPE_CARDVIEW) {
CardviewUserBinding.inflate(layoutInflater, parent, false)
} else {
ItemUserBinding.inflate(layoutInflater, parent, false)
}

return UserViewHolder(binding)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
override fun onBindViewHolder(holder: UserViewHolder, position: Int) = with(holder.binding) {
val user = users[position]
holder.itemView.setUserView(user, isCardview, onItemClicked)

val itemUserBinding = if (getItemViewType(position) == VIEW_TYPE_CARDVIEW) {
(this as CardviewUserBinding).itemViewUser
} else {
this as ItemUserBinding
}

itemUserBinding.setUserView(user, isCardview, onItemClicked)
}

override fun getItemCount() = users.size
Expand All @@ -52,4 +68,6 @@ class UserAdapter(
const val VIEW_TYPE_CARDVIEW = 1
const val VIEW_TYPE_NORMAL = 2
}
}

class UserViewHolder(val binding: ViewBinding) : ViewHolder(binding.root)
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class SelectDriveDialog : FullScreenBottomSheetDialog() {
AccountUtils.getAllUsers().observe(viewLifecycleOwner) { users ->
if (users.size > 1) {
val selectedUser = users.find { it.id == selectedUserId.value } ?: users.first()
binding.userCardview.root.setUserView(selectedUser) {
binding.userCardview.itemViewUser.setUserView(selectedUser) {
popupWindow = PopupWindow(
popupLayoutBinding.root,
binding.userCardview.root.width,
Expand All @@ -81,7 +81,7 @@ class SelectDriveDialog : FullScreenBottomSheetDialog() {
selectedUserId.value = user.id
driveListAdapter.setDrives(getDriveList())

binding.userCardview.root.setUserView(user) { popupWindow.showAsDropDown(binding.userCardview.root) }
binding.userCardview.itemViewUser.setUserView(user) { popupWindow.showAsDropDown(binding.userCardview.root) }

popupWindow.dismiss()
}
Expand Down
9 changes: 3 additions & 6 deletions app/src/main/java/com/infomaniak/drive/utils/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import com.infomaniak.drive.data.models.Shareable
import com.infomaniak.drive.data.models.drive.Category
import com.infomaniak.drive.data.models.drive.Drive
import com.infomaniak.drive.databinding.CardviewFileListBinding
import com.infomaniak.drive.databinding.ItemUserBinding
import com.infomaniak.drive.ui.MainActivity
import com.infomaniak.drive.ui.MainViewModel
import com.infomaniak.drive.ui.OnlyOfficeActivity
Expand All @@ -74,10 +75,6 @@ import com.infomaniak.lib.core.utils.*
import com.infomaniak.lib.core.utils.SnackbarUtils.showSnackbar
import com.infomaniak.lib.login.InfomaniakLogin
import io.realm.RealmList
import kotlinx.android.synthetic.main.item_user.view.chevron
import kotlinx.android.synthetic.main.item_user.view.userAvatar
import kotlinx.android.synthetic.main.item_user.view.userEmail
import kotlinx.android.synthetic.main.item_user.view.userName
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min
Expand Down Expand Up @@ -141,12 +138,12 @@ fun Activity.setColorNavigationBar(appBar: Boolean = false) = with(window) {

fun String.isValidUrl(): Boolean = Patterns.WEB_URL.matcher(this).matches()

fun View.setUserView(user: User, showChevron: Boolean = true, onItemClicked: (user: User) -> Unit) {
fun ItemUserBinding.setUserView(user: User, showChevron: Boolean = true, onItemClicked: (user: User) -> Unit) {
userName.text = user.displayName
userEmail.text = user.email
userAvatar.loadAvatar(user)
chevron.isVisible = showChevron
setOnClickListener { onItemClicked(user) }
root.setOnClickListener { onItemClicked(user) }
}

fun ImageView.animateRotation(isDeployed: Boolean = false) {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/cardview_user.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
android:layout_marginVertical="@dimen/marginStandardSmall">

<include
android:id="@+id/itemviewUser"
android:id="@+id/itemViewUser"
layout="@layout/item_user" />

</com.google.android.material.card.MaterialCardView>
</com.google.android.material.card.MaterialCardView>
Loading