Skip to content

Commit

Permalink
Merge : add pull request
Browse files Browse the repository at this point in the history
  • Loading branch information
J-Jamet committed Jun 12, 2017
2 parents 5f8b816 + 9b8b2d4 commit ced3af9
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 16 deletions.
6 changes: 5 additions & 1 deletion ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Add the JitPack repository in your build.gradle at the end of repositories:
And add the dependency
```
dependencies {
compile 'com.github.Kunzisoft:Android-SwitchDateTimePicker:1.6'
compile 'com.github.Kunzisoft:Android-SwitchDateTimePicker:1.7'
}
```

Expand Down Expand Up @@ -84,6 +84,10 @@ In your *styles.xml*, you can redefine each style separately, but you must keep
```
Styles elements : https://github.com/Kunzisoft/Android-SwitchDateTimePicker/blob/master/switchdatetime/src/main/res/values/styles.xml

#### AlertStyle
To customize the AlertDialog that is shown, use :
`void setAlertStyle(@StyleRes int styleId)`

### Sample
You can see
https://github.com/J-Jamet/Android-SwitchDateTimePicker/blob/master/sample/src/main/java/com/kunzisoft/switchdatetimesample/Sample.java
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
classpath 'com.android.tools.build:gradle:2.3.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ protected void onCreate(Bundle savedInstanceState) {
if(dateTimeFragment == null) {
dateTimeFragment = SwitchDateTimeDialogFragment.newInstance(
getString(R.string.label_datetime_dialog),
getString(R.string.positive_button_datetime_picker),
getString(R.string.negative_button_datetime_picker)
getString(android.R.string.ok),
getString(android.R.string.cancel)
);
}

Expand Down Expand Up @@ -100,4 +100,4 @@ public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putCharSequence(STATE_TEXTVIEW, textView.getText());
super.onSaveInstanceState(savedInstanceState);
}
}
}
1 change: 0 additions & 1 deletion sample/src/main/res/layout/activity_sample.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@
android:layout_below="@+id/button"
android:layout_centerHorizontal="true" />


</RelativeLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -58,9 +59,11 @@ public class SwitchDateTimeDialogFragment extends DialogFragment {
private int day = UNDEFINED_TIME_VALUE;
private int hourOfDay = UNDEFINED_TIME_VALUE;
private int minute = UNDEFINED_TIME_VALUE;
private boolean assignDefaultDateTimeCalendar;

private boolean is24HoursMode = false;
private int startAtPosition = 0;
private int alertStyleId;

private SimpleDateFormat dayAndMonthSimpleDate;
private SimpleDateFormat yearSimpleDate;
Expand Down Expand Up @@ -122,24 +125,26 @@ public void onSaveInstanceState(Bundle savedInstanceState) {
mNegativeButton = getArguments().getString(TAG_NEGATIVE_BUTTON);
}

if (savedInstanceState != null) {
if (!assignDefaultDateTimeCalendar && savedInstanceState != null) {
// Restore value from saved state
dateTimeCalendar.setTime(new Date(savedInstanceState.getLong(STATE_DATETIME)));
}

// Init values with current time if setDefault is not used
if(year == UNDEFINED_TIME_VALUE)
if(assignDefaultDateTimeCalendar || year == UNDEFINED_TIME_VALUE)
year = dateTimeCalendar.get(Calendar.YEAR);
if(month == UNDEFINED_TIME_VALUE)
if(assignDefaultDateTimeCalendar || month == UNDEFINED_TIME_VALUE)
month = dateTimeCalendar.get(Calendar.MONTH);
if(day == UNDEFINED_TIME_VALUE)
if(assignDefaultDateTimeCalendar || day == UNDEFINED_TIME_VALUE)
day = dateTimeCalendar.get(Calendar.DAY_OF_MONTH);
if(hourOfDay == UNDEFINED_TIME_VALUE)
if(assignDefaultDateTimeCalendar || hourOfDay == UNDEFINED_TIME_VALUE)
hourOfDay = dateTimeCalendar.get(Calendar.HOUR_OF_DAY);
if(minute == UNDEFINED_TIME_VALUE)
if(assignDefaultDateTimeCalendar || minute == UNDEFINED_TIME_VALUE)
minute = dateTimeCalendar.get(Calendar.MINUTE);
assignAllValuesToCalendar();

assignDefaultDateTimeCalendar = false;

// Throw exception if default select date isn't between minimumDateTime and maximumDateTime
if(dateTimeCalendar.before(minimumDateTime) || dateTimeCalendar.after(maximumDateTime))
throw new RuntimeException("Default date " + dateTimeCalendar.getTime() + " must be between "
Expand Down Expand Up @@ -301,10 +306,15 @@ public void onYearSelected(View view, int yearPicker) {
});

// Assign buttons
AlertDialog.Builder db = new AlertDialog.Builder(getContext());
AlertDialog.Builder db;
if (alertStyleId != 0) {
db = new AlertDialog.Builder(getContext(), alertStyleId);
} else {
db = new AlertDialog.Builder(getContext());
}
db.setView(dateTimeLayout);
if(mPositiveButton == null)
mPositiveButton = getString(R.string.positive_button_datetime_picker);
mPositiveButton = getString(android.R.string.ok);
db.setPositiveButton(mPositiveButton, new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Expand All @@ -315,7 +325,7 @@ public void onClick(DialogInterface dialog, int which) {
}
});
if(mNegativeButton == null)
mNegativeButton = getString(R.string.negative_button_datetime_picker);
mNegativeButton = getString(android.R.string.cancel);
db.setNegativeButton(mNegativeButton, new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Expand Down Expand Up @@ -496,6 +506,7 @@ public int getMinute() {
*/
public void setDefaultDateTime(Date date) {
this.dateTimeCalendar.setTime(date);
this.assignDefaultDateTimeCalendar = true;
}

/**
Expand Down Expand Up @@ -552,13 +563,21 @@ public void setSimpleDateMonthAndDayFormat(SimpleDateFormat simpleDateFormat) th
}

/**
* Define if time miust be in 24 hours mode or in 12 hours, must be applied before "show"
* Define if time must be in 24 hours mode or in 12 hours, must be applied before "show"
* @param is24HoursMode
*/
public void set24HoursMode(boolean is24HoursMode) {
this.is24HoursMode = is24HoursMode;
}

/**
* Define if the AlertDialog must be styled, must be applied before "show"
* @param styleId
*/
public void setAlertStyle(@StyleRes int styleId) {
this.alertStyleId = styleId;
}

/**
* Class exception if SimpleDateFormat contains something else that "d" or/and "M"
*/
Expand Down
1 change: 1 addition & 0 deletions switchdatetime/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<!-- Title -->
<string name="label_datetime_dialog">DateTime</string>

<!-- deprecated, use android.R.string.ok and android.R.string.cancel instead -->
<string name="positive_button_datetime_picker">OK</string>
<string name="negative_button_datetime_picker">Cancel</string>

Expand Down

0 comments on commit ced3af9

Please sign in to comment.