Skip to content

Commit

Permalink
Properly update push state and request actions
Browse files Browse the repository at this point in the history
Remove eventBus dependency
  • Loading branch information
p1gp1g committed Oct 29, 2024
1 parent eaa4af1 commit 7eecbb9
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 47 deletions.
2 changes: 0 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ lifecycleRuntimeKtx = "2.7.0"
activityCompose = "1.8.2"
composeBom = "2024.02.02"
unifiedpush = "3.0.0-rc2"
eventbus = "3.3.1"
volley = "1.2.1"
tinkWebpush = "1.11.0"

Expand All @@ -32,6 +31,5 @@ kotlinGradlePlugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", ver
kotlinxCollectionsImmutable = { module = "org.jetbrains.kotlinx:kotlinx-collections-immutable", version.ref = "collectionsImmutable" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
unifiedpush = { group = "org.unifiedpush.android", name = "connector", version.ref = "unifiedpush"}
eventbus = { group = "org.greenrobot", name = "eventbus", version.ref = "eventbus" }
volley = { group = "com.android.volley", name = "volley", version.ref = "volley" }
tink-webpush = { group = "com.google.crypto.tink", name = "apps-webpush", version.ref = "tinkWebpush"}
1 change: 0 additions & 1 deletion samples/client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ dependencies {
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.material3)
implementation(libs.kotlinxCollectionsImmutable)
implementation(libs.eventbus)
implementation(libs.volley)
implementation(libs.tink.webpush)
testImplementation(libs.junit)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package global.covesa.sdk.client

import android.app.Activity
import org.unifiedpush.android.connector.UnifiedPush

class ActionEvent(private val type: Type) {
enum class Type {
RegisterPush,
UnregisterPush,
SendNotification,
}

fun handleAction(activity: Activity) {
when(type) {
Type.RegisterPush -> registerPush(activity)
Type.UnregisterPush -> UnifiedPush.unregisterApp(activity)
Type.SendNotification -> MockApplicationServer(activity).MockApi().sendNotification()
}
}

private fun registerPush(activity: Activity) {
UnifiedPush.tryUseCurrentOrDefaultDistributor(
activity
) {
UnifiedPush.registerApp(
activity,
vapid = MockApplicationServer(activity).MockApi().getVapidPubKey()
)
}
}
}
26 changes: 26 additions & 0 deletions samples/client/src/main/java/global/covesa/sdk/client/EventBus.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package global.covesa.sdk.client

import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.filterIsInstance
import kotlin.coroutines.coroutineContext


object EventBus {
private val _events = MutableSharedFlow<Any>()
val events = _events.asSharedFlow()

suspend fun publish(event: Any) {
_events.emit(event)
}

suspend inline fun <reified T> subscribe(crossinline onEvent: (T) -> Unit) {
events.filterIsInstance<T>()
.collectLatest { event ->
coroutineContext.ensureActive()
onEvent(event)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,17 @@ import global.covesa.sdk.api.client.LightsServiceClient
import global.covesa.sdk.api.client.ServicesCatalogClient
import global.covesa.sdk.client.ui.MainUi
import global.covesa.sdk.client.ui.theme.CovesaSDKTheme
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch


class MainActivity : ComponentActivity() {

private var viewModel: MainViewModel? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setContent {
viewModel = viewModel {
val viewModel = viewModel {
MainViewModel(
context = this@MainActivity,
lightsServiceClient = LightsServiceClient(this@MainActivity),
Expand All @@ -30,23 +27,15 @@ class MainActivity : ComponentActivity() {
}

CovesaSDKTheme {
MainUi(viewModel!!)
MainUi(viewModel)
}
}
subscribeActions()
}

@Subscribe(threadMode = ThreadMode.MAIN)
fun onMessageEvent(event: PushSubscriptionEvent?) {
viewModel?.refreshPushRegistration()
}

override fun onStart() {
super.onStart()
EventBus.getDefault().register(this);
}

override fun onStop() {
super.onStop()
EventBus.getDefault().unregister(this);
private fun subscribeActions() {
CoroutineScope(Dispatchers.IO).launch {
EventBus.subscribe<ActionEvent> { it.handleAction(this@MainActivity) }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import org.unifiedpush.android.connector.UnifiedPush

class MainViewModel(
private val context: Context,
context: Context,
private val lightsServiceClient: LightsServiceClient,
private val servicesCatalogClient: ServicesCatalogClient
) : ViewModel() {
Expand All @@ -31,7 +30,7 @@ class MainViewModel(
var installedServicesUiState by mutableStateOf(InstalledServicesUiState())
private set

var pushUiState by mutableStateOf(PushUiState())
var pushUiState by mutableStateOf(PushUiState(context))
private set

init {
Expand Down Expand Up @@ -59,8 +58,11 @@ class MainViewModel(
}
)
}

refreshPushRegistration()
viewModelScope.launch {
EventBus.subscribe<PushSubscriptionEvent> {
pushUiState = pushUiState.copy(registered = it.registered)
}
}
}

fun setInternalLight(lightState: LightState) {
Expand All @@ -71,29 +73,20 @@ class MainViewModel(

fun sendPushNotification() {
viewModelScope.launch {
MockApplicationServer(context).MockApi().sendNotification()
EventBus.publish(ActionEvent(ActionEvent.Type.SendNotification))
}
}

fun registerPushService() {
viewModelScope.launch {
UnifiedPush.tryUseDefaultDistributor(context) {
UnifiedPush.registerApp(context, vapid = MockApplicationServer(context).MockApi().getVapidPubKey())
}
EventBus.publish(ActionEvent(ActionEvent.Type.RegisterPush))
}
}

fun unregisterPushService() {
viewModelScope.launch {
UnifiedPush.unregisterApp(context)
EventBus.publish(ActionEvent(ActionEvent.Type.UnregisterPush))
pushUiState = pushUiState.copy(registered = false)
}
}

fun refreshPushRegistration() {
viewModelScope.launch {
val registered = UnifiedPush.getAckDistributor(context) != null
pushUiState = pushUiState.copy(registered = registered)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import android.content.Context
import android.util.Log
import global.covesa.sdk.api.client.PushService
import global.covesa.sdk.client.ui.Notification
import org.greenrobot.eventbus.EventBus
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.unifiedpush.android.connector.FailedReason
import org.unifiedpush.android.connector.data.PushEndpoint
import org.unifiedpush.android.connector.data.PushMessage

class PushServiceImpl: PushService() {
override fun onNewEndpoint(context: Context, endpoint: PushEndpoint, instance: String) {
MockApplicationServer(context).MockApi().storePushEndpoint(endpoint)
EventBus.getDefault().post(PushSubscriptionEvent())
publishEvent(true)
}

override fun onMessage(context: Context, message: PushMessage, instance: String) {
Expand All @@ -26,7 +28,16 @@ class PushServiceImpl: PushService() {

override fun onUnregistered(context: Context, instance: String) {
MockApplicationServer(context).MockApi().storePushEndpoint(null)
EventBus.getDefault().post(PushSubscriptionEvent())
publishEvent(false)
}

/**
* Update the UI
*/
private fun publishEvent(registered: Boolean) {
CoroutineScope(Dispatchers.IO).launch {
EventBus.publish(PushSubscriptionEvent(registered))
}
}

private companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package global.covesa.sdk.client

class PushSubscriptionEvent {
}
data class PushSubscriptionEvent(
val registered: Boolean
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package global.covesa.sdk.client.ui

import android.content.Context
import org.unifiedpush.android.connector.UnifiedPush

data class PushUiState (
val registered: Boolean = false
)
) {
constructor(context: Context) : this(
registered = UnifiedPush.getAckDistributor(context) != null
)
}

0 comments on commit 7eecbb9

Please sign in to comment.