From 8b8652d44c3a92156915a92e73bef3f268610888 Mon Sep 17 00:00:00 2001 From: "Andrei.Rosca" Date: Fri, 15 Jun 2018 16:15:55 +0200 Subject: [PATCH] undo delete - code format --- ...anager.java => DeleteDownloadManager.java} | 36 +++++-------------- .../newpipe/download/DownloadActivity.java | 14 ++++---- .../giga/ui/adapter/MissionAdapter.java | 12 +++---- .../giga/ui/fragment/MissionsFragment.java | 22 ++++++------ 4 files changed, 31 insertions(+), 53 deletions(-) rename app/src/main/java/org/schabi/newpipe/download/{DeleteManager.java => DeleteDownloadManager.java} (86%) diff --git a/app/src/main/java/org/schabi/newpipe/download/DeleteManager.java b/app/src/main/java/org/schabi/newpipe/download/DeleteDownloadManager.java similarity index 86% rename from app/src/main/java/org/schabi/newpipe/download/DeleteManager.java rename to app/src/main/java/org/schabi/newpipe/download/DeleteDownloadManager.java index 210cf668d..f2912a6fa 100644 --- a/app/src/main/java/org/schabi/newpipe/download/DeleteManager.java +++ b/app/src/main/java/org/schabi/newpipe/download/DeleteDownloadManager.java @@ -24,7 +24,7 @@ import io.reactivex.subjects.PublishSubject; import us.shandian.giga.get.DownloadManager; import us.shandian.giga.get.DownloadMission; -public class DeleteManager { +public class DeleteDownloadManager { private static final String KEY_STATE = "delete_manager_state"; @@ -34,7 +34,7 @@ public class DeleteManager { private DownloadManager mDownloadManager; private PublishSubject publishSubject = PublishSubject.create(); - DeleteManager(Activity activity) { + DeleteDownloadManager(Activity activity) { mPendingMap = new HashSet<>(); mDisposableList = new ArrayList<>(); mView = activity.findViewById(android.R.id.content); @@ -59,19 +59,13 @@ public class DeleteManager { public void setDownloadManager(@NonNull DownloadManager downloadManager) { mDownloadManager = downloadManager; - if (mPendingMap.size() < 1) { - //nothing to do - return; - } + if (mPendingMap.size() < 1) return; showUndoDeleteSnackbar(); } public void restoreState(@Nullable Bundle savedInstanceState) { - if (savedInstanceState == null) { - // nothing to do - return; - } + if (savedInstanceState == null) return; List list = savedInstanceState.getStringArrayList(KEY_STATE); if (list != null) { @@ -80,10 +74,7 @@ public class DeleteManager { } public void saveState(@Nullable Bundle outState) { - if (outState == null) { - // nothing to do - return; - } + if (outState == null) return; for (Disposable disposable : mDisposableList) { disposable.dispose(); @@ -93,10 +84,7 @@ public class DeleteManager { } private void showUndoDeleteSnackbar() { - if (mPendingMap.size() < 1) { - // nothing to do - return; - } + if (mPendingMap.size() < 1) return; String url = mPendingMap.iterator().next(); @@ -128,11 +116,11 @@ public class DeleteManager { @Override public void onDismissed(Snackbar transientBottomBar, int event) { if (!disposable.isDisposed()) { - mPendingMap.remove(mission.url); Completable.fromAction(() -> deletePending(mission)) .subscribeOn(Schedulers.io()) .subscribe(); } + mPendingMap.remove(mission.url); snackbar.removeCallback(this); mDisposableList.remove(disposable); showUndoDeleteSnackbar(); @@ -143,10 +131,7 @@ public class DeleteManager { } public void deletePending() { - if (mPendingMap.size() < 1) { - // nothing to do - return; - } + if (mPendingMap.size() < 1) return; HashSet idSet = new HashSet<>(); for (int i = 0; i < mDownloadManager.getCount(); i++) { @@ -163,11 +148,6 @@ public class DeleteManager { } private void deletePending(@NonNull DownloadMission mission) { - if (!contains(mission)) { - // nothing to do - return; - } - for (int i = 0; i < mDownloadManager.getCount(); i++) { if (mission.url.equals(mDownloadManager.getMission(i).url)) { mDownloadManager.deleteMission(i); diff --git a/app/src/main/java/org/schabi/newpipe/download/DownloadActivity.java b/app/src/main/java/org/schabi/newpipe/download/DownloadActivity.java index f9c48a533..4a2c85149 100644 --- a/app/src/main/java/org/schabi/newpipe/download/DownloadActivity.java +++ b/app/src/main/java/org/schabi/newpipe/download/DownloadActivity.java @@ -24,7 +24,7 @@ import us.shandian.giga.ui.fragment.MissionsFragment; public class DownloadActivity extends AppCompatActivity { private static final String MISSIONS_FRAGMENT_TAG = "fragment_tag"; - private DeleteManager mDeleteManager; + private DeleteDownloadManager mDeleteDownloadManager; @Override protected void onCreate(Bundle savedInstanceState) { @@ -47,12 +47,12 @@ public class DownloadActivity extends AppCompatActivity { actionBar.setDisplayShowTitleEnabled(true); } - mDeleteManager = new DeleteManager(this); - mDeleteManager.restoreState(savedInstanceState); + mDeleteDownloadManager = new DeleteDownloadManager(this); + mDeleteDownloadManager.restoreState(savedInstanceState); MissionsFragment fragment = (MissionsFragment) getFragmentManager().findFragmentByTag(MISSIONS_FRAGMENT_TAG); if (fragment != null) { - fragment.setDeleteManager(mDeleteManager); + fragment.setDeleteManager(mDeleteDownloadManager); } else { getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override @@ -66,13 +66,13 @@ public class DownloadActivity extends AppCompatActivity { @Override protected void onSaveInstanceState(Bundle outState) { - mDeleteManager.saveState(outState); + mDeleteDownloadManager.saveState(outState); super.onSaveInstanceState(outState); } private void updateFragments() { MissionsFragment fragment = new AllMissionsFragment(); - fragment.setDeleteManager(mDeleteManager); + fragment.setDeleteManager(mDeleteDownloadManager); getFragmentManager().beginTransaction() .replace(R.id.frame, fragment, MISSIONS_FRAGMENT_TAG) @@ -114,7 +114,7 @@ public class DownloadActivity extends AppCompatActivity { } private void deletePending() { - Completable.fromAction(mDeleteManager::deletePending) + Completable.fromAction(mDeleteDownloadManager::deletePending) .subscribeOn(Schedulers.io()) .subscribe(); } diff --git a/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java b/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java index 4054c8a15..8127c3467 100644 --- a/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java +++ b/app/src/main/java/us/shandian/giga/ui/adapter/MissionAdapter.java @@ -25,7 +25,7 @@ import android.widget.TextView; import android.widget.Toast; import org.schabi.newpipe.R; -import org.schabi.newpipe.download.DeleteManager; +import org.schabi.newpipe.download.DeleteDownloadManager; import java.io.File; import java.lang.ref.WeakReference; @@ -56,15 +56,15 @@ public class MissionAdapter extends RecyclerView.Adapter mItemList; private DownloadManagerService.DMBinder mBinder; private int mLayout; - public MissionAdapter(Activity context, DownloadManagerService.DMBinder binder, DownloadManager downloadManager, DeleteManager deleteManager, boolean isLinear) { + public MissionAdapter(Activity context, DownloadManagerService.DMBinder binder, DownloadManager downloadManager, DeleteDownloadManager deleteDownloadManager, boolean isLinear) { mContext = context; mDownloadManager = downloadManager; - mDeleteManager = deleteManager; + mDeleteDownloadManager = deleteDownloadManager; mBinder = binder; mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); @@ -79,7 +79,7 @@ public class MissionAdapter extends RecyclerView.Adapter { + if (mDeleteDownloadManager != null) { + mDeleteDisposable = mDeleteDownloadManager.getUndoObservable().subscribe(mission -> { if (mAdapter != null) { mAdapter.updateItemList(); mAdapter.notifyDataSetChanged(); @@ -164,7 +162,7 @@ public abstract class MissionsFragment extends Fragment { } private void updateList() { - mAdapter = new MissionAdapter((Activity) mActivity, mBinder, mDownloadManager, mDeleteManager, mLinear); + mAdapter = new MissionAdapter((Activity) mActivity, mBinder, mDownloadManager, mDeleteDownloadManager, mLinear); if (mLinear) { mList.setLayoutManager(mLinearManager);