Skip to content

Commit

Permalink
simple data provider support remove
Browse files Browse the repository at this point in the history
  • Loading branch information
iielse committed Aug 8, 2022
1 parent 00fe840 commit de9f65e
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 19 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ dependencies {
implementation 'com.google.android.exoplayer:exoplayer:2.15.0'

implementation project(':imageviewer')
// implementation 'com.github.iielse:imageviewer:2.1.15'
// implementation 'com.github.iielse:imageviewer:2.1.16'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'io.reactivex.rxjava2:rxjava:2.2.19'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
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
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.*
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.RecyclerView
import com.github.iielse.imageviewer.ImageViewerActionViewModel
import com.github.iielse.imageviewer.ImageViewerBuilder
import com.github.iielse.imageviewer.core.*
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.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.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.demo.utils.inflate
import com.github.iielse.imageviewer.demo.utils.lifecycleOwner
import com.github.iielse.imageviewer.demo.utils.setOnClickCallback
import com.github.iielse.imageviewer.demo.utils.toast
import com.github.iielse.imageviewer.utils.Config
import com.github.iielse.imageviewer.viewholders.PhotoViewHolder
import com.github.iielse.imageviewer.viewholders.SubsamplingViewHolder
Expand Down Expand Up @@ -83,8 +90,11 @@ class SimpleViewerCustomizer : LifecycleEventObserver, VHCustomizer, OverlayCust
viewHolder.itemView.findViewById<TextView>(R.id.exText).text = myData.desc
viewHolder.itemView.findViewById<View>(R.id.remove).setOnClickListener {
if (ViewerHelper.loadAllAtOnce) {
toast("SimpleDataProvider的写法不支持删除")
Handler(Looper.getMainLooper()).postDelayed({ toast("LoadAllAtOnce(off)支持动态删除功能") } , 500)
val target = listOf(data)
Service.api.asyncDelete(target) {
testDataViewModel?.remove(target)
viewerViewModel?.remove(target)
}
}
else {
val target = listOf(data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ object Service {
"https://images.unsplash.com/photo-1496963729609-7d408fa580b5?ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60",
"https://images.unsplash.com/photo-1531959870249-9f9b729efcf4?ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60"
)
// private val myData by lazy { listOf(originMyData.toMutableList().first()).toMutableList() }
private val myData by lazy { originMyData.toMutableList() }
val api = Api(myData)

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ ext {
buildToolsVersion= "30.0.3"
minSdkVersion = 21
targetSdkVersion = 30
versionCode = 222
versionName = "2.1.16"
versionCode = 223
versionName = "2.1.17"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package com.github.iielse.imageviewer

import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -32,6 +35,7 @@ open class ImageViewerDialogFragment : BaseDialogFragment() {
private val initKey by lazy { requireDataProvider().loadInitial().first().id() }
private val transformer by lazy { requireTransformer() }
private val adapter by lazy { ImageViewerAdapter(initKey) }
private val taskId = 110
private var submitPagingData = false

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -116,13 +120,18 @@ open class ImageViewerDialogFragment : BaseDialogFragment() {
}

override fun onPageSelected(position: Int) {
if (submitPagingData) return Unit.also { submitPagingData = false }

val currentKey = viewModel.snapshot[position].id()
val holder = binding.viewer.findViewWithKeyTag(R.id.viewer_adapter_item_key, currentKey)
?.getTag(R.id.viewer_adapter_item_holder) as? RecyclerView.ViewHolder?
?: return

if (submitPagingData) {
submitPagingData = false
viewerHandler.removeMessages(taskId)
viewerHandler.sendMessageDelayed(Message.obtain(viewerHandler, taskId, position, 0, holder), Config.VIEWER_FIRST_PAGE_SELECTED_DELAY)
return
}
viewerHandler.removeMessages(taskId)
userCallback.onPageSelected(position, holder)
}
}
Expand All @@ -135,6 +144,7 @@ open class ImageViewerDialogFragment : BaseDialogFragment() {

override fun onDestroyView() {
super.onDestroyView()
viewerHandler.removeMessages(taskId)
adapter.setListener(null)
binding.viewer.unregisterOnPageChangeCallback(pagerCallback)
binding.viewer.adapter = null
Expand All @@ -157,6 +167,14 @@ open class ImageViewerDialogFragment : BaseDialogFragment() {
}
}

private val viewerHandler by lazy {
Handler(Looper.getMainLooper()) {
it.target.removeMessages(it.what)
userCallback.onPageSelected(it.arg1, it.obj as RecyclerView.ViewHolder)
true
}
}

open class Factory {
open fun build(): ImageViewerDialogFragment = ImageViewerDialogFragment()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class Repository {
?: list.find { it.id() > last }
?: return Unit.also { emptyCallback() }
dataList.value = listOf(target)
dataProvider.exclude(exclude, target)
adapter.refresh()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ interface DataProvider {
fun loadInitial(): List<Photo> = emptyList()
fun loadAfter(key: Long, callback: (List<Photo>) -> Unit) {}
fun loadBefore(key: Long, callback: (List<Photo>) -> Unit) {}
fun exclude(exclude: List<Photo>, target: Photo) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,33 @@ import android.os.Looper
import kotlin.math.min

class SimpleDataProvider(
val init: Photo,
val list: List<Photo>
init: Photo,
list: List<Photo>
) : DataProvider {
override fun loadInitial() = listOf(init)
private var _init = init
private var _list = list

override fun loadInitial() = listOf(_init)
override fun loadAfter(key: Long, callback: (List<Photo>) -> Unit) {
val idx = list.indexOfFirst { it.id() == key }
val idx = _list.indexOfFirst { it.id() == key }
val result: List<Photo> = if (idx < 0) emptyList()
else list.subList(idx + 1, list.size)
else _list.subList(idx + 1, _list.size)
Handler(Looper.getMainLooper()).post {
callback(result)
}
}

override fun loadBefore(key: Long, callback: (List<Photo>) -> Unit) {
val idx = list.indexOfFirst { it.id() == key }
val idx = _list.indexOfFirst { it.id() == key }
val result: List<Photo> = if (idx < 0) emptyList()
else list.subList(0, min(idx, list.size))
else _list.subList(0, min(idx, _list.size))
Handler(Looper.getMainLooper()).post {
callback(result)
}
}

override fun exclude(exclude: List<Photo>, target: Photo) {
_init = target
_list = _list.filter { !exclude.contains(it) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ object Config {
var SWIPE_TOUCH_SLOP = 4f
var DISMISS_FRACTION: Float = 0.12f
var TRANSITION_OFFSET_Y = 0
var VIEWER_FIRST_PAGE_SELECTED_DELAY = 300L
}

0 comments on commit de9f65e

Please sign in to comment.