Across modules data transfer with sticky data, scope, lifecycle aware...
- Collect and emit data across modules in json format
- Collect and emit data to specific scope
- Collect emitted data with lifecycle-aware
- Support sticky data
I originally used Any
instead of Json.
Then I have a problem: what if feature A wants to transfer an internal object to feature B but feature B doesn't know about model of feature A?
To avoid duplicate code or need to add a shared model between two modules, I decided to convert the emitted data to json.
So feature B will receive model of feature A in json format, it can parse into a new model or do whatever it wants with the String.
fun emit(
data: Any,
scope: OneDataTransferScope = OneDataTransferScope.Application,
isSticky: Boolean = false
)
data
: data to transferscope
: scope of emitting dataisSticky
: new collector should receive this data or not
Sample:
// emit a string within global scope
OneDataTransfer.get().emit("Value from feature B activity")
// emit an sticky object within FeatureB scope
OneDataTransfer.get()
.emit(TestModel("Sticky event from main act"), OneDataTransferScope.Custom("FeatureB"), true)
fun collect(
collector: Collector,
scope: OneDataTransferScope = OneDataTransferScope.Application,
strategy: OneDataTransferStrategy = OneDataTransferStrategy.Always
)
collector
: the collector (listener)scope
: collect emitted values in this scope onlystrategy
: how we should collect the emitted valuesAlways
: always collect the valuesLifecycleAware
: respect lifecycle to collect values: start collecting when onStart(), stop collecting when onPause(), remove collector when onDestroy()
Sample:
OneDataTransfer.get().collect(object : OneDataTransfer.Collector() {
override fun onDataChanged(data: String) {
// got da data in json format!
}
}, OneDataTransferScope.Custom("FeatureB"), OneDataTransferStrategy.LifecycleAware(this))
If you don't use Lifecycle-Aware Strategy, you should remove collector by manual:
OneDataTransfer.get().removeCollector(collector)