diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/EventListFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/EventListFragment.kt index c79b2ed30..f6b71da11 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/EventListFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/EventListFragment.kt @@ -17,15 +17,21 @@ import com.simplemobiletools.calendar.pro.helpers.EVENT_OCCURRENCE_TS import com.simplemobiletools.calendar.pro.helpers.Formatter import com.simplemobiletools.calendar.pro.models.Event import com.simplemobiletools.calendar.pro.models.ListEvent +import com.simplemobiletools.calendar.pro.models.ListItem import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.MONTH_SECONDS import com.simplemobiletools.commons.interfaces.RefreshRecyclerViewListener +import com.simplemobiletools.commons.views.MyLinearLayoutManager import com.simplemobiletools.commons.views.MyRecyclerView import kotlinx.android.synthetic.main.fragment_event_list.view.* import org.joda.time.DateTime import java.util.* class EventListFragment : MyFragmentHolder(), RefreshRecyclerViewListener { + private val NOT_UPDATING = 0 + private val UPDATE_TOP = 1 + private val UPDATE_BOTTOM = 2 + private var FETCH_INTERVAL = 3 * MONTH_SECONDS private var MIN_EVENTS_TRESHOLD = 30 @@ -33,6 +39,7 @@ class EventListFragment : MyFragmentHolder(), RefreshRecyclerViewListener { private var minFetchedTS = 0L private var maxFetchedTS = 0L private var wereInitialEventsAdded = false + private var bottomItemAtRefresh: ListItem? = null private var use24HourFormat = false @@ -78,21 +85,21 @@ class EventListFragment : MyFragmentHolder(), RefreshRecyclerViewListener { context!!.eventsHelper.getEvents(minFetchedTS, maxFetchedTS) { if (it.size >= MIN_EVENTS_TRESHOLD) { - receivedEvents(it, false) + receivedEvents(it, NOT_UPDATING) } else { if (!wereInitialEventsAdded) { maxFetchedTS += FETCH_INTERVAL } context!!.eventsHelper.getEvents(minFetchedTS, maxFetchedTS) { mEvents = it - receivedEvents(mEvents, false, !wereInitialEventsAdded) + receivedEvents(mEvents, NOT_UPDATING, !wereInitialEventsAdded) } } wereInitialEventsAdded = true } } - private fun receivedEvents(events: ArrayList, scrollAfterUpdating: Boolean, forceRecreation: Boolean = false) { + private fun receivedEvents(events: ArrayList, updateStatus: Int, forceRecreation: Boolean = false) { if (context == null || activity == null) { return } @@ -121,12 +128,17 @@ class EventListFragment : MyFragmentHolder(), RefreshRecyclerViewListener { } override fun updateBottom() { - fetchNextPeriod(true) + fetchNextPeriod() } } } else { (currAdapter as EventListAdapter).updateListItems(listItems) - if (scrollAfterUpdating) { + if (updateStatus == UPDATE_TOP) { + val item = listItems.indexOfFirst { it == bottomItemAtRefresh } + if (item != -1) { + mView.calendar_events_list.scrollToPosition(item) + } + } else if (updateStatus == UPDATE_BOTTOM) { mView.calendar_events_list.smoothScrollBy(0, context!!.resources.getDimension(R.dimen.endless_scroll_move_height).toInt()) } } @@ -151,20 +163,23 @@ class EventListFragment : MyFragmentHolder(), RefreshRecyclerViewListener { } private fun fetchPreviousPeriod() { + val lastPosition = (mView.calendar_events_list.layoutManager as MyLinearLayoutManager).findLastVisibleItemPosition() + bottomItemAtRefresh = (mView.calendar_events_list.adapter as EventListAdapter).listItems[lastPosition] + val oldMinFetchedTS = minFetchedTS - 1 minFetchedTS -= FETCH_INTERVAL context!!.eventsHelper.getEvents(minFetchedTS, oldMinFetchedTS) { mEvents.addAll(0, it) - receivedEvents(mEvents, false) + receivedEvents(mEvents, UPDATE_TOP) } } - private fun fetchNextPeriod(scrollAfterUpdating: Boolean) { + private fun fetchNextPeriod() { val oldMaxFetchedTS = maxFetchedTS + 1 maxFetchedTS += FETCH_INTERVAL context!!.eventsHelper.getEvents(oldMaxFetchedTS, maxFetchedTS) { mEvents.addAll(it) - receivedEvents(mEvents, scrollAfterUpdating) + receivedEvents(mEvents, UPDATE_BOTTOM) } }