Skip to content

Instantly share code, notes, and snippets.

@seanwu1105
Last active June 1, 2022 19:29
Show Gist options
  • Save seanwu1105/f881f23f5cddba4972f3d4311a51ad04 to your computer and use it in GitHub Desktop.
Save seanwu1105/f881f23f5cddba4972f3d4311a51ad04 to your computer and use it in GitHub Desktop.
Random thought on this
data class RepositoryItem<T>(
val value: T,
val status: Adding | AddingFailed | Added | Removing | RemovingFailed
override fun equals(other: RepositoryItem<T>) {
return value == other.value
}
)
interface ListRepository<T> {
val fetched: Flow<List<T>>
val isFetching: Flow<Boolean>
fun list(offset: Int, size: Int) {
fetch(offset, size)
return fetched
}
suspend fun fetch(offset: Int, size: Int): Unit
suspend fun add(item: T): T
suspend fun remove(item: T): T
suspend fun removeAll(predicate: (item: T) -> Boolean): Collection<T>
suspend fun find(predicate: (item: T) -> Boolean): Collection<T>
suspend fun first(predicate: (item: T) -> Boolean): T
}
class RemoteListRepository<T>(
private val serverApis : ListApi,
private val localDb: ListDb
) : ListRepository<T> {}
interface ListDb<T> {
val all: Flow<List<T>>
suspend fun add(item: T): T
suspend fun remove(item: T): T
suspend fun removeAll(predicate: (item: T) -> Boolean): Collection<T>
suspend fun find(predicate: (item: T) -> Boolean): Collection<T>
suspend fun first(predicate: (item: T) -> Boolean): T
}
interface ListApi<T> {
suspend fun fetch(offset: Int, size: Int): List<T>
suspend fun add(item: T): T
suspend fun remove(item: T): T
suspend fun removeAll(predicate: (item: T) -> Boolean): Collection<T>
suspend fun find(predicate: (item: T) -> Boolean): Collection<T>
suspend fun first(predicate: (item: T) -> Boolean): T
}
interface SimpleListApi<T>(val baseUrl: String, val name: String) : ListApi<T> {
@Get("$baseUrl/$name/")
suspend fun fetch(offset: Int, size: Int): List<T>
@Post("$baseUrl/$name/")
suspend fun add(item: T): T
@Delete("$baseUrl/$name/")
suspend fun remove(item: T): T
@Delete("$baseUrl/$name/all/")
suspend fun removeAll(predicate: (item: T) -> Boolean): Collection<T>
@Post("$baseUrl/$name/")
suspend fun find(predicate: (item: T) -> Boolean): Collection<T>
@Post("$baseUrl/$name/")
suspend fun first(predicate: (item: T) -> Boolean): T
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment