Skip to content

Commit

Permalink
upgrade paging2 -> paging3
Browse files Browse the repository at this point in the history
  • Loading branch information
iielse committed Jun 8, 2022
1 parent e637f48 commit 52f24c6
Show file tree
Hide file tree
Showing 27 changed files with 287 additions and 309 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.activity:activity-ktx:1.2.4'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'androidx.viewpager2:viewpager2:1.0.0'
//noinspection GradleDependency
implementation 'androidx.paging:paging-runtime-ktx:2.1.2'

implementation 'com.github.bumptech.glide:glide:4.12.0'
implementation 'com.github.bumptech.glide:glide:4.13.1'
kapt 'com.github.bumptech.glide:compiler:4.12.0'
implementation 'com.github.chrisbanes:PhotoView:2.2.0'
implementation 'com.davemorrissey.labs:subsampling-scale-image-view:3.10.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import com.github.iielse.imageviewer.core.SimpleDataProvider
import com.github.iielse.imageviewer.core.Transformer
import com.github.iielse.imageviewer.demo.R
import com.github.iielse.imageviewer.demo.core.viewer.SimpleImageLoader
import com.github.iielse.imageviewer.demo.data.TestRepository
import com.github.iielse.imageviewer.demo.data.Service

// 自定义Transition startView 尺寸/位置/加载模式
object CustomTransitionHelper {
fun show(view: View) {
val dataList = TestRepository.get().data
val dataList = Service.houMen.toMutableList()
val clickedData = dataList[dataList.size - 1 - (System.currentTimeMillis() % 10).toInt()]
val builder = ImageViewerBuilder(
context = view.context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import com.github.iielse.imageviewer.utils.Config

class MainActivity : AppCompatActivity() {
private val binding by lazy { MainActivityBinding.inflate(layoutInflater) }
private val viewModel by viewModels<TestDataViewModel> { TestDataViewModelFactory() }
private val viewModel by viewModels<TestDataViewModel> { TestDataViewModel.Factory() }
private val adapter by lazy { TestDataAdapter() }

override fun onDestroy() {
Expand All @@ -40,16 +40,16 @@ class MainActivity : AppCompatActivity() {

private fun handleAdapterListener(action: String, item: Any?) {
when (action) {
ITEM_CLICKED -> (item as? MyData?)?.let(::showViewer)
ITEM_CLICKED -> showViewer(item as? MyData?)
}
}

private fun showViewer(item: MyData) {
private fun showViewer(item: MyData?) {
if (item == null) return
// if (item.id == 10L) {
// startActivity(Intent(this, TestActivity::class.java))
// return
// }

ViewerHelper.provideImageViewerBuilder(this, item)
.show()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.github.iielse.imageviewer.demo.business

import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
Expand All @@ -8,17 +11,17 @@ import androidx.lifecycle.*
import androidx.recyclerview.widget.RecyclerView
import com.github.iielse.imageviewer.ImageViewerActionViewModel
import com.github.iielse.imageviewer.ImageViewerBuilder
import com.github.iielse.imageviewer.adapter.ItemType
import com.github.iielse.imageviewer.core.OverlayCustomizer
import com.github.iielse.imageviewer.core.Photo
import com.github.iielse.imageviewer.core.VHCustomizer
import com.github.iielse.imageviewer.core.ViewerCallback
import com.github.iielse.imageviewer.core.*
import com.github.iielse.imageviewer.demo.R
import com.github.iielse.imageviewer.demo.core.ObserverAdapter
import com.github.iielse.imageviewer.demo.data.MyData
import com.github.iielse.imageviewer.demo.data.TestRepository
import com.github.iielse.imageviewer.demo.data.Service
import com.github.iielse.imageviewer.demo.databinding.ItemVideoCustomLayoutBinding
import com.github.iielse.imageviewer.demo.databinding.LayoutIndicatorBinding
import com.github.iielse.imageviewer.demo.utils.*
import com.github.iielse.imageviewer.utils.Config
import com.github.iielse.imageviewer.viewholders.PhotoViewHolder
import com.github.iielse.imageviewer.viewholders.SubsamplingViewHolder
import com.github.iielse.imageviewer.viewholders.VideoViewHolder
import com.github.iielse.imageviewer.widgets.video.ExoVideoView
import com.google.android.exoplayer2.ui.PlayerControlView
Expand All @@ -37,10 +40,7 @@ class SimpleViewerCustomizer : LifecycleEventObserver, VHCustomizer, OverlayCust
private var viewerViewModel: ImageViewerActionViewModel? = null
private var videoTask: Disposable? = null
private var lastVideoVH: RecyclerView.ViewHolder? = null
private var indicatorDecor: View? = null
private var indicator: TextView? = null
private var pre: TextView? = null
private var next: TextView? = null
private var binding: LayoutIndicatorBinding? = null
private var currentPosition = -1

/**
Expand All @@ -60,21 +60,14 @@ class SimpleViewerCustomizer : LifecycleEventObserver, VHCustomizer, OverlayCust
(viewHolder.itemView as? ViewGroup?)?.let {
it.addView(it.inflate(R.layout.item_photo_custom_layout))
}
when (type) {
ItemType.SUBSAMPLING -> {
viewHolder.itemView.findViewById<View>(R.id.subsamplingView)?.setOnClickCallback { viewerViewModel?.dismiss() }
}
ItemType.PHOTO -> {
viewHolder.itemView.findViewById<View>(R.id.photoView)?.setOnClickCallback { viewerViewModel?.dismiss() }
}
ItemType.VIDEO -> {
(viewHolder.itemView as? ViewGroup?)?.let {
it.addView(it.inflate(R.layout.item_video_custom_layout))
}
val playerControlView = viewHolder.itemView.findViewById<PlayerControlView>(R.id.playerControlView)
playerControlView?.visibility = if (ViewerHelper.simplePlayVideo) View.GONE else View.VISIBLE

viewHolder.itemView.findViewById<ExoVideoView>(R.id.videoView)?.let {
when (viewHolder) {
is SubsamplingViewHolder -> viewHolder.binding.subsamplingView.setOnClickCallback { viewerViewModel?.dismiss() }
is PhotoViewHolder -> viewHolder.binding.photoView.setOnClickCallback { viewerViewModel?.dismiss() }
is VideoViewHolder -> {
val customBinding = ItemVideoCustomLayoutBinding.inflate(LayoutInflater.from(viewHolder.binding.root.context))
viewHolder.binding.root.addView(customBinding.root)
customBinding.playerControlView.visibility = if (ViewerHelper.simplePlayVideo) View.GONE else View.VISIBLE
viewHolder.binding.videoView.let {
it.setOnClickCallback { toast("video clicked") }
it.setOnLongClickListener {
toast("video long clicked")
Expand All @@ -89,36 +82,39 @@ class SimpleViewerCustomizer : LifecycleEventObserver, VHCustomizer, OverlayCust
val myData = data as MyData
viewHolder.itemView.findViewById<TextView>(R.id.exText).text = myData.desc
viewHolder.itemView.findViewById<View>(R.id.remove).setOnClickListener {
val target = listOf(data)
TestRepository.get().api.asyncDelete(target) {
viewerViewModel?.remove(listOf(data))
testDataViewModel?.remove(listOf(data))
if (ViewerHelper.loadAllAtOnce) {
toast("SimpleDataProvider的写法不支持删除")
Handler(Looper.getMainLooper()).postDelayed({ toast("LoadAllAtOnce(off)支持动态删除功能") } , 500)
}
else {
val target = listOf(data)
Service.api.asyncDelete(target) {
testDataViewModel?.remove(target)
viewerViewModel?.remove(target)
}
}
}
}

override fun provideView(parent: ViewGroup): View {
return parent.inflate(R.layout.layout_indicator).also {
indicatorDecor = it.findViewById(R.id.indicatorDecor)
indicator = it.findViewById(R.id.indicator)
pre = it.findViewById(R.id.pre)
next = it.findViewById(R.id.next)
pre?.setOnClickCallback { viewerViewModel?.setCurrentItem(currentPosition - 1) }
next?.setOnClickCallback { viewerViewModel?.setCurrentItem(currentPosition + 1) }
it.findViewById<View>(R.id.dismiss).setOnClickCallback { viewerViewModel?.dismiss() }
}
return LayoutIndicatorBinding.inflate(LayoutInflater.from(parent.context), parent, false).also {
binding = it
it.pre.setOnClickCallback { viewerViewModel?.setCurrentItem(currentPosition - 1) }
it.next.setOnClickCallback { viewerViewModel?.setCurrentItem(currentPosition + 1) }
it.dismiss.setOnClickCallback { viewerViewModel?.dismiss() }
}.root
}

override fun onRelease(viewHolder: RecyclerView.ViewHolder, view: View) {
viewHolder.itemView.findViewById<View>(R.id.customizeDecor)
?.animate()?.setDuration(200)?.alpha(0f)?.start()
indicatorDecor?.animate()?.setDuration(200)?.alpha(0f)?.start()
binding?.indicatorDecor?.animate()?.setDuration(200)?.alpha(0f)?.start()
release()
}

override fun onPageSelected(position: Int, viewHolder: RecyclerView.ViewHolder) {
currentPosition = position
indicator?.text = position.toString()
binding?.indicator?.text = position.toString()
processSelectVideo(viewHolder)
}

Expand Down Expand Up @@ -170,9 +166,6 @@ class SimpleViewerCustomizer : LifecycleEventObserver, VHCustomizer, OverlayCust
videoTask?.dispose()
videoTask = null
lastVideoVH = null
indicatorDecor = null
indicator = null
pre = null
next = null
binding = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.github.iielse.imageviewer.demo.core.AdapterCallback
import com.github.iielse.imageviewer.demo.core.ITEM_CLICKED
import com.github.iielse.imageviewer.demo.core.viewer.ViewerTransitionHelper
import com.github.iielse.imageviewer.demo.data.MyData
import com.github.iielse.imageviewer.demo.databinding.ItemImageBinding
import com.github.iielse.imageviewer.demo.utils.setOnClickCallback

class TestDataViewHolder(
parent: ViewGroup,
Expand All @@ -18,15 +18,13 @@ class TestDataViewHolder(
ItemImageBinding.inflate(LayoutInflater.from(parent.context), parent, false)
) : RecyclerView.ViewHolder(binding.root) {
init {
// 初始化点击回调
itemView.setOnClickListener {
(it.tag as? MyData?)?.let { callback.invoke(ITEM_CLICKED, it) }
itemView.setOnClickCallback {
callback.invoke(ITEM_CLICKED, itemView.tag) // 初始化点击回调
}
}

fun bind(item: MyData, pos: Int) {
itemView.tag = item

binding.type.text = when {
item.subsampling -> "subsampling"
item.url.endsWith(".gif") -> "gif"
Expand All @@ -36,9 +34,7 @@ class TestDataViewHolder(

// 测试 fitXY 的过渡动画效果
binding.imageView.scaleType = if (pos == 19) ImageView.ScaleType.FIT_XY else ImageView.ScaleType.CENTER_CROP

Glide.with(binding.imageView).load(item.url).into(binding.imageView)

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,16 @@ import androidx.paging.PagedList
import com.github.iielse.imageviewer.demo.core.Cell
import com.github.iielse.imageviewer.demo.data.*

class TestDataViewModelFactory(
) : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
val repository = TestRepository.get()
if (modelClass.isAssignableFrom(TestDataViewModel::class.java)) {
return TestDataViewModel(repository) as T
}
throw IllegalArgumentException("Unknown ViewModel class")
}
}

class TestDataViewModel(
private val repository: TestRepository
) : ViewModel() {
val dataList: LiveData<PagedList<Cell>> = repository.dataList
fun remove(item: List<MyData>) = repository.reduceDelete(item)
fun remove(item: List<MyData>) = repository.localDelete(item)
fun request() = repository.request(true)

class Factory : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return TestDataViewModel( TestRepository.get()) as T
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import com.github.iielse.imageviewer.demo.core.viewer.FullScreenImageViewerDialo
import com.github.iielse.imageviewer.demo.core.viewer.SimpleImageLoader
import com.github.iielse.imageviewer.demo.core.viewer.SimpleTransformer
import com.github.iielse.imageviewer.demo.data.MyData
import com.github.iielse.imageviewer.demo.data.PAGE_SIZE
import com.github.iielse.imageviewer.demo.data.TestRepository
import com.github.iielse.imageviewer.demo.data.Service
import com.github.iielse.imageviewer.demo.utils.PAGE_SIZE

/**
* viewer的自定义初始化方案
Expand All @@ -26,14 +26,14 @@ object ViewerHelper {
// 数据提供者 一次加载 or 分页
fun myDataProvider(clickedData: MyData): DataProvider {
return if (loadAllAtOnce) {
SimpleDataProvider(TestRepository.get().data)
SimpleDataProvider(Service.houMen)
} else {
object : DataProvider {
override fun loadInitial(): List<Photo> = listOf(clickedData)
override fun loadAfter(key: Long, callback: (List<Photo>) -> Unit)
= TestRepository.get().api.asyncQueryAfter(key, PAGE_SIZE, callback)
= Service.api.asyncQueryAfter(key, PAGE_SIZE, callback)
override fun loadBefore(key: Long, callback: (List<Photo>) -> Unit)
= TestRepository.get().api.asyncQueryBefore(key, PAGE_SIZE, callback)
= Service.api.asyncQueryBefore(key, PAGE_SIZE, callback)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const val ID_ERROR = "id_error"
const val ID_MORE_LOADING = "id_more_loading" // 其实可以用 负数int类型的
const val ID_NO_MORE = "id_no_more"
const val PAGE_INITIAL = 1
const val PAGE_RESET = PAGE_INITIAL - 1
const val ITEM_CLICKED = "item_clicked"
typealias AdapterCallback = (action: String, item: Any?) -> Unit

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import androidx.lifecycle.LiveData
import androidx.paging.DataSource
import androidx.paging.PagedList
import androidx.paging.toLiveData
import com.github.iielse.imageviewer.demo.data.PAGE_SIZE
import com.github.iielse.imageviewer.demo.utils.PAGE_SIZE

data class ListState<T>(
val page: Int = PAGE_INITIAL,
Expand Down Expand Up @@ -98,7 +98,7 @@ fun <T> List<T>.safelySubList(
return this.subList(fromIndex, toIndex)
}

fun <Key, Value> DataSource.Factory<Key, Value>.toLiveData(
fun <Key: Any, Value: Any> DataSource.Factory<Key, Value>.toLiveData(
cellId: (Value) -> String,
requestMore: () -> Unit
): LiveData<PagedList<Value>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import kotlin.math.min

abstract class XPageKeyedDataSource : PageKeyedDataSource<Int, Cell>() {
abstract fun totalCount(): Int
abstract fun loadRange(start: Int, count: Int): List<Cell?>
abstract fun loadRange(start: Int, count: Int): List<Cell>

override fun loadInitial(
params: LoadInitialParams<Int>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.iielse.imageviewer.demo.core.viewer

import android.graphics.Color
import android.os.Build
import android.view.Window
import android.view.WindowManager
import androidx.core.content.ContextCompat
Expand Down
Loading

0 comments on commit 52f24c6

Please sign in to comment.