From 0c16138040890e4e3b2f2dd8578dbeb6530296a5 Mon Sep 17 00:00:00 2001 From: David Vavra Date: Mon, 14 Jul 2014 17:44:12 +0200 Subject: [PATCH] Cleaning up names of new dialogs --- .../eu/inmite/demo/dialogs/MyActivity.java | 20 +- .../lib/dialogs/DatePickerDialogFragment.java | 244 ++++++++++++++++++ ...er.java => IDateDialogCancelListener.java} | 2 +- ...Listener.java => IDateDialogListener.java} | 2 +- ...Listener.java => IListDialogListener.java} | 2 +- .../lib/dialogs/ListDialogFragment.java | 183 +++++++++++++ .../SimpleDatePickerDialogFragment.java | 244 ------------------ .../lib/dialogs/SimpleListDialogFragment.java | 185 ------------- .../SimpleTimePickerDialogFragment.java | 45 ---- .../lib/dialogs/TimePickerDialogFragment.java | 46 ++++ 10 files changed, 486 insertions(+), 487 deletions(-) create mode 100644 library/src/main/java/eu/inmite/android/lib/dialogs/DatePickerDialogFragment.java rename library/src/main/java/eu/inmite/android/lib/dialogs/{ISimpleDialogDateCancelListener.java => IDateDialogCancelListener.java} (93%) rename library/src/main/java/eu/inmite/android/lib/dialogs/{ISimpleDialogDateListener.java => IDateDialogListener.java} (95%) rename library/src/main/java/eu/inmite/android/lib/dialogs/{OnListItemSelectedListener.java => IListDialogListener.java} (71%) create mode 100644 library/src/main/java/eu/inmite/android/lib/dialogs/ListDialogFragment.java delete mode 100644 library/src/main/java/eu/inmite/android/lib/dialogs/SimpleDatePickerDialogFragment.java delete mode 100644 library/src/main/java/eu/inmite/android/lib/dialogs/SimpleListDialogFragment.java delete mode 100644 library/src/main/java/eu/inmite/android/lib/dialogs/SimpleTimePickerDialogFragment.java create mode 100644 library/src/main/java/eu/inmite/android/lib/dialogs/TimePickerDialogFragment.java diff --git a/demo/src/main/java/eu/inmite/demo/dialogs/MyActivity.java b/demo/src/main/java/eu/inmite/demo/dialogs/MyActivity.java index 1983e77..0c14d9d 100644 --- a/demo/src/main/java/eu/inmite/demo/dialogs/MyActivity.java +++ b/demo/src/main/java/eu/inmite/demo/dialogs/MyActivity.java @@ -25,21 +25,21 @@ import java.text.DateFormat; import java.util.Date; +import eu.inmite.android.lib.dialogs.DatePickerDialogFragment; +import eu.inmite.android.lib.dialogs.IDateDialogListener; +import eu.inmite.android.lib.dialogs.IListDialogListener; import eu.inmite.android.lib.dialogs.ISimpleDialogCancelListener; -import eu.inmite.android.lib.dialogs.ISimpleDialogDateListener; import eu.inmite.android.lib.dialogs.ISimpleDialogListener; -import eu.inmite.android.lib.dialogs.OnListItemSelectedListener; +import eu.inmite.android.lib.dialogs.ListDialogFragment; import eu.inmite.android.lib.dialogs.ProgressDialogFragment; -import eu.inmite.android.lib.dialogs.SimpleDatePickerDialogFragment; import eu.inmite.android.lib.dialogs.SimpleDialogFragment; -import eu.inmite.android.lib.dialogs.SimpleListDialogFragment; -import eu.inmite.android.lib.dialogs.SimpleTimePickerDialogFragment; +import eu.inmite.android.lib.dialogs.TimePickerDialogFragment; public class MyActivity extends FragmentActivity implements ISimpleDialogListener, - ISimpleDialogDateListener, + IDateDialogListener, ISimpleDialogCancelListener, - OnListItemSelectedListener { + IListDialogListener { public static final int THEME_DEFAULT_DARK = 0; @@ -100,7 +100,7 @@ public void onClick(View v) { findViewById(R.id.list_dialog).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - SimpleListDialogFragment + ListDialogFragment .createBuilder(c, getSupportFragmentManager()) .setTitle("Your favorite character:") .setItems(new String[]{"Jayne", "Malcolm", "Kaylee", @@ -142,7 +142,7 @@ public void onClick(View v) { findViewById(R.id.time_picker).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - SimpleTimePickerDialogFragment + TimePickerDialogFragment .createBuilder(MyActivity.this, getSupportFragmentManager()) .setDate(new Date()) .set24hour(true) @@ -155,7 +155,7 @@ public void onClick(View v) { findViewById(R.id.date_picker).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - SimpleDatePickerDialogFragment + DatePickerDialogFragment .createBuilder(MyActivity.this, getSupportFragmentManager()) .setDate(new Date()) .set24hour(true) diff --git a/library/src/main/java/eu/inmite/android/lib/dialogs/DatePickerDialogFragment.java b/library/src/main/java/eu/inmite/android/lib/dialogs/DatePickerDialogFragment.java new file mode 100644 index 0000000..762afe8 --- /dev/null +++ b/library/src/main/java/eu/inmite/android/lib/dialogs/DatePickerDialogFragment.java @@ -0,0 +1,244 @@ +package eu.inmite.android.lib.dialogs; + +import android.content.Context; +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.text.TextUtils; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.DatePicker; + +import java.util.Calendar; +import java.util.Date; +import java.util.TimeZone; + +/** + * Dialog with a date picker. Implement {@link eu.inmite.android.lib.dialogs.IDateDialogListener} or {@link eu.inmite.android.lib.dialogs.IDateDialogCancelListener} to handle events. + */ +public class DatePickerDialogFragment extends BaseDialogFragment { + + protected static final String ARG_ZONE = "zone"; + protected static final String ARG_TITLE = "title"; + protected static final String ARG_POSITIVE_BUTTON = "positive_button"; + protected static final String ARG_NEGATIVE_BUTTON = "negative_button"; + protected static final String ARG_DATE = "date"; + protected static final String ARG_24H = "24h"; + + DatePicker mDatePicker; + Calendar mCalendar; + + private int mRequestCode; + + + public static SimpleDialogBuilder createBuilder(Context context, FragmentManager fragmentManager) { + return new SimpleDialogBuilder(context, fragmentManager, DatePickerDialogFragment.class); + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + final Fragment targetFragment = getTargetFragment(); + if (targetFragment != null) { + mRequestCode = getTargetRequestCode(); + } else { + Bundle args = getArguments(); + if (args != null) { + mRequestCode = args.getInt(BaseDialogBuilder.ARG_REQUEST_CODE, 0); + } + } + } + + protected IDateDialogListener getDialogListener() { + final Fragment targetFragment = getTargetFragment(); + if (targetFragment != null) { + if (targetFragment instanceof IDateDialogListener) { + return (IDateDialogListener) targetFragment; + } + } else { + if (getActivity() instanceof IDateDialogListener) { + return (IDateDialogListener) getActivity(); + } + } + return null; + } + + protected IDateDialogCancelListener getCancelListener() { + final Fragment targetFragment = getTargetFragment(); + if (targetFragment != null) { + if (targetFragment instanceof IDateDialogCancelListener) { + return (IDateDialogCancelListener) targetFragment; + } + } else { + if (getActivity() instanceof IDateDialogCancelListener) { + return (IDateDialogCancelListener) getActivity(); + } + } + return null; + } + + @Override + protected BaseDialogFragment.Builder build(BaseDialogFragment.Builder builder) { + final String title = getTitle(); + if (!TextUtils.isEmpty(title)) { + builder.setTitle(title); + } + + final String positiveButtonText = getPositiveButtonText(); + if (!TextUtils.isEmpty(positiveButtonText)) { + builder.setPositiveButton(positiveButtonText, new View.OnClickListener() { + + @Override + public void onClick(View view) { + IDateDialogListener listener = getDialogListener(); + if (listener != null) { + listener.onPositiveButtonClicked(mRequestCode, getDate()); + } + dismiss(); + } + }); + } + + final String negativeButtonText = getNegativeButtonText(); + if (!TextUtils.isEmpty(negativeButtonText)) { + builder.setNegativeButton(negativeButtonText, new View.OnClickListener() { + + @Override + public void onClick(View view) { + IDateDialogListener listener = getDialogListener(); + if (listener != null) { + listener.onNegativeButtonClicked(mRequestCode, getDate()); + } + dismiss(); + } + }); + } + mDatePicker = (DatePicker) LayoutInflater.from(getActivity()).inflate(R.layout.dialog_part_datepicker, null); + builder.setView(mDatePicker); + + TimeZone zone = TimeZone.getTimeZone(getArguments().getString(ARG_ZONE)); + mCalendar = Calendar.getInstance(zone); + mCalendar.setTimeInMillis(getArguments().getLong(ARG_DATE, System.currentTimeMillis())); + mDatePicker.updateDate(mCalendar.get(Calendar.YEAR) + , mCalendar.get(Calendar.MONTH) + , mCalendar.get(Calendar.DAY_OF_MONTH)); + return builder; + } + + protected String getTitle() { + return getArguments().getString(ARG_TITLE); + } + + protected String getPositiveButtonText() { + return getArguments().getString(ARG_POSITIVE_BUTTON); + } + + protected String getNegativeButtonText() { + return getArguments().getString(ARG_NEGATIVE_BUTTON); + } + + public Date getDate() { + mCalendar.set(Calendar.YEAR, mDatePicker.getYear()); + mCalendar.set(Calendar.MONTH, mDatePicker.getMonth()); + mCalendar.set(Calendar.DAY_OF_MONTH, mDatePicker.getDayOfMonth()); + return mCalendar.getTime(); + } + + public static class SimpleDialogBuilder extends BaseDialogBuilder { + Date mDate = new Date(); + String mTimeZone = null; + + private String mTitle; + private String mPositiveButtonText; + private String mNegativeButtonText; + + private boolean mShowDefaultButton = true; + private boolean m24h; + + protected SimpleDialogBuilder(Context context, FragmentManager fragmentManager, Class clazz) { + super(context, fragmentManager, clazz); + } + + public SimpleDialogBuilder setTitle(int titleResourceId) { + mTitle = mContext.getString(titleResourceId); + return this; + } + + + public SimpleDialogBuilder setTitle(String title) { + mTitle = title; + return this; + } + + public SimpleDialogBuilder setPositiveButtonText(int textResourceId) { + mPositiveButtonText = mContext.getString(textResourceId); + return this; + } + + public SimpleDialogBuilder setPositiveButtonText(String text) { + mPositiveButtonText = text; + return this; + } + + public SimpleDialogBuilder setNegativeButtonText(int textResourceId) { + mNegativeButtonText = mContext.getString(textResourceId); + return this; + } + + public SimpleDialogBuilder setNegativeButtonText(String text) { + mNegativeButtonText = text; + return this; + } + + public SimpleDialogBuilder setDate(Date date) { + mDate = date; + return this; + } + + public SimpleDialogBuilder setTimeZone(String zone) { + mTimeZone = zone; + return this; + } + + public SimpleDialogBuilder set24hour(boolean state) { + m24h = state; + return this; + } + + /** + * When there is neither positive nor negative button, default "close" button is created if it was enabled.
+ * Default is true. + */ + public SimpleDialogBuilder hideDefaultButton(boolean hide) { + mShowDefaultButton = !hide; + return this; + } + + + @Override + protected Bundle prepareArguments() { + if (mShowDefaultButton && mPositiveButtonText == null && mNegativeButtonText == null) { + mPositiveButtonText = mContext.getString(R.string.dialog_close); + } + + Bundle args = new Bundle(); + args.putString(SimpleDialogFragment.ARG_TITLE, mTitle); + args.putString(SimpleDialogFragment.ARG_POSITIVE_BUTTON, mPositiveButtonText); + args.putString(SimpleDialogFragment.ARG_NEGATIVE_BUTTON, mNegativeButtonText); + + args.putLong(ARG_DATE, mDate.getTime()); + args.putBoolean(ARG_24H, m24h); + if (mTimeZone != null) { + args.putString(ARG_ZONE, mTimeZone); + } else { + args.putString(ARG_ZONE, "GMT"); + } + return args; + } + + @Override + protected SimpleDialogBuilder self() { + return this; + } + } +} diff --git a/library/src/main/java/eu/inmite/android/lib/dialogs/ISimpleDialogDateCancelListener.java b/library/src/main/java/eu/inmite/android/lib/dialogs/IDateDialogCancelListener.java similarity index 93% rename from library/src/main/java/eu/inmite/android/lib/dialogs/ISimpleDialogDateCancelListener.java rename to library/src/main/java/eu/inmite/android/lib/dialogs/IDateDialogCancelListener.java index add2936..4828f82 100644 --- a/library/src/main/java/eu/inmite/android/lib/dialogs/ISimpleDialogDateCancelListener.java +++ b/library/src/main/java/eu/inmite/android/lib/dialogs/IDateDialogCancelListener.java @@ -22,6 +22,6 @@ * @author Tomáš Kypta * @since 13/05/2013 */ -public interface ISimpleDialogDateCancelListener { +public interface IDateDialogCancelListener { public void onCancelled(int requestCode, Date date); } diff --git a/library/src/main/java/eu/inmite/android/lib/dialogs/ISimpleDialogDateListener.java b/library/src/main/java/eu/inmite/android/lib/dialogs/IDateDialogListener.java similarity index 95% rename from library/src/main/java/eu/inmite/android/lib/dialogs/ISimpleDialogDateListener.java rename to library/src/main/java/eu/inmite/android/lib/dialogs/IDateDialogListener.java index 59bef52..f4cee37 100644 --- a/library/src/main/java/eu/inmite/android/lib/dialogs/ISimpleDialogDateListener.java +++ b/library/src/main/java/eu/inmite/android/lib/dialogs/IDateDialogListener.java @@ -23,7 +23,7 @@ * * @author David Vávra (david@inmite.eu) */ -public interface ISimpleDialogDateListener { +public interface IDateDialogListener { public void onPositiveButtonClicked(int requestCode, Date date); public void onNegativeButtonClicked(int requestCode, Date date); diff --git a/library/src/main/java/eu/inmite/android/lib/dialogs/OnListItemSelectedListener.java b/library/src/main/java/eu/inmite/android/lib/dialogs/IListDialogListener.java similarity index 71% rename from library/src/main/java/eu/inmite/android/lib/dialogs/OnListItemSelectedListener.java rename to library/src/main/java/eu/inmite/android/lib/dialogs/IListDialogListener.java index f260750..21109f4 100644 --- a/library/src/main/java/eu/inmite/android/lib/dialogs/OnListItemSelectedListener.java +++ b/library/src/main/java/eu/inmite/android/lib/dialogs/IListDialogListener.java @@ -3,7 +3,7 @@ /** * */ -public interface OnListItemSelectedListener { +public interface IListDialogListener { public void onListItemSelected(String value, int number); } diff --git a/library/src/main/java/eu/inmite/android/lib/dialogs/ListDialogFragment.java b/library/src/main/java/eu/inmite/android/lib/dialogs/ListDialogFragment.java new file mode 100644 index 0000000..7b73512 --- /dev/null +++ b/library/src/main/java/eu/inmite/android/lib/dialogs/ListDialogFragment.java @@ -0,0 +1,183 @@ +package eu.inmite.android.lib.dialogs; + +import android.content.Context; +import android.content.res.Resources; +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.text.TextUtils; +import android.view.View; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.ListAdapter; + +/** + * Dialog with a list of options. Implement {@link eu.inmite.android.lib.dialogs.IListDialogListener} to handle selection. + */ +public class ListDialogFragment extends BaseDialogFragment { + + private static String ARG_ITEMS = "items"; + + public static SimpleListDialogBuilder createBuilder(Context context, + FragmentManager fragmentManager) { + return new SimpleListDialogBuilder(context, fragmentManager); + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + if (getArguments() == null) { + throw new IllegalArgumentException( + "use SimpleListDialogBuilder to construct this dialog"); + } + } + + public static class SimpleListDialogBuilder extends BaseDialogBuilder { + + private String title; + + private String[] items; + + private String cancelButtonText; + + private boolean mShowDefaultButton = true; + + public SimpleListDialogBuilder(Context context, FragmentManager fragmentManager) { + super(context, fragmentManager, ListDialogFragment.class); + } + + @Override + protected SimpleListDialogBuilder self() { + return this; + } + + private Resources getResources() { + return mContext.getResources(); + } + + public SimpleListDialogBuilder setTitle(String title) { + this.title = title; + return this; + } + + public SimpleListDialogBuilder setTitle(int titleResID) { + this.title = getResources().getString(titleResID); + return this; + } + + public SimpleListDialogBuilder setItems(String[] items) { + this.items = items; + return this; + } + + public SimpleListDialogBuilder setItems(int itemsArrayResID) { + this.items = getResources().getStringArray(itemsArrayResID); + return this; + } + + public SimpleListDialogBuilder setCancelButtonText(String text) { + this.cancelButtonText = text; + return this; + } + + public SimpleListDialogBuilder setCancelButtonText(int cancelBttTextResID) { + this.cancelButtonText = getResources().getString(cancelBttTextResID); + return this; + } + + @Override + public ListDialogFragment show() { + return (ListDialogFragment) super.show(); + } + + /** + * When there is neither positive nor negative button, default "close" button is created if + * it was enabled.
+ * Default is true. + */ + public SimpleListDialogBuilder hideDefaultButton(boolean hide) { + mShowDefaultButton = !hide; + return this; + } + + @Override + protected Bundle prepareArguments() { + if (mShowDefaultButton && cancelButtonText == null) { + cancelButtonText = mContext.getString(R.string.dialog_close); + } + + Bundle args = new Bundle(); + args.putString(SimpleDialogFragment.ARG_TITLE, title); + args.putString(SimpleDialogFragment.ARG_POSITIVE_BUTTON, cancelButtonText); + args.putStringArray(ARG_ITEMS, items); + + return args; + } + } + + @Override + protected Builder build(Builder builder) { + final String title = getTitle(); + if (!TextUtils.isEmpty(title)) { + builder.setTitle(title); + } + + if (!TextUtils.isEmpty(getPositiveButtonText())) { + builder.setPositiveButton(getPositiveButtonText(), new View.OnClickListener() { + @Override + public void onClick(View view) { + dismiss(); + } + }); + } + + final String[] items = getItems(); + if (items != null && items.length > 0) { + ListAdapter adapter = new ArrayAdapter(getActivity(), + R.layout.dialog_list_item, + R.id.list_item_text, + items); + + builder.setItems(adapter, 0, new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + IListDialogListener onListItemSelectedListener = getDialogListener(); + if (onListItemSelectedListener != null) { + onListItemSelectedListener + .onListItemSelected(getItems()[position], position); + dismiss(); + } + } + }); + } + + return builder; + } + + private IListDialogListener getDialogListener() { + final Fragment targetFragment = getTargetFragment(); + if (targetFragment != null) { + if (targetFragment instanceof IListDialogListener) { + return (IListDialogListener) targetFragment; + } + } else { + if (getActivity() instanceof IListDialogListener) { + return (IListDialogListener) getActivity(); + } + } + return null; + } + + private String getTitle() { + return getArguments().getString(SimpleDialogFragment.ARG_TITLE); + } + + private String[] getItems() { + return getArguments().getStringArray(ARG_ITEMS); + } + + private String getPositiveButtonText() { + return getArguments().getString(SimpleDialogFragment.ARG_POSITIVE_BUTTON); + } + +} diff --git a/library/src/main/java/eu/inmite/android/lib/dialogs/SimpleDatePickerDialogFragment.java b/library/src/main/java/eu/inmite/android/lib/dialogs/SimpleDatePickerDialogFragment.java deleted file mode 100644 index 3e2e114..0000000 --- a/library/src/main/java/eu/inmite/android/lib/dialogs/SimpleDatePickerDialogFragment.java +++ /dev/null @@ -1,244 +0,0 @@ -package eu.inmite.android.lib.dialogs; - -import java.util.Calendar; -import java.util.Date; -import java.util.TimeZone; - -import eu.inmite.android.lib.dialogs.BaseDialogFragment; -import eu.inmite.android.lib.dialogs.ISimpleDialogDateCancelListener; -import eu.inmite.android.lib.dialogs.ISimpleDialogDateListener; -import android.content.Context; -import android.os.Bundle; -import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentManager; -import android.text.TextUtils; -import android.view.LayoutInflater; -import android.view.View; -import android.widget.DatePicker; - -public class SimpleDatePickerDialogFragment extends BaseDialogFragment { - - protected static final String ARG_ZONE = "zone"; - protected static final String ARG_TITLE = "title"; - protected static final String ARG_POSITIVE_BUTTON = "positive_button"; - protected static final String ARG_NEGATIVE_BUTTON = "negative_button"; - protected static final String ARG_DATE = "date"; - protected static final String ARG_24H = "24h"; - - DatePicker mDatePicker; - Calendar mCalendar; - - private int mRequestCode; - - - public static SimpleDialogBuilder createBuilder(Context context, FragmentManager fragmentManager) { - return new SimpleDialogBuilder(context, fragmentManager, SimpleDatePickerDialogFragment.class); - } - - @Override - public void onActivityCreated(Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); - final Fragment targetFragment = getTargetFragment(); - if (targetFragment != null) { - mRequestCode = getTargetRequestCode(); - } else { - Bundle args = getArguments(); - if (args != null) { - mRequestCode = args.getInt(BaseDialogBuilder.ARG_REQUEST_CODE, 0); - } - } - } - - protected ISimpleDialogDateListener getDialogListener() { - final Fragment targetFragment = getTargetFragment(); - if (targetFragment != null) { - if (targetFragment instanceof ISimpleDialogDateListener) { - return (ISimpleDialogDateListener) targetFragment; - } - } else { - if (getActivity() instanceof ISimpleDialogDateListener) { - return (ISimpleDialogDateListener) getActivity(); - } - } - return null; - } - - protected ISimpleDialogDateCancelListener getCancelListener() { - final Fragment targetFragment = getTargetFragment(); - if (targetFragment != null) { - if (targetFragment instanceof ISimpleDialogDateCancelListener) { - return (ISimpleDialogDateCancelListener) targetFragment; - } - } else { - if (getActivity() instanceof ISimpleDialogDateCancelListener) { - return (ISimpleDialogDateCancelListener) getActivity(); - } - } - return null; - } - - @Override - protected BaseDialogFragment.Builder build(BaseDialogFragment.Builder builder) { - final String title = getTitle(); - if (!TextUtils.isEmpty(title)) { - builder.setTitle(title); - } - - final String positiveButtonText = getPositiveButtonText(); - if (!TextUtils.isEmpty(positiveButtonText)) { - builder.setPositiveButton(positiveButtonText, new View.OnClickListener() { - - @Override - public void onClick(View view) { - ISimpleDialogDateListener listener = getDialogListener(); - if (listener != null) { - listener.onPositiveButtonClicked(mRequestCode,getDate()); - } - dismiss(); - } - }); - } - - final String negativeButtonText = getNegativeButtonText(); - if (!TextUtils.isEmpty(negativeButtonText)) { - builder.setNegativeButton(negativeButtonText, new View.OnClickListener() { - - @Override - public void onClick(View view) { - ISimpleDialogDateListener listener = getDialogListener(); - if (listener != null) { - listener.onNegativeButtonClicked(mRequestCode,getDate()); - } - dismiss(); - } - }); - } - mDatePicker=(DatePicker) LayoutInflater.from(getActivity()).inflate(R.layout.dialog_part_datepicker,null); - builder.setView(mDatePicker); - - TimeZone zone=TimeZone.getTimeZone(getArguments().getString(ARG_ZONE)); - mCalendar=Calendar.getInstance(zone); - mCalendar.setTimeInMillis(getArguments().getLong(ARG_DATE,System.currentTimeMillis())); - mDatePicker.updateDate(mCalendar.get(Calendar.YEAR) - ,mCalendar.get(Calendar.MONTH) - ,mCalendar.get(Calendar.DAY_OF_MONTH)); - return builder; - } - - protected String getTitle() { - return getArguments().getString(ARG_TITLE); - } - - protected String getPositiveButtonText() { - return getArguments().getString(ARG_POSITIVE_BUTTON); - } - - protected String getNegativeButtonText() { - return getArguments().getString(ARG_NEGATIVE_BUTTON); - } - - public Date getDate() { - mCalendar.set(Calendar.YEAR,mDatePicker.getYear()); - mCalendar.set(Calendar.MONTH,mDatePicker.getMonth()); - mCalendar.set(Calendar.DAY_OF_MONTH,mDatePicker.getDayOfMonth()); - return mCalendar.getTime(); - } - - public static class SimpleDialogBuilder extends BaseDialogBuilder { - Date mDate=new Date(); - String mTimeZone=null; - - private String mTitle; - private String mPositiveButtonText; - private String mNegativeButtonText; - - private boolean mShowDefaultButton = true; - private boolean m24h; - - protected SimpleDialogBuilder(Context context, FragmentManager fragmentManager, Class clazz) { - super(context, fragmentManager, clazz); - } - - public SimpleDialogBuilder setTitle(int titleResourceId) { - mTitle = mContext.getString(titleResourceId); - return this; - } - - - public SimpleDialogBuilder setTitle(String title) { - mTitle = title; - return this; - } - - public SimpleDialogBuilder setPositiveButtonText(int textResourceId) { - mPositiveButtonText = mContext.getString(textResourceId); - return this; - } - - public SimpleDialogBuilder setPositiveButtonText(String text) { - mPositiveButtonText = text; - return this; - } - - public SimpleDialogBuilder setNegativeButtonText(int textResourceId) { - mNegativeButtonText = mContext.getString(textResourceId); - return this; - } - - public SimpleDialogBuilder setNegativeButtonText(String text) { - mNegativeButtonText = text; - return this; - } - - public SimpleDialogBuilder setDate(Date date) { - mDate=date; - return this; - } - - public SimpleDialogBuilder setTimeZone(String zone) { - mTimeZone=zone; - return this; - } - - public SimpleDialogBuilder set24hour(boolean state) { - m24h=state; - return this; - } - - /** - * When there is neither positive nor negative button, default "close" button is created if it was enabled.
- * Default is true. - */ - public SimpleDialogBuilder hideDefaultButton(boolean hide) { - mShowDefaultButton = !hide; - return this; - } - - - @Override - protected Bundle prepareArguments() { - if (mShowDefaultButton && mPositiveButtonText == null && mNegativeButtonText == null) { - mPositiveButtonText = mContext.getString(R.string.dialog_close); - } - - Bundle args = new Bundle(); - args.putString(SimpleDialogFragment.ARG_TITLE, mTitle); - args.putString(SimpleDialogFragment.ARG_POSITIVE_BUTTON, mPositiveButtonText); - args.putString(SimpleDialogFragment.ARG_NEGATIVE_BUTTON, mNegativeButtonText); - - args.putLong(ARG_DATE,mDate.getTime()); - args.putBoolean(ARG_24H,m24h); - if (mTimeZone!=null) { - args.putString(ARG_ZONE,mTimeZone); - } else { - args.putString(ARG_ZONE,"GMT"); - } - return args; - } - - @Override - protected SimpleDialogBuilder self() { - return this; - } - } -} diff --git a/library/src/main/java/eu/inmite/android/lib/dialogs/SimpleListDialogFragment.java b/library/src/main/java/eu/inmite/android/lib/dialogs/SimpleListDialogFragment.java deleted file mode 100644 index f60b18a..0000000 --- a/library/src/main/java/eu/inmite/android/lib/dialogs/SimpleListDialogFragment.java +++ /dev/null @@ -1,185 +0,0 @@ -package eu.inmite.android.lib.dialogs; - -import android.content.Context; -import android.content.res.Resources; -import android.os.Bundle; -import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentManager; -import android.text.TextUtils; -import android.view.View; -import android.widget.AdapterView; -import android.widget.ArrayAdapter; -import android.widget.ListAdapter; - -/** - * - */ -public class SimpleListDialogFragment extends BaseDialogFragment { - - private static String ARG_ITEMS = "items"; - - public static String TAG = "list"; - - public static SimpleListDialogBuilder createBuilder(Context context, - FragmentManager fragmentManager) { - return new SimpleListDialogBuilder(context, fragmentManager); - } - - @Override - public void onActivityCreated(Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); - if (getArguments() == null) { - throw new IllegalArgumentException( - "use SimpleListDialogBuilder to construct this dialog"); - } - } - - public static class SimpleListDialogBuilder extends BaseDialogBuilder { - - private String title; - - private String[] items; - - private String cancelButtonText; - - private boolean mShowDefaultButton = true; - - public SimpleListDialogBuilder(Context context, FragmentManager fragmentManager) { - super(context, fragmentManager, SimpleListDialogFragment.class); - } - - @Override - protected SimpleListDialogBuilder self() { - return this; - } - - private Resources getResources() { - return mContext.getResources(); - } - - public SimpleListDialogBuilder setTitle(String title) { - this.title = title; - return this; - } - - public SimpleListDialogBuilder setTitle(int titleResID) { - this.title = getResources().getString(titleResID); - return this; - } - - public SimpleListDialogBuilder setItems(String[] items) { - this.items = items; - return this; - } - - public SimpleListDialogBuilder setItems(int itemsArrayResID) { - this.items = getResources().getStringArray(itemsArrayResID); - return this; - } - - public SimpleListDialogBuilder setCancelButtonText(String text) { - this.cancelButtonText = text; - return this; - } - - public SimpleListDialogBuilder setCancelButtonText(int cancelBttTextResID) { - this.cancelButtonText = getResources().getString(cancelBttTextResID); - return this; - } - - @Override - public SimpleListDialogFragment show() { - return (SimpleListDialogFragment) super.show(); - } - - /** - * When there is neither positive nor negative button, default "close" button is created if - * it was enabled.
- * Default is true. - */ - public SimpleListDialogBuilder hideDefaultButton(boolean hide) { - mShowDefaultButton = !hide; - return this; - } - - @Override - protected Bundle prepareArguments() { - if (mShowDefaultButton && cancelButtonText == null) { - cancelButtonText = mContext.getString(R.string.dialog_close); - } - - Bundle args = new Bundle(); - args.putString(SimpleDialogFragment.ARG_TITLE, title); - args.putString(SimpleDialogFragment.ARG_POSITIVE_BUTTON, cancelButtonText); - args.putStringArray(ARG_ITEMS, items); - - return args; - } - } - - @Override - protected Builder build(Builder builder) { - final String title = getTitle(); - if (!TextUtils.isEmpty(title)) { - builder.setTitle(title); - } - - if (!TextUtils.isEmpty(getPositiveButtonText())) { - builder.setPositiveButton(getPositiveButtonText(), new View.OnClickListener() { - @Override - public void onClick(View view) { - dismiss(); - } - }); - } - - final String[] items = getItems(); - if (items != null && items.length > 0) { - ListAdapter adapter = new ArrayAdapter(getActivity(), - R.layout.dialog_list_item, - R.id.list_item_text, - items); - - builder.setItems(adapter, 0, new AdapterView.OnItemClickListener() { - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - OnListItemSelectedListener onListItemSelectedListener = getDialogListener(); - if (onListItemSelectedListener != null) { - onListItemSelectedListener - .onListItemSelected(getItems()[position], position); - dismiss(); - } - } - }); - } - - return builder; - } - - private OnListItemSelectedListener getDialogListener() { - final Fragment targetFragment = getTargetFragment(); - if (targetFragment != null) { - if (targetFragment instanceof OnListItemSelectedListener) { - return (OnListItemSelectedListener) targetFragment; - } - } else { - if (getActivity() instanceof OnListItemSelectedListener) { - return (OnListItemSelectedListener) getActivity(); - } - } - return null; - } - - private String getTitle() { - return getArguments().getString(SimpleDialogFragment.ARG_TITLE); - } - - private String[] getItems() { - return getArguments().getStringArray(ARG_ITEMS); - } - - private String getPositiveButtonText() { - return getArguments().getString(SimpleDialogFragment.ARG_POSITIVE_BUTTON); - } - -} diff --git a/library/src/main/java/eu/inmite/android/lib/dialogs/SimpleTimePickerDialogFragment.java b/library/src/main/java/eu/inmite/android/lib/dialogs/SimpleTimePickerDialogFragment.java deleted file mode 100644 index f281b3a..0000000 --- a/library/src/main/java/eu/inmite/android/lib/dialogs/SimpleTimePickerDialogFragment.java +++ /dev/null @@ -1,45 +0,0 @@ -package eu.inmite.android.lib.dialogs; - -import java.util.Calendar; -import java.util.Date; -import java.util.TimeZone; - -import android.content.Context; -import android.os.Bundle; -import android.support.v4.app.FragmentManager; -import android.view.LayoutInflater; -import android.widget.TimePicker; - -public class SimpleTimePickerDialogFragment extends SimpleDatePickerDialogFragment { - - TimePicker mTimePicker; - Calendar mCalendar; - - - public static SimpleDialogBuilder createBuilder(Context context, FragmentManager fragmentManager) { - return new SimpleDialogBuilder(context, fragmentManager, SimpleTimePickerDialogFragment.class); - } - - @Override - protected BaseDialogFragment.Builder build(BaseDialogFragment.Builder builder) { - builder=super.build(builder); - mTimePicker=(TimePicker) LayoutInflater.from(getActivity()).inflate(R.layout.dialog_part_timepicker,null); - mTimePicker.setIs24HourView(getArguments().getBoolean(ARG_24H)); - builder.setView(mTimePicker); - - TimeZone zone=TimeZone.getTimeZone(getArguments().getString(ARG_ZONE)); - mCalendar=Calendar.getInstance(zone); - mCalendar.setTimeInMillis(getArguments().getLong(ARG_DATE,System.currentTimeMillis())); - - mTimePicker.setCurrentHour(mCalendar.get(Calendar.HOUR_OF_DAY)); - mTimePicker.setCurrentMinute(mCalendar.get(Calendar.MINUTE)); -// mTimePicker.setCurrentSecond(mCalendar.get(Calendar.SECOND)); - return builder; - } - - public Date getDate() { - mCalendar.set(Calendar.HOUR_OF_DAY,mTimePicker.getCurrentHour()); - mCalendar.set(Calendar.MINUTE,mTimePicker.getCurrentMinute()); - return mCalendar.getTime(); - } -} diff --git a/library/src/main/java/eu/inmite/android/lib/dialogs/TimePickerDialogFragment.java b/library/src/main/java/eu/inmite/android/lib/dialogs/TimePickerDialogFragment.java new file mode 100644 index 0000000..0eeecfe --- /dev/null +++ b/library/src/main/java/eu/inmite/android/lib/dialogs/TimePickerDialogFragment.java @@ -0,0 +1,46 @@ +package eu.inmite.android.lib.dialogs; + +import android.content.Context; +import android.support.v4.app.FragmentManager; +import android.view.LayoutInflater; +import android.widget.TimePicker; + +import java.util.Calendar; +import java.util.Date; +import java.util.TimeZone; + +/** + * Dialog with a time picker. Implement {@link eu.inmite.android.lib.dialogs.IDateDialogListener} or {@link eu.inmite.android.lib.dialogs.IDateDialogCancelListener} to handle events. + */ +public class TimePickerDialogFragment extends DatePickerDialogFragment { + + TimePicker mTimePicker; + Calendar mCalendar; + + + public static SimpleDialogBuilder createBuilder(Context context, FragmentManager fragmentManager) { + return new SimpleDialogBuilder(context, fragmentManager, TimePickerDialogFragment.class); + } + + @Override + protected BaseDialogFragment.Builder build(BaseDialogFragment.Builder builder) { + builder = super.build(builder); + mTimePicker = (TimePicker) LayoutInflater.from(getActivity()).inflate(R.layout.dialog_part_timepicker, null); + mTimePicker.setIs24HourView(getArguments().getBoolean(ARG_24H)); + builder.setView(mTimePicker); + + TimeZone zone = TimeZone.getTimeZone(getArguments().getString(ARG_ZONE)); + mCalendar = Calendar.getInstance(zone); + mCalendar.setTimeInMillis(getArguments().getLong(ARG_DATE, System.currentTimeMillis())); + + mTimePicker.setCurrentHour(mCalendar.get(Calendar.HOUR_OF_DAY)); + mTimePicker.setCurrentMinute(mCalendar.get(Calendar.MINUTE)); + return builder; + } + + public Date getDate() { + mCalendar.set(Calendar.HOUR_OF_DAY, mTimePicker.getCurrentHour()); + mCalendar.set(Calendar.MINUTE, mTimePicker.getCurrentMinute()); + return mCalendar.getTime(); + } +}