-
-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from yogeshpaliyal/feature/reminder
Feature/reminder
- Loading branch information
Showing
21 changed files
with
510 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
app/src/main/java/dev/spikeysanju/einsen/components/DatePicker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* | ||
* * Copyright 2021 Spikey Sanju | ||
* * | ||
* * Licensed under the Apache License, Version 2.0 (the "License"); | ||
* * you may not use this file except in compliance with the License. | ||
* * You may obtain a copy of the License at | ||
* * | ||
* * https://www.apache.org/licenses/LICENSE-2.0 | ||
* * | ||
* * Unless required by applicable law or agreed to in writing, software | ||
* * distributed under the License is distributed on an "AS IS" BASIS, | ||
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* * See the License for the specific language governing permissions and | ||
* * limitations under the License. | ||
* | ||
* | ||
*/ | ||
|
||
package dev.spikeysanju.einsen.components | ||
|
||
import android.content.Context | ||
import androidx.fragment.app.FragmentActivity | ||
import com.google.android.material.datepicker.CalendarConstraints | ||
import com.google.android.material.datepicker.MaterialDatePicker | ||
import com.google.android.material.timepicker.MaterialTimePicker | ||
import dev.spikeysanju.einsen.R | ||
import dev.spikeysanju.einsen.utils.DateValidator | ||
import java.util.* | ||
|
||
fun Context?.showDatePicker( | ||
defaultCalendar: Calendar, | ||
onDismiss: (() -> Unit)? = null, | ||
minDate: Long = Calendar.getInstance().also { | ||
it.set(Calendar.MINUTE, 0) | ||
it.set(Calendar.HOUR_OF_DAY, 0) | ||
it.set(Calendar.SECOND, 0) | ||
it.set(Calendar.MILLISECOND, 0) | ||
}.timeInMillis, | ||
onDateSelect: (Calendar) -> Unit | ||
) { | ||
(this as? FragmentActivity)?.supportFragmentManager?.let { manager -> | ||
|
||
val calendarConstraints = CalendarConstraints.Builder().setStart(minDate) | ||
.setValidator(DateValidator(minDate)) | ||
.build() | ||
|
||
val builder = MaterialDatePicker.Builder.datePicker() | ||
.setTheme(R.style.ThemeOverlay_App_DatePicker) | ||
.setCalendarConstraints(calendarConstraints) | ||
.setSelection(defaultCalendar.timeInMillis) | ||
.build() | ||
|
||
builder.addOnPositiveButtonClickListener { selectedDate -> | ||
val newCal = Calendar.getInstance() | ||
newCal.timeInMillis = selectedDate | ||
onDateSelect.invoke(newCal) | ||
} | ||
builder.addOnDismissListener { | ||
onDismiss?.invoke() | ||
} | ||
builder.show(manager, "DatePicker") | ||
} | ||
} | ||
|
||
fun Context?.showTimePicker( | ||
defaultCalendar: Calendar, | ||
onDismiss: (() -> Unit)? = null, | ||
onTimeSelected: (Calendar) -> Unit | ||
) { | ||
(this as? FragmentActivity)?.supportFragmentManager?.let { manager -> | ||
val builder = MaterialTimePicker.Builder() | ||
.setHour(defaultCalendar.get(Calendar.HOUR_OF_DAY)) | ||
.setMinute(defaultCalendar.get(Calendar.MINUTE)) | ||
.build() | ||
builder.addOnPositiveButtonClickListener { | ||
defaultCalendar.set(Calendar.HOUR_OF_DAY, builder.hour) | ||
defaultCalendar.set(Calendar.MINUTE, builder.minute) | ||
onTimeSelected.invoke(defaultCalendar) | ||
} | ||
builder.addOnDismissListener { | ||
onDismiss?.invoke() | ||
} | ||
builder.show(manager, "TimePicker") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
app/src/main/java/dev/spikeysanju/einsen/utils/DateTimeHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* | ||
* * Copyright 2021 Spikey Sanju | ||
* * | ||
* * Licensed under the Apache License, Version 2.0 (the "License"); | ||
* * you may not use this file except in compliance with the License. | ||
* * You may obtain a copy of the License at | ||
* * | ||
* * https://www.apache.org/licenses/LICENSE-2.0 | ||
* * | ||
* * Unless required by applicable law or agreed to in writing, software | ||
* * distributed under the License is distributed on an "AS IS" BASIS, | ||
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* * See the License for the specific language governing permissions and | ||
* * limitations under the License. | ||
* | ||
* | ||
*/ | ||
|
||
package dev.spikeysanju.einsen.utils | ||
|
||
import java.text.ParseException | ||
import java.text.SimpleDateFormat | ||
import java.util.* | ||
|
||
const val DUE_DATE_FORMAT = "dd MMM yyyy hh:mm aa" | ||
|
||
fun formatCalendar(calendar: Calendar, dateTimeFormat: String? = DUE_DATE_FORMAT): String { | ||
val simpleDateFormat = SimpleDateFormat(dateTimeFormat, Locale.getDefault()) | ||
return simpleDateFormat.format(calendar.time) | ||
} | ||
|
||
fun getCalendar(dateTime: String): Calendar { | ||
val simpleDateFormat = SimpleDateFormat(DUE_DATE_FORMAT, Locale.getDefault()) | ||
val cal = Calendar.getInstance() | ||
try { | ||
simpleDateFormat.parse(dateTime)?.let { | ||
cal.time = it | ||
} | ||
} catch (e: ParseException) { | ||
e.printStackTrace() | ||
} | ||
return cal | ||
} |
67 changes: 67 additions & 0 deletions
67
app/src/main/java/dev/spikeysanju/einsen/utils/DateValidator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* | ||
* * Copyright 2021 Spikey Sanju | ||
* * | ||
* * Licensed under the Apache License, Version 2.0 (the "License"); | ||
* * you may not use this file except in compliance with the License. | ||
* * You may obtain a copy of the License at | ||
* * | ||
* * https://www.apache.org/licenses/LICENSE-2.0 | ||
* * | ||
* * Unless required by applicable law or agreed to in writing, software | ||
* * distributed under the License is distributed on an "AS IS" BASIS, | ||
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* * See the License for the specific language governing permissions and | ||
* * limitations under the License. | ||
* | ||
* | ||
*/ | ||
|
||
package dev.spikeysanju.einsen.utils | ||
|
||
import android.os.Parcel | ||
import android.os.Parcelable | ||
import com.google.android.material.datepicker.CalendarConstraints | ||
|
||
class DateValidator() : CalendarConstraints.DateValidator { | ||
var startDate: Long = -1 | ||
var endDate: Long = -1 | ||
|
||
constructor(startDate: Long = -1, endDate: Long = -1) : this() { | ||
this.startDate = startDate | ||
this.endDate = endDate | ||
} | ||
|
||
constructor(parcel: Parcel) : this() { | ||
startDate = parcel.readLong() | ||
endDate = parcel.readLong() | ||
} | ||
|
||
override fun writeToParcel(parcel: Parcel, flags: Int) { | ||
parcel.writeLong(startDate) | ||
parcel.writeLong(endDate) | ||
} | ||
|
||
override fun isValid(date: Long): Boolean { | ||
return if (startDate != -1L && endDate != -1L && date in startDate..endDate) { | ||
// valid date between start and end date | ||
true | ||
} else if (startDate == -1L && date < endDate) { | ||
true | ||
} else endDate == -1L && date >= startDate | ||
} | ||
|
||
override fun describeContents(): Int { | ||
return 0 | ||
} | ||
|
||
companion object CREATOR : Parcelable.Creator<DateValidator> { | ||
override fun createFromParcel(parcel: Parcel): DateValidator { | ||
return DateValidator(parcel) | ||
} | ||
|
||
override fun newArray(size: Int): Array<DateValidator?> { | ||
return arrayOfNulls(size) | ||
} | ||
} | ||
} |
Oops, something went wrong.