Commit 8211148f authored by Donald's avatar Donald

clean

parent 0df69fb3
......@@ -5,6 +5,7 @@ import android.media.MediaScannerConnection;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
import android.util.Pair;
import org.horaapps.leafpic.util.StringUtils;
import org.horaapps.leafpic.util.file.DeleteException;
......@@ -33,19 +34,18 @@ public class MediaHelper {
}
public static Observable<Media> deleteMedia(Context context, ArrayList<Media> mediaToDelete) {
public static Observable<Pair<Media, Boolean>> deleteMedia(Context context, ArrayList<Media> mediaToDelete) {
return Observable.create(subscriber -> {
for (Media media : mediaToDelete) {
boolean deleteSuccess = internalDeleteMedia(context, media);
Log.v("delete-internal", media.getPath() + " " + deleteSuccess);
if (deleteSuccess) subscriber.onNext(media);
else subscriber.onError(new DeleteException(media));
subscriber.onNext(new Pair<>(media, deleteSuccess));
}
subscriber.onComplete();
});
}
private static boolean internalDeleteMedia(Context context, Media media) {
public static boolean internalDeleteMedia(Context context, Media media) {
File file = new File(media.getPath());
boolean success = StorageHelper.deleteFile(context, file);
if (success)
......
package org.horaapps.leafpic.delete;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialogFragment;
import android.support.v7.widget.AppCompatButton;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
......@@ -17,7 +18,6 @@ import org.horaapps.leafpic.R;
import org.horaapps.leafpic.data.Media;
import org.horaapps.leafpic.data.MediaHelper;
import org.horaapps.leafpic.util.StringUtils;
import org.horaapps.leafpic.util.file.DeleteException;
import org.horaapps.liz.ThemeHelper;
import java.util.ArrayList;
......@@ -28,6 +28,7 @@ import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
......@@ -64,6 +65,8 @@ public class DeleteMediaBottomSheet extends BottomSheetDialogFragment {
DeleteMediaListener listener;
boolean cancelRequested = false;
CompositeDisposable disposable = new CompositeDisposable();
public void setListener(DeleteMediaListener listener) {
this.listener = listener;
}
......@@ -84,7 +87,26 @@ public class DeleteMediaBottomSheet extends BottomSheetDialogFragment {
progress.setText(String.format(Locale.ENGLISH, "%d/%d", p, progress.getMax()));
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
ThemeHelper th = ThemeHelper.getInstanceLoaded(getContext());
View view = inflater.inflate(R.layout.bottom_sheet_delete_media, container, false);
ButterKnife.bind(this, view);
view.setBackgroundColor(th.getBackgroundColor());
header.setBackgroundColor(th.getPrimaryColor());
txtErrors.setTextColor(th.getTextColor());
progress.setFinishedStrokeColor(th.getAccentColor());
progress.setTextColor(th.getTextColor());
return view;
}
/* @Override
public void setupDialog(Dialog dialog, int style) {
ThemeHelper th = ThemeHelper.getInstanceLoaded(getContext());
......@@ -102,7 +124,7 @@ public class DeleteMediaBottomSheet extends BottomSheetDialogFragment {
setProgress(0);
dialog.setContentView(view);
}
}*/
private void showErrors(HashSet<String> errors) {
StringBuilder b = new StringBuilder();
......@@ -127,43 +149,67 @@ public class DeleteMediaBottomSheet extends BottomSheetDialogFragment {
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
public void onDestroy() {
disposable.dispose();
super.onDestroy();
media = getArguments().getParcelableArrayList(EXTRA_MEDIA);
}
private void done() {
setCancelable(true);
listener.onCompleted();
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Bundle arguments = getArguments();
if (arguments == null || (media = arguments.getParcelableArrayList(EXTRA_MEDIA)) == null) {
done();
return;
}
progress.setMax(media.size());
setProgress(0);
HashSet<String> errors = new HashSet<>(0);
Disposable end = MediaHelper.deleteMedia(getContext(), media)
.subscribeOn(Schedulers.io())
Disposable subscribe = MediaHelper.deleteMedia(getContext(), media)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.takeUntil(m -> !cancelRequested)
.takeUntil(mediaBooleanPair -> !cancelRequested)
.subscribe(
m -> {
listener.onDeleted(m);
setProgress((int) (progress.getProgress() + 1));
},
throwable -> {
setProgress((int) (progress.getProgress() + 1));
if (throwable instanceof DeleteException) {
errors.add(((DeleteException) throwable).getMedia().getPath());
} else {
errors.add(throwable.getLocalizedMessage());
}
pair -> {
if (pair.second)
listener.onDeleted(pair.first);
else
errors.add(pair.first.getName());
setProgress((int) (progress.getProgress() + 1));
}, err -> {
},
() -> {
setCancelable(true);
listener.onCompleted();
if (errors.size() == 0)
//Log.wtf("asd","ads");
dismiss();
else {
if (errors.size() > 0)
showErrors(errors);
}
}
);
done();
});
disposable.add(subscribe);
/*for (Media m : media) {
if(cancelRequested) break;
boolean deleteSuccess = MediaHelper.internalDeleteMedia(getContext(), m);
if (deleteSuccess) {
listener.onDeleted(m);
} else {
errors.add(m.getPath());
}
setProgress((int) (progress.getProgress() + 1));
}*/
/*if (errors.size() > 0)
showErrors(errors);
else dismiss();*/
}
......
......@@ -441,15 +441,18 @@ public class RvMediaFragment extends BaseFragment {
//adapter.clearSelected();
//updateToolbar();
adapter.invalidateSelectedCount();
Log.wtf("delete", "main complete ");
}
@Override
public void onDeleted(Media media) {
Log.wtf("delete", "main deleted " + media.getDisplayPath());
adapter.removeSelectedMedia(media);
//updateToolbar();
}
});
deleteMediaBottomSheet.show(getFragmentManager(), DeleteMediaBottomSheet.TAG);
deleteMediaBottomSheet.showNow(getFragmentManager(), null);
// deleteMediaBottomSheet.show(getFragmentManager(), null);
......
......@@ -9,7 +9,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.0-beta3'
classpath 'com.android.tools.build:gradle:3.1.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment