Refactoring

pull/167/head
M66B 5 years ago
parent 071ad5f85a
commit 884826b61b

@ -34,9 +34,7 @@ import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
@ -44,6 +42,8 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.constraintlayout.widget.Group;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.snackbar.Snackbar;
import com.sun.mail.imap.IMAPFolder;
@ -69,7 +69,6 @@ public class ActivityEML extends ActivityBase {
private Uri uri;
private Result result;
private MessageHelper.AttachmentPart apart;
private static final int REQUEST_ATTACHMENT = 1;
@Override
@ -82,27 +81,14 @@ public class ActivityEML extends ActivityBase {
final TextView tvTo = findViewById(R.id.tvTo);
final TextView tvFrom = findViewById(R.id.tvFrom);
final TextView tvSubject = findViewById(R.id.tvSubject);
final ListView lvAttachment = findViewById(R.id.lvAttachment);
final RecyclerView rvAttachment = findViewById(R.id.rvAttachment);
final TextView tvBody = findViewById(R.id.tvBody);
final ContentLoadingProgressBar pbWait = findViewById(R.id.pbWait);
final Group grpReady = findViewById(R.id.grpReady);
lvAttachment.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
apart = result.parts.getAttachmentParts().get(position);
Intent create = new Intent(Intent.ACTION_CREATE_DOCUMENT);
create.addCategory(Intent.CATEGORY_OPENABLE);
create.setType(apart.attachment.getMimeType());
if (!TextUtils.isEmpty(apart.attachment.name))
create.putExtra(Intent.EXTRA_TITLE, apart.attachment.name);
if (create.resolveActivity(getPackageManager()) == null)
ToastEx.makeText(ActivityEML.this, R.string.title_no_saf, Toast.LENGTH_LONG).show();
else
startActivityForResult(Helper.getChooser(ActivityEML.this, create), REQUEST_ATTACHMENT);
}
});
rvAttachment.setHasFixedSize(false);
LinearLayoutManager llm = new LinearLayoutManager(this);
rvAttachment.setLayoutManager(llm);
grpReady.setVisibility(View.GONE);
@ -183,9 +169,27 @@ public class ActivityEML extends ActivityBase {
attachments.add(sb.toString());
}
ArrayAdapter adapter = new ArrayAdapter<>(
ActivityEML.this, R.layout.list_item1, android.R.id.text1, attachments);
lvAttachment.setAdapter(adapter);
AdapterAttachmentEML adapter = new AdapterAttachmentEML(
ActivityEML.this,
result.parts.getAttachmentParts(),
new AdapterAttachmentEML.IEML() {
@Override
public void onSelected(MessageHelper.AttachmentPart apart) {
ActivityEML.this.apart = apart;
Intent create = new Intent(Intent.ACTION_CREATE_DOCUMENT);
create.addCategory(Intent.CATEGORY_OPENABLE);
create.setType(apart.attachment.getMimeType());
if (!TextUtils.isEmpty(apart.attachment.name))
create.putExtra(Intent.EXTRA_TITLE, apart.attachment.name);
if (create.resolveActivity(getPackageManager()) == null)
ToastEx.makeText(ActivityEML.this, R.string.title_no_saf, Toast.LENGTH_LONG).show();
else
startActivityForResult(Helper.getChooser(ActivityEML.this, create), REQUEST_ATTACHMENT);
}
});
rvAttachment.setAdapter(adapter);
tvBody.setText(result.body);
grpReady.setVisibility(View.VISIBLE);

@ -154,7 +154,9 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION)
return;
final EntityAttachment attachment = items.get(pos);
EntityAttachment attachment = items.get(pos);
if (attachment == null)
return;
if (view.getId() == R.id.ibDelete)
onDelete(attachment);

@ -0,0 +1,120 @@
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail 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.
FairEmail 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 FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2019 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class AdapterAttachmentEML extends RecyclerView.Adapter<AdapterAttachmentEML.ViewHolder> {
private LayoutInflater inflater;
private IEML intf;
private List<MessageHelper.AttachmentPart> aparts;
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private View view;
private TextView tvName;
private TextView tvSize;
private TextView tvType;
ViewHolder(View itemView) {
super(itemView);
view = itemView.findViewById(R.id.clItem);
tvName = itemView.findViewById(R.id.tvName);
tvSize = itemView.findViewById(R.id.tvSize);
tvType = itemView.findViewById(R.id.tvType);
}
private void wire() {
view.setOnClickListener(this);
}
private void unwire() {
view.setOnClickListener(null);
}
private void bindTo(MessageHelper.AttachmentPart apart) {
tvName.setText(apart.attachment.name);
if (apart.attachment.size != null)
tvSize.setText(Helper.humanReadableByteCount(apart.attachment.size, true));
tvSize.setVisibility(apart.attachment.size == null ? View.GONE : View.VISIBLE);
StringBuilder sb = new StringBuilder();
sb.append(apart.attachment.type);
if (apart.attachment.disposition != null)
sb.append(' ').append(apart.attachment.disposition);
tvType.setText(sb.toString());
}
@Override
public void onClick(View view) {
int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION)
return;
MessageHelper.AttachmentPart apart = aparts.get(pos);
if (apart != null)
intf.onSelected(apart);
}
}
AdapterAttachmentEML(Context context, List<MessageHelper.AttachmentPart> aparts, IEML intf) {
this.inflater = LayoutInflater.from(context);
this.aparts = aparts;
this.intf = intf;
setHasStableIds(false);
}
@Override
public int getItemCount() {
return aparts.size();
}
@Override
@NonNull
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(inflater.inflate(R.layout.item_attachment_eml, parent, false));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.unwire();
MessageHelper.AttachmentPart apart = aparts.get(position);
holder.bindTo(apart);
holder.wire();
}
interface IEML {
void onSelected(MessageHelper.AttachmentPart apart);
}
}

@ -1,46 +0,0 @@
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail 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.
FairEmail 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 FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2019 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;
public class ListViewEx extends ListView {
public ListViewEx(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ListViewEx(Context context) {
super(context);
}
public ListViewEx(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(
widthMeasureSpec,
MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST));
}
}

@ -103,8 +103,8 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvSubject" />
<eu.faircode.email.ListViewEx
android:id="@+id/lvAttachment"
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvAttachment"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
@ -119,7 +119,7 @@
android:background="?attr/colorSeparator"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/lvAttachment" />
app:layout_constraintTop_toBottomOf="@id/rvAttachment" />
<TextView
android:id="@+id/tvBody"
@ -142,7 +142,7 @@
vSeparatorHeader,tvFromTitle,tvFrom,
tvToTitle,tvTo,
tvSubject,
vSeparatorAttachments,lvAttachment,
vSeparatorAttachments,rvAttachment,
vSeparatorBody,tvBody" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/clItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/activatableItemBackground"
android:foreground="?attr/selectableItemBackground"
android:padding="3dp">
<ImageView
android:id="@+id/ivAttachments"
android:layout_width="24dp"
android:layout_height="24dp"
android:contentDescription="@string/title_legend_attachment"
app:layout_constraintBottom_toBottomOf="@+id/tvType"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/baseline_attachment_24" />
<TextView
android:id="@+id/tvName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:layout_marginEnd="6dp"
android:ellipsize="middle"
android:singleLine="true"
android:text="Name"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintEnd_toStartOf="@+id/tvSize"
app:layout_constraintHorizontal_weight="4"
app:layout_constraintStart_toEndOf="@id/ivAttachments"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvType"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ellipsize="start"
android:singleLine="true"
android:text="text/plain"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintEnd_toStartOf="@+id/tvSize"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toStartOf="@id/tvName"
app:layout_constraintTop_toBottomOf="@id/tvName" />
<TextView
android:id="@+id/tvSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:text="10 kB"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintBottom_toBottomOf="@id/tvType"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/tvType"
app:layout_constraintTop_toTopOf="@id/tvName" />
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

@ -1,17 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:padding="3dp">
<TextView
android:id="@android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text1"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Loading…
Cancel
Save