Improved FilterOptionsDialog by changing the way it communicates with the DatePickerFragment, using an interface instead of a Handler.
This commit is contained in:
parent
d1bce06ec2
commit
484e4bde68
2 changed files with 50 additions and 57 deletions
|
@ -4,8 +4,6 @@ package cy.agorise.bitsybitshareswallet.fragments
|
||||||
import android.app.Dialog
|
import android.app.Dialog
|
||||||
import android.content.res.Resources
|
import android.content.res.Resources
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.Handler
|
|
||||||
import android.os.Message
|
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import androidx.fragment.app.DialogFragment
|
import androidx.fragment.app.DialogFragment
|
||||||
import android.widget.*
|
import android.widget.*
|
||||||
|
@ -32,15 +30,13 @@ import kotlin.collections.ArrayList
|
||||||
* Creates a Dialog that communicates with {@link TransactionsActivity} to give it parameters about
|
* Creates a Dialog that communicates with {@link TransactionsActivity} to give it parameters about
|
||||||
* how to filter the list of Transactions
|
* how to filter the list of Transactions
|
||||||
*/
|
*/
|
||||||
class FilterOptionsDialog : DialogFragment() {
|
class FilterOptionsDialog : DialogFragment(), DatePickerFragment.OnDateSetListener {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val TAG = "FilterOptionsDialog"
|
private const val TAG = "FilterOptionsDialog"
|
||||||
|
|
||||||
const val KEY_FILTER_OPTIONS = "key_filter_options"
|
const val KEY_FILTER_OPTIONS = "key_filter_options"
|
||||||
|
|
||||||
const val KEY_TIMESTAMP = "key_timestamp"
|
|
||||||
|
|
||||||
const val START_DATE_PICKER = 0
|
const val START_DATE_PICKER = 0
|
||||||
const val END_DATE_PICKER = 1
|
const val END_DATE_PICKER = 1
|
||||||
}
|
}
|
||||||
|
@ -66,8 +62,6 @@ class FilterOptionsDialog : DialogFragment() {
|
||||||
|
|
||||||
private var mCallback: OnFilterOptionsSelectedListener? = null
|
private var mCallback: OnFilterOptionsSelectedListener? = null
|
||||||
|
|
||||||
private lateinit var mDatePickerHandler: DatePickerHandler
|
|
||||||
|
|
||||||
private var dateFormat: SimpleDateFormat = SimpleDateFormat("d/MMM/yyyy",
|
private var dateFormat: SimpleDateFormat = SimpleDateFormat("d/MMM/yyyy",
|
||||||
ConfigurationCompat.getLocales(Resources.getSystem().configuration)[0])
|
ConfigurationCompat.getLocales(Resources.getSystem().configuration)[0])
|
||||||
|
|
||||||
|
@ -79,17 +73,8 @@ class FilterOptionsDialog : DialogFragment() {
|
||||||
|
|
||||||
private lateinit var mCurrency: Currency
|
private lateinit var mCurrency: Currency
|
||||||
|
|
||||||
/**
|
override fun onDateSet(which: Int, timestamp: Long) {
|
||||||
* DatePicker message handler.
|
when(which) {
|
||||||
*/
|
|
||||||
inner class DatePickerHandler : Handler() {
|
|
||||||
|
|
||||||
override fun handleMessage(msg: Message) {
|
|
||||||
super.handleMessage(msg)
|
|
||||||
val bundle = msg.data
|
|
||||||
val timestamp = bundle.get(KEY_TIMESTAMP) as Long
|
|
||||||
//Log.d(TAG, "timestamp: $timestamp")
|
|
||||||
when (msg.arg1) {
|
|
||||||
START_DATE_PICKER -> {
|
START_DATE_PICKER -> {
|
||||||
mFilterOptions.startDate = timestamp
|
mFilterOptions.startDate = timestamp
|
||||||
|
|
||||||
|
@ -112,7 +97,6 @@ class FilterOptionsDialog : DialogFragment() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private fun updateDateTextViews() {
|
private fun updateDateTextViews() {
|
||||||
var date = Date(mFilterOptions.startDate)
|
var date = Date(mFilterOptions.startDate)
|
||||||
|
@ -135,9 +119,6 @@ class FilterOptionsDialog : DialogFragment() {
|
||||||
|
|
||||||
mFilterOptions = arguments?.getParcelable(KEY_FILTER_OPTIONS)!!
|
mFilterOptions = arguments?.getParcelable(KEY_FILTER_OPTIONS)!!
|
||||||
|
|
||||||
// Initialize handler for communication with the DatePicker
|
|
||||||
mDatePickerHandler = DatePickerHandler()
|
|
||||||
|
|
||||||
val builder = AlertDialog.Builder(context!!)
|
val builder = AlertDialog.Builder(context!!)
|
||||||
.setTitle(getString(R.string.title_filter_options))
|
.setTitle(getString(R.string.title_filter_options))
|
||||||
.setPositiveButton(getString(R.string.button__filter)) { _, _ -> validateFields() }
|
.setPositiveButton(getString(R.string.button__filter)) { _, _ -> validateFields() }
|
||||||
|
@ -266,9 +247,8 @@ class FilterOptionsDialog : DialogFragment() {
|
||||||
currentTime = mFilterOptions.endDate
|
currentTime = mFilterOptions.endDate
|
||||||
}
|
}
|
||||||
|
|
||||||
val datePickerFragment = DatePickerFragment.newInstance(which, currentTime,
|
val datePickerFragment = DatePickerFragment.newInstance(which, currentTime, maxTime)
|
||||||
maxTime, mDatePickerHandler)
|
datePickerFragment.show(childFragmentManager, "date-picker")
|
||||||
datePickerFragment.show(activity!!.supportFragmentManager, "date-picker")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun validateFields() {
|
private fun validateFields() {
|
||||||
|
|
|
@ -3,13 +3,16 @@ package cy.agorise.bitsybitshareswallet.views
|
||||||
import android.app.DatePickerDialog
|
import android.app.DatePickerDialog
|
||||||
import android.app.Dialog
|
import android.app.Dialog
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.Message
|
|
||||||
import android.widget.DatePicker
|
import android.widget.DatePicker
|
||||||
import androidx.fragment.app.DialogFragment
|
import androidx.fragment.app.DialogFragment
|
||||||
|
import androidx.fragment.app.Fragment
|
||||||
import com.google.android.material.picker.MaterialDatePickerDialog
|
import com.google.android.material.picker.MaterialDatePickerDialog
|
||||||
import cy.agorise.bitsybitshareswallet.fragments.FilterOptionsDialog
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lets the user select a Date and communicates the selection back to the parent fragment
|
||||||
|
* using the OnDateSetListener interface, which has to be implemented by the parent.
|
||||||
|
*/
|
||||||
class DatePickerFragment : DialogFragment(), DatePickerDialog.OnDateSetListener {
|
class DatePickerFragment : DialogFragment(), DatePickerDialog.OnDateSetListener {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
@ -19,27 +22,23 @@ class DatePickerFragment : DialogFragment(), DatePickerDialog.OnDateSetListener
|
||||||
const val KEY_CURRENT = "key_current"
|
const val KEY_CURRENT = "key_current"
|
||||||
const val KEY_MAX = "key_max"
|
const val KEY_MAX = "key_max"
|
||||||
|
|
||||||
fun newInstance(
|
fun newInstance(which: Int, currentTime: Long, maxTime: Long): DatePickerFragment {
|
||||||
which: Int, currentTime: Long, maxTime: Long,
|
|
||||||
handler: FilterOptionsDialog.DatePickerHandler
|
|
||||||
): DatePickerFragment {
|
|
||||||
val f = DatePickerFragment()
|
val f = DatePickerFragment()
|
||||||
val bundle = Bundle()
|
val bundle = Bundle()
|
||||||
bundle.putInt(KEY_WHICH, which)
|
bundle.putInt(KEY_WHICH, which)
|
||||||
bundle.putLong(KEY_CURRENT, currentTime)
|
bundle.putLong(KEY_CURRENT, currentTime)
|
||||||
bundle.putLong(KEY_MAX, maxTime)
|
bundle.putLong(KEY_MAX, maxTime)
|
||||||
f.arguments = bundle
|
f.arguments = bundle
|
||||||
f.setHandler(handler)
|
|
||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private var which: Int = 0
|
/**
|
||||||
private var mHandler: FilterOptionsDialog.DatePickerHandler? = null
|
* Callback used to communicate the date selection back to the parent
|
||||||
|
*/
|
||||||
|
private var mCallback: OnDateSetListener? = null
|
||||||
|
|
||||||
fun setHandler(handler: FilterOptionsDialog.DatePickerHandler) {
|
private var which: Int = 0
|
||||||
mHandler = handler
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
@ -47,6 +46,8 @@ class DatePickerFragment : DialogFragment(), DatePickerDialog.OnDateSetListener
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||||
|
onAttachToParentFragment(parentFragment)
|
||||||
|
|
||||||
val currentTime = arguments!!.getLong(KEY_CURRENT)
|
val currentTime = arguments!!.getLong(KEY_CURRENT)
|
||||||
val maxTime = arguments!!.getLong(KEY_MAX)
|
val maxTime = arguments!!.getLong(KEY_MAX)
|
||||||
|
|
||||||
|
@ -68,13 +69,25 @@ class DatePickerFragment : DialogFragment(), DatePickerDialog.OnDateSetListener
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDateSet(view: DatePicker, year: Int, month: Int, day: Int) {
|
override fun onDateSet(view: DatePicker, year: Int, month: Int, day: Int) {
|
||||||
val msg = Message.obtain()
|
|
||||||
msg.arg1 = which
|
|
||||||
val calendar = GregorianCalendar()
|
val calendar = GregorianCalendar()
|
||||||
calendar.set(year, month, day)
|
calendar.set(year, month, day)
|
||||||
val bundle = Bundle()
|
mCallback?.onDateSet(which, calendar.time.time)
|
||||||
bundle.putLong(FilterOptionsDialog.KEY_TIMESTAMP, calendar.time.time)
|
}
|
||||||
msg.data = bundle
|
|
||||||
mHandler!!.sendMessage(msg)
|
/**
|
||||||
|
* Attaches the current [DialogFragment] to its [Fragment] parent, to initialize the
|
||||||
|
* [OnDateSetListener] interface
|
||||||
|
*/
|
||||||
|
private fun onAttachToParentFragment(fragment: Fragment?) {
|
||||||
|
try {
|
||||||
|
mCallback = fragment as OnDateSetListener
|
||||||
|
} catch (e: ClassCastException) {
|
||||||
|
throw ClassCastException("$fragment must implement OnDateSetListener")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Container Activity must implement this interface
|
||||||
|
interface OnDateSetListener {
|
||||||
|
fun onDateSet(which: Int, timestamp: Long)
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue