Commit 0e653abc authored by Donald's avatar Donald

progress dialog showing errors when deleting media

parent d36c7617
......@@ -115,7 +115,8 @@ dependencies {
// debug Only
//debugCompile project(':inappstoragereader')
debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.1'
debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.3'
releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.3'
// TODO check them out
......
......@@ -27,4 +27,6 @@
@butterknife.* <methods>;
}
-keepnames class * { @butterknife.Bind *;}
\ No newline at end of file
-keepnames class * { @butterknife.Bind *;}
-dontwarn okio.**
\ No newline at end of file
......@@ -54,6 +54,7 @@ import org.horaapps.leafpic.util.AlertDialogsHelper;
import org.horaapps.leafpic.util.Measure;
import org.horaapps.leafpic.util.Security;
import org.horaapps.leafpic.util.StringUtils;
import org.horaapps.leafpic.util.file.DeleteException;
import org.horaapps.leafpic.views.HackyViewPager;
import org.horaapps.liz.ColorPalette;
......@@ -365,7 +366,7 @@ public class SingleMediaActivity extends SharedMediaActivity {
}
},
err -> {
if (err instanceof MediaHelper.DeleteException)
if (err instanceof DeleteException)
Toast.makeText(this, R.string.delete_error, Toast.LENGTH_SHORT).show();
else
Toast.makeText(this, err.getMessage(), Toast.LENGTH_SHORT).show();
......
......@@ -262,12 +262,19 @@ public class MediaAdapter extends ThemedAdapter<MediaAdapter.ViewHolder> {
});
}
public void remove(Media media) {
int i = this.media.indexOf(media);
this.media.remove(i);
notifyItemRemoved(i);
}
@Override
public void refreshTheme(ThemeHelper theme) {
placeholder = theme.getPlaceHolder();
//super.refreshTheme(theme);
}
/**
* On longpress, it finds the last or the first selected image before or after the targetIndex
* and selects them all.
......
package org.horaapps.leafpic.adapters;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.mikepenz.community_material_typeface_library.CommunityMaterial;
import com.mikepenz.iconics.view.IconicsImageView;
import org.horaapps.leafpic.R;
import org.horaapps.leafpic.util.file.DeleteException;
import org.horaapps.liz.ThemeHelper;
import org.horaapps.liz.ThemedViewHolder;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
* Created by dnld on 9/19/17.
*/
public class ProgressAdapter extends BaseAdapter<ProgressAdapter.ListItem, ProgressAdapter.ViewHolder> {
public static class ListItem {
String name;
Exception ex;
public ListItem(DeleteException ex) {
this.name = ex.getMedia().getName();
this.ex = ex;
}
public ListItem(String name) {
this.name = name;
ex = new Exception("No error message");
}
public String getName() {
return name;
}
}
public interface OnProgress {
void onNewItem(ListItem item);
}
public void add(ListItem item, boolean hasErrors) {
if (hasErrors)
this.add(item);
if (listener != null) {
listener.onNewItem(item);
}
}
private OnProgress listener;
public void setListener(OnProgress listener) {
this.listener = listener;
}
public ProgressAdapter(Context context) {
super(context);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.list_progress_item, parent, false));
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ListItem element = getElement(position);
holder.name.setText(element.name);
//CommunityMaterial.Icon.cmd_delete_sweep
holder.icon.setIcon(CommunityMaterial.Icon.cmd_alert_octagram);
holder.cont.setOnClickListener(view -> Toast.makeText(holder.cont.getContext(), element.ex.getMessage(), Toast.LENGTH_SHORT).show());
}
static class ViewHolder extends ThemedViewHolder {
@BindView(R.id.folder_icon_bottom_sheet_item)
IconicsImageView icon;
@BindView(R.id.name_folder)
TextView name;
@BindView(R.id.ll_album_bottom_sheet_item)
LinearLayout cont;
ViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
@Override
public void refreshTheme(ThemeHelper themeHelper) {
}
}
}
......@@ -6,6 +6,7 @@ import android.net.Uri;
import android.provider.MediaStore;
import org.horaapps.leafpic.util.StringUtils;
import org.horaapps.leafpic.util.file.DeleteException;
import java.io.File;
import java.util.ArrayList;
......@@ -19,18 +20,13 @@ import io.reactivex.Observable;
public class MediaHelper {
private static Uri external = MediaStore.Files.getContentUri("external");
public static class DeleteException extends Exception {
public DeleteException() {
super("Cannot delete file");
}
}
public static Observable<Media> deleteMedia(Context context, Media mediaToDelete) {
return Observable.create(subscriber -> {
boolean deleteSuccess = internalDeleteMedia(context, mediaToDelete);
if (deleteSuccess) subscriber.onNext(mediaToDelete);
else subscriber.onError(new DeleteException());
else subscriber.onError(new DeleteException(mediaToDelete));
subscriber.onComplete();
});
}
......@@ -41,7 +37,7 @@ public class MediaHelper {
for (Media media : mediaToDelete) {
boolean deleteSuccess = internalDeleteMedia(context, media);
if (deleteSuccess) subscriber.onNext(media);
else subscriber.onError(new DeleteException());
else subscriber.onError(new DeleteException(media));
}
subscriber.onComplete();
});
......
......@@ -29,6 +29,7 @@ import org.horaapps.leafpic.activities.MainActivity;
import org.horaapps.leafpic.activities.PaletteActivity;
import org.horaapps.leafpic.activities.SingleMediaActivity;
import org.horaapps.leafpic.adapters.MediaAdapter;
import org.horaapps.leafpic.adapters.ProgressAdapter;
import org.horaapps.leafpic.data.Album;
import org.horaapps.leafpic.data.AlbumsHelper;
import org.horaapps.leafpic.data.HandlingAlbums;
......@@ -42,8 +43,10 @@ import org.horaapps.leafpic.data.sort.SortingOrder;
import org.horaapps.leafpic.util.AlertDialogsHelper;
import org.horaapps.leafpic.util.Measure;
import org.horaapps.leafpic.util.StringUtils;
import org.horaapps.leafpic.util.file.DeleteException;
import org.horaapps.leafpic.views.GridSpacingItemDecoration;
import org.horaapps.liz.ThemeHelper;
import org.horaapps.liz.ThemedActivity;
import java.util.ArrayList;
import java.util.Locale;
......@@ -387,6 +390,37 @@ public class RvMediaFragment extends BaseFragment {
adapter.changeSortingOrder(sortingOrder);
AlbumsHelper.setSortingOrder(getContext(), sortingOrder);
return true;
case R.id.delete:
ProgressAdapter errorsAdapter = new ProgressAdapter(getContext());
ArrayList<Media> selected = adapter.getSelected();
AlertDialog alertDialog = AlertDialogsHelper.getProgressDialogWithErrors(((ThemedActivity) getActivity()), R.string.deleting_images, errorsAdapter, selected.size());
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, this.getString(R.string.cancel).toUpperCase(), (dialog, id) -> {
alertDialog.dismiss();
});
alertDialog.show();
MediaHelper.deleteMedia(getContext(), selected)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(m -> {
adapter.remove(m);
errorsAdapter.add(new ProgressAdapter.ListItem(m.getName()), false);
},
throwable -> {
if (throwable instanceof DeleteException)
errorsAdapter.add(new ProgressAdapter.ListItem(
(DeleteException) throwable), true);
},
() -> {
if (errorsAdapter.getItemCount() == 0)
alertDialog.dismiss();
adapter.clearSelected();
});
return true;
}
return super.onOptionsItemSelected(item);
......
......@@ -10,10 +10,13 @@ import android.net.Uri;
import android.support.annotation.StringRes;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.method.LinkMovementMethod;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.OvershootInterpolator;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
......@@ -29,6 +32,7 @@ import com.drew.lang.GeoLocation;
import com.orhanobut.hawk.Hawk;
import org.horaapps.leafpic.R;
import org.horaapps.leafpic.adapters.ProgressAdapter;
import org.horaapps.leafpic.data.Media;
import org.horaapps.leafpic.data.metadata.MediaDetailsMap;
import org.horaapps.liz.ThemeHelper;
......@@ -41,6 +45,7 @@ import java.lang.reflect.Field;
import java.util.Locale;
import in.uncod.android.bypass.Bypass;
import jp.wasabeef.recyclerview.animators.LandingAnimator;
/**
* Created by dnld on 19/05/16.
......@@ -51,7 +56,7 @@ public class AlertDialogsHelper {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity, activity.getDialogStyle());
View dialogLayout = activity.getLayoutInflater().inflate(org.horaapps.leafpic.R.layout.dialog_insert_text, null);
TextView textViewTitle = (TextView) dialogLayout.findViewById(org.horaapps.leafpic.R.id.rename_title);
TextView textViewTitle = dialogLayout.findViewById(R.id.rename_title);
((CardView) dialogLayout.findViewById(org.horaapps.leafpic.R.id.dialog_chose_provider_title)).setCardBackgroundColor(activity.getCardBackgroundColor());
textViewTitle.setBackgroundColor(activity.getPrimaryColor());
......@@ -80,8 +85,8 @@ public class AlertDialogsHelper {
AlertDialog.Builder builder = new AlertDialog.Builder(activity,activity.getDialogStyle());
View dialogLayout = activity.getLayoutInflater().inflate(R.layout.dialog_text, null);
TextView dialogTitle = (TextView) dialogLayout.findViewById(org.horaapps.leafpic.R.id.text_dialog_title);
TextView dialogMessage = (TextView) dialogLayout.findViewById(org.horaapps.leafpic.R.id.text_dialog_message);
TextView dialogTitle = dialogLayout.findViewById(R.id.text_dialog_title);
TextView dialogMessage = dialogLayout.findViewById(R.id.text_dialog_message);
((CardView) dialogLayout.findViewById(org.horaapps.leafpic.R.id.message_card)).setCardBackgroundColor(activity.getCardBackgroundColor());
dialogTitle.setBackgroundColor(activity.getPrimaryColor());
......@@ -92,11 +97,40 @@ public class AlertDialogsHelper {
return builder.create();
}
public static AlertDialog getProgressDialogWithErrors(ThemedActivity activity, @StringRes int title, ProgressAdapter adapter, int max) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity, activity.getDialogStyle());
View dialogLayout = activity.getLayoutInflater().inflate(R.layout.dialog_list_progress, null);
final int[] progress = {0};
TextView dialogTitle = dialogLayout.findViewById(R.id.text_dialog_title);
TextView progressMessage = dialogLayout.findViewById(R.id.name_folder);
((ProgressBar) dialogLayout.findViewById(org.horaapps.leafpic.R.id.progress_dialog_loading)).getIndeterminateDrawable()
.setColorFilter(activity.getPrimaryColor(), android.graphics.PorterDuff.Mode.SRC_ATOP);
adapter.setListener(item -> {
progress[0]++;
dialogTitle.setText(activity.getString(title, progress[0], max));
progressMessage.setText(item.getName());
});
RecyclerView rv = dialogLayout.findViewById(R.id.rv_progress);
rv.setLayoutManager(new LinearLayoutManager(activity));
rv.setHasFixedSize(true);
rv.setItemAnimator(new LandingAnimator(new OvershootInterpolator(1f)));
rv.setAdapter(adapter);
((CardView) dialogLayout.findViewById(org.horaapps.leafpic.R.id.message_card)).setCardBackgroundColor(activity.getCardBackgroundColor());
dialogTitle.setBackgroundColor(activity.getPrimaryColor());
dialogTitle.setText(activity.getString(title, progress[0], max));
builder.setView(dialogLayout);
return builder.create();
}
public static AlertDialog getProgressDialog(final ThemedActivity activity, String title, String message){
AlertDialog.Builder progressDialog = new AlertDialog.Builder(activity, activity.getDialogStyle());
View dialogLayout = activity.getLayoutInflater().inflate(org.horaapps.leafpic.R.layout.dialog_progress, null);
TextView dialogTitle = (TextView) dialogLayout.findViewById(org.horaapps.leafpic.R.id.progress_dialog_title);
TextView dialogMessage = (TextView) dialogLayout.findViewById(org.horaapps.leafpic.R.id.progress_dialog_text);
TextView dialogTitle = dialogLayout.findViewById(R.id.progress_dialog_title);
TextView dialogMessage = dialogLayout.findViewById(R.id.progress_dialog_text);
dialogTitle.setBackgroundColor(activity.getPrimaryColor());
((CardView) dialogLayout.findViewById(org.horaapps.leafpic.R.id.progress_dialog_card)).setCardBackgroundColor(activity.getCardBackgroundColor());
......@@ -116,7 +150,7 @@ public class AlertDialogsHelper {
AlertDialog.Builder detailsDialogBuilder = new AlertDialog.Builder(activity, activity.getDialogStyle());
MediaDetailsMap<String, String> mainDetails = new MediaDetailsMap<>();//f.getMainDetails(activity.getApplicationContext());
final View dialogLayout = activity.getLayoutInflater().inflate(org.horaapps.leafpic.R.layout.dialog_media_detail, null);
ImageView imgMap = (ImageView) dialogLayout.findViewById(org.horaapps.leafpic.R.id.photo_map);
ImageView imgMap = dialogLayout.findViewById(R.id.photo_map);
dialogLayout.findViewById(org.horaapps.leafpic.R.id.details_title).setBackgroundColor(activity.getPrimaryColor());
((CardView) dialogLayout.findViewById(org.horaapps.leafpic.R.id.photo_details_card)).setCardBackgroundColor(activity.getCardBackgroundColor());
......@@ -147,7 +181,7 @@ public class AlertDialogsHelper {
} else imgMap.setVisibility(View.GONE);
final TextView showMoreText = (TextView) dialogLayout.findViewById(R.id.details_showmore);
final TextView showMoreText = dialogLayout.findViewById(R.id.details_showmore);
showMoreText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
......@@ -162,7 +196,7 @@ public class AlertDialogsHelper {
}
private static void loadDetails(View dialogLayout, ThemedActivity activity, MediaDetailsMap<String, String> metadata) {
LinearLayout detailsTable = (LinearLayout) dialogLayout.findViewById(R.id.ll_list_details);
LinearLayout detailsTable = dialogLayout.findViewById(R.id.ll_list_details);
int tenPxInDp = Measure.pxToDp (10, activity);
......@@ -199,10 +233,10 @@ public class AlertDialogsHelper {
final AlertDialog.Builder changelogDialogBuilder = new AlertDialog.Builder(activity, activity.getDialogStyle());
View dialogLayout = activity.getLayoutInflater().inflate(R.layout.dialog_changelog, null);
TextView dialogTitle = (TextView) dialogLayout.findViewById(R.id.dialog_changelog_title);
TextView dialogMessage = (TextView) dialogLayout.findViewById(R.id.dialog_changelog_text);
CardView cvBackground = (CardView) dialogLayout.findViewById(R.id.dialog_changelog_card);
ScrollView scrChangelog = (ScrollView) dialogLayout.findViewById(R.id.changelog_scrollview);
TextView dialogTitle = dialogLayout.findViewById(R.id.dialog_changelog_title);
TextView dialogMessage = dialogLayout.findViewById(R.id.dialog_changelog_text);
CardView cvBackground = dialogLayout.findViewById(R.id.dialog_changelog_card);
ScrollView scrChangelog = dialogLayout.findViewById(R.id.changelog_scrollview);
cvBackground.setCardBackgroundColor(activity.getCardBackgroundColor());
dialogTitle.setBackgroundColor(activity.getPrimaryColor());
......
package org.horaapps.leafpic.util.file;
import org.horaapps.leafpic.data.Media;
/**
* Created by dnld on 9/19/17.
*/
public class DeleteException extends Exception {
Media media;
public DeleteException(Media media) {
super("Cannot delete Media");
this.media = media;
}
public DeleteException() {
this(null);
}
public Media getMedia() {
return media;
}
}
\ No newline at end of file
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="2dp"
android:id="@+id/message_card">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/text_dialog_title"
android:layout_width="match_parent"
android:textColor="@color/md_dark_primary_text"
android:layout_height="wrap_content"
android:background="@color/md_red_500"
android:padding="@dimen/big_spacing"
android:textSize="@dimen/sub_big_text"
android:textStyle="bold"
android:text="Dialog title..." />
<LinearLayout
android:id="@+id/text_dialog_rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingRight="@dimen/sub_medium_spacing"
android:paddingLeft="@dimen/sub_medium_spacing"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ll_album_bottom_sheet_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="@drawable/ripple"
android:orientation="horizontal"
android:gravity="center_vertical"
android:background="@drawable/ripple">
<ProgressBar
android:id="@+id/progress_dialog_loading"
android:layout_width="@dimen/progress_circle_width_height"
android:layout_height="@dimen/progress_circle_width_height"
android:layout_gravity="center_vertical"
android:paddingRight="@dimen/sub_medium_spacing"
android:paddingLeft="@dimen/sub_medium_spacing"
android:indeterminateTint="@color/md_white_1000" />
<TextView
android:id="@+id/name_folder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="2"
android:ellipsize="middle"
android:textColor="@color/md_dark_background"
android:text=""
android:textSize="14sp" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rv_progress" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</android.support.v7.widget.CardView>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/ll_album_bottom_sheet_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="@drawable/ripple"
android:orientation="horizontal"
android:clickable="true"
android:gravity="center_vertical"
android:background="@drawable/ripple"
android:paddingTop="@dimen/small_spacing"
android:paddingBottom="@dimen/small_spacing">
<com.mikepenz.iconics.view.IconicsImageView
android:id="@+id/folder_icon_bottom_sheet_item"
android:layout_height="14dp"
android:layout_width="14dp"
app:iiv_icon="gmd-folder"
android:layout_marginLeft="@dimen/big_spacing"
android:layout_marginRight="@dimen/big_spacing" />
<TextView
android:id="@+id/name_folder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="2"
android:ellipsize="middle"
android:textColor="@color/md_dark_background"
android:textSize="14sp" />
</LinearLayout>
\ No newline at end of file
......@@ -394,7 +394,8 @@ and this could make the media inaccessible from other apps. Use the exclude opti
<string name="error_occured_open_media">An error occurred opening this media.</string>
<string name="error_thumbnail">Unable to find a thumbnail for the album!</string>
<string name="no_app_to_perform">Unable to found an app to perform this action.</string>
<string name="deleting_images">We are thorwing away all your media!</string>
<string name="deleting_images">Threw away %d/%d media!</string>
<string name="start_slide_show">Start Slide</string>
<string name="stop_slide_show">Stop Slide</string>
<string name="errors">Errors</string>
</resources>
......@@ -8,7 +8,7 @@ buildscript {
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-beta4'
classpath 'com.android.tools.build:gradle:3.0.0-beta6'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
......@@ -16,7 +16,7 @@ buildscript {
}
project.ext {
supportVersion = "26.0.2"
supportVersion = "26.1.0"
sdkVersion = 26
buildToolsVersion = '26.0.1'
}
......
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