Skip to content

Commit

Permalink
update paging to stable and refactttor viewHolder for photos
Browse files Browse the repository at this point in the history
  • Loading branch information
leonlatsch committed Jul 21, 2021
1 parent 1f049bc commit e2e63b5
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 72 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ android {
dependencies {
val roomVersion = "2.3.0"
val coroutinesVersion = "1.3.7"
val pagingVersion = "3.0.0-alpha11"
val pagingVersion = "3.0.1"
val daggerVersion = "2.31"
val hiltVersion = "2.31-alpha"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
package dev.leonlatsch.photok.gallery.ui

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.ObservableArrayList
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import dev.leonlatsch.photok.databinding.PhotoItemBinding
import dev.leonlatsch.photok.model.database.entity.Photo
import dev.leonlatsch.photok.model.repositories.PhotoRepository
import kotlin.reflect.KFunction1
Expand Down Expand Up @@ -60,8 +62,10 @@ class PhotoAdapter(
holderItem.bindTo(this, getItem(position))
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PhotoItemViewHolder =
PhotoItemViewHolder(parent, context, photoRepository)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PhotoItemViewHolder {
val binding = PhotoItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return PhotoItemViewHolder(binding, photoRepository)
}

/**
* Called by ui. On Click.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,11 @@

package dev.leonlatsch.photok.gallery.ui

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.setPadding
import androidx.databinding.ObservableList
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import dev.leonlatsch.photok.R
import dev.leonlatsch.photok.databinding.PhotoItemBinding
import dev.leonlatsch.photok.model.database.entity.Photo
import dev.leonlatsch.photok.model.repositories.PhotoRepository
import dev.leonlatsch.photok.other.extensions.hide
Expand All @@ -42,43 +36,36 @@ import timber.log.Timber
* Uses multi selection logic in [PhotoAdapter].
* Loads the thumbnail
*
* @param parent The parent [ViewGroup]
* @param context Required by [photoRepository]
* @param binding
* @param photoRepository Used to load the thumbnail
*
* @since 1.0.0
* @author Leon Latsch
*/
class PhotoItemViewHolder(
parent: ViewGroup,
private val context: Context,
private val binding: PhotoItemBinding,
private val photoRepository: PhotoRepository
) : RecyclerView.ViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.photo_item, parent, false)
) {
private val imageView: ImageView = itemView.findViewById(R.id.photoItemImageView)
private val imageContainer: ConstraintLayout =
itemView.findViewById(R.id.photoItemImageContainer)
private val checkBox: CheckBox = itemView.findViewById(R.id.photoItemCheckBox)
private val videoIcon: ImageView = itemView.findViewById(R.id.photoItemVideoIcon)
) : RecyclerView.ViewHolder(binding.root) {

var photo: Photo? = null
private lateinit var adapter: PhotoAdapter

/**
* Binds the parent adapter and the photo to the ViewHolder.
*/
fun bindTo(adapter: PhotoAdapter, photo: Photo?) {
this.photo = photo
fun bindTo(adapter: PhotoAdapter, photoItem: Photo?) {
this.photo = photoItem
this.adapter = adapter

photo ?: return

if (photo.type.isVideo) {
videoIcon.show()
if (photo!!.type.isVideo) {
binding.photoItemVideoIcon.show()
} else {
binding.photoItemVideoIcon.hide()
}

imageView.setOnClickListener {
binding.photoItemImageView.setOnClickListener {
if (adapter.isMultiSelectMode.value!!) {
// If the item clicked is the last selected item
if (adapter.isLastSelectedItem(layoutPosition)) {
Expand All @@ -92,7 +79,7 @@ class PhotoItemViewHolder(
}
}

imageView.setOnLongClickListener {
binding.photoItemImageView.setOnLongClickListener {
if (!adapter.isMultiSelectMode.value!!) {
adapter.enableSelection()
setItemChecked(true)
Expand All @@ -102,9 +89,9 @@ class PhotoItemViewHolder(

adapter.isMultiSelectMode.observe(adapter.lifecycleOwner, {
if (it) { // When selection gets enabled, show the checkbox
checkBox.show()
binding.photoItemCheckBox.show()
} else {
checkBox.hide()
binding.photoItemCheckBox.hide()
}
})

Expand Down Expand Up @@ -164,8 +151,8 @@ class PhotoItemViewHolder(
val isSelected = adapter.isItemSelected(layoutPosition)
val padding = if (isSelected) 20 else 0

checkBox.isChecked = isSelected
imageContainer.setPadding(padding)
binding.photoItemCheckBox.isChecked = isSelected
binding.photoItemImageContainer.setPadding(padding)
}

private fun setItemChecked(checked: Boolean) {
Expand All @@ -180,6 +167,7 @@ class PhotoItemViewHolder(

/**
* Load the thumbnail for the [photo].
* TODO: Move this somewhere else. Data should not be loaded in the view layer
*/
private fun loadThumbnail() {
GlobalScope.launch(Dispatchers.IO) {
Expand All @@ -192,10 +180,10 @@ class PhotoItemViewHolder(
}

onMain {
Glide.with(context)
Glide.with(binding.root.context)
.asBitmap()
.load(thumbnailBytes)
.into(imageView)
.into(binding.photoItemImageView)
}
}
}
Expand Down
83 changes: 45 additions & 38 deletions app/src/main/res/layout/photo_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,53 +14,60 @@
~ limitations under the License.
-->

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="0.5dp">
<layout>

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/photoItemImageContainer"
<data>

</data>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="wrap_content"
android:padding="0.5dp">

<ImageView
android:id="@+id/photoItemImageView"
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/photoItemImageContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:src="@color/gray" />
android:layout_height="match_parent">

<ImageView
android:id="@+id/photoItemImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:src="@color/gray" />

<ImageView
android:id="@+id/photoItemVideoIcon"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/ic_videocam"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.05"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.95"
app:tint="@color/lightGray" />

</androidx.constraintlayout.widget.ConstraintLayout>

<ImageView
android:id="@+id/photoItemVideoIcon"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/ic_videocam"
android:visibility="gone"
<CheckBox
android:id="@+id/photoItemCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@android:color/white"
android:clickable="false"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.05"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.95"
app:tint="@color/lightGray" />
app:layout_constraintVertical_bias="0.05" />

</androidx.constraintlayout.widget.ConstraintLayout>

<CheckBox
android:id="@+id/photoItemCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@android:color/white"
android:clickable="false"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.05"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.05" />


</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

0 comments on commit e2e63b5

Please sign in to comment.