Specify whether dismissing an item requires a list rebuild or not.

The controller in charge of dismissing an item will have an insight into
whether it will cause a re-query for an existing cursor or not. If a re-query
will occur in response to a `ContentResolver#notifyChange()` invokation (in this
case in response to updating `AppPrefs`), then the `UpdatesAdapter` doesn't
need to rebuild itself yet. If it is a status update, then it should update
the adapter right away.

Seeing as the controller was already returning one thing (a message to be
displayed in a `Toast` and now it also needs to return an opinion on whether
to rebuild the adapter or not, this has been extracted into a value object
which has a message and a rebuild adapter flag.
This commit is contained in:
Peter Serwylo 2017-09-27 23:48:43 +10:00
parent 25897df85b
commit b5ae78cf4d
9 changed files with 67 additions and 29 deletions

View File

@ -26,7 +26,6 @@ import android.widget.ImageButton;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoader;
@ -43,6 +42,7 @@ import org.fdroid.fdroid.installer.ApkCache;
import org.fdroid.fdroid.installer.InstallManagerService; import org.fdroid.fdroid.installer.InstallManagerService;
import org.fdroid.fdroid.installer.Installer; import org.fdroid.fdroid.installer.Installer;
import org.fdroid.fdroid.installer.InstallerFactory; import org.fdroid.fdroid.installer.InstallerFactory;
import org.fdroid.fdroid.views.updates.DismissResult;
import java.io.File; import java.io.File;
import java.util.Iterator; import java.util.Iterator;
@ -203,13 +203,13 @@ public abstract class AppListItemController extends RecyclerView.ViewHolder {
* This mainly exists to keep the API consistent, in that the {@link App} is threaded through to the relevant * This mainly exists to keep the API consistent, in that the {@link App} is threaded through to the relevant
* method with a guarantee that it is not null, rather than every method having to check if it is null or not. * method with a guarantee that it is not null, rather than every method having to check if it is null or not.
*/ */
public final void onDismiss() { @NonNull
public final DismissResult onDismiss() {
if (currentApp != null && canDismiss()) { if (currentApp != null && canDismiss()) {
CharSequence message = onDismissApp(currentApp); return onDismissApp(currentApp);
if (message != null) {
Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
}
} }
return new DismissResult();
} }
/** /**
@ -218,9 +218,9 @@ public abstract class AppListItemController extends RecyclerView.ViewHolder {
* a {@link android.widget.Toast} for a {@link android.widget.Toast#LENGTH_SHORT} time. * a {@link android.widget.Toast} for a {@link android.widget.Toast#LENGTH_SHORT} time.
* @see #canDismiss() This must also be overriden and should return true. * @see #canDismiss() This must also be overriden and should return true.
*/ */
@Nullable @NonNull
protected CharSequence onDismissApp(@NonNull App app) { protected DismissResult onDismissApp(@NonNull App app) {
return null; return new DismissResult();
} }
/** /**

View File

@ -379,7 +379,7 @@ public class MainActivity extends AppCompatActivity implements BottomNavigationB
AppUpdateStatusManager manager = AppUpdateStatusManager.getInstance(context); AppUpdateStatusManager manager = AppUpdateStatusManager.getInstance(context);
String reason = intent.getStringExtra(AppUpdateStatusManager.EXTRA_REASON_FOR_CHANGE); String reason = intent.getStringExtra(AppUpdateStatusManager.EXTRA_REASON_FOR_CHANGE);
switch(intent.getAction()) { switch (intent.getAction()) {
// Apps which are added/removed from the list due to becoming ready to install or a repo being // Apps which are added/removed from the list due to becoming ready to install or a repo being
// disabled both cause us to increase/decrease our badge count respectively. // disabled both cause us to increase/decrease our badge count respectively.
case AppUpdateStatusManager.BROADCAST_APPSTATUS_LIST_CHANGED: case AppUpdateStatusManager.BROADCAST_APPSTATUS_LIST_CHANGED:

View File

@ -0,0 +1,29 @@
package org.fdroid.fdroid.views.updates;
import android.support.annotation.Nullable;
/**
* When dismissing an item from the Updates tab, there is two different things we need to return.
* This is a dumb data object to represent these things, because a method is only allowed to return one thing.
*/
public class DismissResult {
@Nullable
public final CharSequence message;
public final boolean requiresAdapterRefresh;
public DismissResult() {
this(null, false);
}
public DismissResult(boolean requiresAdapterRefresh) {
this(null, requiresAdapterRefresh);
}
public DismissResult(@Nullable CharSequence message, boolean requiresAdapterRefresh) {
this.message = message;
this.requiresAdapterRefresh = requiresAdapterRefresh;
}
}

View File

@ -343,14 +343,9 @@ public class UpdatesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
/** /**
* If an item representing an {@link org.fdroid.fdroid.AppUpdateStatusManager.AppUpdateStatus} is dismissed, * If an item representing an {@link org.fdroid.fdroid.AppUpdateStatusManager.AppUpdateStatus} is dismissed,
* then we should rebuild the list of app statuses and update the adapter. If it was an updateable app which was * then we should rebuild the list of app statuses and update the adapter.
* dismissed, then it is still harmless to rebuild the list of items anyway, but this isn't strictly required
* because the act of marking an app as "Ignored" will trigger the {@link AppProvider} to re-query for a list
* of updateable apps, separate to this method. However, by the time we get here it is a bit tricky to figure
* out which item was dismissed, so for simplicity (and to make the behaviour more deterministic) we always
* rebuild the full updates list.
*/ */
public void onItemDismissed() { public void refreshStatuses() {
onAppStatusRemoved(); onAppStatusRemoved();
} }
} }

View File

@ -1,7 +1,9 @@
package org.fdroid.fdroid.views.updates; package org.fdroid.fdroid.views.updates;
import android.content.Context;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper; import android.support.v7.widget.helper.ItemTouchHelper;
import android.widget.Toast;
import org.fdroid.fdroid.views.apps.AppListItemController; import org.fdroid.fdroid.views.apps.AppListItemController;
import org.fdroid.fdroid.views.updates.items.AppStatusListItemController; import org.fdroid.fdroid.views.updates.items.AppStatusListItemController;
@ -31,9 +33,11 @@ import org.fdroid.fdroid.views.updates.items.UpdateableAppListItemController;
*/ */
public class UpdatesItemTouchCallback extends ItemTouchHelper.Callback { public class UpdatesItemTouchCallback extends ItemTouchHelper.Callback {
private final Context context;
private final UpdatesAdapter adapter; private final UpdatesAdapter adapter;
public UpdatesItemTouchCallback(UpdatesAdapter adapter) { public UpdatesItemTouchCallback(Context context, UpdatesAdapter adapter) {
this.context = context;
this.adapter = adapter; this.adapter = adapter;
} }
@ -58,8 +62,15 @@ public class UpdatesItemTouchCallback extends ItemTouchHelper.Callback {
@Override @Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
AppListItemController controller = (AppListItemController) viewHolder; AppListItemController controller = (AppListItemController) viewHolder;
controller.onDismiss(); DismissResult result = controller.onDismiss();
adapter.onItemDismissed();
if (result.message != null) {
Toast.makeText(context, result.message, Toast.LENGTH_SHORT).show();
}
if (result.requiresAdapterRefresh) {
adapter.refreshStatuses();
}
} }
@Override @Override

View File

@ -29,7 +29,7 @@ public class UpdatesViewBinder {
list.setLayoutManager(new LinearLayoutManager(activity)); list.setLayoutManager(new LinearLayoutManager(activity));
list.setAdapter(adapter); list.setAdapter(adapter);
ItemTouchHelper touchHelper = new ItemTouchHelper(new UpdatesItemTouchCallback(adapter)); ItemTouchHelper touchHelper = new ItemTouchHelper(new UpdatesItemTouchCallback(activity, adapter));
touchHelper.attachToRecyclerView(list); touchHelper.attachToRecyclerView(list);
emptyState = (TextView) view.findViewById(R.id.empty_state); emptyState = (TextView) view.findViewById(R.id.empty_state);

View File

@ -11,6 +11,7 @@ import org.fdroid.fdroid.R;
import org.fdroid.fdroid.data.App; import org.fdroid.fdroid.data.App;
import org.fdroid.fdroid.views.apps.AppListItemController; import org.fdroid.fdroid.views.apps.AppListItemController;
import org.fdroid.fdroid.views.apps.AppListItemState; import org.fdroid.fdroid.views.apps.AppListItemState;
import org.fdroid.fdroid.views.updates.DismissResult;
/** /**
* Shows apps which are: * Shows apps which are:
@ -51,9 +52,9 @@ public class AppStatusListItemController extends AppListItemController {
return true; return true;
} }
@Nullable @NonNull
@Override @Override
protected CharSequence onDismissApp(@NonNull App app) { protected DismissResult onDismissApp(@NonNull App app) {
AppUpdateStatus status = getCurrentStatus(); AppUpdateStatus status = getCurrentStatus();
if (status != null) { if (status != null) {
AppUpdateStatusManager manager = AppUpdateStatusManager.getInstance(activity); AppUpdateStatusManager manager = AppUpdateStatusManager.getInstance(activity);
@ -69,6 +70,6 @@ public class AppStatusListItemController extends AppListItemController {
} }
} }
return null; return new DismissResult(true);
} }
} }

View File

@ -24,6 +24,7 @@ import org.fdroid.fdroid.installer.Installer;
import org.fdroid.fdroid.installer.InstallerService; import org.fdroid.fdroid.installer.InstallerService;
import org.fdroid.fdroid.views.apps.AppListItemController; import org.fdroid.fdroid.views.apps.AppListItemController;
import org.fdroid.fdroid.views.apps.AppListItemState; import org.fdroid.fdroid.views.apps.AppListItemState;
import org.fdroid.fdroid.views.updates.DismissResult;
/** /**
* Tell the user that an app they have installed has a known vulnerability. * Tell the user that an app they have installed has a known vulnerability.
@ -88,9 +89,9 @@ public class KnownVulnAppListItemController extends AppListItemController {
} }
@Override @Override
protected CharSequence onDismissApp(@NonNull App app) { protected DismissResult onDismissApp(@NonNull App app) {
this.ignoreVulnerableApp(app); this.ignoreVulnerableApp(app);
return activity.getString(R.string.app_list__dismiss_vulnerable_app); return new DismissResult(activity.getString(R.string.app_list__dismiss_vulnerable_app), false);
} }
@Override @Override

View File

@ -12,6 +12,7 @@ import org.fdroid.fdroid.data.AppPrefs;
import org.fdroid.fdroid.data.AppPrefsProvider; import org.fdroid.fdroid.data.AppPrefsProvider;
import org.fdroid.fdroid.views.apps.AppListItemController; import org.fdroid.fdroid.views.apps.AppListItemController;
import org.fdroid.fdroid.views.apps.AppListItemState; import org.fdroid.fdroid.views.apps.AppListItemState;
import org.fdroid.fdroid.views.updates.DismissResult;
/** /**
* Very trimmed down list item. Only displays the app icon, name, and a download button. * Very trimmed down list item. Only displays the app icon, name, and a download button.
@ -38,14 +39,14 @@ public class UpdateableAppListItemController extends AppListItemController {
} }
@Override @Override
@Nullable @NonNull
protected CharSequence onDismissApp(@NonNull App app) { protected DismissResult onDismissApp(@NonNull App app) {
AppPrefs prefs = app.getPrefs(activity); AppPrefs prefs = app.getPrefs(activity);
prefs.ignoreThisUpdate = app.suggestedVersionCode; prefs.ignoreThisUpdate = app.suggestedVersionCode;
// The act of updating here will trigger a re-query of the "can update" apps, so no need to do anything else // The act of updating here will trigger a re-query of the "can update" apps, so no need to do anything else
// to update the UI in response to this. // to update the UI in response to this.
AppPrefsProvider.Helper.update(activity, app, prefs); AppPrefsProvider.Helper.update(activity, app, prefs);
return activity.getString(R.string.app_list__dismiss_app_update); return new DismissResult(activity.getString(R.string.app_list__dismiss_app_update), false);
} }
} }