Skip to content

Commit

Permalink
refactor picking address
Browse files Browse the repository at this point in the history
Signed-off-by: Amr Hossam <[email protected]>
  • Loading branch information
amrhossamdev committed Dec 25, 2024
1 parent 94ad2d6 commit 362c7d2
Showing 1 changed file with 45 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,28 +167,55 @@ public Intent parseResult(int i, @Nullable Intent addressIntent) {
if (addressIntent == null) {
return;
}

Uri addressUri = addressIntent.getData();
String[] projection = new String[]{ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS};
if (addressUri == null) {
return;
}

String[] projection = {ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS};

try (Cursor cursor = getContext().getContentResolver().query(addressUri, projection, null, null, null)) {
if (cursor != null && cursor.moveToFirst()) {
int addressIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS);
String address = cursor.getString(addressIndex).replace("\n", ", ");
int addressInputId = addressIntent.getIntExtra(ADDRESS_INPUT_ID_KEY, -1);
TextView addressInput = getActivity().findViewById(addressInputId);
addressInput.post(() -> addressInput.setText(address));
if (addressInputId == mToAddressTextArea.getId() && mBuilder.ready()) {
new AlertDialog.Builder(getContext())
.setTitle(R.string.plan_trip)
.setMessage(R.string.do_you_want_to_plan_the_trip_now)
.setPositiveButton(R.string.ok, (dialog, which) -> checkRequestAndSubmit())
.setNegativeButton(R.string.cancel, null)
.create()
.show();
}
if (cursor == null || !cursor.moveToFirst()) {
return;
}

String address = extractAddress(cursor);
int addressInputId = addressIntent.getIntExtra(ADDRESS_INPUT_ID_KEY, -1);
if (addressInputId == -1) {
return;
}

updateAddressInput(address, addressInputId);
updateAddressData(address, addressInputId);
}
};

private String extractAddress(Cursor cursor) {
int addressIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS);
return cursor.getString(addressIndex).replace("\n", ", ");
}

private void updateAddressInput(String address, int addressInputId) {
TextView addressInput = getActivity().findViewById(addressInputId);
addressInput.post(() -> addressInput.setText(address));
addressInput.requestFocus();
}

private void updateAddressData(String address, int addressInputId) {
CustomAddress customAddress = CustomAddress.getEmptyAddress();
customAddress.setAddressLine(0, address);

if (addressInputId == mFromAddressTextArea.getId()) {
mFromAddress = customAddress;
mBuilder.setFrom(mFromAddress);
} else if (addressInputId == mToAddressTextArea.getId()) {
mToAddress = customAddress;
mBuilder.setTo(mToAddress);
}
}


private final ActivityResultLauncher<TextView> mSelectAddressFromContactLauncher = registerForActivityResult(
selectAddressFromContactContract,
addressIntentActivityResultCallback
Expand Down Expand Up @@ -373,6 +400,8 @@ private void checkRequestAndSubmit() {
if (mBuilder.ready() && mListener != null) {
mFromAddressTextArea.dismissDropDown();
mToAddressTextArea.dismissDropDown();
mFromAddressTextArea.clearFocus();
mToAddressTextArea.clearFocus();
UIUtils.closeKeyboard(getContext(), mFromAddressTextArea);
mListener.onTripRequestReady();
}
Expand Down

0 comments on commit 362c7d2

Please sign in to comment.