Skip to content

Commit f307330

Browse files
author
liushuai1
committed
refactoring payload update way
1 parent fd50a2a commit f307330

File tree

9 files changed

+231
-114
lines changed

9 files changed

+231
-114
lines changed

app/src/main/java/com/silencedut/diffadapterdemo/LOLActivity.kt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,7 @@ class LOLActivity : AppCompatActivity(){
6161
if (it != null) {
6262
val list = mutableListOf<BaseMutableData<*>>()
6363
list.addAll(it)
64-
list.addAll(it)
65-
list.addAll(it)
66-
list.addAll(it)
67-
list.addAll(it)
68-
list.addAll(it)
69-
list.addAll(it)
70-
list.addAll(it)
64+
7165
diffAdapter.datas = list
7266
}
7367

app/src/main/java/com/silencedut/diffadapterdemo/LegendViewModel.kt

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.silencedut.diffadapterdemo
22

33
import android.arch.lifecycle.MutableLiveData
44
import android.arch.lifecycle.ViewModel
5+
import android.os.Bundle
56
import android.util.Log
67
import com.silencedut.core.Transfer
78
import com.silencedut.core.provider.legend.ILegendDateProvider
@@ -18,7 +19,7 @@ import com.silencedut.diffadapterdemo.adapter.SkinViewData
1819
* @author SilenceDut
1920
* @date 2019/1/17
2021
*/
21-
class LegendViewModel: ViewModel(), LegendNotification.LegendInfo, LegendNotification.LegendsList{
22+
class LegendViewModel : ViewModel(), LegendNotification.LegendInfo, LegendNotification.LegendsList {
2223

2324
val legendsData = MutableLiveData<List<BaseMutableData<*>>>()
2425
private val legendBaseInfoData = MutableLiveData<LegendBaseInfo>()
@@ -47,11 +48,11 @@ class LegendViewModel: ViewModel(), LegendNotification.LegendInfo, LegendNotific
4748
Transfer.getImpl(ILegendDateProvider::class.java).legendsDataChanged()
4849
}
4950

50-
fun updateSkinHolder(legendId : Long) {
51+
fun updateSkinHolder(legendId: Long) {
5152
Transfer.getImpl(ILegendDateProvider::class.java).updateLegendSkin(legendId)
5253
}
5354

54-
fun updateLegendHolder(legendId : Long) {
55+
fun updateLegendHolder(legendId: Long) {
5556
Transfer.getImpl(ILegendDateProvider::class.java).updateLegendNameAndPrice(legendId)
5657
}
5758

@@ -61,28 +62,37 @@ class LegendViewModel: ViewModel(), LegendNotification.LegendInfo, LegendNotific
6162
}
6263
}
6364

64-
fun convertToAdapterData(legend: Legend) : BaseMutableData<*> {
65-
return when(legend.type) {
66-
Type.LEGEND -> LegendViewData(legend.id, Transfer.getImpl(ILegendDateProvider::class.java).baseLegendData(legend.id)
67-
, Transfer.getImpl(ILegendDateProvider::class.java).legendPrice(legend.id)?.price)
68-
Type.SKIN -> SkinViewData(legend.id, Transfer.getImpl(ILegendDateProvider::class.java).baseLegendData(legend.id)?.iconUrl
69-
, Transfer.getImpl(ILegendDateProvider::class.java).legendSkin(legend.id))
65+
fun convertToAdapterData(legend: Legend): BaseMutableData<*> {
66+
return when (legend.type) {
67+
Type.LEGEND -> LegendViewData(legend.id,
68+
Transfer.getImpl(ILegendDateProvider::class.java).baseLegendData(legend.id)
69+
, Transfer.getImpl(ILegendDateProvider::class.java).legendPrice(legend.id)?.price)
70+
Type.SKIN -> SkinViewData(legend.id,
71+
Transfer.getImpl(ILegendDateProvider::class.java).baseLegendData(legend.id)?.iconUrl
72+
, Transfer.getImpl(ILegendDateProvider::class.java).legendSkin(legend.id))
7073
}
7174
}
7275

73-
7476
fun addUpdateMediator(diffAdapter: DiffAdapter) {
7577
//如果变化的数据可能引起多种类型的holder的刷新,UpdateFunction的类型传入基础BaseMutableData<*就行,在applyChange在根据类型进行改变
76-
diffAdapter.addUpdateMediator(legendBaseInfoData, object : UpdateFunction<LegendBaseInfo, BaseMutableData<*>> {
78+
diffAdapter.addUpdateMediator(legendBaseInfoData, object : UpdatePayloadFunction<LegendBaseInfo, BaseMutableData<*>> {
7779
override fun providerMatchFeature(input: LegendBaseInfo): Any {
7880
return input.id
7981
}
8082

81-
override fun applyChange(input: LegendBaseInfo, originalData: BaseMutableData<*>): BaseMutableData<*> {
82-
Log.d(TAG,"applyChange $input")
83-
return when(originalData) {
84-
is LegendViewData -> LegendViewData(originalData.id, input, originalData.price)
85-
is SkinViewData -> SkinViewData(originalData.id, input.iconUrl, originalData.legendSkin)
83+
override fun applyChange(input: LegendBaseInfo, originalData: BaseMutableData<*>, payloadKeys: MutableSet<String>): BaseMutableData<*> {
84+
Log.d(TAG, "applyChange $input")
85+
return when (originalData) {
86+
is LegendViewData -> {
87+
originalData.legendBaseInfo = input
88+
payloadKeys.add(LegendViewData.KEY_BASE_INFO)
89+
originalData
90+
}
91+
is SkinViewData -> { //不使用payload方式更新
92+
93+
originalData.legendIcon = input.iconUrl
94+
originalData
95+
}
8696
else -> {
8797
originalData
8898
}
@@ -91,7 +101,7 @@ class LegendViewModel: ViewModel(), LegendNotification.LegendInfo, LegendNotific
91101
})
92102

93103
//如果变化的数据只需要特定类型的Holder刷新,类型即可指定
94-
diffAdapter.addUpdateMediator(legendPriceData, object : UpdatePayloadFunction<LegendPrice, LegendViewData>() {
104+
diffAdapter.addUpdateMediator(legendPriceData, object : UpdatePayloadFunction<LegendPrice, LegendViewData> {
95105
override fun applyChange(
96106
input: LegendPrice, originalData: LegendViewData, payloadKeys: MutableSet<String>
97107
): LegendViewData {
@@ -101,38 +111,38 @@ class LegendViewModel: ViewModel(), LegendNotification.LegendInfo, LegendNotific
101111
}
102112

103113
override fun providerMatchFeature(input: LegendPrice): Any {
114+
Log.d(TAG, "providerMatchFeature ${input.id}")
104115
return input.id
105116
}
106117
})
107118

108119
//如果变化的数据只需要特定类型的Holder刷新,类型即可指定
109-
diffAdapter.addUpdateMediator(legendSkinData, object : UpdateFunction<LegendSkin, SkinViewData> {
120+
diffAdapter.addUpdateMediator(legendSkinData, object : UpdateFunction<LegendSkin, SkinViewData> {
110121
override fun providerMatchFeature(input: LegendSkin): Any {
111122
return input.id
112123
}
113124

114125
override fun applyChange(input: LegendSkin, originalData: SkinViewData): SkinViewData {
115-
Log.d(TAG,"applyChange legendSkinData $input")
116-
//可以在原对象上修改
117-
originalData.legendSkin = input
118-
return originalData
126+
Log.d(TAG, "applyChange legendSkinData $input")
127+
//可以在原对象上修改
128+
originalData.legendSkin = input
129+
return originalData
119130
}
120131
})
121132
}
122133

123134
override fun onLegendBaseInfoFetched(legendBaseInfo: LegendBaseInfo) {
124-
Log.d(TAG,"onLegendBaseInfoFetched $legendBaseInfo")
135+
Log.d(TAG, "onLegendBaseInfoFetched $legendBaseInfo")
125136
legendBaseInfoData.value = legendBaseInfo
126137
}
127138

128139
override fun onLegendPriceFetched(legendPrice: LegendPrice) {
129-
Log.d(TAG,"onLegendPriceFetched $legendPrice")
130-
legendPriceData.postValue(legendPrice)
131-
140+
Log.d(TAG, "onLegendPriceFetched $legendPrice")
141+
legendPriceData.value = legendPrice
132142
}
133143

134144
override fun onLegendSkinsFetched(legendSkin: LegendSkin) {
135-
Log.d(TAG,"onLegendSkinsFetched $legendSkin")
145+
Log.d(TAG, "onLegendSkinsFetched $legendSkin")
136146
legendSkinData.value = legendSkin
137147
}
138148
}

app/src/main/java/com/silencedut/diffadapterdemo/adapter/LegendHolder.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,15 @@ class LegendHolder(itemViewRoot: View, recyclerAdapter: DiffAdapter): BaseDiffVi
6161
/**
6262
* 最高效的更新方式,如果不是频繁更新的可以不实现这个方法
6363
*/
64-
override fun updatePartWithPayload(data: LegendViewData, payload: Bundle, position: Int) {
6564

66-
Log.d(TAG,"position :"+position+"updatePartWithPayload payload :"+payload.getString(LegendViewData.KEY_BASE_INFO)+",price : "+payload.getString(LegendViewData.KEY_PRICE)+",data:"+data)
67-
if(payload.getString(LegendViewData.KEY_BASE_INFO)!=null) {
65+
66+
override fun updatePartWithPayload(newData: LegendViewData?, payloadKeys: MutableSet<String>, position: Int) {
67+
super.updatePartWithPayload(newData, payloadKeys, position)
68+
Log.d(TAG,"position :"+position+"updatePartWithPayloadKey payload :,data:"+payloadKeys)
69+
if(payloadKeys.contains(LegendViewData.KEY_BASE_INFO)) {
6870
updateBaseInfo(data)
6971
}
70-
if(payload.getString(LegendViewData.KEY_PRICE)!=null) {
72+
if(payloadKeys.contains(LegendViewData.KEY_PRICE)) {
7173
updatePrice(data)
7274
}
7375
}

app/src/main/java/com/silencedut/diffadapterdemo/adapter/LegendViewData.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.silencedut.diffadapterdemo.adapter
22

33
import android.os.Bundle
4+
import android.util.Log
45
import com.silencedut.core.provider.legend.pojo.LegendBaseInfo
56
import com.silencedut.diffadapter.data.BaseMutableData
67
import com.silencedut.diffadapterdemo.R
@@ -33,11 +34,15 @@ data class LegendViewData(var id:Long, var legendBaseInfo: LegendBaseInfo?, var
3334
*/
3435
override fun appendDiffPayload(newData: LegendViewData, diffPayloadBundle: Bundle) {
3536
super.appendDiffPayload(newData, diffPayloadBundle)
37+
38+
Log.d("LegendViewData","appendDiffPayload:"+newData)
3639
if(this.legendBaseInfo != newData.legendBaseInfo) {
3740
diffPayloadBundle.putString(KEY_BASE_INFO, KEY_BASE_INFO)
41+
Log.d("LegendViewData","appendDiffPayload"+KEY_BASE_INFO)
3842
}
3943
if(this.price != newData.price) {
4044
diffPayloadBundle.putString(KEY_PRICE, KEY_PRICE)
45+
Log.d("LegendViewData","appendDiffPayload"+KEY_PRICE)
4146
}
4247

4348
}

0 commit comments

Comments
 (0)