2121
2222package io .scalaproject .vault ;
2323
24+ import android .app .Activity ;
2425import android .app .AlertDialog ;
2526import android .content .Context ;
2627import android .content .DialogInterface ;
28+ import android .content .Intent ;
29+ import android .graphics .Bitmap ;
2730import android .os .Bundle ;
2831import androidx .annotation .Nullable ;
2932
3033import com .google .android .material .dialog .MaterialAlertDialogBuilder ;
3134import com .google .android .material .textfield .TextInputLayout ;
3235import androidx .fragment .app .Fragment ;
33- import androidx .recyclerview .widget .LinearLayoutManager ;
3436import androidx .recyclerview .widget .RecyclerView ;
3537
3638
39+ import android .provider .MediaStore ;
3740import android .view .LayoutInflater ;
3841import android .view .Menu ;
3942import android .view .MenuInflater ;
4043import android .view .View ;
4144import android .view .ViewGroup ;
4245import android .view .WindowManager ;
4346import android .widget .Button ;
47+ import android .widget .ImageView ;
4448import android .widget .LinearLayout ;
4549
4650import io .scalaproject .vault .data .Contact ;
@@ -232,22 +236,36 @@ public void refreshContacts() {
232236 private EditDialog editDialog = null ; // for preventing opening of multiple dialogs
233237
234238 private EditDialog createEditDialog (final Contact contact ) {
235- if (editDialog != null ) return null ; // we are already open
239+ if (contactEditTmp != null ) {
240+ if (editDialog != null ) {
241+ editDialog .closeDialog ();
242+ editDialog = null ;
243+ }
244+
245+ return new EditDialog (contact );
246+ }
247+
248+ if (editDialog != null )
249+ return null ; // we are already open
250+
236251 editDialog = new EditDialog (contact );
252+
237253 return editDialog ;
238254 }
239255
256+ private Contact contactEditTmp = null ;
257+
240258 class EditDialog {
241- Contact contactEdit = null ;
242- Contact contactEditBackup = null ;
259+ Contact contactEdit ;
260+ Contact contactEditBackup ;
243261
244262 private boolean applyChanges () {
245263 final String contactName = etContactName .getEditText ().getText ().toString ().trim ();
246264 if (contactName .isEmpty ()) {
247265 etContactName .setError (getString (R .string .contact_value_empty ));
248266 return false ;
249267 } else {
250- contactEdit .setName (etContactName . getEditText (). getText (). toString (). trim () );
268+ contactEdit .setName (contactName );
251269 }
252270
253271 final String walletAddress = etWalletAddress .getEditText ().getText ().toString ().trim ();
@@ -258,12 +276,24 @@ private boolean applyChanges() {
258276 etWalletAddress .setError (getString (R .string .generate_check_address ));
259277 return false ;
260278 } else {
261- contactEdit .setAddress (etWalletAddress . getEditText (). getText (). toString (). trim () );
279+ contactEdit .setAddress (walletAddress );
262280 }
263281
264282 return true ;
265283 }
266284
285+ private boolean applyChangesTmp () {
286+ contactEditTmp = new Contact ();
287+
288+ final String contactName = etContactName .getEditText ().getText ().toString ().trim ();
289+ contactEditTmp .setName (contactName );
290+
291+ final String walletAddress = etWalletAddress .getEditText ().getText ().toString ().trim ();
292+ contactEditTmp .setAddress (walletAddress );
293+
294+ return true ;
295+ }
296+
267297 private boolean shutdown = false ;
268298
269299 private void apply () {
@@ -306,6 +336,9 @@ private void showKeyboard() {
306336
307337 TextInputLayout etContactName ;
308338 TextInputLayout etWalletAddress ;
339+ ImageView ivAvatar ;
340+
341+ public static final int GET_FROM_GALLERY = 1 ;
309342
310343 EditDialog (final Contact contact ) {
311344 MaterialAlertDialogBuilder alertDialogBuilder = new MaterialAlertDialogBuilder (getActivity (), R .style .MaterialAlertDialogCustom );
@@ -315,15 +348,33 @@ private void showKeyboard() {
315348
316349 etContactName = promptsView .findViewById (R .id .etContactName );
317350 etWalletAddress = promptsView .findViewById (R .id .etWalletAddress );
351+ ivAvatar = promptsView .findViewById (R .id .ivAvatar );
352+
353+ Button btnSelectImage = promptsView .findViewById (R .id .btnSelectImage );
354+ btnSelectImage .setOnClickListener (new View .OnClickListener () {
355+ @ Override
356+ public void onClick (View view ) {
357+ applyChangesTmp ();
358+ pickImage ();
359+ }
360+ });
318361
319362 if (contact != null ) {
320363 contactEdit = contact ;
321364 contactEditBackup = new Contact (contact );
322365 etContactName .getEditText ().setText (contact .getName ());
323366 etWalletAddress .getEditText ().setText (contact .getAddress ());
367+
368+ Bitmap avatar = contact .getAvatar ();
369+ if (avatar != null )
370+ ivAvatar .setImageBitmap (contact .getAvatar ());
371+ else {
372+ ivAvatar .setImageBitmap (Helper .getBitmap (getContext (), R .drawable .ic_contact_avatar ));
373+ }
324374 } else {
325375 contactEdit = new Contact ();
326376 contactEditBackup = null ;
377+ ivAvatar .setImageBitmap (Helper .getBitmap (getContext (), R .drawable .ic_contact_avatar ));
327378 }
328379
329380 // set dialog message
@@ -340,6 +391,7 @@ public void onClick(DialogInterface dialog, int id) {
340391 });
341392
342393 editDialog = alertDialogBuilder .create ();
394+
343395 // these need to be here, since we don't always close the dialog
344396 editDialog .setOnShowListener (new DialogInterface .OnShowListener () {
345397 @ Override
@@ -360,5 +412,35 @@ public void onClick(View view) {
360412
361413 refreshContacts ();
362414 }
415+
416+ public void pickImage () {
417+ Intent intent = new Intent (Intent .ACTION_PICK , MediaStore .Images .Media .INTERNAL_CONTENT_URI );
418+ intent .setType ("image/*" );
419+ intent .putExtra ("crop" , "true" );
420+ intent .putExtra ("scale" , true );
421+ intent .putExtra ("outputX" , 256 );
422+ intent .putExtra ("outputY" , 256 );
423+ intent .putExtra ("aspectX" , 1 );
424+ intent .putExtra ("aspectY" , 1 );
425+ intent .putExtra ("return-data" , true );
426+
427+ startActivityForResult (intent , 1 );
428+ }
429+ }
430+
431+ public void onActivityResult (int requestCode , int resultCode , Intent data ) {
432+ if (requestCode == EditDialog .GET_FROM_GALLERY & resultCode == Activity .RESULT_OK ) {
433+ Timber .d ("AddressBook.onActivityResult" );
434+
435+ // Already save the cropped image
436+ Bitmap bitmap = Helper .getCroppedBitmap ((Bitmap ) data .getExtras ().get ("data" ));
437+
438+ contactEditTmp .setAvatar (bitmap );
439+
440+ EditDialog diag = createEditDialog (contactEditTmp );
441+ if (diag != null ) {
442+ diag .show ();
443+ }
444+ }
363445 }
364446}
0 commit comments