|
package `in`.coronainfotech.punyakam.daterange |
|
|
|
import `in`.coronainfotech.punyakam.R |
|
import android.os.Bundle |
|
import android.support.design.widget.TabLayout |
|
import android.support.v4.app.DialogFragment |
|
import android.support.v4.app.Fragment |
|
import android.support.v4.app.FragmentManager |
|
import android.support.v4.app.FragmentPagerAdapter |
|
import android.support.v4.view.ViewPager |
|
import android.view.LayoutInflater |
|
import android.view.View |
|
import android.view.ViewGroup |
|
import kotlinx.android.synthetic.main.dialog_picker_tabs.view.* |
|
|
|
class RangePickerDialog : DialogFragment(), FromChangeListener { |
|
|
|
var tabLayout: TabLayout? = null |
|
var viewPager: ViewPager? = null |
|
var adapter: PickerTabAdapter? = null |
|
var fromTimePickerFragment: TimePickerFragment? = null |
|
var toTimePickerFragment: TimePickerFragment? = null |
|
private var rangeSetListener: RangeSetListener? = null; |
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { |
|
return inflater.inflate(R.layout.dialog_picker_tabs, container, false) |
|
} |
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
|
super.onViewCreated(view, savedInstanceState) |
|
tabLayout = view.tabLayout |
|
viewPager = view.masterViewPager; |
|
adapter = PickerTabAdapter(childFragmentManager) |
|
fromTimePickerFragment = TimePickerFragment() |
|
toTimePickerFragment = TimePickerFragment() |
|
fromTimePickerFragment!!.setChangeListener(this) |
|
adapter!!.addFragment("From", fromTimePickerFragment!!) |
|
adapter!!.addFragment("TO", toTimePickerFragment!!) |
|
viewPager!!.adapter = adapter |
|
tabLayout!!.setupWithViewPager(viewPager) |
|
view.btnOkay.setOnClickListener { |
|
if (rangeSetListener != null) { |
|
val fromHour = fromTimePickerFragment!!.getHour() |
|
val fromMinute = fromTimePickerFragment!!.getMinute() |
|
val toHour = toTimePickerFragment!!.getHour() |
|
val toMinute = toTimePickerFragment!!.getMinute() |
|
rangeSetListener!!.onRangeSet( |
|
fromHour, |
|
fromMinute, |
|
toHour, |
|
toMinute |
|
) |
|
} |
|
if (dialog != null) |
|
dialog.dismiss() |
|
} |
|
} |
|
|
|
fun setRangeListener(listener: RangeSetListener) { |
|
this.rangeSetListener = listener |
|
} |
|
|
|
override fun onTimeSet(fromHour: Int, fromMinute: Int) { |
|
toTimePickerFragment!!.setMinimumTime(fromHour, fromMinute) |
|
} |
|
|
|
inner class PickerTabAdapter(private val fm: FragmentManager) : FragmentPagerAdapter(fm) { |
|
var mFragmentCollection: MutableList<Fragment> = ArrayList() |
|
var mTitleCollection: MutableList<String> = ArrayList() |
|
|
|
fun addFragment(title: String, fragment: Fragment) { |
|
mTitleCollection.add(title) |
|
mFragmentCollection.add(fragment) |
|
} |
|
|
|
override fun getPageTitle(position: Int): CharSequence? { |
|
return mTitleCollection[position] |
|
} |
|
|
|
override fun getItem(position: Int): Fragment { |
|
val frgm = mFragmentCollection[position] |
|
val bundle = Bundle() |
|
bundle.putInt("position", position); |
|
frgm.arguments = bundle; |
|
return frgm |
|
} |
|
|
|
override fun getCount(): Int { |
|
return mFragmentCollection.size |
|
} |
|
} |
|
} |