Skip to content

Commit

Permalink
Merge pull request #93 from AlanCheen/feature/differ
Browse files Browse the repository at this point in the history
Feature/differ
  • Loading branch information
AlanCheen authored Aug 2, 2022
2 parents d096ed9 + ee8c956 commit e18d3bc
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 86 deletions.
7 changes: 2 additions & 5 deletions app/src/main/java/me/yifeiyuan/flapdev/FlapApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ import androidx.multidex.MultiDexApplication
import me.yifeiyuan.flap.Flap
import me.yifeiyuan.flap.apt.delegates.*
import me.yifeiyuan.flap.hook.ApmHook
import me.yifeiyuan.flap.hook.DebugHelperHook
import me.yifeiyuan.flap.ktmodule.KtModuleComponentDelegate
import me.yifeiyuan.flap.hook.LoggingHook
import me.yifeiyuan.flapdev.components.CustomViewTypeComponentDelegate
import me.yifeiyuan.flapdev.components.generictest.GenericFlapComponentDelegate
import me.yifeiyuan.flapdev.components.SimpleTextComponentDelegate
import me.yifeiyuan.ktx.foundation.othermodule.JavaModuleComponentDelegate
import me.yifeiyuan.ktx.foundation.othermodule.vb.ViewBindingComponentDelegate

/**
* Flap
Expand Down Expand Up @@ -46,7 +43,7 @@ class FlapApplication : MultiDexApplication() {
)

//也是全局
registerAdapterHooks(DebugHelperHook(), ApmHook())
registerAdapterHooks(LoggingHook(), ApmHook())

//打开日志
setDebug(true)
Expand Down
65 changes: 49 additions & 16 deletions app/src/main/java/me/yifeiyuan/flapdev/components/DiffComponent.kt
Original file line number Diff line number Diff line change
@@ -1,38 +1,48 @@
package me.yifeiyuan.flapdev.components

import android.os.SystemClock
import android.view.View
import me.yifeiyuan.flap.Component
import me.yifeiyuan.flap.FlapAdapter
import me.yifeiyuan.flap.annotations.Delegate
import me.yifeiyuan.flap.delegate.AdapterDelegate
import me.yifeiyuan.flap.diff.DiffModel
import me.yifeiyuan.flap.differ.IDiffer
import me.yifeiyuan.flap.ext.bindButton
import me.yifeiyuan.flap.ext.bindTextView
import me.yifeiyuan.flapdev.R

/**
* Created by 程序亦非猿 on 2022/8/1.
*
* 如果都返回 true ,内容修改后再下拉刷新,不会有 onbind 行为
*/

class TestDiffModel(var content: String, var age: Int, var desc: String) : DiffModel {
class TestDiffModel(var content: String, var id: Int, var desc: String) : IDiffer {

override fun areItemsTheSame(other: DiffModel): Boolean {
return false
}

override fun areContentsTheSame(other: DiffModel): Boolean {
return false
override fun areItemsTheSame(newItem: Any): Boolean {
if (newItem.javaClass == TestDiffModel::class.java) {
return id == (newItem as TestDiffModel).id
} else {
return false
}
// return true
}

override fun equals(other: Any?): Boolean {
return super.equals(other)
override fun areContentsTheSame(newItem: Any): Boolean {
if (newItem.javaClass == TestDiffModel::class.java) {
return content == (newItem as TestDiffModel).content
} else {
return false
}
// return true
}

override fun hashCode(): Int {
return super.hashCode()
override fun getChangePayload(newItem: Any): Any? {
return (newItem as TestDiffModel).content
}

override fun toString(): String {
return super.toString()
return "(id=$id,content='$content',desc='$desc')TestDiffModel"
}
}

Expand All @@ -44,14 +54,37 @@ class DiffComponent(view: View) : Component<TestDiffModel>(view) {

override fun onBind(model: TestDiffModel, position: Int, payloads: List<Any>, adapter: FlapAdapter, delegate: AdapterDelegate<*, *>) {

bindTextView(R.id.content){
if (payloads.isNotEmpty()) {
bindTextView(R.id.content){
text = "展示 content :${payloads.get(0)}"
}
return
}

bindTextView(R.id.content) {
text = "展示 content :${model.content}"
}

bindTextView(R.id.id) {
text = "展示 ID :${model.id}"
}

bindTextView(R.id.desc) {
text = "展示 desc :${model.desc}"
}
bindTextView(R.id.age){

bindButton(R.id.modifyId) {
setOnClickListener {
model.id = (SystemClock.uptimeMillis() % 10000).toInt()
adapter.notifyItemChanged(position)
}
}
bindTextView(R.id.content){

bindButton(R.id.modifyContent) {
setOnClickListener {
model.content = "Content:" + (SystemClock.uptimeMillis() % 10000).toInt().toString()
adapter.notifyItemChanged(position)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
package me.yifeiyuan.flapdev.showcase

import android.os.Bundle
import android.os.Handler
import android.view.View
import me.yifeiyuan.flap.FlapAdapter
import me.yifeiyuan.flap.diff.DiffModel
import me.yifeiyuan.flap.diff.FlapDiffAdapter
import me.yifeiyuan.flap.differ.IDiffer
import me.yifeiyuan.flap.differ.FlapDifferAdapter
import me.yifeiyuan.flapdev.components.TestDiffModel
import java.util.ArrayList


/**
*
* 测试说明,点击按钮后,再下拉刷新,只有被修改了的数据才会有刷新动画
*
* Created by 程序亦非猿 on 2022/8/1.
*/
class DiffAdapterTestcase : BaseTestcaseFragment() {

override fun createAdapter(): FlapAdapter {
return FlapDiffAdapter<DiffModel>().apply {
return FlapDifferAdapter<IDiffer>().apply {
setData(createRefreshData())
}
}

override fun onInit(view: View) {
super.onInit(view)
}

override fun createRefreshData(size: Int): MutableList<Any> {
val list = mutableListOf<Any>()
repeat(20){
Expand Down
33 changes: 20 additions & 13 deletions app/src/main/res/layout/component_diff.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,33 @@
android:background="#C1C3CF">

<TextView
android:id="@+id/content"
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginLeft="16dp"
android:gravity="center"
android:text="展示 Content"
android:textColor="#1565C0"
android:gravity="start"
android:text="展示 id"
android:textColor="#9575CD"
android:layout_marginTop="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/age"
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginLeft="16dp"
android:gravity="center"
android:text="展示 age"
android:textColor="#9575CD"
app:layout_constraintLeft_toRightOf="@id/content"
app:layout_constraintTop_toTopOf="parent" />
android:gravity="start"
android:text="展示 Content"
android:textColor="#1565C0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/id" />

<TextView
android:id="@+id/desc"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:gravity="center"
android:gravity="start"
android:text="展示 desc"
android:textColor="#A67145"
app:layout_constraintLeft_toRightOf="@id/content"
Expand All @@ -58,14 +59,20 @@
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/modifyAge"
android:id="@+id/modifyId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改 Age"
android:text="修改 Id"
android:textAllCaps="false"
app:layout_constraintLeft_toRightOf="@id/modifyContent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

<View
android:layout_width="match_parent"
android:layout_height="8dp"
android:background="#FFFFFF"
app:layout_constraintTop_toBottomOf="@id/buttons" />

</androidx.constraintlayout.widget.ConstraintLayout>
21 changes: 0 additions & 21 deletions flap/src/main/java/me/yifeiyuan/flap/diff/DiffModel.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.yifeiyuan.flap.diff
package me.yifeiyuan.flap.differ

import android.annotation.SuppressLint
import androidx.recyclerview.widget.AdapterListUpdateCallback
import androidx.recyclerview.widget.AsyncDifferConfig
import androidx.recyclerview.widget.AsyncListDiffer
Expand All @@ -8,7 +9,7 @@ import me.yifeiyuan.flap.FlapAdapter
import java.util.*

/**
* FlapDiffAdapter supports AsyncListDiffer feature.
* 支持 AsyncListDiffer 更高效的刷新。
*
* Created by 程序亦非猿 on 2021/9/22.
*
Expand All @@ -17,22 +18,32 @@ import java.util.*
* @since 2020/9/22
* @since 3.0
*/
class FlapDiffAdapter<T : DiffModel> : FlapAdapter {
class FlapDifferAdapter<T : Any> : FlapAdapter {

private val differ: AsyncListDiffer<T>

constructor() {
differ = AsyncListDiffer(this, object : ItemCallback<T>() {
override fun areItemsTheSame(oldItem: T, newItem: T): Boolean {
return oldItem.areItemsTheSame(newItem)
if (oldItem is IDiffer) {
return oldItem.areItemsTheSame(newItem)
}
return oldItem.javaClass == newItem.javaClass
}

@SuppressLint("DiffUtilEquals")
override fun areContentsTheSame(oldItem: T, newItem: T): Boolean {
return oldItem.areContentsTheSame(newItem)
if (oldItem is IDiffer) {
return oldItem.areContentsTheSame(newItem)
}
return oldItem.equals(newItem)
}

override fun getChangePayload(oldItem: T, newItem: T): Any? {
return oldItem.getChangePayload(newItem)
if (oldItem is IDiffer) {
return oldItem.getChangePayload(newItem)
}
return null
}
})
}
Expand All @@ -59,15 +70,26 @@ class FlapDiffAdapter<T : DiffModel> : FlapAdapter {
return differ.currentList.size
}

override fun getItemData(position: Int): Any {
return differ.currentList[position] as Any
override fun getItemData(position: Int): T {
return differ.currentList[position]
}

@Deprecated(message = "请使用 submitList", replaceWith = ReplaceWith("submitList(appendDataList)", "me.yifeiyuan.flap.differ.FlapDifferAdapter"))
override fun setData(newDataList: MutableList<Any>) {
val data = ArrayList<T>()
for (o in newDataList) {
data.add(o as T)
}
submitList(data)
}

@Deprecated(message = "请使用 submitList", replaceWith = ReplaceWith("submitList(appendDataList)", "me.yifeiyuan.flap.differ.FlapDifferAdapter"))
override fun appendData(appendDataList: MutableList<Any>) {
val data = ArrayList<T>()
data.addAll(differ.currentList)
appendDataList.forEach {
data.add(it as T)
}
submitList(data)
}
}
48 changes: 48 additions & 0 deletions flap/src/main/java/me/yifeiyuan/flap/differ/IDiffer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package me.yifeiyuan.flap.differ

/**
*
* 实现 IDiffer 接口,可以高效刷新
*
* @see FlapDifferAdapter
* @see androidx.recyclerview.widget.AsyncListDiffer
*
* Created by 程序亦非猿 on 2021/9/22.
*
* Flap Github: <a>https://github.com/AlanCheen/Flap</a>
* @author 程序亦非猿 [Follow me](<a> https://github.com/AlanCheen</a>)
* @since 2020/9/22
* @since 3.0.0
*/
interface IDiffer {

/**
* 是否是同一个 item,如果有 id 之类的属性,就比较它们是否相等
* this.id == newItem.id
*
* 当返回 true,则会继续调用 areContentsTheSame
*
* @see androidx.recyclerview.widget.DiffUtil.ItemCallback.areItemsTheSame
*/
fun areItemsTheSame(newItem: Any): Boolean {
return this.javaClass == newItem.javaClass
}

/**
*
* equals
* 当 areItemsTheSame 返回 true 的时候会调用这个方法
*
* @see androidx.recyclerview.widget.DiffUtil.ItemCallback.areContentsTheSame
*/
fun areContentsTheSame(newItem: Any): Boolean {
return equals(newItem)
}

/**
* 当 areItemsTheSame 返回 true , 并且 areContentsTheSame 返回 false, 就会调用该方法
*
* @see androidx.recyclerview.widget.DiffUtil.ItemCallback.getChangePayload
*/
fun getChangePayload(newItem: Any): Any? = null
}
2 changes: 1 addition & 1 deletion flap/src/main/java/me/yifeiyuan/flap/hook/AdapterHook.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import me.yifeiyuan.flap.FlapAdapter
*
* 内置的一些 AdapterHook 实现:
* @see ApmHook 一个简易的 APM 工具
* @see DebugHelperHook 一个简易的调试工具
* @see LoggingHook 一个简易的调试工具
* @see PreloadHook 实现预取检测功能,可以用做预加载或加载更多功能
*
* Created by 程序亦非猿 on 2021/9/22.
Expand Down
Loading

0 comments on commit e18d3bc

Please sign in to comment.