Skip to content

Commit aef6231

Browse files
committed
Allow to edit contact in address book
1 parent 21ca1b0 commit aef6231

File tree

2 files changed

+67
-42
lines changed

2 files changed

+67
-42
lines changed

app/src/main/java/io/scalaproject/vault/AddressBookFragment.java

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import android.view.ViewGroup;
4545
import android.view.WindowManager;
4646
import android.widget.Button;
47+
import android.widget.ImageButton;
4748
import android.widget.ImageView;
4849
import android.widget.LinearLayout;
4950

@@ -187,11 +188,15 @@ public void onClick(DialogInterface dialogInterface, int i) {
187188
.show();
188189
}
189190

191+
private boolean newContact = false;
192+
190193
// Callbacks from NodeInfoAdapter
191194
@Override
192195
public void onSelectContact(final View view, final Contact contact) {
193196
Timber.d("onSelectContact");
194197

198+
newContact = false;
199+
195200
if(readonly) {
196201
Config.write(Config.CONFIG_KEY_SELECTED_ADDRESS, contact.getAddress());
197202
activityCallback.onBackPressed();
@@ -208,6 +213,8 @@ public void onSelectContact(final View view, final Contact contact) {
208213
public void onClick(View v) {
209214
int id = v.getId();
210215
if (id == R.id.fabAddContact) {
216+
newContact = true;
217+
211218
EditDialog diag = createEditDialog(null);
212219
if (diag != null) {
213220
diag.show();
@@ -236,29 +243,20 @@ public void refreshContacts() {
236243
private EditDialog editDialog = null; // for preventing opening of multiple dialogs
237244

238245
private EditDialog createEditDialog(final Contact contact) {
239-
if(contactEditTmp != null) {
240-
if(editDialog != null) {
241-
editDialog.closeDialog();
242-
editDialog = null;
243-
}
244-
245-
return new EditDialog(contact);
246+
if (editDialog != null) {
247+
editDialog.closeDialog();
248+
editDialog = null;
246249
}
247250

248-
if (editDialog != null)
249-
return null; // we are already open
250-
251251
editDialog = new EditDialog(contact);
252252

253253
return editDialog;
254254
}
255255

256-
private Contact contactEditTmp = null;
256+
private Contact contactEdit = null;
257+
private Contact contactEditBackup = null;
257258

258259
class EditDialog {
259-
Contact contactEdit;
260-
Contact contactEditBackup;
261-
262260
private boolean applyChanges() {
263261
final String contactName = etContactName.getEditText().getText().toString().trim();
264262
if (contactName.isEmpty()) {
@@ -272,7 +270,7 @@ private boolean applyChanges() {
272270
if (walletAddress.isEmpty()) {
273271
etWalletAddress.setError(getString(R.string.contact_value_empty));
274272
return false;
275-
} else if (!Wallet.isAddressValid(walletAddress)){
273+
} else if (!Wallet.isAddressValid(walletAddress)) {
276274
etWalletAddress.setError(getString(R.string.generate_check_address));
277275
return false;
278276
} else {
@@ -283,26 +281,23 @@ private boolean applyChanges() {
283281
}
284282

285283
private boolean applyChangesTmp() {
286-
contactEditTmp = new Contact();
287-
288284
final String contactName = etContactName.getEditText().getText().toString().trim();
289-
contactEditTmp.setName(contactName);
285+
contactEdit.setName(contactName);
290286

291287
final String walletAddress = etWalletAddress.getEditText().getText().toString().trim();
292-
contactEditTmp.setAddress(walletAddress);
288+
contactEdit.setAddress(walletAddress);
293289

294290
return true;
295291
}
296292

297-
private boolean shutdown = false;
298-
299293
private void apply() {
300294
if (applyChanges()) {
301295
closeDialog();
302296

303-
contactsAdapter.addContact(contactEdit);
297+
if (newContact)
298+
contactsAdapter.addContact(contactEdit);
304299

305-
shutdown = true;
300+
contactsAdapter.dataSetChanged();
306301

307302
refreshContacts();
308303
}
@@ -328,10 +323,6 @@ private void show() {
328323
editDialog.show();
329324
}
330325

331-
private void showKeyboard() {
332-
Helper.showKeyboard(editDialog);
333-
}
334-
335326
androidx.appcompat.app.AlertDialog editDialog = null;
336327

337328
TextInputLayout etContactName;
@@ -350,6 +341,17 @@ private void showKeyboard() {
350341
etWalletAddress = promptsView.findViewById(R.id.etWalletAddress);
351342
ivAvatar = promptsView.findViewById(R.id.ivAvatar);
352343

344+
ImageButton bPasteAddress = promptsView.findViewById(R.id.bPasteAddress);
345+
bPasteAddress.setOnClickListener(new View.OnClickListener() {
346+
@Override
347+
public void onClick(View view) {
348+
final String clip = Helper.getClipBoardText(getActivity());
349+
if (clip == null) return;
350+
351+
etWalletAddress.getEditText().setText(clip);
352+
}
353+
});
354+
353355
Button btnSelectImage = promptsView.findViewById(R.id.btnSelectImage);
354356
btnSelectImage.setOnClickListener(new View.OnClickListener() {
355357
@Override
@@ -361,7 +363,10 @@ public void onClick(View view) {
361363

362364
if (contact != null) {
363365
contactEdit = contact;
364-
contactEditBackup = new Contact(contact);
366+
367+
if(contactEditBackup == null)
368+
contactEditBackup = new Contact(contact);
369+
365370
etContactName.getEditText().setText(contact.getName());
366371
etWalletAddress.getEditText().setText(contact.getAddress());
367372

@@ -435,9 +440,9 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
435440
// Already save the cropped image
436441
Bitmap bitmap = Helper.getCroppedBitmap((Bitmap) data.getExtras().get("data"));
437442

438-
contactEditTmp.setAvatar(bitmap);
443+
contactEdit.setAvatar(bitmap);
439444

440-
EditDialog diag = createEditDialog(contactEditTmp);
445+
EditDialog diag = createEditDialog(contactEdit);
441446
if (diag != null) {
442447
diag.show();
443448
}

app/src/main/res/layout/prompt_editcontact.xml

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,41 @@
3030
android:textAlignment="textStart" />
3131
</com.google.android.material.textfield.TextInputLayout>
3232

33-
<com.google.android.material.textfield.TextInputLayout
34-
android:id="@+id/etWalletAddress"
35-
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
36-
android:layout_width="match_parent"
33+
<RelativeLayout
34+
android:layout_width="wrap_content"
3735
android:layout_height="wrap_content"
38-
app:errorEnabled="true">
36+
android:orientation="horizontal">
3937

40-
<com.google.android.material.textfield.TextInputEditText
41-
style="@style/ScalaEdit"
38+
<com.google.android.material.textfield.TextInputLayout
39+
android:id="@+id/etWalletAddress"
40+
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
4241
android:layout_width="match_parent"
43-
android:layout_height="match_parent"
44-
android:hint="Wallet Address"
45-
android:imeOptions="actionDone"
46-
android:textAlignment="textStart" />
47-
</com.google.android.material.textfield.TextInputLayout>
42+
android:layout_height="wrap_content"
43+
android:layout_alignParentStart="true"
44+
android:layout_toStartOf="@+id/bPasteAddress"
45+
app:errorEnabled="true">
46+
47+
<com.google.android.material.textfield.TextInputEditText
48+
style="@style/ScalaEdit"
49+
android:layout_width="match_parent"
50+
android:layout_height="match_parent"
51+
android:hint="Wallet Address"
52+
android:imeOptions="actionDone"
53+
android:textAlignment="textStart" />
54+
</com.google.android.material.textfield.TextInputLayout>
55+
56+
<ImageButton
57+
android:id="@+id/bPasteAddress"
58+
style="@style/ScalaText.Button.Small"
59+
android:layout_width="wrap_content"
60+
android:layout_height="56dp"
61+
android:layout_marginStart="8dp"
62+
android:layout_gravity="center"
63+
android:layout_alignParentEnd="true"
64+
android:background="?android:selectableItemBackgroundBorderless"
65+
android:src="@drawable/ic_content_paste_24dp" />
66+
67+
</RelativeLayout>
4868

4969
<LinearLayout
5070
android:layout_width="wrap_content"

0 commit comments

Comments
 (0)