@@ -2,6 +2,7 @@ package com.silencedut.diffadapterdemo
2
2
3
3
import android.arch.lifecycle.MutableLiveData
4
4
import android.arch.lifecycle.ViewModel
5
+ import android.os.Bundle
5
6
import android.util.Log
6
7
import com.silencedut.core.Transfer
7
8
import com.silencedut.core.provider.legend.ILegendDateProvider
@@ -18,7 +19,7 @@ import com.silencedut.diffadapterdemo.adapter.SkinViewData
18
19
* @author SilenceDut
19
20
* @date 2019/1/17
20
21
*/
21
- class LegendViewModel : ViewModel (), LegendNotification.LegendInfo, LegendNotification.LegendsList{
22
+ class LegendViewModel : ViewModel (), LegendNotification.LegendInfo, LegendNotification.LegendsList {
22
23
23
24
val legendsData = MutableLiveData <List <BaseMutableData <* >>>()
24
25
private val legendBaseInfoData = MutableLiveData <LegendBaseInfo >()
@@ -47,11 +48,11 @@ class LegendViewModel: ViewModel(), LegendNotification.LegendInfo, LegendNotific
47
48
Transfer .getImpl(ILegendDateProvider ::class .java).legendsDataChanged()
48
49
}
49
50
50
- fun updateSkinHolder (legendId : Long ) {
51
+ fun updateSkinHolder (legendId : Long ) {
51
52
Transfer .getImpl(ILegendDateProvider ::class .java).updateLegendSkin(legendId)
52
53
}
53
54
54
- fun updateLegendHolder (legendId : Long ) {
55
+ fun updateLegendHolder (legendId : Long ) {
55
56
Transfer .getImpl(ILegendDateProvider ::class .java).updateLegendNameAndPrice(legendId)
56
57
}
57
58
@@ -61,28 +62,37 @@ class LegendViewModel: ViewModel(), LegendNotification.LegendInfo, LegendNotific
61
62
}
62
63
}
63
64
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))
70
73
}
71
74
}
72
75
73
-
74
76
fun addUpdateMediator (diffAdapter : DiffAdapter ) {
75
77
// 如果变化的数据可能引起多种类型的holder的刷新,UpdateFunction的类型传入基础BaseMutableData<*就行,在applyChange在根据类型进行改变
76
- diffAdapter.addUpdateMediator(legendBaseInfoData, object : UpdateFunction <LegendBaseInfo , BaseMutableData <* >> {
78
+ diffAdapter.addUpdateMediator(legendBaseInfoData, object : UpdatePayloadFunction <LegendBaseInfo , BaseMutableData <* >> {
77
79
override fun providerMatchFeature (input : LegendBaseInfo ): Any {
78
80
return input.id
79
81
}
80
82
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
+ }
86
96
else -> {
87
97
originalData
88
98
}
@@ -91,7 +101,7 @@ class LegendViewModel: ViewModel(), LegendNotification.LegendInfo, LegendNotific
91
101
})
92
102
93
103
// 如果变化的数据只需要特定类型的Holder刷新,类型即可指定
94
- diffAdapter.addUpdateMediator(legendPriceData, object : UpdatePayloadFunction <LegendPrice , LegendViewData >() {
104
+ diffAdapter.addUpdateMediator(legendPriceData, object : UpdatePayloadFunction <LegendPrice , LegendViewData > {
95
105
override fun applyChange (
96
106
input : LegendPrice , originalData : LegendViewData , payloadKeys : MutableSet <String >
97
107
): LegendViewData {
@@ -101,38 +111,38 @@ class LegendViewModel: ViewModel(), LegendNotification.LegendInfo, LegendNotific
101
111
}
102
112
103
113
override fun providerMatchFeature (input : LegendPrice ): Any {
114
+ Log .d(TAG , " providerMatchFeature ${input.id} " )
104
115
return input.id
105
116
}
106
117
})
107
118
108
119
// 如果变化的数据只需要特定类型的Holder刷新,类型即可指定
109
- diffAdapter.addUpdateMediator(legendSkinData, object : UpdateFunction <LegendSkin , SkinViewData > {
120
+ diffAdapter.addUpdateMediator(legendSkinData, object : UpdateFunction <LegendSkin , SkinViewData > {
110
121
override fun providerMatchFeature (input : LegendSkin ): Any {
111
122
return input.id
112
123
}
113
124
114
125
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
119
130
}
120
131
})
121
132
}
122
133
123
134
override fun onLegendBaseInfoFetched (legendBaseInfo : LegendBaseInfo ) {
124
- Log .d(TAG ," onLegendBaseInfoFetched $legendBaseInfo " )
135
+ Log .d(TAG , " onLegendBaseInfoFetched $legendBaseInfo " )
125
136
legendBaseInfoData.value = legendBaseInfo
126
137
}
127
138
128
139
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
132
142
}
133
143
134
144
override fun onLegendSkinsFetched (legendSkin : LegendSkin ) {
135
- Log .d(TAG ," onLegendSkinsFetched $legendSkin " )
145
+ Log .d(TAG , " onLegendSkinsFetched $legendSkin " )
136
146
legendSkinData.value = legendSkin
137
147
}
138
148
}
0 commit comments