Skip to content

Commit

Permalink
[QueryList] allow specifying a custom handler
Browse files Browse the repository at this point in the history
  • Loading branch information
agrosner committed Feb 17, 2021
1 parent 52b75d2 commit c00560b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
8 changes: 8 additions & 0 deletions lib/src/main/kotlin/com/dbflow5/query/BaseModelQueriable.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dbflow5.query

import android.os.Handler
import com.dbflow5.adapter.RetrievalAdapter
import com.dbflow5.adapter.queriable.ListModelLoader
import com.dbflow5.adapter.queriable.SingleModelLoader
Expand Down Expand Up @@ -99,3 +100,10 @@ protected constructor(table: Class<TModel>)
table.retrievalAdapter.nonCacheableSingleModelLoader
}
}

/**
* Constructs a flowQueryList allowing a custom [Handler].
*/
fun <T : Any> ModelQueriable<T>.flowQueryList(databaseWrapper: DatabaseWrapper, refreshHandler: Handler) =
FlowQueryList.Builder(modelQueriable = this, databaseWrapper = databaseWrapper,
refreshHandler = refreshHandler).build()
21 changes: 15 additions & 6 deletions lib/src/main/kotlin/com/dbflow5/query/list/FlowQueryList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class FlowQueryList<T : Any>(
/**
* Holds the table cursor
*/
val internalCursorList: FlowCursorList<T>)
val internalCursorList: FlowCursorList<T>,
private val refreshHandler: Handler = globalRefreshHandler)
: List<T>, IFlowCursorIterator<T> by internalCursorList {

private var pendingRefresh = false
Expand Down Expand Up @@ -45,9 +46,11 @@ class FlowQueryList<T : Any>(
internal constructor(builder: Builder<T>) : this(
internalCursorList = FlowCursorList.Builder(builder.modelQueriable, builder.databaseWrapper)
.cursor(builder.cursor)
.build()
.build(),
refreshHandler = builder.refreshHandler
)


fun addOnCursorRefreshListener(onCursorRefreshListener: OnCursorRefreshListener<T>) {
internalCursorList.addOnCursorRefreshListener(onCursorRefreshListener)
}
Expand Down Expand Up @@ -83,7 +86,7 @@ class FlowQueryList<T : Any>(
}
pendingRefresh = true
}
REFRESH_HANDLER.post(refreshRunnable)
refreshHandler.post(refreshRunnable)
}


Expand Down Expand Up @@ -178,19 +181,25 @@ class FlowQueryList<T : Any>(
internal var cursor: FlowCursor? = null
internal var modelQueriable: ModelQueriable<T>
internal val databaseWrapper: DatabaseWrapper
internal val refreshHandler: Handler

internal constructor(cursorList: FlowCursorList<T>) {
internal constructor(cursorList: FlowCursorList<T>,
refreshHandler: Handler = globalRefreshHandler) {
this.databaseWrapper = cursorList.databaseWrapper
table = cursorList.table
cursor = cursorList.cursor
modelQueriable = cursorList.modelQueriable
this.refreshHandler = refreshHandler
}

constructor(modelQueriable: ModelQueriable<T>,
databaseWrapper: DatabaseWrapper) {
databaseWrapper: DatabaseWrapper,
refreshHandler: Handler = globalRefreshHandler
) {
this.databaseWrapper = databaseWrapper
this.table = modelQueriable.table
this.modelQueriable = modelQueriable
this.refreshHandler = refreshHandler
}

fun cursor(cursor: FlowCursor) = apply {
Expand All @@ -202,7 +211,7 @@ class FlowQueryList<T : Any>(

companion object {

private val REFRESH_HANDLER = Handler(Looper.myLooper())
private val globalRefreshHandler = Handler(Looper.myLooper())
}


Expand Down

0 comments on commit c00560b

Please sign in to comment.