Create DateFragmentPicker, which shows a dialog with a calendar where the user can select a date. This picker is used in TransactiondFragment's filter dialog to filter transactions by date. The picker looks nice in both day and night mode.

This commit is contained in:
Severiano Jaramillo 2019-01-09 20:39:42 -06:00
parent 5470ca1523
commit f62286747c
3 changed files with 120 additions and 39 deletions

View file

@ -6,11 +6,13 @@ import android.content.res.Resources
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.View
import androidx.fragment.app.DialogFragment
import android.widget.*
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import cy.agorise.bitsybitshareswallet.R
import cy.agorise.bitsybitshareswallet.views.DatePickerFragment
import java.text.SimpleDateFormat
import java.util.*
import kotlin.ClassCastException
@ -39,7 +41,7 @@ class FilterOptionsDialog : DialogFragment() {
private var mCallback: OnFilterOptionsSelectedListener? = null
private var mDatePickerHandler: DatePickerHandler? = null
private lateinit var mDatePickerHandler: DatePickerHandler
private var dateFormat: SimpleDateFormat = SimpleDateFormat("d/MMM/yyyy",
Resources.getSystem().configuration.locale)
@ -175,21 +177,21 @@ class FilterOptionsDialog : DialogFragment() {
// Initialize Date range
cbDateRange = view.findViewById(R.id.cbDateRange)
// llDateRange = view.findViewById(R.id.llDateRange)
// cbDateRange.setOnCheckedChangeListener { _, isChecked ->
// llDateRange.visibility = if(isChecked) View.GONE else View.VISIBLE }
llDateRange = view.findViewById(R.id.llDateRange)
cbDateRange.setOnCheckedChangeListener { _, isChecked ->
llDateRange.visibility = if(isChecked) View.GONE else View.VISIBLE }
cbDateRange.isChecked = arguments!!.getBoolean(KEY_FILTER_DATE_RANGE_ALL, true)
//
// tvStartDate = view.findViewById(R.id.tvStartDate)
// tvEndDate = view.findViewById(R.id.tvEndDate)
//
// startDate = arguments!!.getLong(KEY_FILTER_START_DATE, 0)
// tvStartDate.setOnClickListener(mDateClickListener)
//
// endDate = arguments!!.getLong(KEY_FILTER_END_DATE, 0)
// tvEndDate.setOnClickListener(mDateClickListener)
//
// updateDateTextViews()
tvStartDate = view.findViewById(R.id.tvStartDate)
tvEndDate = view.findViewById(R.id.tvEndDate)
startDate = arguments!!.getLong(KEY_FILTER_START_DATE, 0)
tvStartDate.setOnClickListener(mDateClickListener)
endDate = arguments!!.getLong(KEY_FILTER_END_DATE, 0)
tvEndDate.setOnClickListener(mDateClickListener)
updateDateTextViews()
// Initialize Cryptocurrency
cbCryptocurrency = view.findViewById(R.id.cbCryptocurrency)
@ -257,29 +259,29 @@ class FilterOptionsDialog : DialogFragment() {
// sCryptocurrency.setSelection(index)
// }
// private val mDateClickListener = View.OnClickListener { v ->
// val calendar = Calendar.getInstance()
//
// // Variable used to select that date on the calendar
// var currentTime = calendar.timeInMillis
// var maxTime = currentTime
//
// var which = -1
// if (v.id == R.id.tvStartDate) {
// which = START_DATE_PICKER
// currentTime = startDate
// calendar.timeInMillis = endDate
// calendar.add(Calendar.MONTH, -1)
// maxTime = calendar.timeInMillis
// } else if (v.id == R.id.tvEndDate) {
// which = END_DATE_PICKER
// currentTime = endDate
// }
//
// val datePickerFragment = DatePickerFragment.newInstance(which, currentTime,
// maxTime, mDatePickerHandler)
// datePickerFragment.show(activity!!.supportFragmentManager, "date-picker")
// }
private val mDateClickListener = View.OnClickListener { v ->
val calendar = Calendar.getInstance()
// Variable used to select that date on the calendar
var currentTime = calendar.timeInMillis
var maxTime = currentTime
var which = -1
if (v.id == R.id.tvStartDate) {
which = START_DATE_PICKER
currentTime = startDate
calendar.timeInMillis = endDate
calendar.add(Calendar.MONTH, -1)
maxTime = calendar.timeInMillis
} else if (v.id == R.id.tvEndDate) {
which = END_DATE_PICKER
currentTime = endDate
}
val datePickerFragment = DatePickerFragment.newInstance(which, currentTime,
maxTime, mDatePickerHandler)
datePickerFragment.show(activity!!.supportFragmentManager, "date-picker")
}
private fun validateFields() {
val filterTransactionsDirection = when {

View file

@ -0,0 +1,79 @@
package cy.agorise.bitsybitshareswallet.views
import android.app.DatePickerDialog
import android.app.Dialog
import android.os.Bundle
import android.os.Message
import android.widget.DatePicker
import androidx.fragment.app.DialogFragment
import cy.agorise.bitsybitshareswallet.fragments.FilterOptionsDialog
import java.util.*
class DatePickerFragment : DialogFragment(), DatePickerDialog.OnDateSetListener {
companion object {
const val TAG = "DatePickerFragment"
const val KEY_WHICH = "key_which"
const val KEY_CURRENT = "key_current"
const val KEY_MAX = "key_max"
fun newInstance(
which: Int, currentTime: Long, maxTime: Long,
handler: FilterOptionsDialog.DatePickerHandler
): DatePickerFragment {
val f = DatePickerFragment()
val bundle = Bundle()
bundle.putInt(KEY_WHICH, which)
bundle.putLong(KEY_CURRENT, currentTime)
bundle.putLong(KEY_MAX, maxTime)
f.arguments = bundle
f.setHandler(handler)
return f
}
}
private var which: Int = 0
private var mHandler: FilterOptionsDialog.DatePickerHandler? = null
fun setHandler(handler: FilterOptionsDialog.DatePickerHandler) {
mHandler = handler
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
which = arguments!!.getInt(KEY_WHICH)
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val currentTime = arguments!!.getLong(KEY_CURRENT)
val maxTime = arguments!!.getLong(KEY_MAX)
// Use the current date as the default date in the picker
val calendar = Calendar.getInstance()
calendar.timeInMillis = currentTime
val year = calendar.get(Calendar.YEAR)
val month = calendar.get(Calendar.MONTH)
val day = calendar.get(Calendar.DAY_OF_MONTH)
// Create a new instance of DatePickerDialog and return it
val datePicker = DatePickerDialog(activity!!, this, year, month, day)
// Set maximum date allowed to today
datePicker.datePicker.maxDate = maxTime
return datePicker
}
override fun onDateSet(view: DatePicker, year: Int, month: Int, day: Int) {
val msg = Message.obtain()
msg.arg1 = which
val calendar = GregorianCalendar()
calendar.set(year, month, day)
val bundle = Bundle()
bundle.putLong(FilterOptionsDialog.KEY_TIMESTAMP, calendar.time.time)
msg.data = bundle
mHandler!!.sendMessage(msg)
}
}

View file

@ -17,7 +17,7 @@
<style name="Theme.Bitsy.AppBarOverlay" parent="ThemeOverlay.MaterialComponents.Dark.ActionBar"/>
<!-- Base application dark theme. -->
<style name="Theme.Bitsy.Dark" parent="Theme.MaterialComponents">
<style name="Theme.Bitsy.Dark" parent="Theme.MaterialComponents.Bridge">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>