just refresh the weekly fragments if events change, dont recreate them

This commit is contained in:
tibbi 2018-02-10 12:07:57 +01:00
parent e77ceeff2a
commit bffb0ba0a1
3 changed files with 20 additions and 5 deletions

View file

@ -31,4 +31,10 @@ class MyWeekPagerAdapter(fm: FragmentManager, val mWeekTimestamps: List<Int>, va
mFragments[pos - 1]?.updateScrollY(y)
mFragments[pos + 1]?.updateScrollY(y)
}
fun updateCalendars(pos: Int) {
for (i in -1..1) {
mFragments[pos + i]?.updateCalendar()
}
}
}

View file

@ -109,7 +109,7 @@ class WeekFragment : Fragment(), WeeklyCalendar {
override fun onResume() {
super.onResume()
setupDayLabels()
mCalendar.updateWeeklyCalendar(mWeekTimestamp)
updateCalendar()
mView.week_events_scrollview.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
@ -140,6 +140,10 @@ class WeekFragment : Fragment(), WeeklyCalendar {
}
}
fun updateCalendar() {
mCalendar.updateWeeklyCalendar(mWeekTimestamp)
}
private fun setupDayLabels() {
var curDay = Formatter.getDateTimeFromTS(mWeekTimestamp)
val textColor = context!!.config.textColor
@ -233,18 +237,21 @@ class WeekFragment : Fragment(), WeeklyCalendar {
if (newHash == lastHash) {
return
}
lastHash = newHash
this.events = events
updateEvents()
}
private fun updateEvents() {
if (mWasDestroyed)
if (mWasDestroyed) {
return
}
activity!!.runOnUiThread {
if (context != null && isAdded)
if (context != null && isAdded) {
addEvents()
}
}
}
@ -443,7 +450,8 @@ class WeekFragment : Fragment(), WeeklyCalendar {
private fun getColumnWithId(id: Int) = mView.findViewById<ViewGroup>(mRes.getIdentifier("week_column_$id", "id", context!!.packageName))
fun updateScrollY(y: Int) {
if (wasFragmentInit)
if (wasFragmentInit) {
mView.week_events_scrollview.scrollY = y
}
}
}

View file

@ -124,7 +124,8 @@ class WeekFragmentsHolder : MyFragmentHolder(), WeekFragmentListener {
}
override fun refreshEvents() {
setupFragment()
val viewPager = weekHolder?.week_view_view_pager
(viewPager?.adapter as? MyWeekPagerAdapter)?.updateCalendars(viewPager.currentItem)
}
override fun shouldGoToTodayBeVisible() = currentWeekTS != thisWeekTS