Refactoring

pull/147/head
M66B 6 years ago
parent f49f398419
commit 533c763457

@ -246,7 +246,6 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
grpExpanded = itemView.findViewById(R.id.grpExpanded);
tvBody.setMovementMethod(new UrlHandler());
}
private void wire() {
@ -306,20 +305,20 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
private void bindTo(int position, final TupleMessageEx message) {
final DB db = DB.getInstance(context);
final boolean show_expanded = properties.isExpanded(message.id);
boolean show_addresses = properties.showAddresses(message.id);
boolean show_headers = properties.showHeaders(message.id);
final boolean show_expanded = properties.getValue("expanded", message.id);
boolean show_addresses = !properties.getValue("addresses", message.id);
boolean show_headers = properties.getValue("headers", message.id);
if (viewType == ViewType.THREAD) {
if (show_expanded) {
if (!properties.isFrozen(message.id)) {
if (!properties.getValue("frozen", message.id)) {
itemView.setHasTransientState(true);
properties.setFrozen(message.id, true);
properties.setValue("frozen", message.id, true);
}
} else {
if (properties.isFrozen(message.id)) {
if (properties.getValue("frozen", message.id)) {
itemView.setHasTransientState(false);
properties.setFrozen(message.id, false);
properties.setValue("frozen", message.id, false);
}
}
}
@ -682,15 +681,15 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
.putExtra("action", "edit")
.putExtra("id", message.id));
else {
boolean expanded = !properties.isExpanded(message.id);
properties.setExpanded(message.id, expanded);
boolean expanded = !properties.getValue("expanded", message.id);
properties.setValue("expanded", message.id, expanded);
notifyItemChanged(pos);
}
}
private void onToggleAddresses(int pos, TupleMessageEx message) {
boolean addresses = !properties.showAddresses(message.id);
properties.setAddresses(message.id, addresses);
boolean addresses = !properties.getValue("addresses", message.id);
properties.setValue("addresses", message.id, addresses);
notifyItemChanged(pos);
}
@ -735,7 +734,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
}
private void onShowImagesConfirmed(final TupleMessageEx message) {
properties.setImages(message.id, true);
properties.setValue("images", message.id, true);
btnImages.setEnabled(false);
Bundle args = new Bundle();
@ -784,8 +783,8 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
SpannedString ss = new SpannedString(body);
boolean has_images = (ss.getSpans(0, ss.length(), ImageSpan.class).length > 0);
boolean show_expanded = properties.isExpanded(message.id);
boolean show_images = properties.showImages(message.id);
boolean show_expanded = properties.getValue("expanded", message.id);
boolean show_images = properties.getValue("images", message.id);
btnHtml.setVisibility(hasWebView && show_expanded ? View.VISIBLE : View.GONE);
btnImages.setVisibility(has_images && show_expanded && !show_images ? View.VISIBLE : View.GONE);
@ -803,7 +802,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
return Html.fromHtml(HtmlHelper.sanitize(body), new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
boolean show = properties.showImages(message.id);
boolean show = properties.getValue("images", message.id);
return HtmlHelper.decodeImage(source, context, message.id, show);
}
}, new Html.TagHandler() {
@ -1088,7 +1087,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
@Override
protected void onLoaded(Bundle args, Void ignored) {
properties.setExpanded(data.message.id, false);
properties.setValue("expanded", data.message.id, false);
notifyDataSetChanged();
}
@ -1131,8 +1130,8 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
}
private void onShowHeaders(ActionData data) {
boolean show_headers = !properties.showHeaders(data.message.id);
properties.setHeaders(data.message.id, show_headers);
boolean show_headers = !properties.getValue("headers", data.message.id);
properties.setValue("headers", data.message.id, show_headers);
if (show_headers && data.message.headers == null) {
grpHeaders.setVisibility(View.VISIBLE);
pbHeaders.setVisibility(View.VISIBLE);
@ -1298,7 +1297,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
}
private void onMore(final ActionData data) {
boolean show_headers = properties.showHeaders(data.message.id);
boolean show_headers = properties.getValue("headers", data.message.id);
View anchor = bnvActions.findViewById(R.id.action_more);
PopupMenu popupMenu = new PopupMenu(context, anchor);
@ -1690,25 +1689,9 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
}
interface IProperties {
void setExpanded(long id, boolean expand);
void setFrozen(long id, boolean freeze);
void setAddresses(long id, boolean show);
void setHeaders(long id, boolean show);
void setImages(long id, boolean show);
boolean isExpanded(long id);
boolean isFrozen(long id);
boolean showAddresses(long id);
boolean showHeaders(long id);
void setValue(String name, long id, boolean enabled);
boolean showImages(long id);
boolean getValue(String name, long id);
void move(long id, String target, boolean type);
}

@ -47,7 +47,9 @@ import com.google.android.material.snackbar.Snackbar;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -116,11 +118,7 @@ public class FragmentMessages extends FragmentEx {
private int autoCloseCount = 0;
private boolean autoExpand = true;
private List<Long> expanded = new ArrayList<>();
private List<Long> frozen = new ArrayList<>();
private List<Long> addresses = new ArrayList<>();
private List<Long> headers = new ArrayList<>();
private List<Long> images = new ArrayList<>();
private Map<String, List<Long>> values = new HashMap<>();
private BoundaryCallbackMessages searchCallback = null;
@ -303,69 +301,22 @@ public class FragmentMessages extends FragmentEx {
zoom,
new AdapterMessage.IProperties() {
@Override
public void setExpanded(long id, boolean expand) {
if (expand) {
expanded.add(id);
handleExpand(id);
public void setValue(String name, long id, boolean enabled) {
if (!values.containsKey(name))
values.put(name, new ArrayList<Long>());
if (enabled) {
values.get(name).add(id);
if ("expanded".equals(name))
handleExpand(id);
} else
expanded.remove(id);
values.get(name).remove(id);
}
@Override
public void setFrozen(long id, boolean freeze) {
if (freeze)
frozen.add(id);
else
frozen.remove(id);
}
@Override
public void setAddresses(long id, boolean show) {
if (show)
addresses.remove(id);
else
addresses.add(id);
}
@Override
public void setHeaders(long id, boolean show) {
if (show)
headers.add(id);
else
headers.remove(id);
}
@Override
public void setImages(long id, boolean show) {
if (show)
images.add(id);
else
images.remove(id);
}
@Override
public boolean isExpanded(long id) {
return expanded.contains(id);
}
@Override
public boolean isFrozen(long id) {
return frozen.contains(id);
}
@Override
public boolean showAddresses(long id) {
return !addresses.contains(id);
}
@Override
public boolean showHeaders(long id) {
return headers.contains(id);
}
@Override
public boolean showImages(long id) {
return images.contains(id);
public boolean getValue(String name, long id) {
if (values.containsKey(name))
return values.get(name).contains(id);
return false;
}
@Override
@ -469,7 +420,7 @@ public class FragmentMessages extends FragmentEx {
TupleMessageEx message = ((AdapterMessage) rvMessage.getAdapter()).getCurrentList().get(pos);
if (message == null ||
expanded.contains(message.id) ||
(values.containsKey("expanded") && values.get("expanded").contains(message.id)) ||
EntityFolder.DRAFTS.equals(message.folderType) ||
EntityFolder.OUTBOX.equals(message.folderType))
return 0;
@ -1201,9 +1152,11 @@ public class FragmentMessages extends FragmentEx {
super.onSaveInstanceState(outState);
outState.putBoolean("autoExpand", autoExpand);
outState.putInt("autoCloseCount", autoCloseCount);
outState.putLongArray("expanded", Helper.toLongArray(expanded));
outState.putLongArray("headers", Helper.toLongArray(headers));
outState.putLongArray("images", Helper.toLongArray(images));
outState.putStringArray("values", values.keySet().toArray(new String[0]));
for (String name : values.keySet())
outState.putLongArray(name, Helper.toLongArray(values.get(name)));
if (selectionTracker != null)
selectionTracker.onSaveInstanceState(outState);
}
@ -1215,9 +1168,14 @@ public class FragmentMessages extends FragmentEx {
if (savedInstanceState != null) {
autoExpand = savedInstanceState.getBoolean("autoExpand");
autoCloseCount = savedInstanceState.getInt("autoCloseCount");
expanded = Helper.fromLongArray(savedInstanceState.getLongArray("expanded"));
headers = Helper.fromLongArray(savedInstanceState.getLongArray("headers"));
images = Helper.fromLongArray(savedInstanceState.getLongArray("images"));
String[] names = savedInstanceState.getStringArray("values");
for (String name : names) {
values.put(name, new ArrayList<Long>());
for (Long value : savedInstanceState.getLongArray(name))
values.get(name).add(value);
}
if (selectionTracker != null)
selectionTracker.onRestoreInstanceState(savedInstanceState);
}
@ -1726,7 +1684,9 @@ public class FragmentMessages extends FragmentEx {
if (expand != null &&
(expand.content || !metered || (expand.size != null && expand.size < download))) {
expanded.add(expand.id);
if (!values.containsKey("expanded"))
values.put("expanded", new ArrayList<Long>());
values.get("expanded").add(expand.id);
handleExpand(expand.id);
}
} else {
@ -1949,8 +1909,8 @@ public class FragmentMessages extends FragmentEx {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
if (prefs.getBoolean("collapse", false))
if (expanded.size() > 0) {
expanded.clear();
if (values.containsKey("expanded") && values.get("expanded").size() > 0) {
values.get("expanded").clear();
adapter.notifyDataSetChanged();
return true;
}

Loading…
Cancel
Save