Skip to content

Commit

Permalink
The datepicker shows the last selected date
Browse files Browse the repository at this point in the history
  • Loading branch information
m-i-n-a-r committed Oct 3, 2020
1 parent 1fce4e5 commit 8895561
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
10 changes: 7 additions & 3 deletions app/src/main/java/com/minar/birday/activities/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,12 @@ class MainActivity : AppCompatActivity() {
val eventDate = customView.findViewById<TextView>(R.id.dateEvent)
val countYear = customView.findViewById<CheckBox>(R.id.countYearCheckbox)
val endDate = Calendar.getInstance()
val formatter: DateTimeFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
var dateDialog: MaterialDialog? = null

// To automatically show the last selected date, parse it to another Calendar object
val lastDate = Calendar.getInstance()

// Update the boolean value on each click
countYear.setOnCheckedChangeListener { _, isChecked ->
countYearValue = isChecked
Expand All @@ -170,14 +174,14 @@ class MainActivity : AppCompatActivity() {
dateDialog = MaterialDialog(this).show {
cancelable(false)
cancelOnTouchOutside(false)
datePicker(maxDate = endDate) { _, date ->
datePicker(maxDate = endDate, currentDate = lastDate) { _, date ->
val year = date.get(Calendar.YEAR)
val month = date.get(Calendar.MONTH) + 1
val day = date.get(Calendar.DAY_OF_MONTH)
eventDateValue = LocalDate.of(year, month, day)
val formatter: DateTimeFormatter =
DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
eventDate.text = eventDateValue.format(formatter)
// If ok is pressed, the last selected date is saved if the dialog is reopened
lastDate.set(year, month - 1, day)
}
}
Handler(Looper.getMainLooper()).postDelayed({ dateDialog = null }, 750)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import androidx.recyclerview.widget.RecyclerView
import com.minar.birday.fragments.HomeFragment
import com.minar.birday.R
import com.minar.birday.model.EventResult
import com.minar.birday.utilities.OnItemClickListener
import com.minar.birday.listeners.OnItemClickListener
import kotlinx.android.synthetic.main.event_row.view.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down
12 changes: 8 additions & 4 deletions app/src/main/java/com/minar/birday/fragments/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import com.minar.birday.R
import com.minar.birday.activities.MainActivity
import com.minar.birday.activities.SplashActivity
import com.minar.birday.adapters.EventAdapter
import com.minar.birday.listeners.OnItemClickListener
import com.minar.birday.model.Event
import com.minar.birday.model.EventResult
import com.minar.birday.utilities.*
Expand Down Expand Up @@ -447,17 +448,18 @@ class HomeFragment : Fragment() {
val endDate = Calendar.getInstance()
var dateDialog: MaterialDialog? = null

// To automatically show the last selected date, parse it to another Calendar object
val lastDate = Calendar.getInstance()
lastDate.set(eventDateValue.year, eventDateValue.monthValue - 1, eventDateValue.dayOfMonth)

// Update the boolean value on each click
countYear.setOnCheckedChangeListener { _, isChecked ->
countYearValue = isChecked
}

eventDate.setOnClickListener {
// Prevent double dialogs on fast click
if(dateDialog == null) {
// To automatically show the last selected date, parse it to another Calendar object
val lastDate = Calendar.getInstance()
lastDate.set(eventDateValue.year, eventDateValue.monthValue - 1, eventDateValue.dayOfMonth)
if (dateDialog == null) {
dateDialog = MaterialDialog(act).show {
cancelable(false)
cancelOnTouchOutside(false)
Expand All @@ -467,6 +469,8 @@ class HomeFragment : Fragment() {
val day = date.get(Calendar.DAY_OF_MONTH)
eventDateValue = LocalDate.of(year, month, day)
eventDate.text = eventDateValue.format(formatter)
// If ok is pressed, the last selected date is saved if the dialog is reopened
lastDate.set(year, month - 1, day)
}
}
Handler(Looper.getMainLooper()).postDelayed({ dateDialog = null }, 750)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.minar.birday.utilities
package com.minar.birday.listeners

import android.view.View

Expand Down

0 comments on commit 8895561

Please sign in to comment.