Refactoring

This commit is contained in:
M66B
2019-11-11 09:30:33 +01:00
parent 071ad5f85a
commit 884826b61b
7 changed files with 219 additions and 91 deletions

View File

@@ -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);

View File

@@ -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);

View File

@@ -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);
}
}

View File

@@ -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));
}
}