Add guard contidion to prevent notifying when inappropriate.

Although the adapter tries to keep in sync with the app status update
manager, there may be times when this is not successful. In such
circumstances, it seems safe to just guard against invalid situations,
rather than trying to assert an error or fall over.

Fixes #922.
This commit is contained in:
Peter Serwylo 2017-04-07 16:05:52 +10:00
parent 42965701a3
commit 9521b9a72c

View File

@ -316,7 +316,7 @@ public class UpdatesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
// After adding the new item to our list (somewhere) we can then look it back up again in
// order to notify the recycler view and scroll to that item.
int positionOfNewApp = 0;
int positionOfNewApp = -1;
for (int i = 0; i < appsToShowStatus.size(); i++) {
if (TextUtils.equals(appsToShowStatus.get(i).status.getUniqueKey(), apkUrl)) {
positionOfNewApp = i;
@ -324,17 +324,19 @@ public class UpdatesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
}
}
notifyItemInserted(positionOfNewApp);
if (positionOfNewApp != -1) {
notifyItemInserted(positionOfNewApp);
if (recyclerView != null) {
recyclerView.smoothScrollToPosition(positionOfNewApp);
if (recyclerView != null) {
recyclerView.smoothScrollToPosition(positionOfNewApp);
}
}
}
private void onAppStatusRemoved(String apkUrl) {
// Find out where the item is in our internal data structure, so that we can remove it and
// also notify the recycler view appropriately.
int positionOfOldApp = 0;
int positionOfOldApp = -1;
for (int i = 0; i < appsToShowStatus.size(); i++) {
if (TextUtils.equals(appsToShowStatus.get(i).status.getUniqueKey(), apkUrl)) {
positionOfOldApp = i;
@ -342,10 +344,12 @@ public class UpdatesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
}
}
appsToShowStatus.remove(positionOfOldApp);
if (positionOfOldApp != -1) {
appsToShowStatus.remove(positionOfOldApp);
populateItems();
notifyItemRemoved(positionOfOldApp);
populateItems();
notifyItemRemoved(positionOfOldApp);
}
}
private final BroadcastReceiver receiverAppStatusChanges = new BroadcastReceiver() {