Merge 'SharedItemsListAdapter' and 'SharedItemsGridAdapter'

Signed-off-by: Tim Krüger <t@timkrueger.me>
This commit is contained in:
Tim Krüger 2022-05-19 12:21:00 +02:00
parent fd76f8231d
commit b2f59ccb6c
No known key found for this signature in database
GPG key ID: FECE3A7222C52A4E
9 changed files with 227 additions and 247 deletions

View file

@ -12,17 +12,16 @@ import androidx.recyclerview.widget.RecyclerView
import autodagger.AutoInjector
import com.google.android.material.tabs.TabLayout
import com.nextcloud.talk.R
import com.nextcloud.talk.shareditems.adapters.SharedItemsGridAdapter
import com.nextcloud.talk.shareditems.adapters.SharedItemsListAdapter
import com.nextcloud.talk.application.NextcloudTalkApplication
import com.nextcloud.talk.databinding.ActivitySharedItemsBinding
import com.nextcloud.talk.models.database.UserEntity
import com.nextcloud.talk.shareditems.adapters.SharedItemsAdapter
import com.nextcloud.talk.shareditems.model.SharedItemType
import com.nextcloud.talk.shareditems.viewmodels.SharedItemsViewModel
import com.nextcloud.talk.utils.DisplayUtils
import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_CONVERSATION_NAME
import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_ROOM_TOKEN
import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_USER_ENTITY
import com.nextcloud.talk.shareditems.viewmodels.SharedItemsViewModel
import javax.inject.Inject
@AutoInjector(NextcloudTalkApplication::class)
@ -70,24 +69,18 @@ class SharedItemsActivity : AppCompatActivity() {
viewModel.sharedItems.observe(this) {
Log.d(TAG, "Items received: $it")
if (viewModel.currentItemType == SharedItemType.MEDIA) {
val adapter = SharedItemsGridAdapter()
adapter.items = it.items
adapter.authHeader = it.authHeader
binding.imageRecycler.adapter = adapter
val layoutManager = GridLayoutManager(this, SPAN_COUNT)
binding.imageRecycler.layoutManager = layoutManager
val showGrid = viewModel.currentItemType == SharedItemType.MEDIA
val layoutManager = if (showGrid) {
GridLayoutManager(this, SPAN_COUNT)
} else {
val adapter = SharedItemsListAdapter()
adapter.items = it.items
adapter.authHeader = it.authHeader
binding.imageRecycler.adapter = adapter
val layoutManager = LinearLayoutManager(this)
layoutManager.orientation = LinearLayoutManager.VERTICAL
binding.imageRecycler.layoutManager = layoutManager
LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
}
val adapter = SharedItemsAdapter(showGrid, userEntity.username, userEntity.token).apply {
items = it.items
}
binding.imageRecycler.adapter = adapter
binding.imageRecycler.layoutManager = layoutManager
}
binding.imageRecycler.addOnScrollListener(object : RecyclerView.OnScrollListener() {

View file

@ -0,0 +1,50 @@
package com.nextcloud.talk.shareditems.adapters
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.nextcloud.talk.databinding.SharedItemGridBinding
import com.nextcloud.talk.databinding.SharedItemListBinding
import com.nextcloud.talk.shareditems.model.SharedItem
class SharedItemsAdapter(
private val showGrid: Boolean,
private val userName: String,
private val userToken: String
) : RecyclerView.Adapter<SharedItemsViewHolder>() {
var items: List<SharedItem> = emptyList()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SharedItemsViewHolder {
return if (showGrid) {
SharedItemsGridViewHolder(
SharedItemGridBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
),
userName,
userToken
)
} else {
SharedItemsListViewHolder(
SharedItemListBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
),
userName,
userToken
)
}
}
override fun onBindViewHolder(holder: SharedItemsViewHolder, position: Int) {
holder.onBind(items[position])
}
override fun getItemCount(): Int {
return items.size
}
}

View file

@ -1,105 +0,0 @@
package com.nextcloud.talk.shareditems.adapters
import android.net.Uri
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.drawee.controller.BaseControllerListener
import com.facebook.drawee.controller.ControllerListener
import com.facebook.drawee.interfaces.DraweeController
import com.facebook.drawee.view.SimpleDraweeView
import com.facebook.imagepipeline.common.RotationOptions
import com.facebook.imagepipeline.image.ImageInfo
import com.facebook.imagepipeline.request.ImageRequestBuilder
import com.nextcloud.talk.databinding.SharedItemGridBinding
import com.nextcloud.talk.shareditems.model.SharedItem
import com.nextcloud.talk.utils.DrawableUtils
import com.nextcloud.talk.utils.FileViewerUtils
class SharedItemsGridAdapter : RecyclerView.Adapter<SharedItemsGridAdapter.ViewHolder>() {
companion object {
private val TAG = SharedItemsGridAdapter::class.simpleName
}
class ViewHolder(val binding: SharedItemGridBinding, itemView: View) : RecyclerView.ViewHolder(itemView)
var authHeader: Map<String, String> = emptyMap()
var items: List<SharedItem> = emptyList()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val binding = SharedItemGridBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return ViewHolder(binding, binding.root)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val currentItem = items[position]
if (currentItem.previewAvailable == true) {
val imageRequest = ImageRequestBuilder.newBuilderWithSource(Uri.parse(currentItem.previewLink))
.setProgressiveRenderingEnabled(true)
.setRotationOptions(RotationOptions.autoRotate())
.disableDiskCache()
.setHeaders(authHeader)
.build()
val listener: ControllerListener<ImageInfo?> = object : BaseControllerListener<ImageInfo?>() {
override fun onFailure(id: String, e: Throwable) {
Log.w(TAG, "Failed to load image. A static mimetype image will be used", e)
setStaticMimetypeImage(currentItem, holder)
}
}
val draweeController: DraweeController = Fresco.newDraweeControllerBuilder()
.setOldController(holder.binding.image.controller)
.setAutoPlayAnimations(true)
.setImageRequest(imageRequest)
.setControllerListener(listener)
.build()
holder.binding.image.controller = draweeController
} else {
setStaticMimetypeImage(currentItem, holder)
}
val fileViewerUtils = FileViewerUtils(holder.binding.image.context, currentItem.userEntity)
holder.binding.image.setOnClickListener {
fileViewerUtils.openFile(
FileViewerUtils.FileInfo(currentItem.id, currentItem.name, currentItem.fileSize),
currentItem.path,
currentItem.link,
currentItem.mimeType,
FileViewerUtils.ProgressUi(
holder.binding.progressBar,
null,
it as SimpleDraweeView
)
)
}
fileViewerUtils.resumeToUpdateViewsByProgress(
currentItem.name,
currentItem.id,
currentItem.mimeType,
FileViewerUtils.ProgressUi(holder.binding.progressBar, null, holder.binding.image)
)
}
private fun setStaticMimetypeImage(
currentItem: SharedItem,
holder: ViewHolder
) {
val drawableResourceId = DrawableUtils.getDrawableResourceIdForMimeType(currentItem.mimeType)
val drawable = ContextCompat.getDrawable(holder.binding.image.context, drawableResourceId)
holder.binding.image.hierarchy.setPlaceholderImage(drawable)
}
override fun getItemCount(): Int {
return items.size
}
}

View file

@ -0,0 +1,20 @@
package com.nextcloud.talk.shareditems.adapters
import android.view.View
import android.widget.ProgressBar
import com.facebook.drawee.view.SimpleDraweeView
import com.nextcloud.talk.databinding.SharedItemGridBinding
class SharedItemsGridViewHolder(
override val binding: SharedItemGridBinding,
userName: String,
userToken: String
) : SharedItemsViewHolder(binding, userName, userToken) {
override val image: SimpleDraweeView
get() = binding.image
override val clickTarget: View
get() = binding.image
override val progressBar: ProgressBar
get() = binding.progressBar
}

View file

@ -1,119 +0,0 @@
package com.nextcloud.talk.shareditems.adapters
import android.net.Uri
import android.text.format.Formatter
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.drawee.controller.BaseControllerListener
import com.facebook.drawee.controller.ControllerListener
import com.facebook.drawee.interfaces.DraweeController
import com.facebook.imagepipeline.common.RotationOptions
import com.facebook.imagepipeline.image.ImageInfo
import com.facebook.imagepipeline.request.ImageRequestBuilder
import com.nextcloud.talk.databinding.SharedItemListBinding
import com.nextcloud.talk.shareditems.model.SharedItem
import com.nextcloud.talk.utils.DateUtils
import com.nextcloud.talk.utils.DrawableUtils
import com.nextcloud.talk.utils.FileViewerUtils
import com.nextcloud.talk.utils.FileViewerUtils.ProgressUi
class SharedItemsListAdapter : RecyclerView.Adapter<SharedItemsListAdapter.ViewHolder>() {
companion object {
private val TAG = SharedItemsListAdapter::class.simpleName
private const val ONE_SECOND_IN_MILLIS = 1000
}
class ViewHolder(val binding: SharedItemListBinding, itemView: View) : RecyclerView.ViewHolder(itemView)
var authHeader: Map<String, String> = emptyMap()
var items: List<SharedItem> = emptyList()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val binding = SharedItemListBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return ViewHolder(binding, binding.root)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val currentItem = items[position]
holder.binding.fileName.text = currentItem.name
holder.binding.fileSize.text = currentItem.fileSize?.let {
Formatter.formatShortFileSize(
holder.binding.fileSize.context,
it
)
}
holder.binding.fileDate.text = DateUtils.getLocalDateTimeStringFromTimestamp(
currentItem.date * ONE_SECOND_IN_MILLIS
)
if (currentItem.previewAvailable == true) {
val imageRequest = ImageRequestBuilder.newBuilderWithSource(Uri.parse(currentItem.previewLink))
.setProgressiveRenderingEnabled(true)
.setRotationOptions(RotationOptions.autoRotate())
.disableDiskCache()
.setHeaders(authHeader)
.build()
val listener: ControllerListener<ImageInfo?> = object : BaseControllerListener<ImageInfo?>() {
override fun onFailure(id: String, e: Throwable) {
Log.w(TAG, "Failed to load image. A static mimetype image will be used", e)
setStaticMimetypeImage(currentItem, holder)
}
}
val draweeController: DraweeController = Fresco.newDraweeControllerBuilder()
.setOldController(holder.binding.fileImage.controller)
.setAutoPlayAnimations(true)
.setImageRequest(imageRequest)
.setControllerListener(listener)
.build()
holder.binding.fileImage.controller = draweeController
} else {
setStaticMimetypeImage(currentItem, holder)
}
val fileViewerUtils = FileViewerUtils(holder.binding.fileImage.context, currentItem.userEntity)
holder.binding.fileItem.setOnClickListener {
fileViewerUtils.openFile(
FileViewerUtils.FileInfo(currentItem.id, currentItem.name, currentItem.fileSize),
currentItem.path,
currentItem.link,
currentItem.mimeType,
ProgressUi(
holder.binding.progressBar,
null,
holder.binding.fileImage
)
)
}
fileViewerUtils.resumeToUpdateViewsByProgress(
currentItem.name,
currentItem.id,
currentItem.mimeType,
ProgressUi(holder.binding.progressBar, null, holder.binding.fileImage)
)
}
private fun setStaticMimetypeImage(
currentItem: SharedItem,
holder: ViewHolder
) {
val drawableResourceId = DrawableUtils.getDrawableResourceIdForMimeType(currentItem.mimeType)
val drawable = ContextCompat.getDrawable(holder.binding.fileImage.context, drawableResourceId)
holder.binding.fileImage.hierarchy.setPlaceholderImage(drawable)
}
override fun getItemCount(): Int {
return items.size
}
}

View file

@ -0,0 +1,43 @@
package com.nextcloud.talk.shareditems.adapters
import android.text.format.Formatter
import android.view.View
import android.widget.ProgressBar
import com.facebook.drawee.view.SimpleDraweeView
import com.nextcloud.talk.databinding.SharedItemListBinding
import com.nextcloud.talk.shareditems.model.SharedItem
import com.nextcloud.talk.utils.DateUtils
class SharedItemsListViewHolder(
override val binding: SharedItemListBinding,
userName: String,
userToken: String
) : SharedItemsViewHolder(binding, userName, userToken) {
override val image: SimpleDraweeView
get() = binding.fileImage
override val clickTarget: View
get() = binding.fileItem
override val progressBar: ProgressBar
get() = binding.progressBar
override fun onBind(item: SharedItem) {
super.onBind(item)
binding.fileName.text = item.name
binding.fileSize.text = item.fileSize?.let {
Formatter.formatShortFileSize(
binding.fileSize.context,
it
)
}
binding.fileDate.text = DateUtils.getLocalDateTimeStringFromTimestamp(
item.date * ONE_SECOND_IN_MILLIS
)
}
companion object {
private const val ONE_SECOND_IN_MILLIS = 1000
}
}

View file

@ -0,0 +1,100 @@
package com.nextcloud.talk.shareditems.adapters
import android.graphics.drawable.Drawable
import android.net.Uri
import android.util.Log
import android.view.View
import android.widget.ProgressBar
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.drawee.controller.BaseControllerListener
import com.facebook.drawee.controller.ControllerListener
import com.facebook.drawee.interfaces.DraweeController
import com.facebook.drawee.view.SimpleDraweeView
import com.facebook.imagepipeline.common.RotationOptions
import com.facebook.imagepipeline.image.ImageInfo
import com.facebook.imagepipeline.request.ImageRequestBuilder
import com.nextcloud.talk.shareditems.model.SharedItem
import com.nextcloud.talk.utils.ApiUtils
import com.nextcloud.talk.utils.DrawableUtils
import com.nextcloud.talk.utils.FileViewerUtils
abstract class SharedItemsViewHolder(
open val binding: ViewBinding,
userName: String,
userToken: String
) : RecyclerView.ViewHolder(binding.root) {
companion object {
private val TAG = SharedItemsViewHolder::class.simpleName
}
abstract val image: SimpleDraweeView
abstract val clickTarget: View
abstract val progressBar: ProgressBar
private val authHeader = mapOf(Pair("Authorization", ApiUtils.getCredentials(userName, userToken)))
open fun onBind(item: SharedItem) {
image.hierarchy.setPlaceholderImage(staticImage(item.mimeType, image))
if (item.previewAvailable == true) {
image.controller = configurePreview(item)
}
val fileViewerUtils = FileViewerUtils(image.context, item.userEntity)
clickTarget.setOnClickListener {
fileViewerUtils.openFile(
FileViewerUtils.FileInfo(item.id, item.name, item.fileSize),
item.path,
item.link,
item.mimeType,
FileViewerUtils.ProgressUi(
progressBar,
null,
image
)
)
}
fileViewerUtils.resumeToUpdateViewsByProgress(
item.name,
item.id,
item.mimeType,
FileViewerUtils.ProgressUi(progressBar, null, image)
)
}
private fun configurePreview(item: SharedItem): DraweeController {
val imageRequest = ImageRequestBuilder.newBuilderWithSource(Uri.parse(item.previewLink))
.setProgressiveRenderingEnabled(true)
.setRotationOptions(RotationOptions.autoRotate())
.disableDiskCache()
.setHeaders(authHeader)
.build()
val listener: ControllerListener<ImageInfo?> = object : BaseControllerListener<ImageInfo?>() {
override fun onFailure(id: String, e: Throwable) {
Log.w(TAG, "Failed to load image. A static mimetype image will be used", e)
}
}
return Fresco.newDraweeControllerBuilder()
.setOldController(image.controller)
.setAutoPlayAnimations(true)
.setImageRequest(imageRequest)
.setControllerListener(listener)
.build()
}
private fun staticImage(
mimeType: String?,
image: SimpleDraweeView
): Drawable {
val drawableResourceId = DrawableUtils.getDrawableResourceIdForMimeType(mimeType)
return ContextCompat.getDrawable(image.context, drawableResourceId)!!
}
}

View file

@ -3,6 +3,5 @@ package com.nextcloud.talk.shareditems.model
class SharedMediaItems(
val items: List<SharedItem>,
var lastSeenId: Int?,
var moreItemsExisting: Boolean,
val authHeader: Map<String, String>
var moreItemsExisting: Boolean
)

View file

@ -88,8 +88,7 @@ class SharedItemsViewModel @Inject constructor(
SharedMediaItems(
oldItems + newSharedItems!!.items,
newSharedItems!!.lastSeenId,
newSharedItems!!.moreItemsExisting,
newSharedItems!!.authHeader
newSharedItems!!.moreItemsExisting
)
}
}