Skip to content

Commit

Permalink
create diffadapter2
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenceDut committed Jul 24, 2019
1 parent 8f678ca commit 6a07707
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 251 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ dependencies {

implementation 'com.android.support.constraint:constraint-layout:1.1.3'

// api "com.github.silencedut:diffadapter:1.3.8"
// api "com.github.silencedut:diffadapter:2.0.0_alpha"
api "android.arch.lifecycle:extensions:$lifecycle_extensions"
api "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,9 @@ class LOLActivity : AppCompatActivity(){

}

findViewById<View>(R.id.random_add).setOnClickListener { _ ->
findViewById<View>(R.id.random_add).setOnClickListener {
val oneLegend = Transfer.getImpl(ILegendDateProvider::class.java).fetchOneLegends()
diffAdapter.addData(legendViewModel.convertToAdapterData(oneLegend))
diffAdapter.datas.add(0,diffAdapter.datas[(0 until diffAdapter.itemCount-1).random()])
diffAdapter.datas = diffAdapter.datas
}

findViewById<View>(R.id.random_insert).setOnClickListener { _ ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.silencedut.diffadapterdemo

import android.arch.lifecycle.MutableLiveData
import android.arch.lifecycle.ViewModel
import android.os.Bundle
import android.util.Log
import com.silencedut.core.Transfer
import com.silencedut.core.provider.legend.ILegendDateProvider
Expand All @@ -11,7 +10,6 @@ import com.silencedut.core.provider.legend.pojo.*
import com.silencedut.diffadapter.DiffAdapter
import com.silencedut.diffadapter.data.BaseMutableData
import com.silencedut.diffadapter.utils.UpdatePayloadFunction
import com.silencedut.diffadapter.utils.UpdateFunction
import com.silencedut.diffadapterdemo.adapter.LegendViewData
import com.silencedut.diffadapterdemo.adapter.SkinViewData

Expand Down Expand Up @@ -117,17 +115,18 @@ class LegendViewModel : ViewModel(), LegendNotification.LegendInfo, LegendNotifi
})

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

override fun applyChange(input: LegendSkin, originalData: SkinViewData): SkinViewData {
Log.d(TAG, "applyChange legendSkinData $input")
//可以在原对象上修改
override fun applyChange(
input: LegendSkin, originalData: SkinViewData, payloadKeys: MutableSet<String>
): SkinViewData {
originalData.legendSkin = input
return originalData
}

})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
import java.util.Set;

/**
*
* @author SilenceDut
* @date 2018/12/19
*/
class AsyncListUpdateDiffer<T extends BaseMutableData> {
private static final String TAG ="AsyncListUpdateDiffer";
private static final String TAG = "AsyncListUpdateDiffer";
private final ListUpdateCallback mUpdateCallback;
private final AsyncDifferConfig<T> mConfig;
private final ListChangedCallback<T> mListChangedCallback;
Expand All @@ -36,42 +35,48 @@ class AsyncListUpdateDiffer<T extends BaseMutableData> {
static final int DELAY_STEP = 5;
private Handler mDiffHandler;

AsyncListUpdateDiffer(@NonNull DiffAdapter adapter, @NonNull ListChangedCallback<T> listChangedCallback, @NonNull DiffUtil.ItemCallback<T> diffCallback) {
AsyncListUpdateDiffer(@NonNull DiffAdapter adapter, @NonNull ListChangedCallback<T> listChangedCallback,
@NonNull DiffUtil.ItemCallback<T> diffCallback) {
this.mDiffHandler = adapter.mDiffHandler;
this.mUpdateCallback = new AdapterListUpdateCallback(adapter);
this.mConfig = new AsyncDifferConfig.Builder<>(diffCallback).build();
this.mListChangedCallback = listChangedCallback;
updateCurrentList(new ArrayList<T>());
}



void submitList(@Nullable final List<T> newList) {
final long runGeneration = ++this.mMaxScheduledGeneration;
mGenerations.add(runGeneration);
Log.d(TAG,"latchList submitList runGeneration add :"+runGeneration+";;size"+mGenerations.size());
Log.d(TAG, "latchList submitList runGeneration add :" + runGeneration + ";;size" + mGenerations.size());
if (newList != this.mOldList) {
if (newList == null) {
int countRemoved = this.mOldList.size();
syncOldList(null);
updateCurrentList(new ArrayList<T>());
this.mUpdateCallback.onRemoved(0, countRemoved);
mGenerations.remove(runGeneration);
Log.d(TAG,"latchList submitList newList == null runGeneration :"+runGeneration+";;size"+mGenerations.size());
Log.d(TAG, "latchList submitList newList == null runGeneration :" + runGeneration + ";;size" +
mGenerations.size());
} else if (this.mOldList == null) {
syncOldList(newList);
updateSyncTime(newList);
updateCurrentList(new ArrayList<>(newList));
this.mUpdateCallback.onInserted(0, newList.size());
mGenerations.remove(runGeneration);
Log.d(TAG,"latchList submitList mOldList == null runGeneration :"+runGeneration+";;size"+mGenerations.size());
Log.d(TAG, "latchList submitList mOldList == null runGeneration :" + runGeneration + ";;size" +
mGenerations.size());
} else {
doDiff(newList,runGeneration);
doDiff(newList, runGeneration);
}
}
}

private void doDiff(@NonNull final List<T> newList,final long runGeneration) {
private void doDiff(@NonNull final List<T> newList, final long runGeneration) {

if (this.mOldList == null) {
return;
}

final List<T> oldList = new ArrayList<>(this.mOldList);

this.mConfig.getBackgroundThreadExecutor().execute(new Runnable() {
Expand All @@ -82,19 +87,22 @@ public void run() {
public int getOldListSize() {
return oldList.size();
}

@Override
public int getNewListSize() {
return newList.size();
}

@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {

T oldItem = oldList.get(oldItemPosition);
T newItem = newList.get(newItemPosition);
if(oldItem == null || newItem == null) {
if (oldItem == null || newItem == null) {
return false;
}
if(oldItem.getItemViewId()!=newItem.getItemViewId() || oldItem.getClass() != newItem.getClass()) {
if (oldItem.getItemViewId() != newItem.getItemViewId() ||
oldItem.getClass() != newItem.getClass()) {
return false;
}
return AsyncListUpdateDiffer.this.mConfig.getDiffCallback().areItemsTheSame(oldItem, newItem);
Expand All @@ -105,32 +113,37 @@ public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {

T oldItem = oldList.get(oldItemPosition);
T newItem = newList.get(newItemPosition);
if (oldItem != null && newItem != null && oldItem.getClass() == newItem.getClass() ) {
return AsyncListUpdateDiffer.this.mConfig.getDiffCallback().areContentsTheSame(oldItem, newItem);
} else {
if (oldItem != null && newItem != null && oldItem.getClass() == newItem.getClass()) {
return AsyncListUpdateDiffer.this.mConfig.getDiffCallback()
.areContentsTheSame(oldItem, newItem);
} else {
return oldItem == null && newItem == null;
}
}

@Override
@Nullable
public Object getChangePayload(int oldItemPosition, int newItemPosition) {
T oldItem = oldList.get(oldItemPosition);
T newItem = newList.get(newItemPosition);
if (oldItem != null && newItem != null && oldItem.getClass() == newItem.getClass()) {
return AsyncListUpdateDiffer.this.mConfig.getDiffCallback().getChangePayload(oldItem, newItem);
return AsyncListUpdateDiffer.this.mConfig.getDiffCallback()
.getChangePayload(oldItem, newItem);
} else {
return null;
}
}
});
mDiffHandler.post( new Runnable() {
mDiffHandler.post(new Runnable() {
@Override
public void run() {
if (AsyncListUpdateDiffer.this.mMaxScheduledGeneration == runGeneration) {
AsyncListUpdateDiffer.this.latchList(newList, result,runGeneration);
Log.d(TAG,"latchList doDiff runGeneration :"+runGeneration+";;size"+mGenerations.size());
AsyncListUpdateDiffer.this.latchList(newList, result, runGeneration);
Log.d(TAG, "latchList doDiff runGeneration :" + runGeneration + ";;size" +
mGenerations.size());
} else {
Log.d(TAG,"latchList doDiff else runGeneration :"+runGeneration+";;size"+mGenerations.size());
Log.d(TAG, "latchList doDiff else runGeneration :" + runGeneration + ";;size" +
mGenerations.size());
mGenerations.remove(runGeneration);
}
}
Expand All @@ -139,17 +152,18 @@ public void run() {
});
}

private void latchList(@NonNull final List<T> newList, @NonNull final DiffUtil.DiffResult diffResult,final long runGeneration) {
private void latchList(@NonNull final List<T> newList, @NonNull final DiffUtil.DiffResult diffResult,
final long runGeneration) {

long needDelay = mCanSyncTime - SystemClock.elapsedRealtime() ;
if(needDelay <= 0) {
long needDelay = mCanSyncTime - SystemClock.elapsedRealtime();
if (needDelay <= 0) {

syncOldList(newList);
updateSyncTime(newList);
updateCurrentList(new ArrayList<>(newList));
diffResult.dispatchUpdatesTo(AsyncListUpdateDiffer.this.mUpdateCallback);
mGenerations.remove(runGeneration);
Log.d(TAG,"latchList needDelay <= 0 runGeneration :"+runGeneration+";;size"+mGenerations.size());
Log.d(TAG, "latchList needDelay <= 0 runGeneration :" + runGeneration + ";;size" + mGenerations.size());

} else {

Expand All @@ -165,29 +179,28 @@ public void run() {
diffResult.dispatchUpdatesTo(AsyncListUpdateDiffer.this.mUpdateCallback);

}
Log.d(TAG,"latchList else runGeneration :"+runGeneration+";;size"+mGenerations.size());
Log.d(TAG, "latchList else runGeneration :" + runGeneration + ";;size" + mGenerations.size());
mGenerations.remove(runGeneration);
}
}, needDelay );
}, needDelay);
}

}


void updateOldListSize(final @NonNull Runnable listSizeRunnable , final List<T> oldDatas) {
if(mGenerations.size() > 0) {
void updateOldListSize(final @NonNull Runnable listSizeRunnable, final List<T> oldDatas) {
if (mGenerations.size() > 0) {
return;
}

long currentTimeMillis = SystemClock.elapsedRealtime();

if(currentTimeMillis >= mCanSyncTime) {
if (currentTimeMillis >= mCanSyncTime) {

listSizeRunnable.run();
syncOldList(oldDatas);

}else {
final long runGeneration = AsyncListUpdateDiffer.this.mMaxScheduledGeneration;
} else {
final long runGeneration = AsyncListUpdateDiffer.this.mMaxScheduledGeneration;
mDiffHandler.postDelayed(new Runnable() {
@Override
public void run() {
Expand All @@ -198,20 +211,20 @@ public void run() {
syncOldList(oldDatas);
}
}
}, mCanSyncTime - currentTimeMillis );
}, mCanSyncTime - currentTimeMillis);
}
}

private void updateCurrentList(List<T> currentList) {
this.mListChangedCallback.onListChanged(currentList);
}

private void syncOldList(@Nullable List<T> oldData) {
private void syncOldList(@Nullable List<T> oldData) {
this.mOldList = oldData;
}

private void updateSyncTime(@Nullable List<T> oldData) {
mCanSyncTime = SystemClock.elapsedRealtime() + (oldData!=null?oldData.size() * DELAY_STEP:0) ;
private void updateSyncTime(@Nullable List<T> oldData) {
mCanSyncTime = SystemClock.elapsedRealtime() + (oldData != null ? oldData.size() * DELAY_STEP : 0);
}


Expand Down
Loading

0 comments on commit 6a07707

Please sign in to comment.