-
Notifications
You must be signed in to change notification settings - Fork 16
HW-52174. Added datePicker for DateWidget #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1b41abd
initial-gql-integration
ccc5299
Merge remote-tracking branch 'remotes/origin/development' into featur…
2f52506
Merge remote-tracking branch 'remotes/origin/development' into featur…
f41207c
HW-52170 key and field gql integration (#19)
a65f4b2
HW-52174. Added datePicker for DateWidget
azakrevska-epam 3bf9cce
HW-52174. Changed behavior due to comments
azakrevska-epam 8025c89
Merge branch 'development' into task/HW-52174-date-widget
azakrevska-epam d08561e
HW-52174. Moved dialog to the fragment
azakrevska-epam 7510bbd
HW-52174. Moved date fragment outside of widget
azakrevska-epam 22f9f80
HW-52174. Moved call of date fragment inside of add fragment
azakrevska-epam ef1d1d0
HW-52174. Changed due to the review
azakrevska-epam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
ui/src/main/java/com/hyperwallet/android/ui/view/WidgetDateDialogFragment.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| package com.hyperwallet.android.ui.view; | ||
|
|
||
| import android.app.DatePickerDialog; | ||
| import android.app.Dialog; | ||
| import android.content.Context; | ||
| import android.content.DialogInterface; | ||
| import android.os.Bundle; | ||
| import android.widget.DatePicker; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
| import androidx.annotation.Nullable; | ||
| import androidx.fragment.app.DialogFragment; | ||
| import androidx.fragment.app.FragmentManager; | ||
|
|
||
| import com.hyperwallet.android.hyperwallet_ui.R; | ||
| import com.hyperwallet.android.ui.view.widget.DateUtil; | ||
|
|
||
| import java.util.Calendar; | ||
|
|
||
| public class WidgetDateDialogFragment extends DialogFragment { | ||
|
|
||
| public static final String TAG = WidgetDateDialogFragment.class.getName(); | ||
| private static final String ARGUMENT_DATE = "ARGUMENT_DATE"; | ||
| private static final String ARGUMENT_FIELD_NAME = "ARGUMENT_FIELD_NAME"; | ||
| private OnSelectedDateCallback mOnSelectedDateCallback; | ||
| private final DateUtil mDateUtil = new DateUtil(); | ||
|
|
||
| /** | ||
| * Please do not use this to have instance of DateDialogFragment this is reserved for android framework | ||
| */ | ||
| public WidgetDateDialogFragment() { | ||
| setRetainInstance(true); | ||
| } | ||
|
|
||
| public static WidgetDateDialogFragment newInstance(final String date, @NonNull final String fieldName) { | ||
| WidgetDateDialogFragment widgetDateDialogFragment = new WidgetDateDialogFragment(); | ||
| Bundle bundle = new Bundle(); | ||
| bundle.putString(ARGUMENT_DATE, date); | ||
| bundle.putString(ARGUMENT_FIELD_NAME, fieldName); | ||
| widgetDateDialogFragment.setArguments(bundle); | ||
|
|
||
| return widgetDateDialogFragment; | ||
| } | ||
|
|
||
| @Override | ||
| public void onAttach(Context context) { | ||
| super.onAttach(context); | ||
| try { | ||
| mOnSelectedDateCallback = (OnSelectedDateCallback) context; | ||
| } catch (ClassCastException e) { | ||
| throw new ClassCastException(getActivity().toString() | ||
| + " must implement " | ||
| + OnSelectedDateCallback.class.getCanonicalName()); | ||
| } | ||
| } | ||
|
|
||
| public void show(@NonNull FragmentManager manager) { | ||
| show(manager, TAG); | ||
| } | ||
|
|
||
| @Override | ||
| public void onDestroyView() { | ||
| Dialog dialog = getDialog(); | ||
| if (dialog != null && getRetainInstance()) { | ||
| dialog.setDismissMessage(null); | ||
| } | ||
| super.onDestroyView(); | ||
| } | ||
|
|
||
| @NonNull | ||
| @Override | ||
| public Dialog onCreateDialog(@Nullable Bundle state) { | ||
|
|
||
| String storedDate = getArguments() != null ? getArguments().getString(ARGUMENT_DATE) : ""; | ||
| final String fieldName = getArguments().getString(ARGUMENT_FIELD_NAME); | ||
| Calendar calendar; | ||
| try { | ||
| calendar = mDateUtil.convertDateFromServerFormatToCalendar(storedDate); | ||
| } catch (Exception e) { | ||
| calendar = Calendar.getInstance(); | ||
| } | ||
|
|
||
| DatePickerDialog datePickerDialog = new DatePickerDialog(requireContext(), | ||
| R.style.Widget_Hyperwallet_DatePicker, | ||
| new DatePickerDialog.OnDateSetListener() { | ||
| @Override | ||
| public void onDateSet(DatePicker view, int resultYear, int resultMonth, int resultDayOfMonth) { | ||
| final String selectedDate = mDateUtil | ||
| .buildDateFromDateDialogToServerFormat(resultYear, resultMonth, resultDayOfMonth); | ||
| mOnSelectedDateCallback.setSelectedDateField(fieldName, selectedDate); | ||
| } | ||
| }, | ||
| calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)); | ||
|
|
||
| datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, | ||
| getString(R.string.cancel_button_label), | ||
| new DialogInterface.OnClickListener() { | ||
| @Override | ||
| public void onClick(DialogInterface dialog, int which) { | ||
| if (which == DialogInterface.BUTTON_NEGATIVE) { | ||
| mOnSelectedDateCallback.setSelectedDateField(fieldName, null); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| datePickerDialog.setCanceledOnTouchOutside(false); | ||
| return datePickerDialog; | ||
| } | ||
|
|
||
| public interface OnSelectedDateCallback { | ||
| void setSelectedDateField(@NonNull final String fieldName, final String selectedValue); | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
ui/src/main/java/com/hyperwallet/android/ui/view/widget/DateChangedListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /* | ||
| * The MIT License (MIT) | ||
| * Copyright (c) 2018 Hyperwallet Systems Inc. | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and | ||
| * associated documentation files (the "Software"), to deal in the Software without restriction, | ||
| * including without limitation the rights to use, copy, modify, merge, publish, distribute, | ||
| * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT | ||
| * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
| * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
| * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| */ | ||
| package com.hyperwallet.android.ui.view.widget; | ||
|
|
||
| import androidx.annotation.Nullable; | ||
|
|
||
| public interface DateChangedListener { | ||
| void onUpdate(@Nullable final String selectedDate); | ||
| } |
94 changes: 94 additions & 0 deletions
94
ui/src/main/java/com/hyperwallet/android/ui/view/widget/DateUtil.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| /* | ||
| * The MIT License (MIT) | ||
| * Copyright (c) 2018 Hyperwallet Systems Inc. | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and | ||
| * associated documentation files (the "Software"), to deal in the Software without restriction, | ||
| * including without limitation the rights to use, copy, modify, merge, publish, distribute, | ||
| * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT | ||
| * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
| * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
| * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| */ | ||
|
|
||
| package com.hyperwallet.android.ui.view.widget; | ||
|
|
||
| import android.text.TextUtils; | ||
| import android.text.format.DateFormat; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
| import androidx.annotation.Nullable; | ||
|
|
||
| import java.text.ParseException; | ||
| import java.text.SimpleDateFormat; | ||
| import java.util.Calendar; | ||
| import java.util.Locale; | ||
|
|
||
| /** | ||
| * Class is used for manage and convert date {@link DateWidget} | ||
| */ | ||
| public final class DateUtil { | ||
|
|
||
| private static final String SERVER_DATE_PATTERN = "yyyy-MM-dd"; | ||
| private static final String WIDGET_DATE_PATTERN = "dd MMMM yyyy"; | ||
|
|
||
| private final SimpleDateFormat mServerDateFormat = new SimpleDateFormat(SERVER_DATE_PATTERN, Locale.getDefault()); | ||
| private final SimpleDateFormat mWidgetDateFormat; | ||
|
|
||
| public DateUtil() { | ||
| mWidgetDateFormat = new SimpleDateFormat( | ||
| DateFormat.getBestDateTimePattern(Locale.getDefault(), WIDGET_DATE_PATTERN), Locale.getDefault()); | ||
| } | ||
|
|
||
| /** | ||
| * Convert Date from server format (yyyy-MM-dd) to DateWidget format build by BestDateTimePattern @see {@link | ||
| * DateFormat} or (dd MMMM yyyy) | ||
| * | ||
| * @param serverDate Date from server | ||
| * @return String date in the format by BestDateTimePattern or dd MMMM yyyy otherwise | ||
| */ | ||
| @NonNull | ||
| String convertDateFromServerToWidgetFormat(@Nullable final String serverDate) throws ParseException { | ||
| if (!TextUtils.isEmpty(serverDate)) { | ||
| final Calendar calendar = Calendar.getInstance(); | ||
| calendar.setTime(mServerDateFormat.parse(serverDate)); | ||
| return mWidgetDateFormat.format(calendar.getTime()); | ||
| } | ||
| return ""; | ||
| } | ||
|
|
||
| /** | ||
| * Build Date in the server format (yyyy-MM-dd) | ||
| * | ||
| * @param year the selected year | ||
| * @param month the selected month (0-11 for compatibility with {@link Calendar#MONTH}) | ||
| * @param dayOfMonth th selected day of the month (1-31, depending on month) | ||
| * @return String date in the format (yyyy-MM-dd) | ||
| */ | ||
| @NonNull | ||
| public String buildDateFromDateDialogToServerFormat(final int year, final int month, final int dayOfMonth) { | ||
| final Calendar calendar = Calendar.getInstance(); | ||
| calendar.set(year, month, dayOfMonth); | ||
| return mServerDateFormat.format(calendar.getTime()); | ||
| } | ||
|
|
||
| /** | ||
| * Convert Date from server format (yyyy-MM-dd) to Calendar | ||
| * | ||
| * @param serverDate Date from server | ||
| * @return Calendar with date from server | ||
| */ | ||
| @NonNull | ||
| public Calendar convertDateFromServerFormatToCalendar(@Nullable final String serverDate) throws ParseException { | ||
| final Calendar calendar = Calendar.getInstance(); | ||
| if (!TextUtils.isEmpty(serverDate)) { | ||
| calendar.setTime(mServerDateFormat.parse(serverDate)); | ||
| } | ||
| return calendar; | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.