List attachments in message view

This commit is contained in:
M66B 2018-08-03 13:46:25 +00:00
parent 14efe62e91
commit b130da7bc1
5 changed files with 309 additions and 17 deletions

View File

@ -0,0 +1,182 @@
package eu.faircode.email;
/*
This file is part of Safe email.
Safe email is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NetGuard is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with NetGuard. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.util.DiffUtil;
import android.support.v7.util.ListUpdateCallback;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.ViewHolder> {
private Context context;
private List<EntityAttachment> all = new ArrayList<>();
private List<EntityAttachment> filtered = new ArrayList<>();
public class ViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
View itemView;
TextView tvName;
TextView tvType;
ViewHolder(View itemView) {
super(itemView);
this.itemView = itemView;
tvName = itemView.findViewById(R.id.tvName);
tvType = itemView.findViewById(R.id.tvType);
}
private void wire() {
itemView.setOnClickListener(this);
}
private void unwire() {
itemView.setOnClickListener(null);
}
@Override
public void onClick(View view) {
EntityAttachment attachment = filtered.get(getLayoutPosition());
if (attachment.content == null) {
}
}
}
AdapterAttachment(Context context) {
this.context = context;
setHasStableIds(true);
}
public void set(List<EntityAttachment> attachments) {
Log.i(Helper.TAG, "Set attachments=" + attachments.size());
Collections.sort(attachments, new Comparator<EntityAttachment>() {
@Override
public int compare(EntityAttachment a1, EntityAttachment a2) {
return a1.sequence.compareTo(a2.sequence);
}
});
all.clear();
all.addAll(attachments);
DiffUtil.DiffResult diff = DiffUtil.calculateDiff(new MessageDiffCallback(filtered, all));
filtered.clear();
filtered.addAll(all);
diff.dispatchUpdatesTo(new ListUpdateCallback() {
@Override
public void onInserted(int position, int count) {
Log.i(Helper.TAG, "Inserted @" + position + " #" + count);
}
@Override
public void onRemoved(int position, int count) {
Log.i(Helper.TAG, "Removed @" + position + " #" + count);
}
@Override
public void onMoved(int fromPosition, int toPosition) {
Log.i(Helper.TAG, "Moved " + fromPosition + ">" + toPosition);
}
@Override
public void onChanged(int position, int count, Object payload) {
Log.i(Helper.TAG, "Changed @" + position + " #" + count);
}
});
diff.dispatchUpdatesTo(AdapterAttachment.this);
}
private class MessageDiffCallback extends DiffUtil.Callback {
private List<EntityAttachment> prev;
private List<EntityAttachment> next;
MessageDiffCallback(List<EntityAttachment> prev, List<EntityAttachment> next) {
this.prev = prev;
this.next = next;
}
@Override
public int getOldListSize() {
return prev.size();
}
@Override
public int getNewListSize() {
return next.size();
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
EntityAttachment a1 = prev.get(oldItemPosition);
EntityAttachment a2 = next.get(newItemPosition);
return a1.id.equals(a2.id);
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
EntityAttachment a1 = prev.get(oldItemPosition);
EntityAttachment a2 = next.get(newItemPosition);
return a1.equals(a2);
}
}
@Override
public long getItemId(int position) {
return filtered.get(position).id;
}
@Override
public int getItemCount() {
return filtered.size();
}
@Override
@NonNull
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_attachment, parent, false));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.unwire();
EntityAttachment attachment = filtered.get(position);
holder.tvName.setText(attachment.name);
holder.tvType.setText(attachment.type);
holder.wire();
}
}

View File

@ -19,12 +19,19 @@ package eu.faircode.email;
Copyright 2018 by Marcel Bokhorst (M66B)
*/
import android.arch.lifecycle.LiveData;
import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.OnConflictStrategy;
import android.arch.persistence.room.Query;
import java.util.List;
@Dao
public interface DaoAttachment {
@Query("SELECT * FROM attachment WHERE message = :message")
LiveData<List<EntityAttachment>> liveAttachments(long message);
@Insert(onConflict = OnConflictStrategy.REPLACE)
long insertAttachment(EntityAttachment attachment);
}

View File

@ -33,6 +33,8 @@ import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.Layout;
import android.text.Spannable;
@ -53,6 +55,7 @@ import android.widget.Toast;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@ -62,15 +65,18 @@ public class FragmentMessage extends Fragment {
private TextView tvTo;
private TextView tvCc;
private TextView tvBcc;
private RecyclerView rvAttachment;
private TextView tvSubject;
private TextView tvCount;
private BottomNavigationView top_navigation;
private TextView tvBody;
private BottomNavigationView bottom_navigation;
private ProgressBar pbWait;
private Group grpCc;
private Group grpAddress;
private Group grpAttachments;
private Group grpReady;
private AdapterAttachment adapter;
private LiveData<TupleFolderEx> liveFolder;
private ExecutorService executor = Executors.newCachedThreadPool();
@ -91,6 +97,7 @@ public class FragmentMessage extends Fragment {
tvTo = view.findViewById(R.id.tvTo);
tvCc = view.findViewById(R.id.tvCc);
tvBcc = view.findViewById(R.id.tvBcc);
rvAttachment = view.findViewById(R.id.rvAttachment);
tvTime = view.findViewById(R.id.tvTime);
tvSubject = view.findViewById(R.id.tvSubject);
tvCount = view.findViewById(R.id.tvCount);
@ -98,12 +105,13 @@ public class FragmentMessage extends Fragment {
tvBody = view.findViewById(R.id.tvBody);
bottom_navigation = view.findViewById(R.id.bottom_navigation);
pbWait = view.findViewById(R.id.pbWait);
grpCc = view.findViewById(R.id.grpCc);
grpAddress = view.findViewById(R.id.grpAddress);
grpAttachments = view.findViewById(R.id.grpAttachments);
grpReady = view.findViewById(R.id.grpReady);
setHasOptionsMenu(true);
tvBody.setMovementMethod(new LinkMovementMethod() {
tvBody.setMovementMethod(new LinkMovementMethod() {
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
if (event.getAction() != MotionEvent.ACTION_UP)
return super.onTouchEvent(widget, buffer, event);
@ -130,7 +138,7 @@ public class FragmentMessage extends Fragment {
fragment.setArguments(args);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("link");
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("webview");
fragmentTransaction.commit();
}
return true;
@ -185,11 +193,19 @@ public class FragmentMessage extends Fragment {
});
// Initialize
grpCc.setVisibility(View.GONE);
grpAddress.setVisibility(View.GONE);
grpAttachments.setVisibility(View.GONE);
grpReady.setVisibility(View.GONE);
pbWait.setVisibility(View.VISIBLE);
DB db = DB.getInstance(getContext());
rvAttachment.setHasFixedSize(false);
LinearLayoutManager llm = new LinearLayoutManager(getContext());
rvAttachment.setLayoutManager(llm);
adapter = new AdapterAttachment(getContext());
rvAttachment.setAdapter(adapter);
final DB db = DB.getInstance(getContext());
// Observe folder
liveFolder = db.folder().liveFolderEx(folder);
@ -213,7 +229,6 @@ public class FragmentMessage extends Fragment {
tvTime.setText(message.sent == null ? null : df.format(new Date(message.sent)));
tvSubject.setText(message.subject);
tvCount.setText(Integer.toString(message.count));
tvCount.setVisibility(message.count > 1 ? View.VISIBLE : View.GONE);
int visibility = (message.ui_seen ? Typeface.NORMAL : Typeface.BOLD);
tvFrom.setTypeface(null, visibility);
@ -221,6 +236,10 @@ public class FragmentMessage extends Fragment {
tvSubject.setTypeface(null, visibility);
tvCount.setTypeface(null, visibility);
// Observe attachments
db.attachment().liveAttachments(id).removeObservers(FragmentMessage.this);
db.attachment().liveAttachments(id).observe(FragmentMessage.this, attachmentsObserver);
MenuItem actionSeen = top_navigation.getMenu().findItem(R.id.action_seen);
actionSeen.setIcon(message.ui_seen
? R.drawable.baseline_visibility_off_24
@ -269,7 +288,8 @@ public class FragmentMessage extends Fragment {
}
private void onMenuCc() {
grpCc.setVisibility(grpCc.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
if (grpReady.getVisibility() == View.VISIBLE)
grpAddress.setVisibility(grpAddress.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
}
Observer<TupleFolderEx> folderObserver = new Observer<TupleFolderEx>() {
@ -281,6 +301,14 @@ public class FragmentMessage extends Fragment {
}
};
Observer<List<EntityAttachment>> attachmentsObserver = new Observer<List<EntityAttachment>>() {
@Override
public void onChanged(@Nullable List<EntityAttachment> attachments) {
adapter.set(attachments);
grpAttachments.setVisibility(attachments.size() > 0 ? View.VISIBLE : View.GONE);
}
};
private void onActionSeen(final long id) {
executor.submit(new Runnable() {
@Override

View File

@ -13,6 +13,7 @@
android:layout_height="wrap_content"
android:layout_marginEnd="6dp"
android:layout_marginStart="6dp"
android:layout_marginTop="3dp"
android:text="From"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textIsSelectable="true"
@ -57,10 +58,10 @@
app:layout_constraintTop_toTopOf="@id/tvSubject" />
<View
android:id="@+id/vSeparator"
android:id="@+id/vSeparatorAddress"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="6dp"
android:layout_marginTop="3dp"
android:background="?attr/colorSeparator"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvSubject" />
@ -70,11 +71,12 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:layout_marginTop="3dp"
android:maxLines="1"
android:text="@string/title_to"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/vSeparator" />
app:layout_constraintTop_toBottomOf="@id/vSeparatorAddress" />
<TextView
android:id="@+id/tvTo"
@ -82,13 +84,14 @@
android:layout_height="wrap_content"
android:layout_marginEnd="6dp"
android:layout_marginStart="6dp"
android:layout_marginTop="3dp"
android:maxLines="1"
android:text="To"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textIsSelectable="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/tvToTitle"
app:layout_constraintTop_toTopOf="@id/tvToTitle" />
app:layout_constraintTop_toBottomOf="@id/vSeparatorAddress" />
<TextView
android:id="@+id/tvCcTitle"
@ -113,7 +116,7 @@
android:textIsSelectable="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/tvCcTitle"
app:layout_constraintTop_toTopOf="@id/tvCcTitle" />
app:layout_constraintTop_toBottomOf="@id/tvTo" />
<TextView
android:id="@+id/tvBccTitle"
@ -138,19 +141,43 @@
android:textIsSelectable="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/tvBccTitle"
app:layout_constraintTop_toTopOf="@id/tvBccTitle" />
app:layout_constraintTop_toBottomOf="@id/tvCc" />
<View
android:id="@+id/vSeparatorAttachments"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="3dp"
android:background="?attr/colorSeparator"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvBcc" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rvAttachment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="6dp"
android:layout_marginStart="6dp"
android:layout_marginTop="3dp"
android:maxHeight="90dp"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/vSeparatorAttachments" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/top_navigation"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="3dp"
android:background="@color/darkColorSeparator"
app:itemIconTint="@color/colorActionForeground"
app:itemTextColor="@color/colorActionForeground"
app:labelVisibilityMode="unlabeled"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvBcc"
app:layout_constraintTop_toBottomOf="@id/rvAttachment"
app:menu="@menu/action_view_top" />
<ScrollView
@ -159,6 +186,7 @@
android:layout_height="0dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="3dp"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
app:layout_constraintStart_toStartOf="parent"
@ -178,6 +206,7 @@
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:background="@color/colorPrimary"
app:itemIconTint="@color/colorActionForeground"
app:itemTextColor="@color/colorActionForeground"
@ -199,10 +228,16 @@
app:layout_constraintTop_toBottomOf="parent" />
<android.support.constraint.Group
android:id="@+id/grpCc"
android:id="@+id/grpAddress"
android:layout_width="0dp"
android:layout_height="0dp"
app:constraint_referenced_ids="vSeparator,tvToTitle,tvTo,tvCcTitle,tvCc,tvBccTitle,tvBcc" />
app:constraint_referenced_ids="vSeparatorAddress,tvToTitle,tvTo,tvCcTitle,tvCc,tvBccTitle,tvBcc" />
<android.support.constraint.Group
android:id="@+id/grpAttachments"
android:layout_width="0dp"
android:layout_height="0dp"
app:constraint_referenced_ids="vSeparatorAttachments,rvAttachment" />
<android.support.constraint.Group
android:id="@+id/grpReady"

View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
android:layout_marginBottom="6dp"
android:layout_marginTop="6dp">
<ImageView
android:id="@+id/ivAttachments"
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@drawable/baseline_attachment_24"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:text="Name"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintBottom_toBottomOf="@id/ivAttachments"
app:layout_constraintStart_toEndOf="@id/ivAttachments"
app:layout_constraintTop_toTopOf="@id/ivAttachments" />
<TextView
android:id="@+id/tvType"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:gravity="end"
android:text="Type"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintBottom_toBottomOf="@id/ivAttachments"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/tvName"
app:layout_constraintTop_toTopOf="@id/ivAttachments" />
</android.support.constraint.ConstraintLayout>