Skip to content

Commit

Permalink
Silvermans farm
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Ramotar <[email protected]>
  • Loading branch information
matt-ramotar committed Jul 7, 2024
1 parent 1cdb85a commit e834425
Show file tree
Hide file tree
Showing 32 changed files with 890 additions and 303 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ captures
!*.xcworkspace/contents.xcworkspacedata
**/xcshareddata/WorkspaceSettings.xcsettings
*/kover
yarn.lock
yarn.lock
.kotlin/
23 changes: 12 additions & 11 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,28 @@ androidx-core-ktx = "1.12.0"
androidx-espresso-core = "3.5.1"
androidx-material = "1.11.0"
androidx-test-junit = "1.1.5"
compose = "1.6.2"
compose = "1.6.8"
compose-plugin = "1.6.0"
junit = "4.13.2"
kotlin = "1.9.23"
kotlin = "2.0.0"
store = "5.1.0-alpha02"
kermit = "1.2.2"
coroutines = "1.8.0"
coroutines = "1.8.1"
kover = "0.6.0"
dokkaGradlePlugin = "1.9.10"
dokkaGradlePlugin = "1.9.20"
ktlintGradle = "12.1.0"
datetime = "0.6.0"
moleculeGradlePlugin = "1.2.1"
kotlinxSerialization = "1.5.1"
kotlinxCoroutines = "1.8.0"
ksp = "1.9.22-1.0.17"
kotlinxSerialization = "1.6.2"
kotlinxCoroutines = "1.8.1"
ksp = "2.0.0-1.0.22"
ktor = "2.3.7"
jetbrains-compose = "1.6.10"
jetbrainsCompose = "1.6.11"
jetbrainsComposeGradlePlugin = "2.0.0"
sqldelight = "2.0.2"
benchmark = "0.4.10"
compose-compiler = "1.5.11"
compose-bom = "2024.05.00"
compose-bom = "2024.06.00"
lint = "1.2.0"
androidx-activity = "1.9.0"
coil = "3.0.0-alpha06"
Expand Down Expand Up @@ -105,7 +106,7 @@ ktor-serialization-xml = { module = "io.ktor:ktor-serialization-kotlinx-xml", ve
circuit-foundation = { module = "com.slack.circuit:circuit-foundation", version.ref = "circuit" }
swipe = { module = "me.saket.swipe:swipe", version = "1.3.0" }
mockk = {module = "io.mockk:mockk", version.ref = "mockk"}
jetbrains-compose-runtime = {module = "org.jetbrains.compose.runtime:runtime", version.ref="jetbrains-compose"}
jetbrains-compose-runtime = {module = "org.jetbrains.compose.runtime:runtime", version.ref="jetbrainsCompose"}

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
Expand All @@ -114,7 +115,7 @@ kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
compose = { id = "org.jetbrains.compose", version.ref = "jetbrains-compose" }
compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "jetbrainsComposeGradlePlugin" }
ktor = { id = "io.ktor.plugin", version.ref = "ktor" }
kotlin-plugin-parcelize = { id = "org.jetbrains.kotlin.plugin.parcelize", version.ref = "kotlin" }
ktlint = { id = "org.jlleitschuh.gradle.ktlint", version = "12.1.0" }
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#Thu May 30 16:21:01 EDT 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
1 change: 1 addition & 0 deletions paging-runtime/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id("storex.android.library")
id("storex.multiplatform")
id("storex.compose")
}

kotlin {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.mobilenativefoundation.storex.paging.persistence
package org.mobilenativefoundation.storex.paging.persistence.api


import org.mobilenativefoundation.storex.paging.runtime.Identifiable
Expand Down Expand Up @@ -33,6 +33,6 @@ import org.mobilenativefoundation.storex.paging.runtime.Identifier
* @param V The type of the item value.
*/
interface DataPersistence<Id : Identifier<Id>, K : Comparable<K>, V : Identifiable<Id>> {
val itemPersistence: ItemPersistence<Id, K, V>
val pagePersistence: PagePersistence<Id, K, V>
val items: ItemPersistence<Id, K, V>
val pages: PagePersistence<Id, K, V>
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.mobilenativefoundation.storex.paging.persistence
package org.mobilenativefoundation.storex.paging.persistence.api

import kotlinx.coroutines.flow.Flow
import org.mobilenativefoundation.storex.paging.runtime.Identifiable
import org.mobilenativefoundation.storex.paging.runtime.Identifier
import org.mobilenativefoundation.storex.paging.runtime.PagingSource

/**
* Interface for persistence operations related to individual items.
Expand Down Expand Up @@ -33,7 +34,7 @@ interface ItemPersistence<Id : Identifier<Id>, K : Comparable<K>, V : Identifiab
* @param item The item to save or update.
* @return A PersistenceResult indicating success or failure of the operation.
*/
suspend fun saveItem(item: V): PersistenceResult<Unit>
suspend fun saveItem(item: V, params: PagingSource.LoadParams<K>?): PersistenceResult<Unit>

/**
* Removes an item by its identifier.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.mobilenativefoundation.storex.paging.persistence
package org.mobilenativefoundation.storex.paging.persistence.api

import org.mobilenativefoundation.storex.paging.runtime.Identifiable
import org.mobilenativefoundation.storex.paging.runtime.Identifier
Expand All @@ -19,7 +19,7 @@ interface PagePersistence<Id : Identifier<Id>, K : Comparable<K>, V : Identifiab
* @param params The load parameters of the page to check.
* @return A PersistenceResult containing a boolean indicating whether the page exists.
*/
suspend fun pageExists(params: PagingSource.LoadParams<K>): PersistenceResult<Boolean>
suspend fun exists(params: PagingSource.LoadParams<K>): PersistenceResult<Boolean>

/**
* Saves a page of data.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package org.mobilenativefoundation.storex.paging.persistence
package org.mobilenativefoundation.storex.paging.persistence.api

/**
* Represents the result of a data operation, either successful or an error.
*/
sealed class PersistenceResult<out T> {
data object Skipped: PersistenceResult<Nothing>()
data class Success<T>(val data: T) : PersistenceResult<T>()
data class Error(val message: String, val cause: Throwable? = null) :
PersistenceResult<Nothing>()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.mobilenativefoundation.storex.paging.persistence.impl

import org.mobilenativefoundation.storex.paging.persistence.api.DataPersistence
import org.mobilenativefoundation.storex.paging.persistence.api.ItemPersistence
import org.mobilenativefoundation.storex.paging.persistence.api.PagePersistence
import org.mobilenativefoundation.storex.paging.runtime.Identifiable
import org.mobilenativefoundation.storex.paging.runtime.Identifier

class RealDataPersistence<Id : Identifier<Id>, K : Comparable<K>, V : Identifiable<Id>>(
override val items: ItemPersistence<Id, K, V>,
override val pages: PagePersistence<Id, K, V>
) : DataPersistence<Id, K, V>
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.mobilenativefoundation.storex.paging.runtime

import kotlinx.serialization.json.JsonObject

data class ItemState<Id : Identifier<*>, V : Identifiable<Id>>(
val item: V?,
val loadState: SingleLoadState,
Expand Down Expand Up @@ -33,9 +31,12 @@ data class ItemState<Id : Identifier<*>, V : Identifiable<Id>>(
fun <Id : Identifier<*>, V : Identifiable<Id>> errorRefreshing(
item: V?,
error: Throwable,
version: Long,
extras: JsonObject? = null
) = ItemState<Id, V>(item, SingleLoadState.Error.Refreshing(error, extras), version)
version: Long
) = ItemState(
item,
SingleLoadState.Error.Exception(error, SingleLoadState.Error.Context.Refresh),
version
)

fun <Id : Identifier<*>, V : Identifiable<Id>> cleared(
) = ItemState<Id, V>(null, SingleLoadState.Cleared)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package org.mobilenativefoundation.storex.paging.runtime

import kotlinx.coroutines.CoroutineDispatcher
import org.mobilenativefoundation.store.store5.Updater
import org.mobilenativefoundation.storex.paging.custom.FetchingStrategy
import org.mobilenativefoundation.storex.paging.custom.LaunchEffect
import org.mobilenativefoundation.storex.paging.custom.Middleware
import org.mobilenativefoundation.storex.paging.custom.SideEffect
import org.mobilenativefoundation.storex.paging.persistence.api.ItemPersistence
import org.mobilenativefoundation.storex.paging.persistence.api.PagePersistence
import org.mobilenativefoundation.storex.paging.runtime.internal.pagingScope.impl.PagingScopeBuilder

interface PagingScope<Id : Identifier<Id>, K : Comparable<K>, V : Identifiable<Id>> {
Expand All @@ -9,14 +17,28 @@ interface PagingScope<Id : Identifier<Id>, K : Comparable<K>, V : Identifiable<I
fun getUpdatingItemProvider(): UpdatingItemProvider<Id, V>

interface Builder<Id : Identifier<Id>, K : Comparable<K>, V : Identifiable<Id>> {
fun setInitialState(state: PagingState<Id>): Builder<Id, K, V>
fun setInitialLoadParams(params: PagingSource.LoadParams<K>): Builder<Id, K, V>
fun setPagingSource(source: PagingSource<Id, K, V>): Builder<Id, K, V>
fun setCoroutineDispatcher(dispatcher: CoroutineDispatcher): Builder<Id, K, V>
fun addLaunchEffect(effect: LaunchEffect): Builder<Id, K, V>
fun addSideEffect(effect: SideEffect<Id, V>): Builder<Id, K, V>
fun addMiddleware(mw: Middleware<K>): Builder<Id, K, V>
fun setInitialFetchingState(state: FetchingState<Id, K>): Builder<Id, K, V>
fun setFetchingStrategy(strategy: FetchingStrategy<Id, K>): Builder<Id, K, V>
fun setErrorHandlingStrategy(strategy: ErrorHandlingStrategy): Builder<Id, K, V>
fun setItemMemoryCache(cache: MutableMap<Id, V>): Builder<Id, K, V>
fun setPageMemoryCache(cache: MutableMap<K, PagingSource.LoadResult.Data<Id, K, V>>): Builder<Id, K, V>
fun setItemPersistence(persistence: ItemPersistence<Id, K, V>): Builder<Id, K, V>
fun setPagePersistence(persistence: PagePersistence<Id, K, V>): Builder<Id, K, V>
fun setItemUpdater(updater: Updater<Id, V, *>): Builder<Id, K, V>
fun setPlaceholderFactory(placeholderFactory: PlaceholderFactory<Id, K, V>): Builder<Id, K, V>
fun build(): PagingScope<Id, K, V>
}

companion object {
fun <Id : Identifier<Id>, K : Comparable<K>, V : Identifiable<Id>> builder(
pagingConfig: PagingConfig<Id, K>
): Builder<Id, K, V> {
return PagingScopeBuilder(pagingConfig)
}
): Builder<Id, K, V> = PagingScopeBuilder(pagingConfig)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ fun interface PagingSource<Id : Identifier<*>, K : Any, V : Identifiable<Id>> {
enum class Origin {
Network,
MemoryCache,
SourceOfTruth
SourceOfTruth,
Placeholder
}

val normalized: Normalized<Id, K>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.mobilenativefoundation.storex.paging.runtime

interface PlaceholderFactory<Id : Identifier<Id>,K: Comparable<K>, V : Identifiable<Id>> {
fun create(index: Int, params: PagingSource.LoadParams<K>): V
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.mobilenativefoundation.storex.paging.runtime.internal.pager.api

import kotlinx.coroutines.flow.Flow
import org.mobilenativefoundation.storex.paging.persistence.api.PersistenceResult
import org.mobilenativefoundation.storex.paging.runtime.Identifiable
import org.mobilenativefoundation.storex.paging.runtime.Identifier
import org.mobilenativefoundation.storex.paging.runtime.PagingSource

internal interface LinkedHashMapManager<Id : Identifier<Id>, K : Comparable<K>, V : Identifiable<Id>> {
suspend fun isInFlight(key: K): Boolean

suspend fun isCached(key: K): Boolean

suspend fun isInDatabase(params: PagingSource.LoadParams<K>): Boolean


suspend fun appendPlaceholders(params: PagingSource.LoadParams<K>)
suspend fun prependPlaceholders(params: PagingSource.LoadParams<K>)

suspend fun getItem(id: Id): V?

suspend fun getCachedItem(id: Id): V?
suspend fun getPersistedItem(id: Id): V?

suspend fun getCachedPage(key: K): PagingSource. LoadResult. Data<Id, K, V>?
suspend fun getPersistedPage(key: K): PagingSource. LoadResult. Data<Id, K, V>?

suspend fun saveItem(item: V) : PersistenceResult<Unit>
suspend fun getItemsInOrder(): List<V?>

suspend fun putPageNode(key: K, pageNode: PageNode<K>)

suspend fun appendPage(
params: PagingSource.LoadParams<K>,
loadResult: PagingSource.LoadResult.Data<Id, K, V>
)

suspend fun prependPage(
params: PagingSource.LoadParams<K>,
loadResult: PagingSource.LoadResult.Data<Id, K, V>
)

suspend fun removeItem(id: Id): PersistenceResult<Unit>
suspend fun removeAllItems(): PersistenceResult<Unit>

suspend fun removePage(key: K)
suspend fun removeAllPages(): PersistenceResult<Unit>

suspend fun invalidate()

suspend fun queryItems(predicate: (V) -> Boolean): PersistenceResult<List<V>>

fun observeItem(id: Id): Flow<V?>

data class PageNode<K : Any>(
val key: K,
var isPlaceholder: Boolean = false,
var isInFlight: Boolean = false,
var prev: PageNode<K>? = null,
var next: PageNode<K>? = null
)
}
Loading

0 comments on commit e834425

Please sign in to comment.