Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added HMSPicker hour minute only option #2 #368

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class HmsPicker extends LinearLayout implements Button.OnClickListener, B
public static final int SIGN_POSITIVE = 0;
public static final int SIGN_NEGATIVE = 1;

private boolean mHourMinutesOnly;

/**
* Instantiates an HmsPicker object
*
Expand Down Expand Up @@ -119,7 +121,7 @@ private void restyleViews() {
mMinutesLabel.setTextColor(mTextColor);
mMinutesLabel.setBackgroundResource(mKeyBackgroundResId);
}
if (mSecondsLabel != null) {
if (mSecondsLabel != null && !mHourMinutesOnly) {
mSecondsLabel.setTextColor(mTextColor);
mSecondsLabel.setBackgroundResource(mKeyBackgroundResId);
}
Expand All @@ -129,6 +131,10 @@ private void restyleViews() {
}
if (mEnteredHms != null) {
mEnteredHms.setTheme(mTheme);

if(mHourMinutesOnly) {
mEnteredHms.setHourMinutesOnly(true);
}
}
if (mLeft != null) {
mLeft.setTextColor(mTextColor);
Expand Down Expand Up @@ -272,7 +278,11 @@ private void updateKeypad() {
* Hide digit by passing -2 (for highest hours digit only);
*/
protected void updateHms() {
mEnteredHms.setTime(isNegative(), mInput[4], mInput[3], mInput[2], mInput[1], mInput[0]);
if(mHourMinutesOnly) {
mEnteredHms.setTime(isNegative(), mInput[3], mInput[2], mInput[1], mInput[0]);
} else {
mEnteredHms.setTime(isNegative(), mInput[4], mInput[3], mInput[2], mInput[1], mInput[0]);
}
}

private void addClickedNumber(int val) {
Expand Down Expand Up @@ -318,8 +328,11 @@ public void setSetButton(Button b) {
* @return the inputted hours
*/
public int getHours() {
int hours = mInput[4];
return hours;
if(mHourMinutesOnly){
return mInput[3] * 10 + mInput[2];
} else {
return mInput[4];
}
}

/**
Expand All @@ -328,7 +341,11 @@ public int getHours() {
* @return the inputted minutes
*/
public int getMinutes() {
return mInput[3] * 10 + mInput[2];
if(mHourMinutesOnly){
return mInput[1] * 10 + mInput[0];
} else {
return mInput[3] * 10 + mInput[2];
}
}

/**
Expand All @@ -337,7 +354,11 @@ public int getMinutes() {
* @return the inputted seconds
*/
public int getSeconds() {
return mInput[1] * 10 + mInput[0];
if(mHourMinutesOnly){
return 0;
} else {
return mInput[1] * 10 + mInput[0];
}
}

/**
Expand All @@ -359,13 +380,20 @@ public void setPlusMinusVisibility(int visibility) {
* @param seconds the input seconds value
*/
public void setTime(int hours, int minutes, int seconds) {
mInput[4] = hours;
mInput[3] = minutes / 10;
mInput[2] = minutes % 10;
mInput[1] = seconds / 10;
mInput[0] = seconds % 10;
if(mHourMinutesOnly) {
mInput[3] = hours / 10;
mInput[2] = hours % 10;
mInput[1] = minutes / 10;
mInput[0] = minutes % 10;
} else {
mInput[4] = hours;
mInput[3] = minutes / 10;
mInput[2] = minutes % 10;
mInput[1] = seconds / 10;
mInput[0] = seconds % 10;
}

for (int i = 4; i >= 0; i--) {
for (int i=mInputSize - 1; i>=0; i--) {
if (mInput[i] > 0) {
mInputPointer = i;
break;
Expand Down Expand Up @@ -447,12 +475,25 @@ public SavedState[] newArray(int size) {
* @return an int representing the time in seconds
*/
public int getTime() {
return mInput[4] * 3600 + mInput[3] * 600 + mInput[2] * 60 + mInput[1] * 10 + mInput[0];
if(mHourMinutesOnly) {
return mInput[3] * 36_000 + mInput[2] * 3600 + mInput[1] * 600 + mInput[0] * 60;
} else {
return mInput[4] * 3600 + mInput[3] * 600 + mInput[2] * 60 + mInput[1] * 10 + mInput[0];
}
}

public void saveEntryState(Bundle outState, String key) {
outState.putIntArray(key, mInput);
}

public void setHourMinutesOnly(boolean mHourMinutesOnly) {
this.mHourMinutesOnly = mHourMinutesOnly;
if(mHourMinutesOnly) {
mInputSize = 4;
mInput = new int[mInputSize];
restyleViews();
}
}

public void restoreEntryState(Bundle inState, String key) {
int[] input = inState.getIntArray(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class HmsPickerBuilder {
private Integer plusMinusVisibility;
private OnDialogDismissListener mOnDismissListener;

private Boolean mHourMinutesOnly;

/**
* 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
Expand All @@ -39,6 +41,14 @@ public HmsPickerBuilder setPlusMinusVisibility(int plusMinusVisibility) {
return this;
}

/**
* Shows only the hours and minutes.
* @param hourMinutesOnly if True shows only hours and minutes. False by default.
*/
public HmsPickerBuilder setHourMinutesOnly(boolean hourMinutesOnly){
this.mHourMinutesOnly = hourMinutesOnly;
return this;
}

/**
* Attach a FragmentManager. This is required for creation of the Fragment.
Expand Down Expand Up @@ -165,7 +175,8 @@ public void show() {
}
ft.addToBackStack(null);

final HmsPickerDialogFragment fragment = HmsPickerDialogFragment.newInstance(mReference, styleResId, plusMinusVisibility);
final HmsPickerDialogFragment fragment = HmsPickerDialogFragment.newInstance(mReference, styleResId, plusMinusVisibility,
mHourMinutesOnly);
if (targetFragment != null) {
fragment.setTargetFragment(targetFragment, 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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 static final String HOUR_MINUTES_ONLY_KEY = "HmsPickerDialogFragment_HourMinutesOnly";

private HmsPicker mPicker;

Expand All @@ -36,6 +37,7 @@ public class HmsPickerDialogFragment extends DialogFragment {
private int mHours;
private int mMinutes;
private int mSeconds;
private boolean mHourMinutesOnly = false;
private int mPlusMinusVisibility = View.INVISIBLE;
private OnDialogDismissListener mDismissCallback;

Expand All @@ -46,14 +48,18 @@ public class HmsPickerDialogFragment extends DialogFragment {
* @param themeResId the style resource ID for theming
* @return a Picker!
*/
public static HmsPickerDialogFragment newInstance(int reference, int themeResId, Integer plusMinusVisibility) {
public static HmsPickerDialogFragment newInstance(int reference, int themeResId, Integer plusMinusVisibility,
Boolean hourMinutesOnly) {
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);
}
if(hourMinutesOnly != null){
args.putBoolean(HOUR_MINUTES_ONLY_KEY, hourMinutesOnly);
}
frag.setArguments(args);
return frag;
}
Expand All @@ -77,6 +83,9 @@ public void onCreate(Bundle savedInstanceState) {
if (args != null && args.containsKey(PLUS_MINUS_VISIBILITY_KEY)) {
mPlusMinusVisibility = args.getInt(PLUS_MINUS_VISIBILITY_KEY);
}
if(args != null && args.containsKey(HOUR_MINUTES_ONLY_KEY)){
mHourMinutesOnly = args.getBoolean(HOUR_MINUTES_ONLY_KEY);
}

setStyle(DialogFragment.STYLE_NO_TITLE, 0);

Expand Down Expand Up @@ -138,6 +147,7 @@ public void onClick(View view) {
mPicker.setTime(mHours, mMinutes, mSeconds);
mPicker.setTheme(mTheme);
mPicker.setPlusMinusVisibility(mPlusMinusVisibility);
mPicker.setHourMinutesOnly(mHourMinutesOnly);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@

public class HmsView extends LinearLayout {

private ZeroTopPaddingTextView mHoursOnes;
private ZeroTopPaddingTextView mHoursOnes, mHoursTens;
private ZeroTopPaddingTextView mMinutesOnes, mMinutesTens;
private ZeroTopPaddingTextView mSecondsOnes, mSecondsTens;
private ZeroTopPaddingTextView mSecondsOnes, mSecondsTens, mSecondsLabel;
private final Typeface mAndroidClockMonoThin;
private Typeface mOriginalHoursTypeface;
private ZeroTopPaddingTextView mMinusLabel;
private boolean hourMinutesOnly = false;

private ColorStateList mTextColor;

Expand Down Expand Up @@ -63,6 +64,9 @@ public void setTheme(int themeResId) {
}

private void restyleViews() {
if (mHoursTens != null) {
mHoursTens.setTextColor(mTextColor);
}
if (mHoursOnes != null) {
mHoursOnes.setTextColor(mTextColor);
}
Expand All @@ -87,32 +91,62 @@ private void restyleViews() {
protected void onFinishInflate() {
super.onFinishInflate();

mHoursTens = (ZeroTopPaddingTextView) findViewById(R.id.hours_tens);
mHoursOnes = (ZeroTopPaddingTextView) findViewById(R.id.hours_ones);
mMinutesTens = (ZeroTopPaddingTextView) findViewById(R.id.minutes_tens);
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);
mSecondsLabel = (ZeroTopPaddingTextView) findViewById(R.id.seconds_label);

onUpdateViewsVisibility();
}

private void onUpdateViewsVisibility() {
if (mHoursTens != null && !hourMinutesOnly) {
mHoursTens.setVisibility(GONE);
}
if (mHoursOnes != null) {
mOriginalHoursTypeface = mHoursOnes.getTypeface();
mHoursOnes.updatePaddingForBoldDate();
}
if (mMinutesTens != null) {
if (mMinutesTens != null && !hourMinutesOnly) {
mMinutesTens.updatePaddingForBoldDate();
}
if (mMinutesOnes != null) {
if (mMinutesOnes != null && !hourMinutesOnly) {
mMinutesOnes.updatePaddingForBoldDate();
}
// Set the lowest time unit with thin font (excluding hundredths)
if (mSecondsTens != null) {
if (mSecondsTens != null && !hourMinutesOnly) {
mSecondsTens.setTypeface(mAndroidClockMonoThin);
mSecondsTens.updatePadding();
}
if (mSecondsOnes != null) {
if (mSecondsOnes != null && !hourMinutesOnly) {
mSecondsOnes.setTypeface(mAndroidClockMonoThin);
mSecondsOnes.updatePadding();
}
if (mHoursTens != null && hourMinutesOnly) {
mHoursTens.updatePaddingForBoldDate();
mHoursTens.setVisibility(VISIBLE);
}
if (mMinutesTens != null && hourMinutesOnly) {
mMinutesTens.setTypeface(mAndroidClockMonoThin);
mMinutesTens.updatePadding();
}
if (mMinutesOnes != null && hourMinutesOnly) {
mMinutesOnes.setTypeface(mAndroidClockMonoThin);
mMinutesOnes.updatePadding();
}
if (mSecondsTens != null && hourMinutesOnly) {
mSecondsTens.setVisibility(GONE);
}
if (mSecondsOnes != null && hourMinutesOnly) {
mSecondsOnes.setVisibility(GONE);
}
if(mSecondsLabel != null && hourMinutesOnly){
mSecondsLabel.setVisibility(GONE);
}
}

/**
Expand All @@ -129,7 +163,20 @@ public void setTime(int hoursOnesDigit, int minutesTensDigit, int minutesOnesDig
setTime(false, hoursOnesDigit, minutesTensDigit, minutesOnesDigit, secondsTensDigit, secondsOnesDigit);
}

public void setTime(boolean isNegative, int hoursOnesDigit, int minutesTensDigit, int minutesOnesDigit, int secondsTensDigit,
public void setTime(boolean isNegative, int hoursTensDigit, int hoursOnesDigit, int minutesTensDigit,
int minutesOnesDigit) {
if (mHoursTens != null){
mHoursTens.setText(String.format("%d", hoursTensDigit));
}
setTime(isNegative, hoursOnesDigit, minutesTensDigit, minutesOnesDigit, 0, 0);
}

public void setHourMinutesOnly(boolean hourMinutesOnly) {
this.hourMinutesOnly = hourMinutesOnly;
onUpdateViewsVisibility();
}

public void setTime(boolean isNegative, int hoursOnesDigit, int minutesTensDigit, int minutesOnesDigit, int secondsTensDigit,
int secondsOnesDigit) {

mMinusLabel.setVisibility(isNegative ? View.VISIBLE : View.GONE);
Expand Down
8 changes: 8 additions & 0 deletions library/src/main/res/layout/hms_picker_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
android:layout_gravity="center"
android:baselineAligned="false"
android:gravity="top">
<com.codetroopers.betterpickers.widget.ZeroTopPaddingTextView
android:id="@+id/hours_tens"
android:singleLine="true"
android:ellipsize="none"
style="@style/medium_bold_hms"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<com.codetroopers.betterpickers.widget.ZeroTopPaddingTextView
android:id="@+id/hours_ones"
android:singleLine="true"
Expand Down
8 changes: 8 additions & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,14 @@
<category android:name="com.doomonafireball.betterpickers.sample.SAMPLE"/>
</intent-filter>
</activity>
<activity
android:name=".activity.hmspicker.SampleHmsHourMinutesOnlyUsage"
android:label="HMS/Hour Minutes Only Usage">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="com.doomonafireball.betterpickers.sample.SAMPLE" />
</intent-filter>
</activity>
<activity
android:name=".activity.hmspicker.SampleHmsThemeCustom"
android:label="HMS/Theme Custom">
Expand Down