From 19a2c6ec382914500228aa5948f9646eb6e22a2b Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 11 Jul 2022 14:09:26 +0200 Subject: [PATCH] Added extra button to edit signature --- .../java/eu/faircode/email/ActivitySetup.java | 6 +- .../eu/faircode/email/ActivitySignature.java | 8 +- .../java/eu/faircode/email/DaoIdentity.java | 3 + .../email/FragmentDialogSelectIdentity.java | 104 ++++++++++++++++++ .../java/eu/faircode/email/FragmentSetup.java | 47 ++++++++ app/src/main/res/layout/fragment_setup.xml | 17 ++- 6 files changed, 179 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/eu/faircode/email/FragmentDialogSelectIdentity.java diff --git a/app/src/main/java/eu/faircode/email/ActivitySetup.java b/app/src/main/java/eu/faircode/email/ActivitySetup.java index 43b3692002..aff181fbee 100644 --- a/app/src/main/java/eu/faircode/email/ActivitySetup.java +++ b/app/src/main/java/eu/faircode/email/ActivitySetup.java @@ -146,8 +146,10 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac static final int REQUEST_IMPORT_CERTIFICATE = 7; static final int REQUEST_OAUTH = 8; static final int REQUEST_STILL = 9; - static final int REQUEST_DELETE_ACCOUNT = 10; - static final int REQUEST_IMPORT_PROVIDERS = 11; + static final int REQUEST_SELECT_IDENTITY = 10; + static final int REQUEST_EDIT_SIGNATURE = 11; + static final int REQUEST_DELETE_ACCOUNT = 12; + static final int REQUEST_IMPORT_PROVIDERS = 13; static final int PI_MISC = 1; diff --git a/app/src/main/java/eu/faircode/email/ActivitySignature.java b/app/src/main/java/eu/faircode/email/ActivitySignature.java index 43f0c82e34..ea940a7ecb 100644 --- a/app/src/main/java/eu/faircode/email/ActivitySignature.java +++ b/app/src/main/java/eu/faircode/email/ActivitySignature.java @@ -304,14 +304,18 @@ public class ActivitySignature extends ActivityBase { } private void delete() { - Intent result = new Intent(); + Intent result = getIntent(); + if (result == null) + result = new Intent(); result.putExtra("html", (String) null); setResult(RESULT_OK, result); finish(); } private void save() { - Intent result = new Intent(); + Intent result = getIntent(); + if (result == null) + result = new Intent(); result.putExtra("html", getHtml()); setResult(RESULT_OK, result); finish(); diff --git a/app/src/main/java/eu/faircode/email/DaoIdentity.java b/app/src/main/java/eu/faircode/email/DaoIdentity.java index 930c7ffbd6..79ae814e60 100644 --- a/app/src/main/java/eu/faircode/email/DaoIdentity.java +++ b/app/src/main/java/eu/faircode/email/DaoIdentity.java @@ -144,6 +144,9 @@ public interface DaoIdentity { @Query("UPDATE identity SET max_size = :max_size WHERE id = :id AND NOT (max_size IS :max_size)") int setIdentityMaxSize(long id, Long max_size); + @Query("UPDATE identity SET signature = :hmtl WHERE id = :id AND NOT (signature IS :hmtl)") + int setIdentitySignature(long id, String hmtl); + @Query("UPDATE identity SET error = :error WHERE id = :id AND NOT (error IS :error)") int setIdentityError(long id, String error); diff --git a/app/src/main/java/eu/faircode/email/FragmentDialogSelectIdentity.java b/app/src/main/java/eu/faircode/email/FragmentDialogSelectIdentity.java new file mode 100644 index 0000000000..52b53d02be --- /dev/null +++ b/app/src/main/java/eu/faircode/email/FragmentDialogSelectIdentity.java @@ -0,0 +1,104 @@ +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 . + + Copyright 2018-2022 by Marcel Bokhorst (M66B) +*/ + +import static android.app.Activity.RESULT_OK; + +import android.app.Dialog; +import android.content.Context; +import android.content.DialogInterface; +import android.graphics.Color; +import android.os.Bundle; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AlertDialog; + +import java.util.List; + +public class FragmentDialogSelectIdentity extends FragmentDialogBase { + @NonNull + @Override + public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { + final Context context = getContext(); + + final int dp6 = Helper.dp2pixels(context, 6); + final int dp12 = Helper.dp2pixels(context, 12); + + final ArrayAdapter adapter = new ArrayAdapter(context, R.layout.spinner_account, android.R.id.text1) { + @NonNull + @Override + public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { + View view = super.getView(position, convertView, parent); + TupleIdentityEx identity = (TupleIdentityEx) getItem(position); + + View vwColor = view.findViewById(R.id.vwColor); + TextView tv = view.findViewById(android.R.id.text1); + + int vpad = (getCount() > 10 ? dp6 : dp12); + tv.setPadding(0, vpad, 0, vpad); + + vwColor.setBackgroundColor(identity.color == null ? Color.TRANSPARENT : identity.color); + tv.setText(identity.name); + + return view; + } + }; + + // TODO: spinner + new SimpleTask>() { + @Override + protected List onExecute(Context context, Bundle args) { + DB db = DB.getInstance(context); + return db.identity().getComposableIdentities(null); + } + + @Override + protected void onExecuted(Bundle args, List identities) { + adapter.addAll(identities); + } + + @Override + protected void onException(Bundle args, Throwable ex) { + Log.unexpectedError(getParentFragmentManager(), ex); + } + }.execute(this, getArguments(), "select:identity"); + + return new AlertDialog.Builder(context) + .setIcon(R.drawable.twotone_person_24) + .setTitle(R.string.title_list_identities) + .setAdapter(adapter, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + TupleIdentityEx identity = adapter.getItem(which); + Bundle args = getArguments(); + args.putLong("id", identity.id); + args.putString("html", identity.signature); + sendResult(RESULT_OK); + } + }) + .setNegativeButton(android.R.string.cancel, null) + .create(); + } +} \ No newline at end of file diff --git a/app/src/main/java/eu/faircode/email/FragmentSetup.java b/app/src/main/java/eu/faircode/email/FragmentSetup.java index 3a23c65a13..22aadeae11 100644 --- a/app/src/main/java/eu/faircode/email/FragmentSetup.java +++ b/app/src/main/java/eu/faircode/email/FragmentSetup.java @@ -121,6 +121,7 @@ public class FragmentSetup extends FragmentBase { private CardView cardExtra; private TextView tvExtra; private Button btnNotification; + private Button btnSignature; private Button btnReorderAccounts; private Button btnReorderFolders; private Button btnDelete; @@ -202,6 +203,7 @@ public class FragmentSetup extends FragmentBase { cardExtra = view.findViewById(R.id.cardExtra); tvExtra = view.findViewById(R.id.tvExtra); btnNotification = view.findViewById(R.id.btnNotification); + btnSignature = view.findViewById(R.id.btnSignature); btnReorderAccounts = view.findViewById(R.id.btnReorderAccounts); btnReorderFolders = view.findViewById(R.id.btnReorderFolders); btnDelete = view.findViewById(R.id.btnDelete); @@ -657,6 +659,16 @@ public class FragmentSetup extends FragmentBase { } }); + btnSignature.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + FragmentDialogSelectIdentity fragment = new FragmentDialogSelectIdentity(); + fragment.setArguments(new Bundle()); + fragment.setTargetFragment(FragmentSetup.this, ActivitySetup.REQUEST_SELECT_IDENTITY); + fragment.show(getParentFragmentManager(), "select:identity"); + } + }); + btnReorderAccounts.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -1008,6 +1020,14 @@ public class FragmentSetup extends FragmentBase { try { switch (requestCode) { + case ActivitySetup.REQUEST_SELECT_IDENTITY: + if (resultCode == RESULT_OK && data != null) + onSelectIdentity(data.getBundleExtra("args")); + break; + case ActivitySetup.REQUEST_EDIT_SIGNATURE: + if (resultCode == RESULT_OK && data != null) + onEditIdentity(data.getExtras()); + break; case ActivitySetup.REQUEST_DELETE_ACCOUNT: if (resultCode == RESULT_OK && data != null) onDeleteAccount(data.getBundleExtra("args")); @@ -1068,6 +1088,33 @@ public class FragmentSetup extends FragmentBase { btnPermissions.setEnabled(!all); } + private void onSelectIdentity(Bundle args) { + Intent intent = new Intent(getContext(), ActivitySignature.class); + intent.putExtra("id", args.getLong("id")); + intent.putExtra("html", args.getString("html")); + startActivityForResult(intent, ActivitySetup.REQUEST_EDIT_SIGNATURE); + } + + private void onEditIdentity(Bundle args) { + new SimpleTask() { + @Override + protected Void onExecute(Context context, Bundle args) throws Throwable { + long id = args.getLong("id"); + String html = args.getString("html"); + + DB db = DB.getInstance(context); + db.identity().setIdentitySignature(id, html); + + return null; + } + + @Override + protected void onException(Bundle args, Throwable ex) { + Log.unexpectedError(getParentFragmentManager(), ex); + } + }.execute(this, args, "set:signature"); + } + private void onDeleteAccount(Bundle args) { long account = args.getLong("account"); String name = args.getString("name"); diff --git a/app/src/main/res/layout/fragment_setup.xml b/app/src/main/res/layout/fragment_setup.xml index af135608ab..4709950cc5 100644 --- a/app/src/main/res/layout/fragment_setup.xml +++ b/app/src/main/res/layout/fragment_setup.xml @@ -954,6 +954,19 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/btnNotification" /> +