Browse Source

Standard answers, refactoring, cleanup

main
M66B 6 years ago
parent
commit
aa54dabfcb
15 changed files with 268 additions and 41 deletions
  1. +12
    -0
      app/src/main/java/eu/faircode/email/ActivityView.java
  2. +28
    -2
      app/src/main/java/eu/faircode/email/AdapterAnswer.java
  3. +3
    -0
      app/src/main/java/eu/faircode/email/DaoAnswer.java
  4. +110
    -5
      app/src/main/java/eu/faircode/email/FragmentAnswer.java
  5. +8
    -1
      app/src/main/java/eu/faircode/email/FragmentCompose.java
  6. +42
    -0
      app/src/main/java/eu/faircode/email/FragmentMessage.java
  7. +18
    -5
      app/src/main/res/layout/fragment_answer.xml
  8. +8
    -1
      app/src/main/res/layout/fragment_message.xml
  9. +1
    -16
      app/src/main/res/layout/fragment_messages.xml
  10. +11
    -0
      app/src/main/res/layout/item_answer.xml
  11. +16
    -0
      app/src/main/res/menu/action_answer.xml
  12. +0
    -10
      app/src/main/res/menu/action_list_bottom.xml
  13. +0
    -0
      app/src/main/res/menu/action_view.xml
  14. +6
    -0
      app/src/main/res/menu/menu_view.xml
  15. +5
    -1
      app/src/main/res/values/strings.xml

+ 12
- 0
app/src/main/java/eu/faircode/email/ActivityView.java View File

@ -99,6 +99,7 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
static final String ACTION_VIEW_MESSAGES = BuildConfig.APPLICATION_ID + ".VIEW_MESSAGES";
static final String ACTION_VIEW_MESSAGE = BuildConfig.APPLICATION_ID + ".VIEW_MESSAGE";
static final String ACTION_EDIT_FOLDER = BuildConfig.APPLICATION_ID + ".EDIT_FOLDER";
static final String ACTION_EDIT_ANSWER = BuildConfig.APPLICATION_ID + ".EDIT_ANSWER";
static final String ACTION_STORE_ATTACHMENT = BuildConfig.APPLICATION_ID + ".STORE_ATTACHMENT";
static final String ACTION_PURCHASE = BuildConfig.APPLICATION_ID + ".ACTION_PURCHASE";
static final String ACTION_ACTIVATE_PRO = BuildConfig.APPLICATION_ID + ".ACTIVATE_PRO";
@ -362,6 +363,7 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
iff.addAction(ACTION_VIEW_MESSAGES);
iff.addAction(ACTION_VIEW_MESSAGE);
iff.addAction(ACTION_EDIT_FOLDER);
iff.addAction(ACTION_EDIT_ANSWER);
iff.addAction(ACTION_STORE_ATTACHMENT);
iff.addAction(ACTION_PURCHASE);
iff.addAction(ACTION_ACTIVATE_PRO);
@ -636,6 +638,8 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
onViewMessage(intent);
else if (ACTION_EDIT_FOLDER.equals(intent.getAction()))
onEditFolder(intent);
else if (ACTION_EDIT_ANSWER.equals(intent.getAction()))
onEditAnswer(intent);
else if (ACTION_STORE_ATTACHMENT.equals(intent.getAction()))
onStoreAttachment(intent);
else if (ACTION_PURCHASE.equals(intent.getAction()))
@ -702,6 +706,14 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
fragmentTransaction.commit();
}
private void onEditAnswer(Intent intent) {
FragmentAnswer fragment = new FragmentAnswer();
fragment.setArguments(intent.getExtras());
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("answer");
fragmentTransaction.commit();
}
private void onStoreAttachment(Intent intent) {
attachment = intent.getLongExtra("id", -1);
Intent create = new Intent(Intent.ACTION_CREATE_DOCUMENT);


+ 28
- 2
app/src/main/java/eu/faircode/email/AdapterAnswer.java View File

@ -20,6 +20,7 @@ package eu.faircode.email;
*/
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
@ -34,6 +35,7 @@ import java.util.List;
import java.util.Locale;
import androidx.annotation.NonNull;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListUpdateCallback;
import androidx.recyclerview.widget.RecyclerView;
@ -44,7 +46,7 @@ public class AdapterAnswer extends RecyclerView.Adapter<AdapterAnswer.ViewHolder
private List<EntityAnswer> all = new ArrayList<>();
private List<EntityAnswer> filtered = new ArrayList<>();
public class ViewHolder extends RecyclerView.ViewHolder {
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
View itemView;
TextView tvName;
@ -55,9 +57,31 @@ public class AdapterAnswer extends RecyclerView.Adapter<AdapterAnswer.ViewHolder
tvName = itemView.findViewById(R.id.tvName);
}
private void wire() {
itemView.setOnClickListener(this);
}
private void unwire() {
itemView.setOnClickListener(null);
}
private void bindTo(EntityAnswer answer) {
tvName.setText(answer.name);
}
@Override
public void onClick(View v) {
int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION)
return;
EntityAnswer answer = filtered.get(pos);
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.sendBroadcast(
new Intent(ActivityView.ACTION_EDIT_ANSWER)
.putExtra("id", answer.id));
}
}
AdapterAnswer(Context context) {
@ -157,12 +181,14 @@ public class AdapterAnswer extends RecyclerView.Adapter<AdapterAnswer.ViewHolder
@Override
@NonNull
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_folder, parent, false));
return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_answer, parent, false));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.unwire();
EntityAnswer answer = filtered.get(position);
holder.bindTo(answer);
holder.wire();
}
}

+ 3
- 0
app/src/main/java/eu/faircode/email/DaoAnswer.java View File

@ -29,6 +29,9 @@ import androidx.room.Update;
@Dao
public interface DaoAnswer {
@Query("SELECT * FROM answer WHERE id = :id")
EntityAnswer getAnswer(long id);
@Query("SELECT * FROM answer")
LiveData<List<EntityAnswer>> liveAnswers();


+ 110
- 5
app/src/main/java/eu/faircode/email/FragmentAnswer.java View File

@ -19,12 +19,17 @@ package eu.faircode.email;
Copyright 2018 by Marcel Bokhorst (M66B)
*/
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -35,9 +40,21 @@ public class FragmentAnswer extends FragmentEx {
private ViewGroup view;
private TextView etName;
private TextView etText;
private BottomNavigationView bottom_navigation;
private ProgressBar pbWait;
private Group grpReady;
private long id = -1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get arguments
Bundle args = getArguments();
id = (args == null ? -1 : args.getLong("id", -1));
}
@Override
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
@ -46,9 +63,27 @@ public class FragmentAnswer extends FragmentEx {
// Get controls
etName = view.findViewById(R.id.etName);
etText = view.findViewById(R.id.etText);
etText = view.findViewById(R.id.etText);
bottom_navigation = view.findViewById(R.id.bottom_navigation);
pbWait = view.findViewById(R.id.pbWait);
grpReady = view.findViewById(R.id.grpReady);
bottom_navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.action_trash:
onActionTrash();
return true;
case R.id.action_save:
onActionSave();
return true;
}
return false;
}
});
// Initialize
grpReady.setVisibility(View.GONE);
pbWait.setVisibility(View.VISIBLE);
@ -59,17 +94,87 @@ public class FragmentAnswer extends FragmentEx {
public void onActivityCreated(@Nullable final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Get arguments
Bundle args = getArguments();
long id = (args == null ? -1 : args.getLong("id"));
DB.getInstance(getContext()).answer().liveAnswer(id).observe(getViewLifecycleOwner(), new Observer<EntityAnswer>() {
@Override
public void onChanged(EntityAnswer entityAnswer) {
public void onChanged(EntityAnswer answer) {
etName.setText(answer == null ? null : answer.name);
etText.setText(answer == null ? null : answer.text);
bottom_navigation.findViewById(R.id.action_trash).setVisibility(answer == null ? View.GONE : View.VISIBLE);
pbWait.setVisibility(View.GONE);
grpReady.setVisibility(View.VISIBLE);
}
});
}
private void onActionTrash() {
Helper.setViewsEnabled(view, false);
Bundle args = new Bundle();
args.putLong("id", id);
new SimpleTask<Void>() {
@Override
protected Void onLoad(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
DB.getInstance(context).answer().deleteAnswer(id);
return null;
}
@Override
protected void onLoaded(Bundle args, Void data) {
finish();
}
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.setViewsEnabled(view, true);
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
}
}.load(this, args);
}
private void onActionSave() {
Helper.setViewsEnabled(view, false);
Bundle args = new Bundle();
args.putLong("id", id);
args.putString("name", etName.getText().toString());
args.putString("text", etText.getText().toString());
new SimpleTask<Void>() {
@Override
protected Void onLoad(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
String name = args.getString("name");
String text = args.getString("text");
DB db = DB.getInstance(context);
if (id < 0) {
EntityAnswer answer = new EntityAnswer();
answer.name = name;
answer.text = text;
answer.id = db.answer().insertAnswer(answer);
} else {
EntityAnswer answer = db.answer().getAnswer(id);
answer.name = name;
answer.text = text;
db.answer().updateAnswer(answer);
}
return null;
}
@Override
protected void onLoaded(Bundle args, Void data) {
finish();
}
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.setViewsEnabled(view, true);
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
}
}.load(this, args);
}
}

+ 8
- 1
app/src/main/java/eu/faircode/email/FragmentCompose.java View File

@ -357,6 +357,7 @@ public class FragmentCompose extends FragmentEx {
args.putLong("id", getArguments().getLong("id", -1));
args.putLong("account", getArguments().getLong("account", -1));
args.putLong("reference", getArguments().getLong("reference", -1));
args.putLong("answer", getArguments().getLong("answer", -1));
args.putString("to", getArguments().getString("to"));
args.putString("cc", getArguments().getString("cc"));
args.putString("bcc", getArguments().getString("bcc"));
@ -372,6 +373,7 @@ public class FragmentCompose extends FragmentEx {
args.putLong("id", savedInstanceState.getLong("working"));
args.putLong("account", -1);
args.putLong("reference", -1);
args.putLong("answer", -1);
draftLoader.load(this, args);
}
}
@ -697,6 +699,7 @@ public class FragmentCompose extends FragmentEx {
long id = args.getLong("id", -1);
long account = args.getLong("account", -1);
long reference = args.getLong("reference", -1);
long answer = args.getLong("answer", -1);
Log.i(Helper.TAG, "Load draft action=" + action + " id=" + id + " account=" + account + " reference=" + reference);
@ -796,8 +799,12 @@ public class FragmentCompose extends FragmentEx {
}
if ("reply".equals(action) || "reply_all".equals(action)) {
String text = "";
if (answer > 0)
text = db.answer().getAnswer(answer).text;
draft.subject = context.getString(R.string.title_subject_reply, ref.subject);
body = String.format("<br><br>%s %s:<br><br>%s",
body = String.format("%s<br><br>%s %s:<br><br>%s",
Html.escapeHtml(text).replaceAll("\\r?\\n", "<br />"),
Html.escapeHtml(new Date().toString()),
Html.escapeHtml(MessageHelper.getFormattedAddresses(draft.to, true)),
HtmlHelper.sanitize(context, ref.read(context), true));


+ 42
- 0
app/src/main/java/eu/faircode/email/FragmentMessage.java View File

@ -99,6 +99,7 @@ import static android.app.Activity.RESULT_OK;
public class FragmentMessage extends FragmentEx {
private ViewGroup view;
private View vwAnswerAnchor;
private TextView tvFrom;
private TextView tvSize;
private TextView tvTime;
@ -166,6 +167,7 @@ public class FragmentMessage extends FragmentEx {
debug = prefs.getBoolean("debug", false);
// Get controls
vwAnswerAnchor = view.findViewById(R.id.vwAnswerAnchor);
tvFrom = view.findViewById(R.id.tvFrom);
tvSize = view.findViewById(R.id.tvSize);
tvTime = view.findViewById(R.id.tvTime);
@ -668,6 +670,9 @@ public class FragmentMessage extends FragmentEx {
case R.id.menu_reply_all:
onMenuReplyAll();
return true;
case R.id.menu_answer:
onMenuAnswer();
return true;
case R.id.menu_decrypt:
onMenuDecrypt();
return true;
@ -749,6 +754,43 @@ public class FragmentMessage extends FragmentEx {
.putExtra("reference", message.id));
}
private void onMenuAnswer() {
DB.getInstance(getContext()).answer().liveAnswers().observe(getViewLifecycleOwner(), new Observer<List<EntityAnswer>>() {
@Override
public void onChanged(List<EntityAnswer> answers) {
final Collator collator = Collator.getInstance(Locale.getDefault());
collator.setStrength(Collator.SECONDARY); // Case insensitive, process accents etc
Collections.sort(answers, new Comparator<EntityAnswer>() {
@Override
public int compare(EntityAnswer a1, EntityAnswer a2) {
return collator.compare(a1.name, a2.name);
}
});
PopupMenu popupMenu = new PopupMenu(getContext(), vwAnswerAnchor);
int order = 0;
for (EntityAnswer answer : answers)
popupMenu.getMenu().add(Menu.NONE, answer.id.intValue(), order++,
Helper.localizeFolderName(getContext(), answer.name));
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem target) {
startActivity(new Intent(getContext(), ActivityCompose.class)
.putExtra("action", "reply")
.putExtra("reference", message.id)
.putExtra("answer", (long) target.getItemId()));
return true;
}
});
popupMenu.show();
}
});
}
private void onMenuDecrypt() {
Log.i(Helper.TAG, "On decrypt");
try {


+ 18
- 5
app/src/main/res/layout/fragment_answer.xml View File

@ -8,10 +8,10 @@
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:layout_marginEnd="6dp"
android:layout_marginStart="6dp"
android:background="@null"
android:hint="@string/title_body_hint"
android:hint="@string/title_answer_name"
android:inputType="textCapSentences"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintStart_toStartOf="parent"
@ -34,7 +34,7 @@
android:layout_marginStart="6dp"
android:fillViewport="true"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/vSeparator">
@ -45,11 +45,24 @@
android:background="@null"
android:fontFamily="monospace"
android:gravity="top"
android:hint="@string/title_body_hint"
android:hint="@string/title_answer_text"
android:inputType="textCapSentences|textMultiLine"
android:textAppearance="@style/TextAppearance.AppCompat.Small" />
</ScrollView>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
app:itemIconTint="@color/bottomnav_background"
app:itemTextColor="@color/bottomnav_background"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/action_answer" />
<ProgressBar
android:id="@+id/pbWait"
style="@style/Base.Widget.AppCompat.ProgressBar"
@ -65,5 +78,5 @@
android:id="@+id/grpReady"
android:layout_width="0dp"
android:layout_height="0dp"
app:constraint_referenced_ids="etName,vSeparator,scroll" />
app:constraint_referenced_ids="etName,vSeparator,scroll,bottom_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>

+ 8
- 1
app/src/main/res/layout/fragment_message.xml View File

@ -7,6 +7,13 @@
android:orientation="vertical"
tools:context=".ActivityView">
<View
android:id="@+id/vwAnswerAnchor"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvFrom"
android:layout_width="0dp"
@ -309,7 +316,7 @@
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/action_view_bottom" />
app:menu="@menu/action_view" />
<ProgressBar
android:id="@+id/pbWait"


+ 1
- 16
app/src/main/res/layout/fragment_messages.xml View File

@ -23,25 +23,10 @@
android:layout_height="0dp"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical"
app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:background="@color/colorPrimary"
android:visibility="gone"
app:itemIconTint="@color/bottomnav_background"
app:itemTextColor="@color/bottomnav_background"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/action_list_bottom" />
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="@+id/pbWait"


+ 11
- 0
app/src/main/res/layout/item_answer.xml View File

@ -10,6 +10,7 @@
android:layout_height="wrap_content"
android:layout_marginEnd="6dp"
android:layout_marginStart="6dp"
android:layout_marginTop="6dp"
android:ellipsize="end"
android:maxLines="1"
android:text="Name"
@ -17,4 +18,14 @@
app:layout_constraintEnd_toStartOf="@+id/tvSize"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/vSeparator"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="6dp"
android:layout_marginTop="6dp"
android:background="?attr/colorSeparator"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvName" />
</androidx.constraintlayout.widget.ConstraintLayout>

+ 16
- 0
app/src/main/res/menu/action_answer.xml View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_trash"
android:icon="@drawable/baseline_delete_24"
android:title="@string/title_trash"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_save"
android:icon="@drawable/baseline_save_alt_24"
android:title="@string/title_save"
app:showAsAction="ifRoom" />
</menu>

+ 0
- 10
app/src/main/res/menu/action_list_bottom.xml View File

@ -1,10 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_move"
android:icon="@drawable/baseline_folder_24"
android:title="@string/title_move"
app:showAsAction="ifRoom" />
</menu>

app/src/main/res/menu/action_view_bottom.xml → app/src/main/res/menu/action_view.xml View File


+ 6
- 0
app/src/main/res/menu/menu_view.xml View File

@ -32,6 +32,12 @@
android:title="@string/title_reply_all"
app:showAsAction="never" />
<item
android:id="@+id/menu_answer"
android:icon="@drawable/baseline_reply_24"
android:title="@string/title_answer_reply"
app:showAsAction="never" />
<item
android:id="@+id/menu_decrypt"
android:title="@string/title_decrypt"


+ 5
- 1
app/src/main/res/values/strings.xml View File

@ -32,7 +32,7 @@
<string name="title_notification_failed">\'%1$s\' failed</string>
<string name="menu_setup">Setup</string>
<string name="menu_answers">Standard answers</string>
<string name="menu_answers">Answers</string>
<string name="menu_operations">Operations</string>
<string name="menu_legend">Legend</string>
<string name="menu_faq">FAQ</string>
@ -176,6 +176,10 @@
<string name="title_search_hint">Search on server</string>
<string name="title_searching">Searching \'%1$s\'</string>
<string name="title_answer_reply">Standard answer</string>
<string name="title_answer_name">Answer name</string>
<string name="title_answer_text">Answer text</string>
<string name="title_legend_cc">CC/BCC</string>
<string name="title_legend_attachment">Attachment</string>
<string name="title_legend_synchronize">Synchronize</string>


Loading…
Cancel
Save