Convert GalleryPreviewsAdapter holder to compose

This commit is contained in:
tarsin committed 2023-04-10 17:11:55 +08:00
1 parent 7dc28f36dc
commit be1d80de0f
5 files changed
+46 -94

No files matched your search

@@ -1,12 +0,0 @@
package com.hippo.ehviewer.ui.scene
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
abstract class BaseListAdapter<T>(diffCallback: DiffUtil.ItemCallback<T>) :
ListAdapter<T, BaseViewHolder<T>>(diffCallback) {
override fun onBindViewHolder(holder: BaseViewHolder<T>, position: Int) {
val item = getItem(position)
holder.bind(item)
}
}
@@ -1,17 +0,0 @@
package com.hippo.ehviewer.ui.scene
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
abstract class BaseViewHolder<T>(binding: ViewBinding, onClick: (T) -> Unit) :
RecyclerView.ViewHolder(binding.root) {
protected var currentItem: T? = null
init {
itemView.setOnClickListener {
currentItem?.let { onClick(it) }
}
}
abstract fun bind(item: T)
}
@@ -1,33 +1,58 @@
package com.hippo.ehviewer.ui.scene
import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.Text
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import arrow.core.partially1
import com.google.accompanist.themeadapter.material3.Mdc3Theme
import com.hippo.ehviewer.client.data.GalleryPreview
import com.hippo.ehviewer.databinding.ItemGalleryPreviewBinding
import com.hippo.ehviewer.ui.widget.CrystalCard
import com.hippo.ehviewer.ui.widget.EhAsyncPreview
class GalleryPreviewsAdapter(private val onClick: (GalleryPreview) -> Unit) :
BaseListAdapter<GalleryPreview>(PreviewDiffCallback) {
class GalleryPreviewHolder(
private val binding: ItemGalleryPreviewBinding,
onClick: (GalleryPreview) -> Unit,
) : BaseViewHolder<GalleryPreview>(binding, onClick) {
@SuppressLint("SetTextI18n")
override fun bind(item: GalleryPreview) {
currentItem = item
item.load(binding.image)
binding.text.text = (item.position + 1).toString()
}
}
class GalleryPreviewsAdapter(private val onClick: (GalleryPreview) -> Unit) : ListAdapter<GalleryPreview, GalleryPreviewsAdapter.ComposeHolder>(PreviewDiffCallback) {
class ComposeHolder(val composeView: ComposeView) : RecyclerView.ViewHolder(composeView)
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int,
): BaseViewHolder<GalleryPreview> {
val inflater = LayoutInflater.from(parent.context)
val binding = ItemGalleryPreviewBinding.inflate(inflater, parent, false)
return GalleryPreviewHolder(binding, onClick)
): ComposeHolder {
return ComposeHolder(ComposeView(parent.context))
}
override fun onBindViewHolder(holder: ComposeHolder, position: Int) {
val it = getItem(position)
holder.composeView.setContent {
Mdc3Theme {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
) {
Box(contentAlignment = Alignment.Center) {
CrystalCard(
onClick = onClick.partially1(it),
modifier = Modifier
.fillMaxWidth()
.aspectRatio(0.6666667F),
) {
EhAsyncPreview(
model = it,
modifier = Modifier.fillMaxSize(),
)
}
}
Text((it.position + 1).toString())
}
}
}
}
override fun getItemId(position: Int): Long {
@@ -61,7 +61,7 @@ fun EhAsyncPreview(
painter = BitmapPainter(
(it.result.drawable as BitmapDrawable).bitmap.asImageBitmap(),
IntOffset(offsetX, offsetY),
IntSize(clipWidth, clipHeight - 1),
IntSize(clipWidth - 1, clipHeight - 1),
),
)
} else {
@@ -1,44 +0,0 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright 2016 Hippo Seven
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.hippo.ehviewer.widget.FixedThumb xmlns:auto="http://schemas.android.com/apk/res-auto"
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?selectableItemBackground"
auto:aspect="0.667"
auto:maxAspect="0.8"
auto:minAspect="0.5"
auto:retryType="longClick" />
</com.google.android.material.card.MaterialCardView>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" />
</LinearLayout>