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

4 - Replace kotlinx by viewBinding in adapters #1090

Merged
merged 3 commits into from
Oct 25, 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,21 @@ import androidx.core.view.isVisible
import coil.load
import com.infomaniak.drive.R
import com.infomaniak.drive.data.models.FileActivity
import com.infomaniak.drive.databinding.ItemFileActivityBinding
import com.infomaniak.drive.utils.loadAvatar
import com.infomaniak.lib.core.utils.context
import com.infomaniak.lib.core.views.LoaderAdapter
import com.infomaniak.lib.core.views.ViewHolder
import kotlinx.android.synthetic.main.item_file_activity.view.*
import java.text.SimpleDateFormat
import java.util.*

class FileActivitiesAdapter(val isFolder: Boolean) : LoaderAdapter<FileActivity>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_file_activity, parent, false))
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FileActivitiesViewHolder {
return FileActivitiesViewHolder(ItemFileActivityBinding.inflate(LayoutInflater.from(parent.context), parent, false))
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) = with(holder.itemView) {
override fun onBindViewHolder(holder: ViewHolder, position: Int) = with((holder as FileActivitiesViewHolder).binding) {
if (getItemViewType(position) == VIEW_TYPE_LOADING) {
activityDateCardView.startLoading()
activityUserName.resetLoader()
Expand Down Expand Up @@ -79,4 +80,6 @@ class FileActivitiesAdapter(val isFolder: Boolean) : LoaderAdapter<FileActivity>
val simpleDateFormat = SimpleDateFormat("yyyyMMdd")
return simpleDateFormat.format(date1).equals(simpleDateFormat.format(date2))
}

class FileActivitiesViewHolder(val binding: ItemFileActivityBinding) : ViewHolder(binding.root)
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,10 @@ import com.infomaniak.drive.data.models.DriveUser
import com.infomaniak.drive.data.models.Invitation
import com.infomaniak.drive.data.models.Shareable
import com.infomaniak.drive.data.models.Team
import com.infomaniak.drive.databinding.ItemUserBinding
import com.infomaniak.drive.utils.AccountUtils
import com.infomaniak.drive.utils.isEmail
import com.infomaniak.drive.utils.loadAvatar
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

/**
* Note :
Expand Down Expand Up @@ -93,10 +90,11 @@ class AvailableShareableItemsAdapter(
}

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val item = itemList[position]
val itemUserBinding = (convertView?.tag as? ItemUserBinding ?: inflateNewBinding(parent))

return (convertView ?: inflater.inflate(R.layout.item_user, parent, false)).apply {
return with(itemUserBinding) {

val item = itemList[position]
when (item) {
is DriveUser -> {
userAvatar.loadAvatar(item)
Expand All @@ -116,17 +114,19 @@ class AvailableShareableItemsAdapter(
userAvatar.setBackgroundColor(item.getParsedColor())
userName.text = item.name
userEmail.text =
resources.getQuantityString(R.plurals.shareUsersCount, teamUsersCount, teamUsersCount)
context.resources.getQuantityString(R.plurals.shareUsersCount, teamUsersCount, teamUsersCount)
chevron.isGone = true
}
}

setOnClickListener {
onItemClick(item)
}
root.apply { setOnClickListener { onItemClick(item) } }
}
}

private fun inflateNewBinding(parent: ViewGroup): ItemUserBinding {
return ItemUserBinding.inflate(LayoutInflater.from(parent.context), parent, false).apply { root.tag = this }
}

override fun getCount() = itemList.size

override fun getFilter(): Filter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,42 +31,43 @@ import com.infomaniak.drive.data.models.DriveUser
import com.infomaniak.drive.data.models.ExtensionType
import com.infomaniak.drive.data.models.File
import com.infomaniak.drive.data.models.FileActivity
import com.infomaniak.drive.databinding.CardviewHomeFileActivityBinding
import com.infomaniak.drive.databinding.EmptyIconLayoutBinding
import com.infomaniak.drive.utils.loadAny
import com.infomaniak.drive.utils.loadAvatar
import com.infomaniak.lib.core.utils.context
import com.infomaniak.lib.core.views.LoaderAdapter
import com.infomaniak.lib.core.views.LoaderCardView
import com.infomaniak.lib.core.views.ViewHolder
import kotlinx.android.synthetic.main.cardview_home_file_activity.view.*
import kotlinx.android.synthetic.main.empty_icon_layout.view.icon

class LastActivitiesAdapter : LoaderAdapter<FileActivity>() {

var onFileClicked: ((currentFile: File, validPreviewFiles: ArrayList<File>) -> Unit)? = null
var onMoreFilesClicked: ((fileActivity: FileActivity, validPreviewFiles: ArrayList<File>) -> Unit)? = null

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.cardview_home_file_activity, parent, false))
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LastActivitiesViewHolder {
return LastActivitiesViewHolder(
CardviewHomeFileActivityBinding.inflate(LayoutInflater.from(parent.context), parent, false)
)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
(holder.itemView as LoaderCardView).apply {
if (getItemViewType(position) == VIEW_TYPE_LOADING) {
startLoading()
userName.resetLoader()
actionValue.resetLoader()
dateValue.resetLoader()
} else {
stopLoading()
val fileActivity = itemList[position]
fileActivity.user?.let { user -> this.createActivity(fileActivity, user) }
}
override fun onBindViewHolder(holder: ViewHolder, position: Int): Unit = with((holder as LastActivitiesViewHolder).binding) {
if (getItemViewType(position) == VIEW_TYPE_LOADING) {
root.startLoading()
userName.resetLoader()
actionValue.resetLoader()
dateValue.resetLoader()
} else {
root.stopLoading()
val fileActivity = itemList[position]
fileActivity.user?.let { user -> createActivity(fileActivity, user) }
}
}

private fun View.createActivity(fileActivity: FileActivity, user: DriveUser) {
private fun CardviewHomeFileActivityBinding.createActivity(fileActivity: FileActivity, user: DriveUser) {
val fileActivityName: CharSequence = fileActivity.file?.name ?: fileActivity.newPath.substringAfterLast("/")
val sizeMergedFile = fileActivity.mergedFileActivities.size
actionValue.text = resources.getQuantityString(fileActivity.homeTranslation, sizeMergedFile + 1, sizeMergedFile + 1)
actionValue.text =
context.resources.getQuantityString(fileActivity.homeTranslation, sizeMergedFile + 1, sizeMergedFile + 1)
userAvatar.loadAvatar(user)
userName.text = user.displayName
dateValue.text = getRelativeDateTimeString(
Expand All @@ -84,9 +85,9 @@ class LastActivitiesAdapter : LoaderAdapter<FileActivity>() {
fileIcon2.isVisible = true
fileName2.isVisible = true

fileActivity.file.loadPreview(filePreview1, filePreviewIcon1 as ConstraintLayout)
fileActivity.file.loadPreview(filePreview1, filePreviewIcon1)
val file2 = fileActivity.mergedFileActivities[0].file
file2.loadPreview(filePreview2, filePreviewIcon2 as ConstraintLayout)
file2.loadPreview(filePreview2, filePreviewIcon2)
fileIcon2.load(getFileTypeIcon(file2))
fileName2.text = file2?.name ?: fileActivity.mergedFileActivities[0].newPath.substringAfterLast("/")

Expand All @@ -102,7 +103,7 @@ class LastActivitiesAdapter : LoaderAdapter<FileActivity>() {
fileName3.isVisible = true

val file3 = fileActivity.mergedFileActivities[1].file
file3.loadPreview(filePreview3, filePreviewIcon3 as ConstraintLayout)
file3.loadPreview(filePreview3, filePreviewIcon3)
(cardFilePreview1.layoutParams as ConstraintLayout.LayoutParams).dimensionRatio = "1:1"
(cardFilePreview2.layoutParams as ConstraintLayout.LayoutParams).dimensionRatio = "1:1"

Expand All @@ -125,7 +126,7 @@ class LastActivitiesAdapter : LoaderAdapter<FileActivity>() {
fileName2.isGone = true
fileName3.isGone = true
(cardFilePreview1.layoutParams as ConstraintLayout.LayoutParams).dimensionRatio = "3:1"
fileActivity.file.loadPreview(filePreview1, filePreviewIcon1 as ConstraintLayout)
fileActivity.file.loadPreview(filePreview1, filePreviewIcon1)
}

fileActivity.file?.let { file ->
Expand Down Expand Up @@ -165,17 +166,19 @@ class LastActivitiesAdapter : LoaderAdapter<FileActivity>() {
}
}

private fun File?.loadPreview(imageView: ImageView, iconView: ConstraintLayout) {
private fun File?.loadPreview(imageView: ImageView, iconViewBinding: EmptyIconLayoutBinding) {
if (this?.hasThumbnail == true && getFileType() == ExtensionType.IMAGE || this?.getFileType() == ExtensionType.VIDEO) {
iconView.isGone = true
iconViewBinding.root.isGone = true
imageView.isVisible = true
imageView.loadAny(thumbnail(), getFileType().icon)
} else {
imageView.isGone = true
iconView.isVisible = true
iconView.icon.load(getFileTypeIcon(this))
iconViewBinding.root.isVisible = true
iconViewBinding.icon.load(getFileTypeIcon(this))
}
}

private fun getFileTypeIcon(file: File?) = file?.getFileType()?.icon ?: R.drawable.ic_file

class LastActivitiesViewHolder(val binding: CardviewHomeFileActivityBinding) : ViewHolder(binding.root)
}
Loading