Refactoring

pull/206/head
M66B 3 years ago
parent 874c8fa647
commit 6319cf6a3a

@ -20,11 +20,9 @@ package eu.faircode.email;
*/
import android.Manifest;
import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.BitmapFactory;
@ -42,13 +40,10 @@ import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.PopupMenu;
import androidx.core.content.pm.ShortcutInfoCompat;
import androidx.core.content.pm.ShortcutManagerCompat;
@ -316,8 +311,9 @@ public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHold
args.putLong("id", contact.id);
args.putString("name", contact.name);
FragmentEditName fragment = new FragmentEditName();
FragmentDialogEditName fragment = new FragmentDialogEditName();
fragment.setArguments(args);
fragment.setTargetFragment(parentFragment, FragmentContacts.REQUEST_NAME);
fragment.show(parentFragment.getParentFragmentManager(), "contact:edit");
}
@ -519,48 +515,4 @@ public class AdapterContact extends RecyclerView.Adapter<AdapterContact.ViewHold
holder.bindTo(contact);
holder.wire();
}
public static class FragmentEditName extends FragmentDialogBase {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_edit_name, null);
final EditText etName = view.findViewById(R.id.etName);
etName.setText(getArguments().getString("name"));
return new AlertDialog.Builder(getContext())
.setView(view)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Bundle args = new Bundle();
args.putLong("id", getArguments().getLong("id"));
args.putString("name", etName.getText().toString());
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
long id = args.getLong("id");
String name = args.getString("name");
if (TextUtils.isEmpty(name))
name = null;
DB db = DB.getInstance(context);
db.contact().setContactName(id, name);
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(getContext(), getActivity(), args, "edit:name");
}
})
.setNegativeButton(android.R.string.cancel, null)
.create();
}
}
}

@ -86,6 +86,7 @@ public class FragmentContacts extends FragmentBase {
private static final int REQUEST_ACCOUNT = 1;
private static final int REQUEST_IMPORT = 2;
private static final int REQUEST_EXPORT = 3;
static final int REQUEST_NAME = 4;
@Override
@Nullable
@ -259,6 +260,10 @@ public class FragmentContacts extends FragmentBase {
if (resultCode == RESULT_OK && data != null)
handleExport(data);
break;
case REQUEST_NAME:
if (resultCode == RESULT_OK && data != null)
onEditName(data.getBundleExtra("args"));
break;
}
} catch (Throwable ex) {
Log.e(ex);
@ -429,6 +434,29 @@ public class FragmentContacts extends FragmentBase {
}.execute(this, args, "");
}
private void onEditName(Bundle args) {
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) {
long id = args.getLong("id");
String name = args.getString("name");
if (TextUtils.isEmpty(name))
name = null;
DB db = DB.getInstance(context);
db.contact().setContactName(id, name);
return null;
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getParentFragmentManager(), ex);
}
}.execute(this, args, "edit:name");
}
public static class FragmentDelete extends FragmentDialogBase {
@NonNull
@Override

@ -0,0 +1,55 @@
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-2021 by Marcel Bokhorst (M66B)
*/
import static android.app.Activity.RESULT_OK;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
public class FragmentDialogEditName extends FragmentDialogBase {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_edit_name, null);
final EditText etName = view.findViewById(R.id.etName);
etName.setText(getArguments().getString("name"));
return new AlertDialog.Builder(getContext())
.setView(view)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
getArguments().putString("name", etName.getText().toString());
sendResult(RESULT_OK);
}
})
.setNegativeButton(android.R.string.cancel, null)
.create();
}
}
Loading…
Cancel
Save