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.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<DownloadMission> 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<String> 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<Integer> 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);

View File

@ -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();
}

View File

@ -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<MissionAdapter.ViewHold
private Activity mContext;
private LayoutInflater mInflater;
private DownloadManager mDownloadManager;
private DeleteManager mDeleteManager;
private DeleteDownloadManager mDeleteDownloadManager;
private List<DownloadMission> 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<MissionAdapter.ViewHold
for (int i = 0; i < mDownloadManager.getCount(); i++) {
DownloadMission mission = mDownloadManager.getMission(i);
if (!mDeleteManager.contains(mission)) {
if (!mDeleteDownloadManager.contains(mission)) {
mItemList.add(mDownloadManager.getMission(i));
}
}
@ -264,7 +264,7 @@ public class MissionAdapter extends RecyclerView.Adapter<MissionAdapter.ViewHold
return true;
case R.id.delete:
mDeleteManager.add(h.mission);
mDeleteDownloadManager.add(h.mission);
updateItemList();
notifyDataSetChanged();
return true;

View File

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