Skip to content

Reactive Plugin - Android SampleApp

Plugins relationship

The foundations of PlatformHub regarding its use lie in its publicly exposed Plugin interfaces as well as the MessageSender.

It is normal for plugins to only partially implement the interfaces they need, as they will not be concerned with the whole range.

In this article, we showcase some examples of how the Reactive Plugin and the Reachability Plugin are connected each other.


The Reactive Plugin

The following illustrates how we use an EventHandler to manage the subscriptions.

kotlin
override fun subscribe(subscription: SharedFlow<JSONObject>, eventStream: BaseMessage.EventStream) {
    subscription.onEach { result ->
        withContext(Dispatchers.Main) {
            val handler = eventHandler[Events.valueOf(eventStream.messageName)]
            handler?.onEvent(
                payload = result,
                context = context,
                store = if (store.initialised) store else null
            )
        }
    }.launchIn(coroutineScope)
}
kotlin
internal class ConnectivityStatusEventHandler @Inject constructor() : EventHandler() {

    override fun <GROUP : StateGroup<*>, STORE : Store<GROUP>> onEvent(
        payload: JSONObject,
        context: Context,
        store: STORE?
    ) {
        val type = ConnectivityStatus::class.javaObjectType
        val status = getPayloadItem<ConnectivityStatus>(payload, type)
        store?.dispatch(ConnectivityStatusAction.Changed(status.status))
    }
}

The following illustrates how we use an EventHandler to manage the receivers.

kotlin
override fun onReceive(message: BaseMessage.NavigationQuery) = eventHandler[message.messageName]
    .onEvent(
        payload = JSONObject("{}"),
        context = context,
        store = if (store.initialised) store else null
    )
kotlin
internal class ReactivePluginScreenEventHandler @Inject constructor() : EventHandler() {

    override fun <GROUP : StateGroup<*>, STORE : Store<GROUP>> onEvent(
        payload: JSONObject,
        context: Context,
        store: STORE?
    ) {
        context.startActivity(Intent(context, MainActivity::class.java).apply {
            addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
            addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
            addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        })
    }
}

The following illustrates how we send queries to other plugins using the MessageSender.

kotlin
fun send(query: BaseMessage.Query): JSONObject = messageSender.send(query, this)
kotlin
val message = ReactivePlugin.Queries.FetchAccountList
plugin.send(BaseMessage.Query(message.name, pluginName = DOMAIN_MODEL_PLUGIN_NAME))

The Reachability Plugin

The following illustrates how the Reachability Plugin publishes a SharedFlow where the Reactive Plugin will subscribe.

kotlin
override fun publish(eventStream: BaseMessage.EventStream): SharedFlow<EventStreamOutput> {
    return if (eventStream.messageName == ConnectivityStatusChanged.name) {
        connectivityStatusSharedFlow
    } else {
        MutableSharedFlow()
    }
}
kotlin
ConnectivityMonitor.observeConnectionStatus(context).collect { status ->
    Timber.tag("ConnectivityStatus").d(status.status.name)
    connectivityStatusSharedFlow.emit(Result.success(JSONObject(objectToJson(status))))
}