Java Code Examples for android.support.v7.util.DiffUtil#DiffResult

The following examples show how to use android.support.v7.util.DiffUtil#DiffResult . 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: OnlyAdapter.java    From NoAdapter with Apache License 2.0 5 votes vote down vote up
@Override
protected void onPostExecute(DiffUtil.DiffResult diffResult) {
  final OnlyAdapter onlyAdapter = adapterRef.get();
  if (onlyAdapter == null || onlyAdapter.dataVersion != dataVersion) {
    return;
  }
  onlyAdapter.postUpdate(newItems, diffResult);
}
 
Example 2
Source File: RecyclerViewAdapter.java    From journaldev with MIT License 5 votes vote down vote up
public void setData(ArrayList<Model> newData) {

        DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new MyDiffUtilCallBack(newData, data));
        diffResult.dispatchUpdatesTo(this);
        data.clear();
        this.data.addAll(newData);
    }
 
Example 3
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 4
Source File: MainActivity.java    From rxSuggestions with Apache License 2.0 5 votes vote down vote up
void setSuggestions(@NonNull List<String> newStrings) {
    final SuggestionDiff suggestionDiff = new SuggestionDiff(suggestions, newStrings);
    final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(suggestionDiff, true);
    suggestions.clear();
    suggestions.addAll(newStrings);
    diffResult.dispatchUpdatesTo(this);
}
 
Example 5
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 6
Source File: EntityAdapter.java    From homeassist with Apache License 2.0 5 votes vote down vote up
public void updateDisplayList(ArrayList<Entity> newItems) {
    DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new EntityDiffUtilCallback(getDisplayItems(), newItems));

    if (isFilterState()) {
        this.filteredItems = new ArrayList<>();
        this.filteredItems.clear();
        this.filteredItems.addAll(newItems);
    } else {
        this.items.clear();
        this.items.addAll(newItems);
    }
    diffResult.dispatchUpdatesTo(this);
}
 
Example 7
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 8
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 9
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 10
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 11
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 12
Source File: OnlyAdapter.java    From NoAdapter with Apache License 2.0 4 votes vote down vote up
@Override
protected DiffUtil.DiffResult doInBackground(Void... params) {
  return calculateDiff(oldItems, newItems, diffCallback);
}
 
Example 13
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 14
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 15
Source File: OnlyAdapter.java    From NoAdapter with Apache License 2.0 4 votes vote down vote up
private void postUpdate(List<?> newItems, DiffUtil.DiffResult diffResult) {
  items = new ArrayList<>(newItems);
  diffResult.dispatchUpdatesTo(OnlyAdapter.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: DownloadInfoUpdate.java    From IslamicLibraryAndroid with GNU General Public License v3.0 4 votes vote down vote up
public DownloadInfoUpdate(List<DownloadInfo> downloadInfos, DiffUtil.DiffResult diffResult) {
    this.downloadInfos = downloadInfos;
    this.diffResult = diffResult;
    type=TYPE_PROGRESS;
}
 
Example 18
Source File: CommonContract.java    From JReadHub with GNU General Public License v3.0 votes vote down vote up
void onDiffResult(DiffUtil.DiffResult diffResult, List<NewsBean> newData); 
Example 19
Source File: TopicContract.java    From JReadHub with GNU General Public License v3.0 votes vote down vote up
void onDiffResult(DiffUtil.DiffResult diffResult, List<TopicBean> newData); 
Example 20
Source File: RecentMsgContract.java    From Tok-Android with GNU General Public License v3.0 votes vote down vote up
void showRecentMsg(DiffUtil.DiffResult result, List<ConversationItem> conversationList);