Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 3479823

Browse files
committed
Merge pull request #281 from code-troopers/HMSmius
add HmsMinus option
2 parents 5294d8e + abea29b commit 3479823

File tree

15 files changed

+243
-45
lines changed

15 files changed

+243
-45
lines changed

library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPicker.java

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.content.Context;
44
import android.content.res.ColorStateList;
5+
import android.content.res.Resources;
56
import android.content.res.TypedArray;
67
import android.os.Bundle;
78
import android.os.Parcel;
@@ -40,6 +41,10 @@ public class HmsPicker extends LinearLayout implements Button.OnClickListener, B
4041
private int mDeleteDrawableSrcResId;
4142
private int mTheme = -1;
4243

44+
private int mSign;
45+
public static final int SIGN_POSITIVE = 0;
46+
public static final int SIGN_NEGATIVE = 1;
47+
4348
/**
4449
* Instantiates an HmsPicker object
4550
*
@@ -53,7 +58,7 @@ public HmsPicker(Context context) {
5358
* Instantiates an HmsPicker object
5459
*
5560
* @param context the Context required for creation
56-
* @param attrs additional attributes that define custom colors, selectors, and backgrounds.
61+
* @param attrs additional attributes that define custom colors, selectors, and backgrounds.
5762
*/
5863
public HmsPicker(Context context, AttributeSet attrs) {
5964
super(context, attrs);
@@ -125,6 +130,10 @@ private void restyleViews() {
125130
if (mEnteredHms != null) {
126131
mEnteredHms.setTheme(mTheme);
127132
}
133+
if (mLeft != null) {
134+
mLeft.setTextColor(mTextColor);
135+
mLeft.setBackgroundResource(mKeyBackgroundResId);
136+
}
128137
}
129138

130139
@Override
@@ -155,7 +164,7 @@ protected void onFinishInflate() {
155164
mLeft = (Button) v4.findViewById(R.id.key_left);
156165
mNumbers[0] = (Button) v4.findViewById(R.id.key_middle);
157166
mRight = (Button) v4.findViewById(R.id.key_right);
158-
setLeftRightEnabled(false);
167+
setRightEnabled(false);
159168

160169
for (int i = 0; i < 10; i++) {
161170
mNumbers[i].setOnClickListener(this);
@@ -164,6 +173,10 @@ protected void onFinishInflate() {
164173
}
165174
updateHms();
166175

176+
Resources res = mContext.getResources();
177+
mLeft.setText(res.getString(R.string.number_picker_plus_minus));
178+
mLeft.setOnClickListener(this);
179+
167180
mHoursLabel = (TextView) findViewById(R.id.hours_label);
168181
mMinutesLabel = (TextView) findViewById(R.id.minutes_label);
169182
mSecondsLabel = (TextView) findViewById(R.id.seconds_label);
@@ -203,10 +216,20 @@ protected void doOnClick(View v) {
203216
mInput[mInputPointer] = 0;
204217
mInputPointer--;
205218
}
219+
} else if (v == mLeft) {
220+
onLeftClicked();
206221
}
207222
updateKeypad();
208223
}
209224

225+
private void onLeftClicked() {
226+
if (isNegative()) {
227+
mSign = SIGN_POSITIVE;
228+
} else {
229+
mSign = SIGN_NEGATIVE;
230+
}
231+
}
232+
210233
@Override
211234
public boolean onLongClick(View v) {
212235
v.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
@@ -243,13 +266,13 @@ private void updateKeypad() {
243266

244267
/**
245268
* Update the time displayed in the picker:
246-
*
269+
* <p/>
247270
* Put "-" in digits that was not entered by passing -1
248-
*
271+
* <p/>
249272
* Hide digit by passing -2 (for highest hours digit only);
250273
*/
251274
protected void updateHms() {
252-
mEnteredHms.setTime(mInput[4], mInput[3], mInput[2], mInput[1], mInput[0]);
275+
mEnteredHms.setTime(isNegative(), mInput[4], mInput[3], mInput[2], mInput[1], mInput[0]);
253276
}
254277

255278
private void addClickedNumber(int val) {
@@ -317,10 +340,21 @@ public int getSeconds() {
317340
return mInput[1] * 10 + mInput[0];
318341
}
319342

343+
/**
344+
* Using View.GONE, View.VISIBILE, or View.INVISIBLE, set the visibility of the plus/minus indicator
345+
*
346+
* @param visibility an int using Android's View.* convention
347+
*/
348+
public void setPlusMinusVisibility(int visibility) {
349+
if (mLeft != null) {
350+
mLeft.setVisibility(visibility);
351+
}
352+
}
353+
320354
/**
321355
* Set the current hours, minutes, and seconds on the picker.
322356
*
323-
* @param hours the input hours value
357+
* @param hours the input hours value
324358
* @param minutes the input minutes value
325359
* @param seconds the input seconds value
326360
*/
@@ -331,7 +365,7 @@ public void setTime(int hours, int minutes, int seconds) {
331365
mInput[1] = seconds / 10;
332366
mInput[0] = seconds % 10;
333367

334-
for (int i=4; i>=0; i--) {
368+
for (int i = 4; i >= 0; i--) {
335369
if (mInput[i] > 0) {
336370
mInputPointer = i;
337371
break;
@@ -342,7 +376,7 @@ public void setTime(int hours, int minutes, int seconds) {
342376
}
343377

344378

345-
@Override
379+
@Override
346380
public Parcelable onSaveInstanceState() {
347381
final Parcelable parcel = super.onSaveInstanceState();
348382
final SavedState state = new SavedState(parcel);
@@ -433,12 +467,14 @@ public void restoreEntryState(Bundle inState, String key) {
433467
}
434468
}
435469

436-
protected void setLeftRightEnabled(boolean enabled) {
437-
mLeft.setEnabled(enabled);
470+
protected void setRightEnabled(boolean enabled) {
438471
mRight.setEnabled(enabled);
439472
if (!enabled) {
440-
mLeft.setContentDescription(null);
441473
mRight.setContentDescription(null);
442474
}
443475
}
476+
477+
public boolean isNegative() {
478+
return mSign == SIGN_NEGATIVE;
479+
}
444480
}

library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPickerBuilder.java

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,26 @@ public class HmsPickerBuilder {
1919
private Fragment targetFragment;
2020
private int mReference;
2121
private Vector<HmsPickerDialogHandler> mHmsPickerDialogHandlers = new Vector<HmsPickerDialogHandler>();
22+
private Vector<HmsPickerDialogFragment.HmsPickerDialogHandlerV2> mHmsPickerDialogHandlerV2s = new Vector<HmsPickerDialogFragment.HmsPickerDialogHandlerV2>();
2223
private int mHours;
2324
private int mMinutes;
2425
private int mSeconds;
26+
private Integer plusMinusVisibility;
27+
28+
/**
29+
* Set the visibility of the +/- button. This takes an int corresponding to Android's View.VISIBLE, View.INVISIBLE,
30+
* or View.GONE. When using View.INVISIBLE, the +/- button will still be present in the layout but be
31+
* non-clickable. When set to View.GONE, the +/- button will disappear entirely, and the "0" button will occupy its
32+
* space.
33+
*
34+
* @param plusMinusVisibility an int corresponding to View.VISIBLE, View.INVISIBLE, or View.GONE
35+
* @return the current Builder object
36+
*/
37+
public HmsPickerBuilder setPlusMinusVisibility(int plusMinusVisibility) {
38+
this.plusMinusVisibility = plusMinusVisibility;
39+
return this;
40+
}
41+
2542

2643
/**
2744
* Attach a FragmentManager. This is required for creation of the Fragment.
@@ -68,6 +85,32 @@ public HmsPickerBuilder setReference(int reference) {
6885
return this;
6986
}
7087

88+
/**
89+
* @param handler an Object implementing the appropriate Picker Handler
90+
* @return the current Builder object
91+
* @Deprecated : use HmsPickerDialogHandlerV2 that return negative/positive status
92+
* <p/>
93+
* Attach universal objects as additional handlers for notification when the Picker is set. For most use cases, this
94+
* method is not necessary as attachment to an Activity or Fragment is done automatically. If, however, you would
95+
* like additional objects to subscribe to this Picker being set, attach Handlers here.
96+
*/
97+
public HmsPickerBuilder addHmsPickerDialogHandler(HmsPickerDialogHandler handler) {
98+
this.mHmsPickerDialogHandlers.add(handler);
99+
return this;
100+
}
101+
102+
/**
103+
* @param handler the Object to remove
104+
* @return the current Builder object
105+
* @Deprecated : use HmsPickerDialogHandlerV2 that return negative/positive status
106+
* <p/>
107+
* Remove objects previously added as handlers.
108+
*/
109+
public HmsPickerBuilder removeHmsPickerDialogHandler(HmsPickerDialogHandler handler) {
110+
this.mHmsPickerDialogHandlers.remove(handler);
111+
return this;
112+
}
113+
71114
/**
72115
* Attach universal objects as additional handlers for notification when the Picker is set. For most use cases, this
73116
* method is not necessary as attachment to an Activity or Fragment is done automatically. If, however, you would
@@ -76,8 +119,8 @@ public HmsPickerBuilder setReference(int reference) {
76119
* @param handler an Object implementing the appropriate Picker Handler
77120
* @return the current Builder object
78121
*/
79-
public HmsPickerBuilder addHmsPickerDialogHandler(HmsPickerDialogHandler handler) {
80-
this.mHmsPickerDialogHandlers.add(handler);
122+
public HmsPickerBuilder addHmsPickerDialogHandler(HmsPickerDialogFragment.HmsPickerDialogHandlerV2 handler) {
123+
this.mHmsPickerDialogHandlerV2s.add(handler);
81124
return this;
82125
}
83126

@@ -87,15 +130,15 @@ public HmsPickerBuilder addHmsPickerDialogHandler(HmsPickerDialogHandler handler
87130
* @param handler the Object to remove
88131
* @return the current Builder object
89132
*/
90-
public HmsPickerBuilder removeHmsPickerDialogHandler(HmsPickerDialogHandler handler) {
91-
this.mHmsPickerDialogHandlers.remove(handler);
133+
public HmsPickerBuilder removeHmsPickerDialogHandler(HmsPickerDialogFragment.HmsPickerDialogHandlerV2 handler) {
134+
this.mHmsPickerDialogHandlerV2s.remove(handler);
92135
return this;
93136
}
94137

95138
/**
96139
* Set some initial values for the picker
97140
*
98-
* @param hours the initial hours value
141+
* @param hours the initial hours value
99142
* @param minutes the initial minutes value
100143
* @param seconds the initial seconds value
101144
* @return the current Builder object
@@ -147,11 +190,12 @@ public void show() {
147190
}
148191
ft.addToBackStack(null);
149192

150-
final HmsPickerDialogFragment fragment = HmsPickerDialogFragment.newInstance(mReference, styleResId);
193+
final HmsPickerDialogFragment fragment = HmsPickerDialogFragment.newInstance(mReference, styleResId, plusMinusVisibility);
151194
if (targetFragment != null) {
152195
fragment.setTargetFragment(targetFragment, 0);
153196
}
154197
fragment.setHmsPickerDialogHandlers(mHmsPickerDialogHandlers);
198+
fragment.setHmsPickerDialogHandlersV2(mHmsPickerDialogHandlerV2s);
155199

156200
if ((mHours | mMinutes | mSeconds) != 0) {
157201
fragment.setTime(mHours, mMinutes, mSeconds);

library/src/main/java/com/codetroopers/betterpickers/hmspicker/HmsPickerDialogFragment.java

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,21 @@ public class HmsPickerDialogFragment extends DialogFragment {
2222

2323
private static final String REFERENCE_KEY = "HmsPickerDialogFragment_ReferenceKey";
2424
private static final String THEME_RES_ID_KEY = "HmsPickerDialogFragment_ThemeResIdKey";
25+
private static final String PLUS_MINUS_VISIBILITY_KEY = "HmsPickerDialogFragment_PlusMinusVisibilityKey";
2526

2627
private HmsPicker mPicker;
2728

2829
private int mReference = -1;
2930
private int mTheme = -1;
3031
private ColorStateList mTextColor;
3132
private int mDialogBackgroundResId;
33+
@Deprecated
3234
private Vector<HmsPickerDialogHandler> mHmsPickerDialogHandlers = new Vector<HmsPickerDialogHandler>();
35+
private Vector<HmsPickerDialogHandlerV2> mHmsPickerDialogHandlerV2s = new Vector<HmsPickerDialogHandlerV2>();
3336
private int mHours;
3437
private int mMinutes;
3538
private int mSeconds;
39+
private int mPlusMinusVisibility = View.INVISIBLE;
3640

3741
/**
3842
* Create an instance of the Picker (used internally)
@@ -41,11 +45,14 @@ public class HmsPickerDialogFragment extends DialogFragment {
4145
* @param themeResId the style resource ID for theming
4246
* @return a Picker!
4347
*/
44-
public static HmsPickerDialogFragment newInstance(int reference, int themeResId) {
48+
public static HmsPickerDialogFragment newInstance(int reference, int themeResId, Integer plusMinusVisibility) {
4549
final HmsPickerDialogFragment frag = new HmsPickerDialogFragment();
4650
Bundle args = new Bundle();
4751
args.putInt(REFERENCE_KEY, reference);
4852
args.putInt(THEME_RES_ID_KEY, themeResId);
53+
if (plusMinusVisibility != null) {
54+
args.putInt(PLUS_MINUS_VISIBILITY_KEY, plusMinusVisibility);
55+
}
4956
frag.setArguments(args);
5057
return frag;
5158
}
@@ -66,6 +73,9 @@ public void onCreate(Bundle savedInstanceState) {
6673
if (args != null && args.containsKey(THEME_RES_ID_KEY)) {
6774
mTheme = args.getInt(THEME_RES_ID_KEY);
6875
}
76+
if (args != null && args.containsKey(PLUS_MINUS_VISIBILITY_KEY)) {
77+
mPlusMinusVisibility = args.getInt(PLUS_MINUS_VISIBILITY_KEY);
78+
}
6979

7080
setStyle(DialogFragment.STYLE_NO_TITLE, 0);
7181

@@ -104,8 +114,13 @@ public void onClick(View view) {
104114
for (HmsPickerDialogHandler handler : mHmsPickerDialogHandlers) {
105115
handler.onDialogHmsSet(mReference, mPicker.getHours(), mPicker.getMinutes(), mPicker.getSeconds());
106116
}
117+
for (HmsPickerDialogHandlerV2 handler : mHmsPickerDialogHandlerV2s) {
118+
handler.onDialogHmsSet(mReference, mPicker.isNegative(), mPicker.getHours(), mPicker.getMinutes(), mPicker.getSeconds());
119+
}
120+
107121
final Activity activity = getActivity();
108122
final Fragment fragment = getTargetFragment();
123+
109124
if (activity instanceof HmsPickerDialogHandler) {
110125
final HmsPickerDialogHandler act =
111126
(HmsPickerDialogHandler) activity;
@@ -115,6 +130,17 @@ public void onClick(View view) {
115130
(HmsPickerDialogHandler) fragment;
116131
frag.onDialogHmsSet(mReference, mPicker.getHours(), mPicker.getMinutes(), mPicker.getSeconds());
117132
}
133+
134+
if (activity instanceof HmsPickerDialogHandlerV2) {
135+
final HmsPickerDialogHandlerV2 act =
136+
(HmsPickerDialogHandlerV2) activity;
137+
act.onDialogHmsSet(mReference, mPicker.isNegative(), mPicker.getHours(), mPicker.getMinutes(), mPicker.getSeconds());
138+
} else if (fragment instanceof HmsPickerDialogHandlerV2) {
139+
final HmsPickerDialogHandlerV2 frag =
140+
(HmsPickerDialogHandlerV2) fragment;
141+
frag.onDialogHmsSet(mReference, mPicker.isNegative(), mPicker.getHours(), mPicker.getMinutes(), mPicker.getSeconds());
142+
}
143+
118144
dismiss();
119145
}
120146
});
@@ -123,29 +149,47 @@ public void onClick(View view) {
123149
mPicker.setSetButton(doneButton);
124150
mPicker.setTime(mHours, mMinutes, mSeconds);
125151
mPicker.setTheme(mTheme);
152+
mPicker.setPlusMinusVisibility(mPlusMinusVisibility);
126153

127154
getDialog().getWindow().setBackgroundDrawableResource(mDialogBackgroundResId);
128155

129156
return view;
130157
}
131158

159+
public interface HmsPickerDialogHandlerV2 {
160+
161+
void onDialogHmsSet(int reference, boolean isNegative, int hours, int minutes, int seconds);
162+
}
163+
132164
/**
165+
* @Deprecated : use HmsPickerDialogHandlerV2 that return negative/positive status
133166
* This interface allows objects to register for the Picker's set action.
134167
*/
168+
@Deprecated
135169
public interface HmsPickerDialogHandler {
136170

171+
@Deprecated
137172
void onDialogHmsSet(int reference, int hours, int minutes, int seconds);
138173
}
139174

140175
/**
141-
* Attach a Vector of handlers to be notified in addition to the Fragment's Activity and target Fragment.
142-
*
143176
* @param handlers a Vector of handlers
177+
* @Deprecated : use HmsPickerDialogHandlerV2 that return negative/positive status
178+
* Attach a Vector of handlers to be notified in addition to the Fragment's Activity and target Fragment.
144179
*/
180+
@Deprecated
145181
public void setHmsPickerDialogHandlers(Vector<HmsPickerDialogHandler> handlers) {
146182
mHmsPickerDialogHandlers = handlers;
147183
}
148184

185+
/**
186+
* @param handlers a Vector of handlers
187+
* Attach a Vector of handlers to be notified in addition to the Fragment's Activity and target Fragment.
188+
*/
189+
public void setHmsPickerDialogHandlersV2(Vector<HmsPickerDialogHandlerV2> handlers) {
190+
mHmsPickerDialogHandlerV2s = handlers;
191+
}
192+
149193
public void setTime(int hours, int minutes, int seconds) {
150194
this.mHours = hours;
151195
this.mMinutes = minutes;

0 commit comments

Comments
 (0)