mirror of
https://github.com/SimpleMobileTools/Simple-Calendar.git
synced 2025-06-05 21:59:17 +02:00
fetch new items at EventList when scrolled to the bottom
This commit is contained in:
@@ -46,7 +46,7 @@ ext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'com.simplemobiletools:commons:3.21.6'
|
implementation 'com.simplemobiletools:commons:3.21.10'
|
||||||
implementation 'joda-time:joda-time:2.9.9'
|
implementation 'joda-time:joda-time:2.9.9'
|
||||||
implementation 'com.facebook.stetho:stetho:1.5.0'
|
implementation 'com.facebook.stetho:stetho:1.5.0'
|
||||||
implementation 'com.android.support:multidex:1.0.3'
|
implementation 'com.android.support:multidex:1.0.3'
|
||||||
|
@@ -25,7 +25,7 @@ import com.simplemobiletools.commons.views.MyRecyclerView
|
|||||||
import kotlinx.android.synthetic.main.event_list_item.view.*
|
import kotlinx.android.synthetic.main.event_list_item.view.*
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class EventListAdapter(activity: SimpleActivity, val listItems: ArrayList<ListItem>, val allowLongClick: Boolean, val listener: RefreshRecyclerViewListener?,
|
class EventListAdapter(activity: SimpleActivity, var listItems: ArrayList<ListItem>, val allowLongClick: Boolean, val listener: RefreshRecyclerViewListener?,
|
||||||
recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, null, itemClick) {
|
recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, null, itemClick) {
|
||||||
|
|
||||||
private val ITEM_EVENT = 0
|
private val ITEM_EVENT = 0
|
||||||
@@ -37,6 +37,7 @@ class EventListAdapter(activity: SimpleActivity, val listItems: ArrayList<ListIt
|
|||||||
private val dimPastEvents = activity.config.dimPastEvents
|
private val dimPastEvents = activity.config.dimPastEvents
|
||||||
private val now = getNowSeconds()
|
private val now = getNowSeconds()
|
||||||
private var use24HourFormat = activity.config.use24HourFormat
|
private var use24HourFormat = activity.config.use24HourFormat
|
||||||
|
private var currentItemsHash = listItems.hashCode()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
var firstNonPastSectionIndex = -1
|
var firstNonPastSectionIndex = -1
|
||||||
@@ -100,6 +101,16 @@ class EventListAdapter(activity: SimpleActivity, val listItems: ArrayList<ListIt
|
|||||||
notifyDataSetChanged()
|
notifyDataSetChanged()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun updateListItems(newListItems: ArrayList<ListItem>) {
|
||||||
|
if (newListItems.hashCode() != currentItemsHash) {
|
||||||
|
currentItemsHash = newListItems.hashCode()
|
||||||
|
listItems = newListItems.clone() as ArrayList<ListItem>
|
||||||
|
recyclerView.resetItemCount()
|
||||||
|
notifyDataSetChanged()
|
||||||
|
finishActMode()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun setupListEvent(view: View, listEvent: ListEvent) {
|
private fun setupListEvent(view: View, listEvent: ListEvent) {
|
||||||
view.apply {
|
view.apply {
|
||||||
event_section_title.text = listEvent.title
|
event_section_title.text = listEvent.title
|
||||||
|
@@ -137,9 +137,9 @@ fun Context.getRepetitionText(seconds: Int) = when (seconds) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Context.getFilteredEvents(events: List<Event>): List<Event> {
|
fun Context.getFilteredEvents(events: List<Event>): ArrayList<Event> {
|
||||||
val displayEventTypes = config.displayEventTypes
|
val displayEventTypes = config.displayEventTypes
|
||||||
return events.filter { displayEventTypes.contains(it.eventType.toString()) }
|
return events.filter { displayEventTypes.contains(it.eventType.toString()) } as ArrayList<Event>
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Context.notifyRunningEvents() {
|
fun Context.notifyRunningEvents() {
|
||||||
|
@@ -19,15 +19,23 @@ import com.simplemobiletools.calendar.models.Event
|
|||||||
import com.simplemobiletools.calendar.models.ListEvent
|
import com.simplemobiletools.calendar.models.ListEvent
|
||||||
import com.simplemobiletools.commons.extensions.beGoneIf
|
import com.simplemobiletools.commons.extensions.beGoneIf
|
||||||
import com.simplemobiletools.commons.extensions.beVisibleIf
|
import com.simplemobiletools.commons.extensions.beVisibleIf
|
||||||
|
import com.simplemobiletools.commons.helpers.MONTH_SECONDS
|
||||||
import com.simplemobiletools.commons.interfaces.RefreshRecyclerViewListener
|
import com.simplemobiletools.commons.interfaces.RefreshRecyclerViewListener
|
||||||
|
import com.simplemobiletools.commons.views.MyRecyclerView
|
||||||
import kotlinx.android.synthetic.main.fragment_event_list.view.*
|
import kotlinx.android.synthetic.main.fragment_event_list.view.*
|
||||||
import org.joda.time.DateTime
|
import org.joda.time.DateTime
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class EventListFragment : MyFragmentHolder(), RefreshRecyclerViewListener {
|
class EventListFragment : MyFragmentHolder(), RefreshRecyclerViewListener {
|
||||||
private var mEvents: List<Event> = ArrayList()
|
private var FETCH_INTERVAL = 6 * MONTH_SECONDS
|
||||||
|
|
||||||
|
private var mEvents = ArrayList<Event>()
|
||||||
private var prevEventsHash = 0
|
private var prevEventsHash = 0
|
||||||
|
private var minFetchedTS = 0
|
||||||
|
private var maxFetchedTS = 0
|
||||||
|
|
||||||
private var use24HourFormat = false
|
private var use24HourFormat = false
|
||||||
|
|
||||||
lateinit var mView: View
|
lateinit var mView: View
|
||||||
|
|
||||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||||
@@ -57,14 +65,17 @@ class EventListFragment : MyFragmentHolder(), RefreshRecyclerViewListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun checkEvents() {
|
private fun checkEvents() {
|
||||||
val fromTS = DateTime().seconds() - context!!.config.displayPastEvents * 60
|
minFetchedTS = DateTime().minusMonths(3).seconds()
|
||||||
val toTS = DateTime().plusYears(1).seconds()
|
maxFetchedTS = DateTime().plusMonths(6).seconds()
|
||||||
context!!.dbHelper.getEvents(fromTS, toTS) {
|
context!!.dbHelper.getEvents(minFetchedTS, maxFetchedTS) {
|
||||||
receivedEvents(it)
|
receivedEvents(it)
|
||||||
|
if (it.size < 20) {
|
||||||
|
fetchNextPeriod()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun receivedEvents(events: MutableList<Event>) {
|
private fun receivedEvents(events: ArrayList<Event>) {
|
||||||
if (context == null || activity == null) {
|
if (context == null || activity == null) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -79,14 +90,25 @@ class EventListFragment : MyFragmentHolder(), RefreshRecyclerViewListener {
|
|||||||
mEvents = filtered
|
mEvents = filtered
|
||||||
val listItems = context!!.getEventListItems(mEvents)
|
val listItems = context!!.getEventListItems(mEvents)
|
||||||
|
|
||||||
val eventsAdapter = EventListAdapter(activity as SimpleActivity, listItems, true, this, mView.calendar_events_list) {
|
|
||||||
if (it is ListEvent) {
|
|
||||||
editEvent(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
activity?.runOnUiThread {
|
activity?.runOnUiThread {
|
||||||
mView.calendar_events_list.adapter = eventsAdapter
|
val currAdapter = mView.calendar_events_list.adapter
|
||||||
|
if (currAdapter == null) {
|
||||||
|
EventListAdapter(activity as SimpleActivity, listItems, true, this, mView.calendar_events_list) {
|
||||||
|
if (it is ListEvent) {
|
||||||
|
editEvent(it)
|
||||||
|
}
|
||||||
|
}.apply {
|
||||||
|
mView.calendar_events_list.adapter = this
|
||||||
|
}
|
||||||
|
|
||||||
|
mView.calendar_events_list.endlessScrollListener = object : MyRecyclerView.EndlessScrollListener {
|
||||||
|
override fun updateBottom() {
|
||||||
|
fetchNextPeriod()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
(currAdapter as EventListAdapter).updateListItems(listItems)
|
||||||
|
}
|
||||||
checkPlaceholderVisibility()
|
checkPlaceholderVisibility()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -106,6 +128,15 @@ class EventListFragment : MyFragmentHolder(), RefreshRecyclerViewListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun fetchNextPeriod() {
|
||||||
|
val oldMaxFetchedTS = maxFetchedTS + 1
|
||||||
|
maxFetchedTS += FETCH_INTERVAL
|
||||||
|
context!!.dbHelper.getEvents(oldMaxFetchedTS, maxFetchedTS) {
|
||||||
|
mEvents.addAll(it)
|
||||||
|
receivedEvents(mEvents)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun refreshItems() {
|
override fun refreshItems() {
|
||||||
checkEvents()
|
checkEvents()
|
||||||
}
|
}
|
||||||
|
@@ -660,13 +660,13 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
|||||||
}.start()
|
}.start()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getEvents(fromTS: Int, toTS: Int, eventId: Int = -1, callback: (events: MutableList<Event>) -> Unit) {
|
fun getEvents(fromTS: Int, toTS: Int, eventId: Int = -1, callback: (events: ArrayList<Event>) -> Unit) {
|
||||||
Thread {
|
Thread {
|
||||||
getEventsInBackground(fromTS, toTS, eventId, callback)
|
getEventsInBackground(fromTS, toTS, eventId, callback)
|
||||||
}.start()
|
}.start()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getEventsInBackground(fromTS: Int, toTS: Int, eventId: Int = -1, callback: (events: MutableList<Event>) -> Unit) {
|
fun getEventsInBackground(fromTS: Int, toTS: Int, eventId: Int = -1, callback: (events: ArrayList<Event>) -> Unit) {
|
||||||
val events = ArrayList<Event>()
|
val events = ArrayList<Event>()
|
||||||
|
|
||||||
var selection = "$COL_START_TS <= ? AND $COL_END_TS >= ? AND $COL_REPEAT_INTERVAL IS NULL AND $COL_START_TS != 0"
|
var selection = "$COL_START_TS <= ? AND $COL_END_TS >= ? AND $COL_REPEAT_INTERVAL IS NULL AND $COL_START_TS != 0"
|
||||||
@@ -680,7 +680,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
|||||||
|
|
||||||
events.addAll(getAllDayEvents(fromTS, eventId))
|
events.addAll(getAllDayEvents(fromTS, eventId))
|
||||||
|
|
||||||
val filtered = events.distinct().filterNot { it.ignoreEventOccurrences.contains(Formatter.getDayCodeFromTS(it.startTS).toInt()) } as MutableList<Event>
|
val filtered = events.distinct().filterNot { it.ignoreEventOccurrences.contains(Formatter.getDayCodeFromTS(it.startTS).toInt()) } as ArrayList<Event>
|
||||||
callback(filtered)
|
callback(filtered)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -11,6 +11,7 @@
|
|||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:clipToPadding="false"
|
android:clipToPadding="false"
|
||||||
|
android:overScrollMode="never"
|
||||||
android:paddingTop="@dimen/medium_margin"
|
android:paddingTop="@dimen/medium_margin"
|
||||||
android:scrollbars="vertical"
|
android:scrollbars="vertical"
|
||||||
android:visibility="gone"
|
android:visibility="gone"
|
||||||
|
Reference in New Issue
Block a user