Added option to delete attachments

This commit is contained in:
M66B 2018-08-14 14:16:29 +00:00
parent 4ed1064d18
commit badbd134f5
5 changed files with 43 additions and 6 deletions

View File

@ -51,6 +51,7 @@ import androidx.recyclerview.widget.RecyclerView;
public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.ViewHolder> {
private Context context;
private LifecycleOwner owner;
private boolean readonly;
private boolean debug;
private List<EntityAttachment> all = new ArrayList<>();
@ -58,6 +59,7 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
View itemView;
ImageView ivDelete;
TextView tvName;
TextView tvSize;
ImageView ivStatus;
@ -69,6 +71,7 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
super(itemView);
this.itemView = itemView;
ivDelete = itemView.findViewById(R.id.ivDelete);
tvName = itemView.findViewById(R.id.tvName);
tvSize = itemView.findViewById(R.id.tvSize);
ivStatus = itemView.findViewById(R.id.ivStatus);
@ -79,10 +82,12 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
private void wire() {
itemView.setOnClickListener(this);
ivDelete.setOnClickListener(this);
}
private void unwire() {
itemView.setOnClickListener(null);
ivDelete.setOnClickListener(null);
}
private void bindTo(EntityAttachment attachment) {
@ -103,6 +108,8 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
ivStatus.setVisibility(View.VISIBLE);
}
ivDelete.setVisibility(readonly ? View.INVISIBLE : View.VISIBLE);
if (attachment.progress != null)
progressbar.setProgress(attachment.progress);
progressbar.setVisibility(
@ -120,7 +127,24 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
if (pos == RecyclerView.NO_POSITION)
return;
final EntityAttachment attachment = filtered.get(pos);
if (attachment != null)
if (view.getId() == R.id.ivDelete) {
Bundle args = new Bundle();
args.putLong("id", attachment.id);
new SimpleTask<Void>() {
@Override
protected Void onLoad(Context context, Bundle args) {
DB.getInstance(context).attachment().deleteAttachment(attachment.id);
File dir = new File(context.getFilesDir(), "attachments");
File file = new File(dir, attachment.filename);
file.delete();
return null;
}
}.load(context, owner, args);
} else {
if (attachment.filename == null) {
if (attachment.progress == null) {
Bundle args = new Bundle();
@ -183,12 +207,14 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
context.startActivity(intent);
}
}
}
}
AdapterAttachment(Context context, LifecycleOwner owner) {
AdapterAttachment(Context context, LifecycleOwner owner, boolean readonly) {
this.context = context;
this.owner = owner;
this.readonly = readonly;
this.debug = PreferenceManager.getDefaultSharedPreferences(context).getBoolean("debug", false);
setHasStableIds(true);
}

View File

@ -57,4 +57,7 @@ public interface DaoAttachment {
@Update
void updateAttachment(EntityAttachment attachment);
@Query("DELETE FROM attachment WHERE id = :id")
int deleteAttachment(long id);
}

View File

@ -247,7 +247,7 @@ public class FragmentCompose extends FragmentEx {
LinearLayoutManager llm = new LinearLayoutManager(getContext());
rvAttachment.setLayoutManager(llm);
adapter = new AdapterAttachment(getContext(), getViewLifecycleOwner());
adapter = new AdapterAttachment(getContext(), getViewLifecycleOwner(), false);
rvAttachment.setAdapter(adapter);
return view;

View File

@ -227,7 +227,7 @@ public class FragmentMessage extends FragmentEx {
LinearLayoutManager llm = new LinearLayoutManager(getContext());
rvAttachment.setLayoutManager(llm);
adapter = new AdapterAttachment(getContext(), getViewLifecycleOwner());
adapter = new AdapterAttachment(getContext(), getViewLifecycleOwner(), true);
rvAttachment.setAdapter(adapter);
return view;

View File

@ -6,13 +6,21 @@
android:layout_marginBottom="6dp"
android:layout_marginTop="6dp">
<ImageView
android:id="@+id/ivDelete"
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@drawable/baseline_delete_24"
android:visibility="visible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/ivAttachments"
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@drawable/baseline_attachment_24"
app:layout_constraintEnd_toStartOf="@+id/tvName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintStart_toEndOf="@id/ivDelete"
app:layout_constraintTop_toTopOf="parent" />
<TextView