Java Code Examples for android.support.v7.util.DiffUtil#calculateDiff()

The following examples show how to use android.support.v7.util.DiffUtil#calculateDiff() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NotesListAdapter.java    From RoomDb-Sample with Apache License 2.0 5 votes vote down vote up
public void addTasks(List<Note> newNotes) {
    NoteDiffUtil noteDiffUtil = new NoteDiffUtil(notes, newNotes);
    DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(noteDiffUtil);
    notes.clear();
    notes.addAll(newNotes);
    diffResult.dispatchUpdatesTo(this);
}
 
Example 2
Source File: HoverMenu.java    From hover with Apache License 2.0 5 votes vote down vote up
public void notifyMenuChanged() {
    List<Section> oldSections = mSections;
    List<Section> newSections = getSections();
    mSections = newSections;

    if (null != mListUpdateCallback) {
        DiffUtil.Callback diffCallback = new MenuDiffCallback(oldSections, newSections);
        // calculateDiff() can be long-running.  We let it run synchronously because we don't
        // expect many Sections.
        DiffUtil.DiffResult result = DiffUtil.calculateDiff(diffCallback, true);
        result.dispatchUpdatesTo(mListUpdateCallback);
    }
}
 
Example 3
Source File: MaterialsAdapter.java    From settlers-remake with MIT License 5 votes vote down vote up
public void setMaterialStates(List<StockMaterialState> materialStates) {
	if (this.materialStates != null) {
		DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new MaterialsDiffCallback(this.materialStates, materialStates));
		diffResult.dispatchUpdatesTo(this);
	}

	this.materialStates = materialStates;
}
 
Example 4
Source File: ChecklistAdapter.java    From Travel-Mate with MIT License 5 votes vote down vote up
void updateChecklist(List<ChecklistItem> items) {
    ChecklistItemDiffCallback diffCallback = new ChecklistItemDiffCallback(mItems, items);
    DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);
    mItems.clear();
    mItems.addAll(items);
    // this handles all updates, so we don't need manual notifyItem* calls
    diffResult.dispatchUpdatesTo(ChecklistAdapter.this);

    if (mCanAddItems) checkLastItem();
}
 
Example 5
Source File: MixedContentAdapter.java    From Anecdote with Apache License 2.0 5 votes vote down vote up
@Override
public void setData(final List<Anecdote> quotes) {
    if (Looper.myLooper() == Looper.getMainLooper()) {
        DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(
                new AnecdoteListDiffCallback(
                        mAnecdotes,
                        quotes)
        );

        if (!mAnecdotes.isEmpty()) {
            mAnecdotes.clear();
            mAnecdotes.addAll(quotes);
            diffResult.dispatchUpdatesTo(this);
        } else {
            // Prevent recyclerView follow the loading wheel when first items are just added
            mAnecdotes.addAll(quotes);
            this.notifyDataSetChanged();
        }

    } else {
        // Run this on main thread
        Handler mainHandler = new Handler(Looper.getMainLooper());
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                MixedContentAdapter.this.setData(quotes);
            }
        };
        mainHandler.post(runnable);
    }
}
 
Example 6
Source File: OnlyAdapter.java    From NoAdapter with Apache License 2.0 5 votes vote down vote up
@NonNull
private static DiffUtil.DiffResult calculateDiff(
    List<?> oldItems,
    List<?> update,
    DiffCallback diffCallback) {
  return DiffUtil.calculateDiff(new DiffUtilCallback(oldItems, update, diffCallback));
}
 
Example 7
Source File: LogbookActivity.java    From homeassist with Apache License 2.0 5 votes vote down vote up
public void setItems(List<LogSheet> newItems) {
    DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new LogSheetDiffUtilCallback(items, newItems));
    this.items.clear();
    this.items.addAll(newItems);
    diffResult.dispatchUpdatesTo(this);
    //this.items = items;
    //notifyDataSetChanged();
}
 
Example 8
Source File: EntityAdapter.java    From homeassist with Apache License 2.0 5 votes vote down vote up
public void updateFilterList(ArrayList<Entity> newItems) {

        DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new EntityDiffUtilCallback(getDisplayItems(), newItems));

        if (filteredItems == null) {
            filteredItems = new ArrayList<>();
        }
        this.filteredItems.clear();
        this.filteredItems.addAll(newItems);
        diffResult.dispatchUpdatesTo(this);
    }
 
Example 9
Source File: EntityAdapter.java    From homeassist with Apache License 2.0 5 votes vote down vote up
public void clearFilter() {
    if (filteredItems != null) {
        DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new EntityDiffUtilCallback(getDisplayItems(), items));
        filteredItems = null;
        diffResult.dispatchUpdatesTo(this);
    }
}
 
Example 10
Source File: TradeMaterialsAdapter.java    From settlers-remake with MIT License 5 votes vote down vote up
public void setMaterialStates(List<TradeMaterialState> tradeMaterialStates) {
	if (this.tradeMaterialStates != null) {
		DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new MaterialsDiffCallback(this.tradeMaterialStates, tradeMaterialStates));
		diffResult.dispatchUpdatesTo(this);
	}

	this.tradeMaterialStates = tradeMaterialStates;
}
 
Example 11
Source File: EntityAdapter.java    From homeassist with Apache License 2.0 5 votes vote down vote up
public void updateList(ArrayList<Entity> newItems) {
    DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new EntityDiffUtilCallback(getDisplayItems(), newItems));
    this.items.clear();
    this.items.addAll(newItems);

    if (isFilterState()) {
        for (Entity item : items) {
            int filterpos = filteredItems.indexOf(item);
            if (filterpos != -1) {
                filteredItems.set(filterpos, item);
            }
        }
    }
    diffResult.dispatchUpdatesTo(this);
}
 
Example 12
Source File: MoviesAdapter.java    From YTS with MIT License 5 votes vote down vote up
public void updateData(List<BaseMovie.Movie> movieList, boolean newList) {
  if (newList) {
    DiffUtil.DiffResult result = DiffUtil.calculateDiff(
        new MoviesDiffCallback(this.movieList, movieList)
    );
    this.movieList.clear();
    result.dispatchUpdatesTo(this);
  }
  this.movieList.addAll(movieList);
  if (!newList) notifyItemRangeInserted(getItemCount(), this.movieList.size() - 1);
}
 
Example 13
Source File: RecentMsgPresenter.java    From Tok-Android with GNU General Public License v3.0 5 votes vote down vote up
private void updateByCondition(List<ConversationItem> conversationList) {
    if (conversationList != null) {
        DiffUtil.DiffResult result =
            DiffUtil.calculateDiff(new RecentMsgDiff(mCurConversationList, conversationList),
                true);

        mCurConversationList = conversationList;
        mRecentMsgView.showRecentMsg(result, mCurConversationList);
        mRecentMsgView.setEmptyPromptVisible(
            mCurConversationList == null || mCurConversationList.size() == 0);
    }
}
 
Example 14
Source File: BuildingsCategoryFragment.java    From settlers-remake with MIT License 4 votes vote down vote up
void setBuildingViewStates(BuildingViewState[] buildingViewStates) {
	DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new BuildingsDiffCallback(this.buildingViewStates, buildingViewStates));
	diffResult.dispatchUpdatesTo(this);

	this.buildingViewStates = buildingViewStates;
}
 
Example 15
Source File: PeopleAdapter.java    From MaterialMasterDetail with Apache License 2.0 4 votes vote down vote up
public void setPeopleList(List<Person> peopleList) {
    DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new PeopleListDiffCallback(this.peopleList, peopleList));
    this.peopleList.clear();
    this.peopleList.addAll(peopleList);
    diffResult.dispatchUpdatesTo(this);
}
 
Example 16
Source File: DownloadProgressActivity.java    From IslamicLibraryAndroid with GNU General Public License v3.0 4 votes vote down vote up
@Nullable
@Override
protected DownloadInfoUpdate doInBackground(Void... voids) {
    //Convert Long[] to long[]

    while (true) {

        DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        DownloadManager.Query BooksDownloadQuery = new DownloadManager.Query();

        BooksDownloadQuery.setFilterByStatus(DownloadManager.STATUS_RUNNING |
                DownloadManager.STATUS_PAUSED |
                DownloadManager.STATUS_PENDING);

        //Query the download manager about downloads that have been requested.
        Cursor cursor = downloadManager.query(BooksDownloadQuery);
        int progressingDownloadCount = cursor.getCount();

        List<DownloadInfo> progressDownloadInfo = new ArrayList<>();
        if (progressingDownloadCount != 0) {
            for (int i = 0; i < progressingDownloadCount; i++) {
                cursor.moveToPosition(i);
                DownloadInfo downloadInfo = new DownloadInfo(cursor);
                progressDownloadInfo.add(i, downloadInfo);
                Log.d("download_iter", downloadInfo.toString());
            }
            Collections.sort(progressDownloadInfo, (o1, o2) -> {
                if (o1.getProgressPercent() != 0 && o2.getProgressPercent() != 0) {
                    return o1.compareTo(o2);
                } else if (o1.getProgressPercent() != 0 && o2.getProgressPercent() == 0) {
                    return -1;

                } else if (o1.getProgressPercent() == 0 && o2.getProgressPercent() != 0) {
                    return 1;

                } else //(o1.getProgressPercent() == 0 && o2.getProgressPercent() == 0)
                {
                    return o1.compareTo(o2);
                }

            });
        }
        DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(
                new DownloadInfo.DownloadInfoDiffCallback(mOldDownloadList, progressDownloadInfo));
        mOldDownloadList = progressDownloadInfo;
        cursor.close();


        if (progressingDownloadCount != 0) {
            publishProgress(new DownloadInfoUpdate(progressDownloadInfo, diffResult));
        } else {
            return new DownloadInfoUpdate(progressDownloadInfo, diffResult);
        }

        if (isCancelled()) {
            publishProgress(new DownloadInfoUpdate(DownloadInfoUpdate.TYPE_CANCEL));
            DownloadManager.Query non_complete_query = new DownloadManager.Query();
            non_complete_query.setFilterByStatus(DownloadManager.STATUS_FAILED |
                    DownloadManager.STATUS_PENDING |
                    DownloadManager.STATUS_RUNNING);
            Cursor c = downloadManager.query(non_complete_query);
            int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_ID);
            //TODO this loop may cause problems if a download completed and the broadcast is triggered before we cancel it
            while (c.moveToNext()) {
                long enquId = c.getLong(columnIndex);
                downloadManager.remove(enquId);
            }
            DownloadProgressActivity.this.cancelMultipleDownloads(c, columnIndex);
            Intent localIntent =
                    new Intent(BROADCAST_ACTION)
                            .putExtra(EXTRA_NOTIFY_WITHOUT_BOOK_ID, true);
            DownloadProgressActivity.this.sendOrderedBroadcast(localIntent, null);
            c.close();

            return null;
        }
        try {
            Thread.sleep(40);
        } catch (InterruptedException e) {
            Timber.e(e);
        }
    }
}
 
Example 17
Source File: SimpleAdapter.java    From SimpleRecyclerView with Apache License 2.0 4 votes vote down vote up
@Override
public <T extends SimpleCell & Updatable> void addOrUpdateCells(List<T> cells) {
  SimpleDiffCallbackDelegate callbackDelegate = new SimpleDiffCallbackDelegate(this, cells);
  DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(callbackDelegate);
  diffResult.dispatchUpdatesTo(this);
}
 
Example 18
Source File: GoodsInventoryFragment.java    From settlers-remake with MIT License 4 votes vote down vote up
public void setInventoryMaterialStates(InventoryMaterialState[] inventoryMaterialStates) {
	DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new InventoryMaterialsDiffCallback(this.inventoryMaterialStates, inventoryMaterialStates));
	diffResult.dispatchUpdatesTo(this);
	this.inventoryMaterialStates = inventoryMaterialStates;
}
 
Example 19
Source File: RecyclerViewAdapter.java    From pandroid with Apache License 2.0 4 votes vote down vote up
public void addDiff(@NotNull List<? extends T> collection, @NotNull DiffUtil.Callback callback, boolean detectMove) {
    DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(callback, detectMove);
    content.clear();
    content.addAll(collection);
    diffResult.dispatchUpdatesTo(this);
}
 
Example 20
Source File: DiffCallback.java    From Toutiao with Apache License 2.0 4 votes vote down vote up
public static void create(@NonNull Items oldList, @NonNull Items newList, @NonNull MultiTypeAdapter adapter) {
    DiffCallback diffCallback = new DiffCallback(oldList, newList);
    DiffUtil.DiffResult result = DiffUtil.calculateDiff(diffCallback, true);
    result.dispatchUpdatesTo(adapter);
}