Skip to content

Commit

Permalink
[Refactor] Replace notifyDataSetChange with compatible alternatives
Browse files Browse the repository at this point in the history
Signed-off-by: Muntashir Al-Islam <[email protected]>
  • Loading branch information
MuntashirAkon committed Mar 26, 2024
1 parent 316b5f4 commit d8ba865
Show file tree
Hide file tree
Showing 43 changed files with 182 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
WhatsNewDialogViewModel viewModel = new ViewModelProvider(this).get(WhatsNewDialogViewModel.class);
mNewPkgInfo = Objects.requireNonNull(BundleCompat.getParcelable(requireArguments(), ARG_NEW_PKG_INFO, PackageInfo.class));
mOldPkgInfo = Objects.requireNonNull(BundleCompat.getParcelable(requireArguments(), ARG_OLD_PKG_INFO, PackageInfo.class));
RecyclerView recyclerView = (RecyclerView) mDialogView;
recyclerView.setHasFixedSize(true);
RecyclerView recyclerView = mDialogView.findViewById(android.R.id.list);
recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
mAdapter = new WhatsNewRecyclerAdapter(requireContext(), mNewPkgInfo.packageName);
recyclerView.setAdapter(mAdapter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
WhatsNewDialogViewModel viewModel = new ViewModelProvider(this).get(WhatsNewDialogViewModel.class);
PackageInfo newPkgInfo = Objects.requireNonNull(BundleCompat.getParcelable(requireArguments(), ARG_NEW_PKG_INFO, PackageInfo.class));
PackageInfo oldPkgInfo = Objects.requireNonNull(BundleCompat.getParcelable(requireArguments(), ARG_OLD_PKG_INFO, PackageInfo.class));
RecyclerView recyclerView = (RecyclerView) view;
recyclerView.setHasFixedSize(true);
RecyclerView recyclerView = view.findViewById(android.R.id.list);
recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
WhatsNewRecyclerAdapter adapter = new WhatsNewRecyclerAdapter(requireContext(), newPkgInfo.packageName);
recyclerView.setAdapter(adapter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.List;

import io.github.muntashirakon.AppManager.R;
import io.github.muntashirakon.util.AdapterUtils;
import io.github.muntashirakon.AppManager.utils.UIUtils;
import io.github.muntashirakon.AppManager.utils.appearance.ColorCodes;
import io.github.muntashirakon.widget.RecyclerView;
Expand All @@ -41,9 +42,7 @@ class WhatsNewRecyclerAdapter extends RecyclerView.Adapter<WhatsNewRecyclerAdapt
}

void setAdapterList(List<ApkWhatsNewFinder.Change> list) {
mAdapterList.clear();
mAdapterList.addAll(list);
notifyDataSetChanged();
AdapterUtils.notifyDataSetChanged(this, mAdapterList, list);
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public FlagsAdapter(@NonNull Context context, @BackupFlags.BackupFlag int flags,
mSupportedBackupFlagNames = BackupFlags.getFormattedFlagNames(context, mSupportedBackupFlags);
mSelectedFlags = flags;
mDisabledFlags = disabledFlags;
notifyDataSetChanged();
notifyItemRangeInserted(0, mSupportedBackupFlags.size());
}

public int getSelectedFlags() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,12 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
if (metadata.backupFile != null) {
try {
metadata.backupFile.freeze();
++adapter.mFrozenBackupSelectionCount;
} catch (IOException ignore) {
}
}
}
adapter.notifyDataSetChanged();
adapter.notifyItemRangeChanged(0, adapter.getItemCount());
return true;
});
unfreezeMenuItem.setOnMenuItemClickListener(item -> {
Expand All @@ -101,11 +102,12 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
if (metadata.backupFile != null) {
try {
metadata.backupFile.unfreeze();
--adapter.mFrozenBackupSelectionCount;
} catch (IOException ignore) {
}
}
}
adapter.notifyDataSetChanged();
adapter.notifyItemRangeChanged(0, adapter.getItemCount());
return true;
});
popupMenu.show();
Expand Down Expand Up @@ -198,7 +200,7 @@ public BackupAdapter(@NonNull Context context, @NonNull List<MetadataManager.Met
mSelectionListener.onSelectionChanged(backup, mSelectedPositions.size(), true);
}
}
notifyDataSetChanged();
notifyItemRangeInserted(0, mBackups.size());
}

public int selectionCount() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ private void handleIntent(@NonNull Intent intent) {
ArrayList<CharSequence> packageLabels = PackageUtils.packagesToAppLabels(getPackageManager(), mFailedPackages, mUserIds);
RecyclerAdapter adapter = new RecyclerAdapter(packageLabels);
mRecyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
if (packageLabels != null) {
adapter.notifyItemRangeInserted(0, packageLabels.size());
}
mLogViewer.setText(getFormattedLogs(BatchOpsLogger.getAllLogs()));
intent.removeExtra(BatchOpsService.EXTRA_FAILED_PKG);
intent.removeExtra(BatchOpsService.EXTRA_OP_USERS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.List;

import io.github.muntashirakon.AppManager.R;
import io.github.muntashirakon.util.AdapterUtils;
import io.github.muntashirakon.util.UiUtils;


Expand All @@ -37,9 +38,7 @@ public ChangelogRecyclerAdapter() {

public void setAdapterList(@NonNull List<ChangelogItem> list) {
synchronized (mAdapterList) {
mAdapterList.clear();
mAdapterList.addAll(list);
notifyDataSetChanged();
AdapterUtils.notifyDataSetChanged(this, mAdapterList, list);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import io.github.muntashirakon.AppManager.StaticDataset;
import io.github.muntashirakon.AppManager.db.utils.AppDb;
import io.github.muntashirakon.AppManager.details.AppDetailsActivity;
import io.github.muntashirakon.util.AdapterUtils;
import io.github.muntashirakon.AppManager.utils.ThreadUtils;
import io.github.muntashirakon.AppManager.utils.UIUtils;
import io.github.muntashirakon.AppManager.utils.appearance.ColorCodes;
Expand Down Expand Up @@ -254,9 +255,7 @@ public SuggestionsAdapter() {
}

public void setList(@NonNull List<SuggestionObject> suggestions) {
mSuggestions.clear();
mSuggestions.addAll(suggestions);
notifyDataSetChanged();
AdapterUtils.notifyDataSetChanged(this, mSuggestions, suggestions);
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import io.github.muntashirakon.AppManager.R;
import io.github.muntashirakon.AppManager.utils.appearance.ColorCodes;
import io.github.muntashirakon.util.AdapterUtils;
import io.github.muntashirakon.widget.MultiSelectionView;

public class DebloaterRecyclerViewAdapter extends MultiSelectionView.Adapter<DebloaterRecyclerViewAdapter.ViewHolder> {
Expand Down Expand Up @@ -64,8 +65,9 @@ public DebloaterRecyclerViewAdapter(DebloaterActivity activity) {

public void setAdapterList(List<DebloatObject> adapterList) {
synchronized (mLock) {
int previousCount = mAdapterList.size();
mAdapterList = adapterList;
notifyDataSetChanged();
AdapterUtils.notifyDataSetChanged(this, previousCount, mAdapterList.size());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import io.github.muntashirakon.AppManager.settings.Prefs;
import io.github.muntashirakon.AppManager.shortcut.CreateShortcutDialogFragment;
import io.github.muntashirakon.AppManager.types.UserPackagePair;
import io.github.muntashirakon.util.AdapterUtils;
import io.github.muntashirakon.AppManager.utils.ContextUtils;
import io.github.muntashirakon.AppManager.utils.ThreadUtils;
import io.github.muntashirakon.AppManager.utils.UIUtils;
Expand Down Expand Up @@ -342,26 +343,11 @@ void setDefaultList(@NonNull List<AppDetailsItem<?>> list) {
mConstraint = null;
mUserId = UserHandleHidden.myUserId();
}
int previousSize;
int currentSize;
synchronized (mAdapterList) {
previousSize = mAdapterList.size();
mAdapterList.clear();
mAdapterList.addAll(list);
currentSize = mAdapterList.size();
}
ThreadUtils.postOnMainThread(() -> {
if (isDetached()) return;
ProgressIndicatorCompat.setVisibility(progressIndicator, false);
synchronized (mAdapterList) {
if (previousSize != 0) {
notifyItemRangeChanged(0, previousSize);
}
if (previousSize < currentSize) {
notifyItemRangeInserted(previousSize, currentSize - previousSize);
} else if (previousSize > currentSize) {
notifyItemRangeRemoved(currentSize, previousSize - currentSize);
}
AdapterUtils.notifyDataSetChanged(this, mAdapterList, list);
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import io.github.muntashirakon.AppManager.details.struct.AppDetailsFeatureItem;
import io.github.muntashirakon.AppManager.details.struct.AppDetailsItem;
import io.github.muntashirakon.AppManager.details.struct.AppDetailsLibraryItem;
import io.github.muntashirakon.util.AdapterUtils;
import io.github.muntashirakon.AppManager.utils.PackageUtils;
import io.github.muntashirakon.AppManager.utils.ThreadUtils;
import io.github.muntashirakon.AppManager.utils.UIUtils;
Expand Down Expand Up @@ -177,24 +178,11 @@ private class AppDetailsRecyclerAdapter extends RecyclerView.Adapter<AppDetailsR
void setDefaultList(@NonNull List<AppDetailsItem<?>> list) {
ThreadUtils.postOnBackgroundThread(() -> {
mRequestedProperty = mNeededProperty;
int previousSize = mAdapterList.size();
synchronized (mAdapterList) {
mAdapterList.clear();
mAdapterList.addAll(list);
}
int currentSize = mAdapterList.size();
ThreadUtils.postOnMainThread(() -> {
if (isDetached()) return;
ProgressIndicatorCompat.setVisibility(progressIndicator, false);
synchronized (mAdapterList) {
if (previousSize != 0) {
notifyItemRangeChanged(0, previousSize);
}
if (previousSize < currentSize) {
notifyItemRangeInserted(previousSize, currentSize - previousSize);
} else if (previousSize > currentSize) {
notifyItemRangeRemoved(currentSize, previousSize - currentSize);
}
AdapterUtils.notifyDataSetChanged(this, mAdapterList, list);
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import io.github.muntashirakon.AppManager.self.imagecache.ImageLoader;
import io.github.muntashirakon.AppManager.self.pref.TipsPrefs;
import io.github.muntashirakon.AppManager.settings.Prefs;
import io.github.muntashirakon.util.AdapterUtils;
import io.github.muntashirakon.AppManager.utils.DateUtils;
import io.github.muntashirakon.AppManager.utils.ExUtils;
import io.github.muntashirakon.AppManager.utils.LangUtils;
Expand Down Expand Up @@ -397,24 +398,11 @@ void setDefaultList(@NonNull List<AppDetailsItem<?>> list) {
mRequestedProperty = mNeededProperty;
mConstraint = viewModel == null ? null : viewModel.getSearchQuery();
mCanModifyAppOpMode = SelfPermissions.canModifyAppOpMode();
int previousSize = mAdapterList.size();
synchronized (mAdapterList) {
mAdapterList.clear();
mAdapterList.addAll(list);
}
int currentSize = mAdapterList.size();
ThreadUtils.postOnMainThread(() -> {
if (isDetached()) return;
ProgressIndicatorCompat.setVisibility(progressIndicator, false);
synchronized (mAdapterList) {
if (previousSize != 0) {
notifyItemRangeChanged(0, previousSize);
}
if (previousSize < currentSize) {
notifyItemRangeInserted(previousSize, currentSize - previousSize);
} else if (previousSize > currentSize) {
notifyItemRangeRemoved(currentSize, previousSize - currentSize);
}
AdapterUtils.notifyDataSetChanged(this, mAdapterList, list);
}
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.List;

import io.github.muntashirakon.AppManager.R;
import io.github.muntashirakon.util.AdapterUtils;

import static io.github.muntashirakon.AppManager.details.info.ListItem.LIST_ITEM_GROUP_BEGIN;
import static io.github.muntashirakon.AppManager.details.info.ListItem.LIST_ITEM_INLINE;
Expand All @@ -36,9 +37,7 @@ class AppInfoRecyclerAdapter extends RecyclerView.Adapter<AppInfoRecyclerAdapter
}

void setAdapterList(@NonNull List<ListItem> list) {
mAdapterList.clear();
mAdapterList.addAll(list);
notifyDataSetChanged();
AdapterUtils.notifyDataSetChanged(this, mAdapterList, list);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import io.github.muntashirakon.AppManager.R;
import io.github.muntashirakon.AppManager.filters.options.FilterOption;
import io.github.muntashirakon.AppManager.filters.options.FilterOptions;
import io.github.muntashirakon.util.AdapterUtils;
import io.github.muntashirakon.AppManager.utils.ContextUtils;
import io.github.muntashirakon.AppManager.utils.DateUtils;
import io.github.muntashirakon.adapters.SelectedArrayAdapter;
Expand Down Expand Up @@ -395,9 +396,7 @@ public FilterOptionFlagsAdapter(@LayoutRes int layoutId, View.OnClickListener it

public void setFlagMap(@NonNull Map<Integer, CharSequence> flagMap) {
mFlagMap = flagMap;
mFlags.clear();
mFlags.addAll(flagMap.keySet());
notifyItemRangeChanged(0, mFlags.size());
AdapterUtils.notifyDataSetChanged(this, mFlags, flagMap.keySet());
}

public void setFlag(int flag) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@

import io.github.muntashirakon.AppManager.R;
import io.github.muntashirakon.AppManager.self.imagecache.ImageLoader;
import io.github.muntashirakon.util.AdapterUtils;

public class FinderAdapter extends RecyclerView.Adapter<FinderAdapter.ViewHolder> {
private final List<FilterItem.FilteredItemInfo> mAdapterList = new ArrayList<>();

@UiThread
public void setDefaultList(List<FilterItem.FilteredItemInfo> list) {
synchronized (mAdapterList) {
mAdapterList.clear();
mAdapterList.addAll(list);
notifyDataSetChanged();
AdapterUtils.notifyDataSetChanged(this, mAdapterList, list);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import io.github.muntashirakon.AppManager.utils.Utils;
import io.github.muntashirakon.AppManager.utils.appearance.ColorCodes;
import io.github.muntashirakon.io.Path;
import io.github.muntashirakon.util.AdapterUtils;
import io.github.muntashirakon.widget.MultiSelectionView;

class FmAdapter extends MultiSelectionView.Adapter<FmAdapter.ViewHolder> {
Expand All @@ -60,10 +61,8 @@ public FmAdapter(FmViewModel viewModel, FmActivity activity) {
}

public void setFmList(List<FmItem> list) {
mAdapterList.clear();
mAdapterList.addAll(list);
AdapterUtils.notifyDataSetChanged(this, mAdapterList, list);
notifySelectionChange();
notifyDataSetChanged();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Objects;

import io.github.muntashirakon.AppManager.R;
import io.github.muntashirakon.util.AdapterUtils;
import io.github.muntashirakon.AppManager.utils.Utils;

class FmPathListAdapter extends RecyclerView.Adapter<FmPathListAdapter.PathHolder> {
Expand Down Expand Up @@ -58,10 +59,8 @@ public void setCurrentUri(@NonNull Uri currentUri) {
setCurrentPosition(paths.size() - 1);
} else {
// Case 2
mPathParts.clear();
mPathParts.addAll(paths);
mCurrentPosition = mPathParts.size() - 1;
notifyDataSetChanged();
AdapterUtils.notifyDataSetChanged(this, mPathParts, paths);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import io.github.muntashirakon.AppManager.intercept.ActivityInterceptor;
import io.github.muntashirakon.AppManager.self.imagecache.ImageLoader;
import io.github.muntashirakon.AppManager.settings.FeatureController;
import io.github.muntashirakon.util.AdapterUtils;
import io.github.muntashirakon.AppManager.utils.ContextUtils;
import io.github.muntashirakon.AppManager.utils.ThreadUtils;
import io.github.muntashirakon.AppManager.utils.UIUtils;
Expand Down Expand Up @@ -313,13 +314,14 @@ void setFilteredItems(@Nullable String constraint) {

private void filterItems() {
synchronized (mFilteredItems) {
int lastCount = mFilteredItems.size();
mFilteredItems.clear();
for (int i = 0; i < mMatchingActivities.size(); ++i) {
if (mConstraint == null || mMatchingActivities.get(i).matches(mConstraint)) {
mFilteredItems.add(i);
}
}
notifyDataSetChanged();
AdapterUtils.notifyDataSetChanged(this, lastCount, mFilteredItems.size());
}
}

Expand Down
Loading

0 comments on commit d8ba865

Please sign in to comment.