From 679b1bca7a578dae0b205eb2eab1e38266c815ad Mon Sep 17 00:00:00 2001 From: aseik Date: Fri, 6 May 2016 16:39:25 +0200 Subject: [PATCH 1/4] Added negative duration option --- .../betterpickers/hmspicker/HmsPicker.java | 50 ++++++++++++++++--- .../hmspicker/HmsPickerBuilder.java | 18 ++++++- .../hmspicker/HmsPickerDialogFragment.java | 28 ++++++++++- .../betterpickers/hmspicker/HmsView.java | 15 ++++++ .../src/main/res/layout/hms_picker_view.xml | 9 ++++ sample/src/main/AndroidManifest.xml | 8 +++ .../SampleHmsBasicNegativeDurationUsage.java | 47 +++++++++++++++++ 7 files changed, 167 insertions(+), 8 deletions(-) create mode 100644 sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsBasicNegativeDurationUsage.java diff --git a/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPicker.java b/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPicker.java index ad2a3dcf..544b2e41 100644 --- a/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPicker.java +++ b/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPicker.java @@ -2,6 +2,7 @@ import android.content.Context; import android.content.res.ColorStateList; +import android.content.res.Resources; import android.content.res.TypedArray; import android.os.Bundle; import android.os.Parcel; @@ -40,6 +41,10 @@ public class HmsPicker extends LinearLayout implements Button.OnClickListener, B private int mDeleteDrawableSrcResId; private int mTheme = -1; + private int mSign; + public static final int SIGN_POSITIVE = 0; + public static final int SIGN_NEGATIVE = 1; + /** * Instantiates an HmsPicker object * @@ -125,6 +130,10 @@ private void restyleViews() { if (mEnteredHms != null) { mEnteredHms.setTheme(mTheme); } + if(mLeft != null){ + mLeft.setTextColor(mTextColor); + mLeft.setBackgroundResource(mKeyBackgroundResId); + } } @Override @@ -155,7 +164,7 @@ protected void onFinishInflate() { mLeft = (Button) v4.findViewById(R.id.key_left); mNumbers[0] = (Button) v4.findViewById(R.id.key_middle); mRight = (Button) v4.findViewById(R.id.key_right); - setLeftRightEnabled(false); + setRightEnabled(false); for (int i = 0; i < 10; i++) { mNumbers[i].setOnClickListener(this); @@ -164,6 +173,12 @@ protected void onFinishInflate() { } updateHms(); + Resources res = mContext.getResources(); + mLeft.setText(res.getString(R.string.number_picker_plus_minus)); + mLeft.setOnClickListener(this); +// mLabel = (TextView) findViewById(R.id.label); +// mSign = SIGN_POSITIVE; + mHoursLabel = (TextView) findViewById(R.id.hours_label); mMinutesLabel = (TextView) findViewById(R.id.minutes_label); mSecondsLabel = (TextView) findViewById(R.id.seconds_label); @@ -203,10 +218,20 @@ protected void doOnClick(View v) { mInput[mInputPointer] = 0; mInputPointer--; } + } else if(v == mLeft){ + onLeftClicked(); } updateKeypad(); } + private void onLeftClicked() { + if (mSign == SIGN_POSITIVE) { + mSign = SIGN_NEGATIVE; + } else { + mSign = SIGN_POSITIVE; + } + } + @Override public boolean onLongClick(View v) { v.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); @@ -249,7 +274,7 @@ private void updateKeypad() { * Hide digit by passing -2 (for highest hours digit only); */ protected void updateHms() { - mEnteredHms.setTime(mInput[4], mInput[3], mInput[2], mInput[1], mInput[0]); + mEnteredHms.setTime(mSign == SIGN_NEGATIVE, mInput[4], mInput[3], mInput[2], mInput[1], mInput[0]); } private void addClickedNumber(int val) { @@ -317,6 +342,17 @@ public int getSeconds() { return mInput[1] * 10 + mInput[0]; } + /** + * Using View.GONE, View.VISIBILE, or View.INVISIBLE, set the visibility of the plus/minus indicator + * + * @param visibility an int using Android's View.* convention + */ + public void setPlusMinusVisibility(int visibility) { + if (mLeft != null) { + mLeft.setVisibility(visibility); + } + } + /** * Set the current hours, minutes, and seconds on the picker. * @@ -342,7 +378,7 @@ public void setTime(int hours, int minutes, int seconds) { } - @Override + @Override public Parcelable onSaveInstanceState() { final Parcelable parcel = super.onSaveInstanceState(); final SavedState state = new SavedState(parcel); @@ -433,12 +469,14 @@ public void restoreEntryState(Bundle inState, String key) { } } - protected void setLeftRightEnabled(boolean enabled) { - mLeft.setEnabled(enabled); + protected void setRightEnabled(boolean enabled) { mRight.setEnabled(enabled); if (!enabled) { - mLeft.setContentDescription(null); mRight.setContentDescription(null); } } + + public boolean isNegative(){ + return mSign == SIGN_NEGATIVE; + } } diff --git a/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPickerBuilder.java b/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPickerBuilder.java index 769261f8..0c081c7b 100644 --- a/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPickerBuilder.java +++ b/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPickerBuilder.java @@ -22,6 +22,22 @@ public class HmsPickerBuilder { private int mHours; private int mMinutes; private int mSeconds; + private Integer plusMinusVisibility; + + /** + * Set the visibility of the +/- button. This takes an int corresponding to Android's View.VISIBLE, View.INVISIBLE, + * or View.GONE. When using View.INVISIBLE, the +/- button will still be present in the layout but be + * non-clickable. When set to View.GONE, the +/- button will disappear entirely, and the "0" button will occupy its + * space. + * + * @param plusMinusVisibility an int corresponding to View.VISIBLE, View.INVISIBLE, or View.GONE + * @return the current Builder object + */ + public HmsPickerBuilder setPlusMinusVisibility(int plusMinusVisibility) { + this.plusMinusVisibility = plusMinusVisibility; + return this; + } + /** * Attach a FragmentManager. This is required for creation of the Fragment. @@ -147,7 +163,7 @@ public void show() { } ft.addToBackStack(null); - final HmsPickerDialogFragment fragment = HmsPickerDialogFragment.newInstance(mReference, styleResId); + final HmsPickerDialogFragment fragment = HmsPickerDialogFragment.newInstance(mReference, styleResId, plusMinusVisibility); if (targetFragment != null) { fragment.setTargetFragment(targetFragment, 0); } diff --git a/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPickerDialogFragment.java b/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPickerDialogFragment.java index c64452d8..2cdcaf5f 100644 --- a/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPickerDialogFragment.java +++ b/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPickerDialogFragment.java @@ -22,6 +22,7 @@ public class HmsPickerDialogFragment extends DialogFragment { private static final String REFERENCE_KEY = "HmsPickerDialogFragment_ReferenceKey"; private static final String THEME_RES_ID_KEY = "HmsPickerDialogFragment_ThemeResIdKey"; + private static final String PLUS_MINUS_VISIBILITY_KEY = "HmsPickerDialogFragment_PlusMinusVisibilityKey"; private HmsPicker mPicker; @@ -33,6 +34,7 @@ public class HmsPickerDialogFragment extends DialogFragment { private int mHours; private int mMinutes; private int mSeconds; + private int mPlusMinusVisibility = View.INVISIBLE; /** * Create an instance of the Picker (used internally) @@ -41,11 +43,14 @@ public class HmsPickerDialogFragment extends DialogFragment { * @param themeResId the style resource ID for theming * @return a Picker! */ - public static HmsPickerDialogFragment newInstance(int reference, int themeResId) { + public static HmsPickerDialogFragment newInstance(int reference, int themeResId, Integer plusMinusVisibility) { final HmsPickerDialogFragment frag = new HmsPickerDialogFragment(); Bundle args = new Bundle(); args.putInt(REFERENCE_KEY, reference); args.putInt(THEME_RES_ID_KEY, themeResId); + if (plusMinusVisibility != null) { + args.putInt(PLUS_MINUS_VISIBILITY_KEY, plusMinusVisibility); + } frag.setArguments(args); return frag; } @@ -66,6 +71,9 @@ public void onCreate(Bundle savedInstanceState) { if (args != null && args.containsKey(THEME_RES_ID_KEY)) { mTheme = args.getInt(THEME_RES_ID_KEY); } + if (args != null && args.containsKey(PLUS_MINUS_VISIBILITY_KEY)) { + mPlusMinusVisibility = args.getInt(PLUS_MINUS_VISIBILITY_KEY); + } setStyle(DialogFragment.STYLE_NO_TITLE, 0); @@ -106,6 +114,7 @@ public void onClick(View view) { } final Activity activity = getActivity(); final Fragment fragment = getTargetFragment(); + if (activity instanceof HmsPickerDialogHandler) { final HmsPickerDialogHandler act = (HmsPickerDialogHandler) activity; @@ -115,6 +124,17 @@ public void onClick(View view) { (HmsPickerDialogHandler) fragment; frag.onDialogHmsSet(mReference, mPicker.getHours(), mPicker.getMinutes(), mPicker.getSeconds()); } + + if (activity instanceof HmsPickerDialogHandlerV2) { + final HmsPickerDialogHandlerV2 act = + (HmsPickerDialogHandlerV2) activity; + act.onDialogHmsSet(mReference, mPicker.isNegative(), mPicker.getHours(), mPicker.getMinutes(), mPicker.getSeconds()); + } else if (fragment instanceof HmsPickerDialogHandlerV2) { + final HmsPickerDialogHandlerV2 frag = + (HmsPickerDialogHandlerV2) fragment; + frag.onDialogHmsSet(mReference, mPicker.isNegative(), mPicker.getHours(), mPicker.getMinutes(), mPicker.getSeconds()); + } + dismiss(); } }); @@ -123,12 +143,18 @@ public void onClick(View view) { mPicker.setSetButton(doneButton); mPicker.setTime(mHours, mMinutes, mSeconds); mPicker.setTheme(mTheme); + mPicker.setPlusMinusVisibility(mPlusMinusVisibility); getDialog().getWindow().setBackgroundDrawableResource(mDialogBackgroundResId); return view; } + public interface HmsPickerDialogHandlerV2 { + + void onDialogHmsSet(int reference, boolean isNegative, int hours, int minutes, int seconds); + } + /** * This interface allows objects to register for the Picker's set action. */ diff --git a/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsView.java b/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsView.java index bed74f8d..79c652df 100644 --- a/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsView.java +++ b/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsView.java @@ -5,6 +5,7 @@ import android.content.res.TypedArray; import android.graphics.Typeface; import android.util.AttributeSet; +import android.view.View; import android.widget.LinearLayout; import com.codetroopers.betterpickers.R; @@ -17,6 +18,7 @@ public class HmsView extends LinearLayout { private ZeroTopPaddingTextView mSecondsOnes, mSecondsTens; private final Typeface mAndroidClockMonoThin; private Typeface mOriginalHoursTypeface; + private ZeroTopPaddingTextView mMinusLabel; private ColorStateList mTextColor; @@ -76,6 +78,9 @@ private void restyleViews() { if (mSecondsTens != null) { mSecondsTens.setTextColor(mTextColor); } + if (mMinusLabel != null) { + mMinusLabel.setTextColor(mTextColor); + } } @Override @@ -87,6 +92,8 @@ protected void onFinishInflate() { mMinutesOnes = (ZeroTopPaddingTextView) findViewById(R.id.minutes_ones); mSecondsTens = (ZeroTopPaddingTextView) findViewById(R.id.seconds_tens); mSecondsOnes = (ZeroTopPaddingTextView) findViewById(R.id.seconds_ones); + mMinusLabel = (ZeroTopPaddingTextView) findViewById(R.id.minus_label); + if (mHoursOnes != null) { mOriginalHoursTypeface = mHoursOnes.getTypeface(); mHoursOnes.updatePaddingForBoldDate(); @@ -118,7 +125,15 @@ protected void onFinishInflate() { * @param secondsOnesDigit the ones digit of the seconds TextView */ public void setTime(int hoursOnesDigit, int minutesTensDigit, int minutesOnesDigit, int secondsTensDigit, + int secondsOnesDigit) { + setTime(false, hoursOnesDigit, minutesTensDigit, minutesOnesDigit, secondsTensDigit, secondsOnesDigit); + } + + public void setTime(boolean isNegative, int hoursOnesDigit, int minutesTensDigit, int minutesOnesDigit, int secondsTensDigit, int secondsOnesDigit) { + + mMinusLabel.setVisibility(isNegative ? View.VISIBLE : View.GONE); + if (mHoursOnes != null) { mHoursOnes.setText(String.format("%d", hoursOnesDigit)); } diff --git a/library/src/main/res/layout/hms_picker_view.xml b/library/src/main/res/layout/hms_picker_view.xml index 0f3afe08..ec4963ea 100644 --- a/library/src/main/res/layout/hms_picker_view.xml +++ b/library/src/main/res/layout/hms_picker_view.xml @@ -81,6 +81,15 @@ style="@style/label" android:gravity="top" android:text="@string/hms_picker_seconds_label"/> + + + + + + + diff --git a/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsBasicNegativeDurationUsage.java b/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsBasicNegativeDurationUsage.java new file mode 100644 index 00000000..5030d545 --- /dev/null +++ b/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsBasicNegativeDurationUsage.java @@ -0,0 +1,47 @@ +package com.codetroopers.betterpickers.sample.activity.hmspicker; + +import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; + +import com.codetroopers.betterpickers.hmspicker.HmsPickerBuilder; +import com.codetroopers.betterpickers.hmspicker.HmsPickerDialogFragment; +import com.codetroopers.betterpickers.sample.R; +import com.codetroopers.betterpickers.sample.activity.BaseSampleActivity; + +/** + * User: derek Date: 3/17/13 Time: 3:59 PM + */ +public class SampleHmsBasicNegativeDurationUsage extends BaseSampleActivity implements HmsPickerDialogFragment.HmsPickerDialogHandlerV2 { + + private TextView mResultTextView; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.text_and_button); + + mResultTextView = (TextView) findViewById(R.id.text); + Button button = (Button) findViewById(R.id.button); + + mResultTextView.setText(R.string.no_value); + button.setText(R.string.hms_picker_set); + button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + HmsPickerBuilder hpb = new HmsPickerBuilder() + .setPlusMinusVisibility(View.VISIBLE) + .setFragmentManager(getSupportFragmentManager()) + .setStyleResId(R.style.BetterPickersDialogFragment); + hpb.show(); + } + }); + } + + @Override + public void onDialogHmsSet(int reference, boolean isNegative, int hours, int minutes, int seconds) { + mResultTextView.setText(getString(R.string.hms_picker_result_value_multiline, (isNegative ? -1 : 1) * hours, (isNegative ? -1 : 1) * minutes, (isNegative ? -1 : 1) * seconds)); + } + +} From ef6f9fa9e32a1ab1f5f4509da36e264124538f1f Mon Sep 17 00:00:00 2001 From: Florian Chauveau Date: Wed, 11 May 2016 14:36:53 +0200 Subject: [PATCH 2/4] rename negative sample entry --- sample/src/main/AndroidManifest.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sample/src/main/AndroidManifest.xml b/sample/src/main/AndroidManifest.xml index 64d92d79..a9e0894f 100644 --- a/sample/src/main/AndroidManifest.xml +++ b/sample/src/main/AndroidManifest.xml @@ -266,7 +266,7 @@ + android:label="HMS/Negative Duration"> From 800491bdcf230499158ec732ce80decd4a93afaf Mon Sep 17 00:00:00 2001 From: Florian Chauveau Date: Wed, 11 May 2016 14:53:51 +0200 Subject: [PATCH 3/4] deprecate previous handler that not contains negative param --- .../hmspicker/HmsPickerBuilder.java | 38 ++++++++++++++++--- .../hmspicker/HmsPickerDialogFragment.java | 22 ++++++++++- sample/src/main/AndroidManifest.xml | 2 +- .../hmspicker/SampleHmsBasicUsage.java | 6 +-- .../hmspicker/SampleHmsListAdapter.java | 4 +- .../hmspicker/SampleHmsMultipleHandlers.java | 7 ++-- .../SampleHmsMultipleReferences.java | 5 +-- ...va => SampleHmsNegativeDurationUsage.java} | 4 +- .../hmspicker/SampleHmsThemeCustom.java | 7 ++-- .../hmspicker/SampleHmsThemeLight.java | 7 ++-- .../sample/fragment/SampleHmsFragment.java | 7 ++-- sample/src/main/res/values/strings.xml | 2 +- 12 files changed, 76 insertions(+), 35 deletions(-) rename sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/{SampleHmsBasicNegativeDurationUsage.java => SampleHmsNegativeDurationUsage.java} (85%) diff --git a/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPickerBuilder.java b/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPickerBuilder.java index 0c081c7b..1939bcce 100644 --- a/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPickerBuilder.java +++ b/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPickerBuilder.java @@ -19,6 +19,7 @@ public class HmsPickerBuilder { private Fragment targetFragment; private int mReference; private Vector mHmsPickerDialogHandlers = new Vector(); + private Vector mHmsPickerDialogHandlerV2s = new Vector(); private int mHours; private int mMinutes; private int mSeconds; @@ -84,6 +85,32 @@ public HmsPickerBuilder setReference(int reference) { return this; } + /** + * @param handler an Object implementing the appropriate Picker Handler + * @return the current Builder object + * @Deprecated : use HmsPickerDialogHandlerV2 that return negative/positive status + *

+ * Attach universal objects as additional handlers for notification when the Picker is set. For most use cases, this + * method is not necessary as attachment to an Activity or Fragment is done automatically. If, however, you would + * like additional objects to subscribe to this Picker being set, attach Handlers here. + */ + public HmsPickerBuilder addHmsPickerDialogHandler(HmsPickerDialogHandler handler) { + this.mHmsPickerDialogHandlers.add(handler); + return this; + } + + /** + * @param handler the Object to remove + * @return the current Builder object + * @Deprecated : use HmsPickerDialogHandlerV2 that return negative/positive status + *

+ * Remove objects previously added as handlers. + */ + public HmsPickerBuilder removeHmsPickerDialogHandler(HmsPickerDialogHandler handler) { + this.mHmsPickerDialogHandlers.remove(handler); + return this; + } + /** * Attach universal objects as additional handlers for notification when the Picker is set. For most use cases, this * method is not necessary as attachment to an Activity or Fragment is done automatically. If, however, you would @@ -92,8 +119,8 @@ public HmsPickerBuilder setReference(int reference) { * @param handler an Object implementing the appropriate Picker Handler * @return the current Builder object */ - public HmsPickerBuilder addHmsPickerDialogHandler(HmsPickerDialogHandler handler) { - this.mHmsPickerDialogHandlers.add(handler); + public HmsPickerBuilder addHmsPickerDialogHandler(HmsPickerDialogFragment.HmsPickerDialogHandlerV2 handler) { + this.mHmsPickerDialogHandlerV2s.add(handler); return this; } @@ -103,15 +130,15 @@ public HmsPickerBuilder addHmsPickerDialogHandler(HmsPickerDialogHandler handler * @param handler the Object to remove * @return the current Builder object */ - public HmsPickerBuilder removeHmsPickerDialogHandler(HmsPickerDialogHandler handler) { - this.mHmsPickerDialogHandlers.remove(handler); + public HmsPickerBuilder removeHmsPickerDialogHandler(HmsPickerDialogFragment.HmsPickerDialogHandlerV2 handler) { + this.mHmsPickerDialogHandlerV2s.remove(handler); return this; } /** * Set some initial values for the picker * - * @param hours the initial hours value + * @param hours the initial hours value * @param minutes the initial minutes value * @param seconds the initial seconds value * @return the current Builder object @@ -168,6 +195,7 @@ public void show() { fragment.setTargetFragment(targetFragment, 0); } fragment.setHmsPickerDialogHandlers(mHmsPickerDialogHandlers); + fragment.setHmsPickerDialogHandlersV2(mHmsPickerDialogHandlerV2s); if ((mHours | mMinutes | mSeconds) != 0) { fragment.setTime(mHours, mMinutes, mSeconds); diff --git a/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPickerDialogFragment.java b/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPickerDialogFragment.java index 2cdcaf5f..17991d30 100644 --- a/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPickerDialogFragment.java +++ b/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPickerDialogFragment.java @@ -30,7 +30,9 @@ public class HmsPickerDialogFragment extends DialogFragment { private int mTheme = -1; private ColorStateList mTextColor; private int mDialogBackgroundResId; + @Deprecated private Vector mHmsPickerDialogHandlers = new Vector(); + private Vector mHmsPickerDialogHandlerV2s = new Vector(); private int mHours; private int mMinutes; private int mSeconds; @@ -112,6 +114,10 @@ public void onClick(View view) { for (HmsPickerDialogHandler handler : mHmsPickerDialogHandlers) { handler.onDialogHmsSet(mReference, mPicker.getHours(), mPicker.getMinutes(), mPicker.getSeconds()); } + for (HmsPickerDialogHandlerV2 handler : mHmsPickerDialogHandlerV2s) { + handler.onDialogHmsSet(mReference, mPicker.isNegative(), mPicker.getHours(), mPicker.getMinutes(), mPicker.getSeconds()); + } + final Activity activity = getActivity(); final Fragment fragment = getTargetFragment(); @@ -156,22 +162,34 @@ public interface HmsPickerDialogHandlerV2 { } /** + * @Deprecated : use HmsPickerDialogHandlerV2 that return negative/positive status * This interface allows objects to register for the Picker's set action. */ + @Deprecated public interface HmsPickerDialogHandler { + @Deprecated void onDialogHmsSet(int reference, int hours, int minutes, int seconds); } /** - * Attach a Vector of handlers to be notified in addition to the Fragment's Activity and target Fragment. - * * @param handlers a Vector of handlers + * @Deprecated : use HmsPickerDialogHandlerV2 that return negative/positive status + * Attach a Vector of handlers to be notified in addition to the Fragment's Activity and target Fragment. */ + @Deprecated public void setHmsPickerDialogHandlers(Vector handlers) { mHmsPickerDialogHandlers = handlers; } + /** + * @param handlers a Vector of handlers + * Attach a Vector of handlers to be notified in addition to the Fragment's Activity and target Fragment. + */ + public void setHmsPickerDialogHandlersV2(Vector handlers) { + mHmsPickerDialogHandlerV2s = handlers; + } + public void setTime(int hours, int minutes, int seconds) { this.mHours = hours; this.mMinutes = minutes; diff --git a/sample/src/main/AndroidManifest.xml b/sample/src/main/AndroidManifest.xml index a9e0894f..5f8440b3 100644 --- a/sample/src/main/AndroidManifest.xml +++ b/sample/src/main/AndroidManifest.xml @@ -265,7 +265,7 @@ diff --git a/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsBasicUsage.java b/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsBasicUsage.java index acf33dd2..3de5b551 100644 --- a/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsBasicUsage.java +++ b/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsBasicUsage.java @@ -13,7 +13,7 @@ /** * User: derek Date: 3/17/13 Time: 3:59 PM */ -public class SampleHmsBasicUsage extends BaseSampleActivity implements HmsPickerDialogFragment.HmsPickerDialogHandler { +public class SampleHmsBasicUsage extends BaseSampleActivity implements HmsPickerDialogFragment.HmsPickerDialogHandlerV2 { private TextView mResultTextView; @@ -39,7 +39,7 @@ public void onClick(View v) { } @Override - public void onDialogHmsSet(int reference, int hours, int minutes, int seconds) { - mResultTextView.setText(getString(R.string.hms_picker_result_value_multiline, hours, minutes, seconds)); + public void onDialogHmsSet(int reference, boolean isNegative, int hours, int minutes, int seconds) { + mResultTextView.setText(getString(R.string.hms_picker_result_value_multiline, hours, minutes, seconds, isNegative)); } } diff --git a/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsListAdapter.java b/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsListAdapter.java index 38d4a8f6..bc7ab66f 100644 --- a/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsListAdapter.java +++ b/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsListAdapter.java @@ -33,7 +33,7 @@ public void onCreate(Bundle savedInstanceState) { list.setAdapter(new SampleAdapter(this, getSupportFragmentManager())); } - private class SampleAdapter extends BaseAdapter implements HmsPickerDialogFragment.HmsPickerDialogHandler { + private class SampleAdapter extends BaseAdapter implements HmsPickerDialogFragment.HmsPickerDialogHandlerV2 { private ArrayList mHmses; private LayoutInflater mInflater; @@ -115,7 +115,7 @@ public void onClick(View v) { } @Override - public void onDialogHmsSet(int reference, int hours, int minutes, int seconds) { + public void onDialogHmsSet(int reference, boolean isNegative, int hours, int minutes, int seconds) { Hms hms = new Hms(); hms.hours = hours; hms.minutes = minutes; diff --git a/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsMultipleHandlers.java b/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsMultipleHandlers.java index 81e33aa4..0743ad1b 100644 --- a/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsMultipleHandlers.java +++ b/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsMultipleHandlers.java @@ -14,8 +14,7 @@ /** * User: derek Date: 3/17/13 Time: 3:59 PM */ -public class SampleHmsMultipleHandlers extends BaseSampleActivity - implements HmsPickerDialogFragment.HmsPickerDialogHandler { +public class SampleHmsMultipleHandlers extends BaseSampleActivity implements HmsPickerDialogFragment.HmsPickerDialogHandlerV2 { private TextView mResultTextView; @@ -51,7 +50,7 @@ public void onDialogHmsSet(int reference, int hours, int minutes, int seconds) { } @Override - public void onDialogHmsSet(int reference, int hours, int minutes, int seconds) { - mResultTextView.setText(getString(R.string.hms_picker_result_value_multiline, hours, minutes, seconds)); + public void onDialogHmsSet(int reference, boolean isNegative, int hours, int minutes, int seconds) { + mResultTextView.setText(getString(R.string.hms_picker_result_value_multiline, hours, minutes, seconds, isNegative)); } } diff --git a/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsMultipleReferences.java b/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsMultipleReferences.java index 336ced23..3a2071da 100644 --- a/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsMultipleReferences.java +++ b/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsMultipleReferences.java @@ -12,8 +12,7 @@ /** * User: derek Date: 3/17/13 Time: 3:59 PM */ -public class SampleHmsMultipleReferences extends BaseSampleActivity - implements HmsPickerDialogFragment.HmsPickerDialogHandler { +public class SampleHmsMultipleReferences extends BaseSampleActivity implements HmsPickerDialogFragment.HmsPickerDialogHandlerV2 { private static final int BUTTON_ONE_REFERENCE = 0; private static final int BUTTON_TWO_REFERENCE = 1; @@ -82,7 +81,7 @@ public void onClick(View v) { } @Override - public void onDialogHmsSet(int reference, int hours, int minutes, int seconds) { + public void onDialogHmsSet(int reference, boolean isNegative, int hours, int minutes, int seconds) { Button buttonToSet; switch (reference) { case BUTTON_ONE_REFERENCE: diff --git a/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsBasicNegativeDurationUsage.java b/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsNegativeDurationUsage.java similarity index 85% rename from sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsBasicNegativeDurationUsage.java rename to sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsNegativeDurationUsage.java index 5030d545..64b51e23 100644 --- a/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsBasicNegativeDurationUsage.java +++ b/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsNegativeDurationUsage.java @@ -13,7 +13,7 @@ /** * User: derek Date: 3/17/13 Time: 3:59 PM */ -public class SampleHmsBasicNegativeDurationUsage extends BaseSampleActivity implements HmsPickerDialogFragment.HmsPickerDialogHandlerV2 { +public class SampleHmsNegativeDurationUsage extends BaseSampleActivity implements HmsPickerDialogFragment.HmsPickerDialogHandlerV2 { private TextView mResultTextView; @@ -41,7 +41,7 @@ public void onClick(View v) { @Override public void onDialogHmsSet(int reference, boolean isNegative, int hours, int minutes, int seconds) { - mResultTextView.setText(getString(R.string.hms_picker_result_value_multiline, (isNegative ? -1 : 1) * hours, (isNegative ? -1 : 1) * minutes, (isNegative ? -1 : 1) * seconds)); + mResultTextView.setText(getString(R.string.hms_picker_result_value_multiline, hours, minutes, seconds, isNegative)); } } diff --git a/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsThemeCustom.java b/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsThemeCustom.java index 98dd1e43..5f054250 100644 --- a/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsThemeCustom.java +++ b/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsThemeCustom.java @@ -13,8 +13,7 @@ /** * User: derek Date: 3/17/13 Time: 3:59 PM */ -public class SampleHmsThemeCustom extends BaseSampleActivity - implements HmsPickerDialogFragment.HmsPickerDialogHandler { +public class SampleHmsThemeCustom extends BaseSampleActivity implements HmsPickerDialogFragment.HmsPickerDialogHandlerV2 { private TextView mResultTextView; @@ -40,7 +39,7 @@ public void onClick(View v) { } @Override - public void onDialogHmsSet(int reference, int hours, int minutes, int seconds) { - mResultTextView.setText(getString(R.string.hms_picker_result_value_multiline, hours, minutes, seconds)); + public void onDialogHmsSet(int reference, boolean isNegative, int hours, int minutes, int seconds) { + mResultTextView.setText(getString(R.string.hms_picker_result_value_multiline, hours, minutes, seconds, isNegative)); } } diff --git a/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsThemeLight.java b/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsThemeLight.java index be050018..68b0ab47 100644 --- a/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsThemeLight.java +++ b/sample/src/main/java/com/codetroopers/betterpickers/sample/activity/hmspicker/SampleHmsThemeLight.java @@ -13,8 +13,7 @@ /** * User: derek Date: 3/17/13 Time: 3:59 PM */ -public class SampleHmsThemeLight extends BaseSampleActivity - implements HmsPickerDialogFragment.HmsPickerDialogHandler { +public class SampleHmsThemeLight extends BaseSampleActivity implements HmsPickerDialogFragment.HmsPickerDialogHandlerV2 { private TextView mResultTextView; @@ -40,7 +39,7 @@ public void onClick(View v) { } @Override - public void onDialogHmsSet(int reference, int hours, int minutes, int seconds) { - mResultTextView.setText(getString(R.string.hms_picker_result_value_multiline, hours, minutes, seconds)); + public void onDialogHmsSet(int reference, boolean isNegative, int hours, int minutes, int seconds) { + mResultTextView.setText(getString(R.string.hms_picker_result_value_multiline, hours, minutes, seconds, isNegative)); } } diff --git a/sample/src/main/java/com/codetroopers/betterpickers/sample/fragment/SampleHmsFragment.java b/sample/src/main/java/com/codetroopers/betterpickers/sample/fragment/SampleHmsFragment.java index 0987c61b..8c593150 100644 --- a/sample/src/main/java/com/codetroopers/betterpickers/sample/fragment/SampleHmsFragment.java +++ b/sample/src/main/java/com/codetroopers/betterpickers/sample/fragment/SampleHmsFragment.java @@ -15,8 +15,7 @@ /** * User: derek Date: 4/30/13 Time: 7:43 PM */ -public class SampleHmsFragment extends Fragment - implements HmsPickerDialogFragment.HmsPickerDialogHandler { +public class SampleHmsFragment extends Fragment implements HmsPickerDialogFragment.HmsPickerDialogHandlerV2 { private TextView mResultTextView; @@ -44,7 +43,7 @@ public void onClick(View v) { } @Override - public void onDialogHmsSet(int reference, int hours, int minutes, int seconds) { - mResultTextView.setText(getString(R.string.hms_picker_result_value_multiline, hours, minutes, seconds)); + public void onDialogHmsSet(int reference, boolean isNegative, int hours, int minutes, int seconds) { + mResultTextView.setText(getString(R.string.hms_picker_result_value_multiline, hours, minutes, seconds, isNegative)); } } diff --git a/sample/src/main/res/values/strings.xml b/sample/src/main/res/values/strings.xml index f23b6455..64940b2d 100644 --- a/sample/src/main/res/values/strings.xml +++ b/sample/src/main/res/values/strings.xml @@ -15,7 +15,7 @@ Set Expiration %1$s/%2$s Set HMS - Hours: %1$s\nMinutes: %2$s\nSeconds: %3$s + Hours: %1$s\nMinutes: %2$s\nSeconds: %3$s\nNegative : %4$b H:%1$s, M:%2$s, S:%3$s Set Time %1$s:%2$s From abea29bff849230a82e77eca95d01b77385416d3 Mon Sep 17 00:00:00 2001 From: Florian Chauveau Date: Wed, 11 May 2016 15:04:14 +0200 Subject: [PATCH 4/4] refactor : using method isNegativ --- .../betterpickers/hmspicker/HmsPicker.java | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPicker.java b/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPicker.java index 544b2e41..0748d125 100644 --- a/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPicker.java +++ b/library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPicker.java @@ -58,7 +58,7 @@ public HmsPicker(Context context) { * Instantiates an HmsPicker object * * @param context the Context required for creation - * @param attrs additional attributes that define custom colors, selectors, and backgrounds. + * @param attrs additional attributes that define custom colors, selectors, and backgrounds. */ public HmsPicker(Context context, AttributeSet attrs) { super(context, attrs); @@ -130,7 +130,7 @@ private void restyleViews() { if (mEnteredHms != null) { mEnteredHms.setTheme(mTheme); } - if(mLeft != null){ + if (mLeft != null) { mLeft.setTextColor(mTextColor); mLeft.setBackgroundResource(mKeyBackgroundResId); } @@ -176,8 +176,6 @@ protected void onFinishInflate() { Resources res = mContext.getResources(); mLeft.setText(res.getString(R.string.number_picker_plus_minus)); mLeft.setOnClickListener(this); -// mLabel = (TextView) findViewById(R.id.label); -// mSign = SIGN_POSITIVE; mHoursLabel = (TextView) findViewById(R.id.hours_label); mMinutesLabel = (TextView) findViewById(R.id.minutes_label); @@ -218,17 +216,17 @@ protected void doOnClick(View v) { mInput[mInputPointer] = 0; mInputPointer--; } - } else if(v == mLeft){ + } else if (v == mLeft) { onLeftClicked(); } updateKeypad(); } private void onLeftClicked() { - if (mSign == SIGN_POSITIVE) { - mSign = SIGN_NEGATIVE; - } else { + if (isNegative()) { mSign = SIGN_POSITIVE; + } else { + mSign = SIGN_NEGATIVE; } } @@ -268,13 +266,13 @@ private void updateKeypad() { /** * Update the time displayed in the picker: - * + *

* Put "-" in digits that was not entered by passing -1 - * + *

* Hide digit by passing -2 (for highest hours digit only); */ protected void updateHms() { - mEnteredHms.setTime(mSign == SIGN_NEGATIVE, mInput[4], mInput[3], mInput[2], mInput[1], mInput[0]); + mEnteredHms.setTime(isNegative(), mInput[4], mInput[3], mInput[2], mInput[1], mInput[0]); } private void addClickedNumber(int val) { @@ -356,7 +354,7 @@ public void setPlusMinusVisibility(int visibility) { /** * Set the current hours, minutes, and seconds on the picker. * - * @param hours the input hours value + * @param hours the input hours value * @param minutes the input minutes value * @param seconds the input seconds value */ @@ -367,7 +365,7 @@ public void setTime(int hours, int minutes, int seconds) { mInput[1] = seconds / 10; mInput[0] = seconds % 10; - for (int i=4; i>=0; i--) { + for (int i = 4; i >= 0; i--) { if (mInput[i] > 0) { mInputPointer = i; break; @@ -476,7 +474,7 @@ protected void setRightEnabled(boolean enabled) { } } - public boolean isNegative(){ + public boolean isNegative() { return mSign == SIGN_NEGATIVE; } }