undo delete - code format

This commit is contained in:
Andrei.Rosca 2018-06-15 16:15:55 +02:00
parent 09dd044f3d
commit 8b8652d44c
4 changed files with 31 additions and 53 deletions

View File

@ -24,7 +24,7 @@ import io.reactivex.subjects.PublishSubject;
import us.shandian.giga.get.DownloadManager; import us.shandian.giga.get.DownloadManager;
import us.shandian.giga.get.DownloadMission; import us.shandian.giga.get.DownloadMission;
public class DeleteManager { public class DeleteDownloadManager {
private static final String KEY_STATE = "delete_manager_state"; private static final String KEY_STATE = "delete_manager_state";
@ -34,7 +34,7 @@ public class DeleteManager {
private DownloadManager mDownloadManager; private DownloadManager mDownloadManager;
private PublishSubject<DownloadMission> publishSubject = PublishSubject.create(); private PublishSubject<DownloadMission> publishSubject = PublishSubject.create();
DeleteManager(Activity activity) { DeleteDownloadManager(Activity activity) {
mPendingMap = new HashSet<>(); mPendingMap = new HashSet<>();
mDisposableList = new ArrayList<>(); mDisposableList = new ArrayList<>();
mView = activity.findViewById(android.R.id.content); mView = activity.findViewById(android.R.id.content);
@ -59,19 +59,13 @@ public class DeleteManager {
public void setDownloadManager(@NonNull DownloadManager downloadManager) { public void setDownloadManager(@NonNull DownloadManager downloadManager) {
mDownloadManager = downloadManager; mDownloadManager = downloadManager;
if (mPendingMap.size() < 1) { if (mPendingMap.size() < 1) return;
//nothing to do
return;
}
showUndoDeleteSnackbar(); showUndoDeleteSnackbar();
} }
public void restoreState(@Nullable Bundle savedInstanceState) { public void restoreState(@Nullable Bundle savedInstanceState) {
if (savedInstanceState == null) { if (savedInstanceState == null) return;
// nothing to do
return;
}
List<String> list = savedInstanceState.getStringArrayList(KEY_STATE); List<String> list = savedInstanceState.getStringArrayList(KEY_STATE);
if (list != null) { if (list != null) {
@ -80,10 +74,7 @@ public class DeleteManager {
} }
public void saveState(@Nullable Bundle outState) { public void saveState(@Nullable Bundle outState) {
if (outState == null) { if (outState == null) return;
// nothing to do
return;
}
for (Disposable disposable : mDisposableList) { for (Disposable disposable : mDisposableList) {
disposable.dispose(); disposable.dispose();
@ -93,10 +84,7 @@ public class DeleteManager {
} }
private void showUndoDeleteSnackbar() { private void showUndoDeleteSnackbar() {
if (mPendingMap.size() < 1) { if (mPendingMap.size() < 1) return;
// nothing to do
return;
}
String url = mPendingMap.iterator().next(); String url = mPendingMap.iterator().next();
@ -128,11 +116,11 @@ public class DeleteManager {
@Override @Override
public void onDismissed(Snackbar transientBottomBar, int event) { public void onDismissed(Snackbar transientBottomBar, int event) {
if (!disposable.isDisposed()) { if (!disposable.isDisposed()) {
mPendingMap.remove(mission.url);
Completable.fromAction(() -> deletePending(mission)) Completable.fromAction(() -> deletePending(mission))
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.subscribe(); .subscribe();
} }
mPendingMap.remove(mission.url);
snackbar.removeCallback(this); snackbar.removeCallback(this);
mDisposableList.remove(disposable); mDisposableList.remove(disposable);
showUndoDeleteSnackbar(); showUndoDeleteSnackbar();
@ -143,10 +131,7 @@ public class DeleteManager {
} }
public void deletePending() { public void deletePending() {
if (mPendingMap.size() < 1) { if (mPendingMap.size() < 1) return;
// nothing to do
return;
}
HashSet<Integer> idSet = new HashSet<>(); HashSet<Integer> idSet = new HashSet<>();
for (int i = 0; i < mDownloadManager.getCount(); i++) { for (int i = 0; i < mDownloadManager.getCount(); i++) {
@ -163,11 +148,6 @@ public class DeleteManager {
} }
private void deletePending(@NonNull DownloadMission mission) { private void deletePending(@NonNull DownloadMission mission) {
if (!contains(mission)) {
// nothing to do
return;
}
for (int i = 0; i < mDownloadManager.getCount(); i++) { for (int i = 0; i < mDownloadManager.getCount(); i++) {
if (mission.url.equals(mDownloadManager.getMission(i).url)) { if (mission.url.equals(mDownloadManager.getMission(i).url)) {
mDownloadManager.deleteMission(i); mDownloadManager.deleteMission(i);

View File

@ -24,7 +24,7 @@ import us.shandian.giga.ui.fragment.MissionsFragment;
public class DownloadActivity extends AppCompatActivity { public class DownloadActivity extends AppCompatActivity {
private static final String MISSIONS_FRAGMENT_TAG = "fragment_tag"; private static final String MISSIONS_FRAGMENT_TAG = "fragment_tag";
private DeleteManager mDeleteManager; private DeleteDownloadManager mDeleteDownloadManager;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -47,12 +47,12 @@ public class DownloadActivity extends AppCompatActivity {
actionBar.setDisplayShowTitleEnabled(true); actionBar.setDisplayShowTitleEnabled(true);
} }
mDeleteManager = new DeleteManager(this); mDeleteDownloadManager = new DeleteDownloadManager(this);
mDeleteManager.restoreState(savedInstanceState); mDeleteDownloadManager.restoreState(savedInstanceState);
MissionsFragment fragment = (MissionsFragment) getFragmentManager().findFragmentByTag(MISSIONS_FRAGMENT_TAG); MissionsFragment fragment = (MissionsFragment) getFragmentManager().findFragmentByTag(MISSIONS_FRAGMENT_TAG);
if (fragment != null) { if (fragment != null) {
fragment.setDeleteManager(mDeleteManager); fragment.setDeleteManager(mDeleteDownloadManager);
} else { } else {
getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override @Override
@ -66,13 +66,13 @@ public class DownloadActivity extends AppCompatActivity {
@Override @Override
protected void onSaveInstanceState(Bundle outState) { protected void onSaveInstanceState(Bundle outState) {
mDeleteManager.saveState(outState); mDeleteDownloadManager.saveState(outState);
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
} }
private void updateFragments() { private void updateFragments() {
MissionsFragment fragment = new AllMissionsFragment(); MissionsFragment fragment = new AllMissionsFragment();
fragment.setDeleteManager(mDeleteManager); fragment.setDeleteManager(mDeleteDownloadManager);
getFragmentManager().beginTransaction() getFragmentManager().beginTransaction()
.replace(R.id.frame, fragment, MISSIONS_FRAGMENT_TAG) .replace(R.id.frame, fragment, MISSIONS_FRAGMENT_TAG)
@ -114,7 +114,7 @@ public class DownloadActivity extends AppCompatActivity {
} }
private void deletePending() { private void deletePending() {
Completable.fromAction(mDeleteManager::deletePending) Completable.fromAction(mDeleteDownloadManager::deletePending)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.subscribe(); .subscribe();
} }

View File

@ -25,7 +25,7 @@ import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import org.schabi.newpipe.R; import org.schabi.newpipe.R;
import org.schabi.newpipe.download.DeleteManager; import org.schabi.newpipe.download.DeleteDownloadManager;
import java.io.File; import java.io.File;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
@ -56,15 +56,15 @@ public class MissionAdapter extends RecyclerView.Adapter<MissionAdapter.ViewHold
private Activity mContext; private Activity mContext;
private LayoutInflater mInflater; private LayoutInflater mInflater;
private DownloadManager mDownloadManager; private DownloadManager mDownloadManager;
private DeleteManager mDeleteManager; private DeleteDownloadManager mDeleteDownloadManager;
private List<DownloadMission> mItemList; private List<DownloadMission> mItemList;
private DownloadManagerService.DMBinder mBinder; private DownloadManagerService.DMBinder mBinder;
private int mLayout; 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; mContext = context;
mDownloadManager = downloadManager; mDownloadManager = downloadManager;
mDeleteManager = deleteManager; mDeleteDownloadManager = deleteDownloadManager;
mBinder = binder; mBinder = binder;
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@ -79,7 +79,7 @@ public class MissionAdapter extends RecyclerView.Adapter<MissionAdapter.ViewHold
for (int i = 0; i < mDownloadManager.getCount(); i++) { for (int i = 0; i < mDownloadManager.getCount(); i++) {
DownloadMission mission = mDownloadManager.getMission(i); DownloadMission mission = mDownloadManager.getMission(i);
if (!mDeleteManager.contains(mission)) { if (!mDeleteDownloadManager.contains(mission)) {
mItemList.add(mDownloadManager.getMission(i)); mItemList.add(mDownloadManager.getMission(i));
} }
} }
@ -264,7 +264,7 @@ public class MissionAdapter extends RecyclerView.Adapter<MissionAdapter.ViewHold
return true; return true;
case R.id.delete: case R.id.delete:
mDeleteManager.add(h.mission); mDeleteDownloadManager.add(h.mission);
updateItemList(); updateItemList();
notifyDataSetChanged(); notifyDataSetChanged();
return true; return true;

View File

@ -21,12 +21,10 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import org.schabi.newpipe.R; import org.schabi.newpipe.R;
import org.schabi.newpipe.download.DeleteManager; import org.schabi.newpipe.download.DeleteDownloadManager;
import io.reactivex.disposables.Disposable; import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import us.shandian.giga.get.DownloadManager; import us.shandian.giga.get.DownloadManager;
import us.shandian.giga.get.DownloadMission;
import us.shandian.giga.service.DownloadManagerService; import us.shandian.giga.service.DownloadManagerService;
import us.shandian.giga.ui.adapter.MissionAdapter; import us.shandian.giga.ui.adapter.MissionAdapter;
@ -43,7 +41,7 @@ public abstract class MissionsFragment extends Fragment {
private GridLayoutManager mGridManager; private GridLayoutManager mGridManager;
private LinearLayoutManager mLinearManager; private LinearLayoutManager mLinearManager;
private Context mActivity; private Context mActivity;
private DeleteManager mDeleteManager; private DeleteDownloadManager mDeleteDownloadManager;
private Disposable mDeleteDisposable; private Disposable mDeleteDisposable;
private ServiceConnection mConnection = new ServiceConnection() { private ServiceConnection mConnection = new ServiceConnection() {
@ -52,8 +50,8 @@ public abstract class MissionsFragment extends Fragment {
public void onServiceConnected(ComponentName name, IBinder binder) { public void onServiceConnected(ComponentName name, IBinder binder) {
mBinder = (DownloadManagerService.DMBinder) binder; mBinder = (DownloadManagerService.DMBinder) binder;
mDownloadManager = setupDownloadManager(mBinder); mDownloadManager = setupDownloadManager(mBinder);
if (mDeleteManager != null) { if (mDeleteDownloadManager != null) {
mDeleteManager.setDownloadManager(mDownloadManager); mDeleteDownloadManager.setDownloadManager(mDownloadManager);
updateList(); updateList();
} }
} }
@ -66,10 +64,10 @@ public abstract class MissionsFragment extends Fragment {
}; };
public void setDeleteManager(@NonNull DeleteManager deleteManager) { public void setDeleteManager(@NonNull DeleteDownloadManager deleteDownloadManager) {
mDeleteManager = deleteManager; mDeleteDownloadManager = deleteDownloadManager;
if (mDownloadManager != null) { if (mDownloadManager != null) {
mDeleteManager.setDownloadManager(mDownloadManager); mDeleteDownloadManager.setDownloadManager(mDownloadManager);
updateList(); updateList();
} }
} }
@ -126,8 +124,8 @@ public abstract class MissionsFragment extends Fragment {
@Override @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
if (mDeleteManager != null) { if (mDeleteDownloadManager != null) {
mDeleteDisposable = mDeleteManager.getUndoObservable().subscribe(mission -> { mDeleteDisposable = mDeleteDownloadManager.getUndoObservable().subscribe(mission -> {
if (mAdapter != null) { if (mAdapter != null) {
mAdapter.updateItemList(); mAdapter.updateItemList();
mAdapter.notifyDataSetChanged(); mAdapter.notifyDataSetChanged();
@ -164,7 +162,7 @@ public abstract class MissionsFragment extends Fragment {
} }
private void updateList() { private void updateList() {
mAdapter = new MissionAdapter((Activity) mActivity, mBinder, mDownloadManager, mDeleteManager, mLinear); mAdapter = new MissionAdapter((Activity) mActivity, mBinder, mDownloadManager, mDeleteDownloadManager, mLinear);
if (mLinear) { if (mLinear) {
mList.setLayoutManager(mLinearManager); mList.setLayoutManager(mLinearManager);