diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/FilterOptionsDialog.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/FilterOptionsDialog.kt index 6770944..1f106c8 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/FilterOptionsDialog.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/FilterOptionsDialog.kt @@ -4,8 +4,6 @@ package cy.agorise.bitsybitshareswallet.fragments import android.app.Dialog 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.* @@ -32,15 +30,13 @@ import kotlin.collections.ArrayList * Creates a Dialog that communicates with {@link TransactionsActivity} to give it parameters about * how to filter the list of Transactions */ -class FilterOptionsDialog : DialogFragment() { +class FilterOptionsDialog : DialogFragment(), DatePickerFragment.OnDateSetListener { companion object { private const val TAG = "FilterOptionsDialog" const val KEY_FILTER_OPTIONS = "key_filter_options" - const val KEY_TIMESTAMP = "key_timestamp" - const val START_DATE_PICKER = 0 const val END_DATE_PICKER = 1 } @@ -66,8 +62,6 @@ class FilterOptionsDialog : DialogFragment() { private var mCallback: OnFilterOptionsSelectedListener? = null - private lateinit var mDatePickerHandler: DatePickerHandler - private var dateFormat: SimpleDateFormat = SimpleDateFormat("d/MMM/yyyy", ConfigurationCompat.getLocales(Resources.getSystem().configuration)[0]) @@ -79,37 +73,27 @@ class FilterOptionsDialog : DialogFragment() { private lateinit var mCurrency: Currency - /** - * DatePicker message handler. - */ - inner class DatePickerHandler : Handler() { + override fun onDateSet(which: Int, timestamp: Long) { + when(which) { + START_DATE_PICKER -> { + mFilterOptions.startDate = timestamp - 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 -> { - mFilterOptions.startDate = timestamp + updateDateTextViews() + } + END_DATE_PICKER -> { + mFilterOptions.endDate = timestamp - updateDateTextViews() - } - END_DATE_PICKER -> { - mFilterOptions.endDate = timestamp + // Make sure there is at least one moth difference between start and end time + val calendar = Calendar.getInstance() + calendar.timeInMillis = mFilterOptions.endDate + calendar.add(Calendar.MONTH, -1) - // Make sure there is at least one moth difference between start and end time - val calendar = Calendar.getInstance() - calendar.timeInMillis = mFilterOptions.endDate - calendar.add(Calendar.MONTH, -1) + val tmpTime = calendar.timeInMillis - val tmpTime = calendar.timeInMillis + if (tmpTime < mFilterOptions.startDate) + mFilterOptions.startDate = tmpTime - if (tmpTime < mFilterOptions.startDate) - mFilterOptions.startDate = tmpTime - - updateDateTextViews() - } + updateDateTextViews() } } } @@ -135,9 +119,6 @@ class FilterOptionsDialog : DialogFragment() { mFilterOptions = arguments?.getParcelable(KEY_FILTER_OPTIONS)!! - // Initialize handler for communication with the DatePicker - mDatePickerHandler = DatePickerHandler() - val builder = AlertDialog.Builder(context!!) .setTitle(getString(R.string.title_filter_options)) .setPositiveButton(getString(R.string.button__filter)) { _, _ -> validateFields() } @@ -266,9 +247,8 @@ class FilterOptionsDialog : DialogFragment() { currentTime = mFilterOptions.endDate } - val datePickerFragment = DatePickerFragment.newInstance(which, currentTime, - maxTime, mDatePickerHandler) - datePickerFragment.show(activity!!.supportFragmentManager, "date-picker") + val datePickerFragment = DatePickerFragment.newInstance(which, currentTime, maxTime) + datePickerFragment.show(childFragmentManager, "date-picker") } private fun validateFields() { diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/views/DatePickerFragment.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/views/DatePickerFragment.kt index 3fbb266..92aaa75 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/views/DatePickerFragment.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/views/DatePickerFragment.kt @@ -3,13 +3,16 @@ 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 androidx.fragment.app.Fragment import com.google.android.material.picker.MaterialDatePickerDialog -import cy.agorise.bitsybitshareswallet.fragments.FilterOptionsDialog 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 { companion object { @@ -19,27 +22,23 @@ class DatePickerFragment : DialogFragment(), DatePickerDialog.OnDateSetListener const val KEY_CURRENT = "key_current" const val KEY_MAX = "key_max" - fun newInstance( - which: Int, currentTime: Long, maxTime: Long, - handler: FilterOptionsDialog.DatePickerHandler - ): DatePickerFragment { + fun newInstance(which: Int, currentTime: Long, maxTime: Long): 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 + /** + * Callback used to communicate the date selection back to the parent + */ + private var mCallback: OnDateSetListener? = null - fun setHandler(handler: FilterOptionsDialog.DatePickerHandler) { - mHandler = handler - } + private var which: Int = 0 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -47,6 +46,8 @@ class DatePickerFragment : DialogFragment(), DatePickerDialog.OnDateSetListener } override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + onAttachToParentFragment(parentFragment) + val currentTime = arguments!!.getLong(KEY_CURRENT) 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) { - 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) + mCallback?.onDateSet(which, calendar.time.time) + } + + /** + * 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) } } \ No newline at end of file