Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import com.hyperwallet.android.hyperwallet_ui.R;
import com.hyperwallet.android.model.HyperwalletError;
import com.hyperwallet.android.ui.view.WidgetDateDialogFragment;
import com.hyperwallet.android.ui.view.WidgetSelectionDialogFragment;
import com.hyperwallet.android.ui.view.error.DefaultErrorDialogFragment;
import com.hyperwallet.android.ui.view.error.OnNetworkErrorCallback;
Expand All @@ -40,7 +41,7 @@ public class AddTransferMethodActivity extends AppCompatActivity implements
WidgetSelectionDialogFragment.WidgetSelectionItemListener,
AddTransferMethodFragment.OnAddTransferMethodNetworkErrorCallback,
AddTransferMethodFragment.OnLoadTransferMethodConfigurationFieldsNetworkErrorCallback,
OnNetworkErrorCallback {
OnNetworkErrorCallback, WidgetDateDialogFragment.OnSelectedDateCallback {

public static final String EXTRA_TRANSFER_METHOD_COUNTRY = "TRANSFER_METHOD_COUNTRY";
public static final String EXTRA_TRANSFER_METHOD_CURRENCY = "TRANSFER_METHOD_CURRENCY";
Expand Down Expand Up @@ -190,4 +191,14 @@ private AddTransferMethodFragment getAddTransferMethodFragment() {
}
return fragment;
}

@Override
public void setSelectedDateField(@NonNull String fieldName, final String selectedValue) {
FragmentManager fragmentManager = getSupportFragmentManager();
AddTransferMethodFragment addTransferMethodFragment =
(AddTransferMethodFragment) fragmentManager.findFragmentById(R.id.add_transfer_method_fragment);
if (addTransferMethodFragment != null) {
addTransferMethodFragment.onDateSelected(selectedValue, fieldName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@
import com.hyperwallet.android.model.meta.field.HyperwalletFieldGroup;
import com.hyperwallet.android.ui.HyperwalletLocalBroadcast;
import com.hyperwallet.android.ui.repository.RepositoryFactory;
import com.hyperwallet.android.ui.view.WidgetDateDialogFragment;
import com.hyperwallet.android.ui.view.WidgetSelectionDialogFragment;
import com.hyperwallet.android.ui.view.widget.AbstractWidget;
import com.hyperwallet.android.ui.view.widget.DateChangedListener;
import com.hyperwallet.android.ui.view.widget.DateUtil;
import com.hyperwallet.android.ui.view.widget.DateWidget;
import com.hyperwallet.android.ui.view.widget.WidgetEventListener;
import com.hyperwallet.android.ui.view.widget.WidgetFactory;
import com.hyperwallet.android.ui.view.widget.WidgetInputState;
Expand Down Expand Up @@ -94,6 +98,7 @@ public class AddTransferMethodFragment extends Fragment implements WidgetEventLi
private HyperwalletTransferMethod mTransferMethod;
private String mTransferMethodProfileType;
private HashMap<String, WidgetInputState> mWidgetInputStateHashMap;
private final DateUtil mDateUtil = new DateUtil();

/**
* Please do not use this to have instance of AddTransferMethodFragment this is reserved for android framework
Expand Down Expand Up @@ -580,4 +585,33 @@ private boolean performValidation(boolean bypassFocusCheck) {
public boolean isActive() {
return isAdded();
}

@Override
public void openWidgetDateDialog(@Nullable final String date, @NonNull final String fieldName) {
if (getFragmentManager() != null) {
WidgetDateDialogFragment dateDialogFragment = (WidgetDateDialogFragment)
getFragmentManager().findFragmentByTag(WidgetDateDialogFragment.TAG);

if (dateDialogFragment == null) {
dateDialogFragment = WidgetDateDialogFragment.newInstance(date, fieldName);
}

if (!dateDialogFragment.isAdded()) {
dateDialogFragment.show(getFragmentManager());
}
}
}

void onDateSelected(@NonNull final String selectedValue, @NonNull final String fieldName) {
for (int i = 0; i < mDynamicContainer.getChildCount(); i++) {
View view = mDynamicContainer.getChildAt(i);
if (view.getTag() instanceof DateWidget) {
AbstractWidget widget = (AbstractWidget) view.getTag();
if (fieldName.equals(widget.getName()) && widget instanceof DateChangedListener) {
((DateChangedListener) view.getTag()).onUpdate(selectedValue);
return;
}
}
}
}
}
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);
}
}
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);
}
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;
}

}
Loading