Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1c0da31
Support non exception errors from fetcher
eyalgu Feb 29, 2020
0075ada
revert parital changes to store builder to reduce noise
eyalgu Feb 29, 2020
2fe7b7e
finish off diff
eyalgu Mar 11, 2020
9e3801e
Merge remote-tracking branch 'origin/master' into errors
eyalgu Mar 11, 2020
d886e41
Allow to create a FetcherResult.Error without a Throwable. Add tests
eyalgu Mar 12, 2020
47463e6
Add missing funcion and more tests
eyalgu Mar 16, 2020
7279bdf
lint
eyalgu Mar 16, 2020
cae8faa
unflake RxFlowableStoreTest
eyalgu Mar 16, 2020
cea6e71
try to rename FakeFetcher to FakeRxFetcher to (maybe) solve missing c…
eyalgu Mar 16, 2020
2e1b012
Merge remote-tracking branch 'origin/master' into errors
eyalgu Mar 17, 2020
7bd21db
move SourceOfTruth out of impl package
eyalgu Mar 17, 2020
44a5e15
Merge remote-tracking branch 'origin/master' into errors
eyalgu Mar 18, 2020
03768e6
Rename accidental change of RxStoreBuilder.fromMaybe back to formSingle
eyalgu Mar 18, 2020
81548bb
Introduce Fetcher from #139
eyalgu Apr 21, 2020
3981d37
fix Rx artifact
eyalgu Apr 22, 2020
fb7c64d
Merge remote-tracking branch 'origin/master' into errors
eyalgu Apr 22, 2020
738ed50
delete legacy presistor factory
eyalgu Apr 22, 2020
1527bec
fix api file
eyalgu Apr 22, 2020
cb50d9c
move fetcher to be a typealias
eyalgu Apr 22, 2020
ed9d87f
code review comments + clean up documentation
eyalgu Apr 23, 2020
cff04b4
code review comments
eyalgu Apr 27, 2020
3251fbb
Update store/src/main/java/com/dropbox/android/external/store4/Fetche…
eyalgu Apr 28, 2020
5a2df2e
Merge remote-tracking branch 'origin/master' into errors
eyalgu Apr 29, 2020
a0edfcf
Revert "Update sample app's build.gradle to refer to the externally r…
eyalgu Apr 29, 2020
e883e5d
update releasing.md
eyalgu Apr 29, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 48 additions & 34 deletions app/src/main/java/com/dropbox/android/sample/Graph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ import com.dropbox.android.external.fs3.FileSystemPersister
import com.dropbox.android.external.fs3.PathResolver
import com.dropbox.android.external.fs3.SourcePersisterFactory
import com.dropbox.android.external.fs3.filesystem.FileSystemFactory
import com.dropbox.android.external.store4.Fetcher
import com.dropbox.android.external.store4.StoreBuilder
import com.dropbox.android.external.store4.MemoryPolicy
import com.dropbox.android.external.store4.Persister
import com.dropbox.android.external.store4.Store
import com.dropbox.android.external.store4.SourceOfTruth
import com.dropbox.android.external.store4.legacy.BarCode
import com.dropbox.android.external.store4.exceptionsAsErrorsNonFlow
import com.squareup.moshi.Moshi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
Expand All @@ -31,36 +34,46 @@ import java.io.IOException
import kotlin.time.ExperimentalTime
import kotlin.time.seconds

@OptIn(FlowPreview::class, ExperimentalCoroutinesApi::class, ExperimentalTime::class, ExperimentalStdlibApi::class)
@OptIn(
FlowPreview::class,
ExperimentalCoroutinesApi::class,
ExperimentalTime::class,
ExperimentalStdlibApi::class
)
object Graph {
private val moshi = Moshi.Builder().build()

fun provideRoomStore(context: SampleApp): Store<String, List<Post>> {
val db = provideRoom(context)
return StoreBuilder
.fromNonFlow { key: String ->
provideRetrofit().fetchSubreddit(key, 10).data.children.map(::toPosts)
}
.persister(
reader = db.postDao()::loadPosts,
writer = db.postDao()::insertPosts,
delete = db.postDao()::clearFeedBySubredditName,
deleteAll = db.postDao()::clearAllFeeds
.from(
Fetcher.exceptionsAsErrorsNonFlow { key: String ->
provideRetrofit().fetchSubreddit(key, 10).data.children.map(::toPosts)
},
sourceOfTruth = SourceOfTruth.from(
reader = db.postDao()::loadPosts,
writer = db.postDao()::insertPosts,
delete = db.postDao()::clearFeedBySubredditName,
deleteAll = db.postDao()::clearAllFeeds
)
)
.build()
}

fun provideRoomStoreMultiParam(context: SampleApp): Store<Pair<String, RedditConfig>, List<Post>> {
val db = provideRoom(context)
return StoreBuilder
.fromNonFlow<Pair<String, RedditConfig>, List<Post>> { (query, config) ->
provideRetrofit().fetchSubreddit(query, config.limit)
.data.children.map(::toPosts)
}
.persister(reader = { (query, _) -> db.postDao().loadPosts(query) },
writer = { (query, _), posts -> db.postDao().insertPosts(query, posts) },
delete = { (query, _) -> db.postDao().clearFeedBySubredditName(query) },
deleteAll = db.postDao()::clearAllFeeds
.from<Pair<String, RedditConfig>, List<Post>, List<Post>>(
Fetcher.exceptionsAsErrorsNonFlow { (query, config) ->
provideRetrofit().fetchSubreddit(query, config.limit)
.data.children.map(::toPosts)
},
sourceOfTruth = SourceOfTruth.from(
reader = { (query, _) -> db.postDao().loadPosts(query) },
writer = { (query, _), posts -> db.postDao().insertPosts(query, posts) },
delete = { (query, _) -> db.postDao().clearFeedBySubredditName(query) },
deleteAll = db.postDao()::clearAllFeeds
)
)
.build()
}
Expand All @@ -86,25 +99,26 @@ object Graph {
})
val adapter = moshi.adapter<RedditConfig>(RedditConfig::class.java)
return StoreBuilder
.fromNonFlow<Unit, RedditConfig> {
delay(500)
RedditConfig(10)
}
.nonFlowingPersister(
reader = {
runCatching {
val source = fileSystemPersister.read(Unit)
source?.let { adapter.fromJson(it) }
}.getOrNull()
.from<Unit, RedditConfig, RedditConfig>(
Fetcher.exceptionsAsErrorsNonFlow {
delay(500)
RedditConfig(10)
},
writer = { _, config ->
val buffer = Buffer()
withContext(Dispatchers.IO) {
adapter.toJson(buffer, config)
sourceOfTruth = SourceOfTruth.fromNonFlow(
reader = {
runCatching {
val source = fileSystemPersister.read(Unit)
source?.let { adapter.fromJson(it) }
}.getOrNull()
},
writer = { _, config ->
val buffer = Buffer()
withContext(Dispatchers.IO) {
adapter.toJson(buffer, config)
}
fileSystemPersister.write(Unit, buffer)
}
fileSystemPersister.write(Unit, buffer)
}
)
))
.cachePolicy(
MemoryPolicy.builder().setExpireAfterWrite(10.seconds).build()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ internal class StoreState<Key : Any, Output : Any>(
it is StoreResponse.Loading
)
}
if (it is StoreResponse.Error) {
_errors.send(it.error.localizedMessage!!)
when (it) {
is StoreResponse.Error.Exception -> _errors.send(it.error.localizedMessage!!)
is StoreResponse.Error.Message -> _errors.send(it.message)
}
}.transform {
if (it is StoreResponse.Data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.dropbox.android.external.store4.Fetcher
import com.dropbox.android.external.store4.MemoryPolicy
import com.dropbox.android.external.store4.StoreBuilder
import com.dropbox.android.external.store4.StoreRequest
import com.dropbox.android.external.store4.exceptionsAsErrorsNonFlow
import com.dropbox.android.external.store4.fresh
import com.dropbox.android.external.store4.get
import com.dropbox.android.sample.R
Expand Down Expand Up @@ -51,7 +53,9 @@ class StreamFragment : Fragment(), CoroutineScope {
var counter = 0

val store = StoreBuilder
.fromNonFlow { key: Int -> (key * 1000 + counter++).also { delay(1_000) } }
.from(Fetcher.exceptionsAsErrorsNonFlow { key: Int ->
(key * 1000 + counter++).also { delay(1_000) }
})
.cachePolicy(
MemoryPolicy
.builder()
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {
}

ext.versions = [
androidGradlePlugin : '4.0.0-beta01',
androidGradlePlugin : '3.6.0',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not important but probably better to use 3.6.1 ?

kotlin : '1.3.70',
dokkaGradlePlugin : '0.10.0',
ktlintGradle : '9.1.1',
Expand Down
25 changes: 18 additions & 7 deletions store-rx2/api/store-rx2.api
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
public final class com/dropbox/store/rx2/RxFetcherKt {
public static final fun exceptionAsErrorsFlowable (Lcom/dropbox/android/external/store4/Fetcher$Companion;Lkotlin/jvm/functions/Function1;)Lcom/dropbox/android/external/store4/Fetcher;
public static final fun exceptionAsErrorsSingle (Lcom/dropbox/android/external/store4/Fetcher$Companion;Lkotlin/jvm/functions/Function1;)Lcom/dropbox/android/external/store4/Fetcher;
public static final fun fromFlowable (Lcom/dropbox/android/external/store4/Fetcher$Companion;Lkotlin/jvm/functions/Function1;)Lcom/dropbox/android/external/store4/Fetcher;
public static final fun fromSingle (Lcom/dropbox/android/external/store4/Fetcher$Companion;Lkotlin/jvm/functions/Function1;)Lcom/dropbox/android/external/store4/Fetcher;
}

public final class com/dropbox/store/rx2/RxSourceOfTruthKt {
public static final fun fromFlowable (Lcom/dropbox/android/external/store4/SourceOfTruth$Companion;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;)Lcom/dropbox/android/external/store4/SourceOfTruth;
public static synthetic fun fromFlowable$default (Lcom/dropbox/android/external/store4/SourceOfTruth$Companion;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;ILjava/lang/Object;)Lcom/dropbox/android/external/store4/SourceOfTruth;
public static final fun fromMaybe (Lcom/dropbox/android/external/store4/SourceOfTruth$Companion;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;)Lcom/dropbox/android/external/store4/SourceOfTruth;
public static synthetic fun fromMaybe$default (Lcom/dropbox/android/external/store4/SourceOfTruth$Companion;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;ILjava/lang/Object;)Lcom/dropbox/android/external/store4/SourceOfTruth;
}

public final class com/dropbox/store/rx2/RxStoreBuilderKt {
public static final fun withScheduler (Lcom/dropbox/android/external/store4/StoreBuilder;Lio/reactivex/Scheduler;)Lcom/dropbox/android/external/store4/StoreBuilder;
}

public final class com/dropbox/store/rx2/RxStoreKt {
public static final fun freshSingle (Lcom/dropbox/android/external/store4/Store;Ljava/lang/Object;)Lio/reactivex/Single;
public static final fun fromFlowable (Lcom/dropbox/android/external/store4/StoreBuilder$Companion;Lkotlin/jvm/functions/Function1;)Lcom/dropbox/android/external/store4/StoreBuilder;
public static final fun fromSingle (Lcom/dropbox/android/external/store4/StoreBuilder$Companion;Lkotlin/jvm/functions/Function1;)Lcom/dropbox/android/external/store4/StoreBuilder;
public static final fun getSingle (Lcom/dropbox/android/external/store4/Store;Ljava/lang/Object;)Lio/reactivex/Single;
public static final fun observe (Lcom/dropbox/android/external/store4/Store;Lcom/dropbox/android/external/store4/StoreRequest;)Lio/reactivex/Flowable;
public static final fun observeClear (Lcom/dropbox/android/external/store4/Store;Ljava/lang/Object;)Lio/reactivex/Completable;
public static final fun observeClearAll (Lcom/dropbox/android/external/store4/Store;)Lio/reactivex/Completable;
public static final fun withFlowablePersister (Lcom/dropbox/android/external/store4/StoreBuilder;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;)Lcom/dropbox/android/external/store4/StoreBuilder;
public static synthetic fun withFlowablePersister$default (Lcom/dropbox/android/external/store4/StoreBuilder;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;ILjava/lang/Object;)Lcom/dropbox/android/external/store4/StoreBuilder;
public static final fun withScheduler (Lcom/dropbox/android/external/store4/StoreBuilder;Lio/reactivex/Scheduler;)Lcom/dropbox/android/external/store4/StoreBuilder;
public static final fun withSinglePersister (Lcom/dropbox/android/external/store4/StoreBuilder;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;)Lcom/dropbox/android/external/store4/StoreBuilder;
public static synthetic fun withSinglePersister$default (Lcom/dropbox/android/external/store4/StoreBuilder;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function0;ILjava/lang/Object;)Lcom/dropbox/android/external/store4/StoreBuilder;
}

72 changes: 72 additions & 0 deletions store-rx2/src/main/kotlin/com/dropbox/store/rx2/RxFetcher.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.dropbox.store.rx2

import com.dropbox.android.external.store4.Fetcher
import com.dropbox.android.external.store4.FetcherResult
import com.dropbox.android.external.store4.Store
import com.dropbox.android.external.store4.exceptionsAsErrors
import io.reactivex.Flowable
import io.reactivex.Single
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.reactive.asFlow

/**
* Creates a new [Fetcher] from a [Flowable] fetcher.
*
* Use when creating a [Store] that fetches objects in an websocket-like multiple responses
* per request protocol.
*
* @param fetcher a function for fetching a flow of network records.
*/
@FlowPreview
@ExperimentalCoroutinesApi
@ExperimentalStdlibApi
fun <Key : Any, Output : Any> Fetcher.Companion.fromFlowable(
fetcher: (key: Key) -> Flowable<FetcherResult<Output>>
): Fetcher<Key, Output> = from { key: Key -> fetcher(key).asFlow() }

/**
* Creates a new [Fetcher] from a [Flowable] fetcher.
*
* Use when creating a [Store] that fetches objects in an websocket-like multiple responses
* per request protocol.
*
* @param fetcher a function for fetching a flow of network records.
*/
@FlowPreview
@ExperimentalCoroutinesApi
@ExperimentalStdlibApi
fun <Key : Any, Output : Any> Fetcher.Companion.exceptionAsErrorsFlowable(
fetcher: (key: Key) -> Flowable<Output>
): Fetcher<Key, Output> = exceptionsAsErrors { key: Key -> fetcher(key).asFlow() }

/**
* Creates a new [Fetcher] from a [Single] fetcher.
*
* Use when creating a [Store] that fetches objects from a [Single] source that emits one response
*
* @param fetcher a function for fetching a [Single] network response for a [Key]
*/
@FlowPreview
@ExperimentalCoroutinesApi
@ExperimentalStdlibApi
fun <Key : Any, Output : Any> Fetcher.Companion.fromSingle(
fetcher: (key: Key) -> Single<FetcherResult<Output>>
): Fetcher<Key, Output> =
from { key: Key -> fetcher(key).toFlowable().asFlow() }

/**
* Creates a new [Fetcher] from a [Single] fetcher.
*
* Use when creating a [Store] that fetches objects from a [Single] source that emits one response
*
* @param fetcher a function for fetching a [Single] network response for a [Key]
* @param fetcherTransformer used to translate your fetcher's return value to success value
* or error in the case that your fetcher does not communicate errors through exceptions
*/
@FlowPreview
@ExperimentalCoroutinesApi
@ExperimentalStdlibApi
fun <Key : Any, Output : Any> Fetcher.Companion.exceptionAsErrorsSingle(
fetcher: (key: Key) -> Single<Output>
): Fetcher<Key, Output> = exceptionAsErrorsFlowable { key: Key -> fetcher(key).toFlowable() }
73 changes: 73 additions & 0 deletions store-rx2/src/main/kotlin/com/dropbox/store/rx2/RxSourceOfTruth.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.dropbox.store.rx2

import com.dropbox.android.external.store4.Store
import com.dropbox.android.external.store4.SourceOfTruth
import io.reactivex.Completable
import io.reactivex.Flowable
import io.reactivex.Maybe
import io.reactivex.Single
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.reactive.asFlow
import kotlinx.coroutines.rx2.await

/**
* Creates a (Non Flow) [Single] source of truth that is accessible via [reader], [writer],
* [delete], and [deleteAll].
*
* @see com.dropbox.android.external.store4.StoreBuilder.persister
*/
@FlowPreview
@ExperimentalCoroutinesApi
fun <Key : Any, Input : Any, Output : Any> SourceOfTruth.Companion.fromMaybe(
reader: (Key) -> Maybe<Output>,
writer: (Key, Input) -> Completable,
delete: ((Key) -> Completable)? = null,
deleteAll: (() -> Completable)? = null
): SourceOfTruth<Key, Input, Output> {
val deleteFun: (suspend (Key) -> Unit)? =
if (delete != null) { key -> delete(key).await() } else null
val deleteAllFun: (suspend () -> Unit)? = deleteAll?.let { { deleteAll().await() } }
return fromNonFlow(
reader = { key -> reader.invoke(key).await() },
writer = { key, output -> writer.invoke(key, output).await() },
delete = deleteFun,
deleteAll = deleteAllFun
)
}

/**
* Creates a ([Flowable]) source of truth that is accessed via [reader], [writer] and [delete].
*
* For maximal flexibility, [writer]'s record type ([Input]] and [reader]'s record type
* ([Output]) are not identical. This allows us to read one type of objects from network and
* transform them to another type when placing them in local storage.
*
* A source of truth is usually backed by local storage. It's purpose is to eliminate the need
* for waiting on network update before local modifications are available (via [Store.stream]).
*
* @param reader reads records from the source of truth
* @param writer writes records **coming in from the fetcher (network)** to the source of truth.
* Writing local user updates to the source of truth via [Store] is currently not supported.
* @param delete deletes records in the source of truth for the give key
* @param deleteAll deletes all records in the source of truth
*
*/
@FlowPreview
@ExperimentalCoroutinesApi
fun <Key : Any, Input : Any, Output : Any> SourceOfTruth.Companion.fromFlowable(
reader: (Key) -> Flowable<Output>,
writer: (Key, Input) -> Completable,
delete: ((Key) -> Completable)? = null,
deleteAll: (() -> Completable)? = null
): SourceOfTruth<Key, Input, Output> {
val deleteFun: (suspend (Key) -> Unit)? =
if (delete != null) { key -> delete(key).await() } else null
val deleteAllFun: (suspend () -> Unit)? = deleteAll?.let { { deleteAll().await() } }
return from(
reader = { key -> reader.invoke(key).asFlow() },
writer = { key, output -> writer.invoke(key, output).await() },
delete = deleteFun,
deleteAll = deleteAllFun
)
}
Loading