
Replica
KMM library for organizing of network communication in a declarative way.
Motivation
While libraries such as Retrofit and Kotlin Serialization help to make network requests, Replica takes on another challenges.
It is important to keep data up to date on a client. But we shouldn't call network requests too frequently to not waste server resources.
Modern Android applications need sophisticated UX-patterns such as stale-while-revalidate, pull-to-refresh, pagination and optimistic updates. Developers have to implement it over and over again without the right tool at hand.
Classical imperative approach to networking is complex and error-prone. It is hard to achieve desired behavior by manually orchestrating individual network calls.
To summarize, Replica helps to:
- Keep client data up to date
- Save server resources
- Create great UX
- Write simple and maintainable code
Features
Replica provides:
Gradle Setup
dependencies {
def replicaVersion = '1.8.0-alpha'
// Basic usage
implementation "com.github.aartikov:replica-core:$replicaVersion"
// Automatic reaction on changes of network connection status
implementation "com.github.aartikov:replica-android-network:$replicaVersion"
// Transforming and combining replicas
implementation "com.github.aartikov:replica-algebra:$replicaVersion"
// Integration with ViewModel library
implementation "com.github.aartikov:replica-view-model:$replicaVersion"
// Integration with Decompose library
implementation "com.github.aartikov:replica-decompose:$replicaVersion"
// Debug tool
debugImplementation "com.github.aartikov:replica-devtools:$replicaVersion"
releaseImplementation "com.github.aartikov:replica-devtools-noop:$replicaVersion"
}
How it works
The library is based on a concept called data replication. The idea is simple. There is some data on a server. A client needs this data. Replica automatically represent server data on a client side and keeps it in sync.
Replica provides data replication primitives called replicas. Each replica targets some chunk of data on a server and replicates it on a client.
And the last concept is replica observers. Replica observers are required to display data on UI. An observer connects to a replica and transfers its state to UI. A replica knows how many observers it has and performs complex behaviour based on this information.
Basic usage
Typical code with Replica looks like that:
- Create a replica in Repository
class PokemonRepository(
replicaClient: ReplicaClient,
api: PokemonApi
) {
val pokemonsReplica: Replica<List<Pokemon>> = replicaClient.createReplica(
name = "pokemons",
settings = ReplicaSettings(staleTime = 1.minutes),
fetcher = {
api.getPokemons().toDomain()
}
)
}
- Observe a replica in ViewModel
class PokemonListViewModel(
private val pokemonsReplica: Replica<List<Pokemon>>,
errorHandler: ErrorHandler
) : ViewModel(), Activable by activable() {
val pokemonsState = pokemonsReplica.observe(this, errorHandler)
}
- Display state on UI
viewLifecycleOwner.lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.pokemonsState.collect { state ->
val (loading, data, error) = state
when {
data != null -> {
}
loading -> {
}
error != null -> {
}
}
}
}
}
Replica DevTools
Replica has a debug tool integrated to Android Studio. It allows to monitor replicas, their states and observers.
Of course, dark theme is supported.
Learn more
Samples
- Simple sample - shows basic usage of Replica.
- Advanced sample - shows advanced techniques: observing data by dynamic key, optimistic updates, integration with Decompose. Architecture on this sample is based on MobileUp Android Template.
Documentation
Replica Documentation - a website with detailed documentation (WIP).
Contact the author
Artur Artikov
telegram - @aartikov, email - a.artikov@gmail.com
Special thanks
To Egor Belov for the help with Replica DevTools and Unit tests.
License