From 77c5bcb5bf204c595d29842e99fda7d235f7fe75 Mon Sep 17 00:00:00 2001 From: sim Date: Tue, 29 Oct 2024 09:28:10 +0000 Subject: [PATCH] Properly update push state and request actions Remove eventBus dependency --- gradle/libs.versions.toml | 2 -- samples/client/build.gradle.kts | 1 - .../global/covesa/sdk/client/ActionEvent.kt | 31 +++++++++++++++++++ .../java/global/covesa/sdk/client/EventBus.kt | 26 ++++++++++++++++ .../global/covesa/sdk/client/MainActivity.kt | 30 ++++++------------ .../global/covesa/sdk/client/MainViewModel.kt | 27 ++++++---------- .../covesa/sdk/client/PushServiceImpl.kt | 17 ++++++++-- .../sdk/client/PushSubscriptionEvent.kt | 5 +-- .../covesa/sdk/client/ui/PushUiState.kt | 9 +++++- 9 files changed, 102 insertions(+), 46 deletions(-) create mode 100644 samples/client/src/main/java/global/covesa/sdk/client/ActionEvent.kt create mode 100644 samples/client/src/main/java/global/covesa/sdk/client/EventBus.kt diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index abe8279..919d959 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -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" @@ -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"} diff --git a/samples/client/build.gradle.kts b/samples/client/build.gradle.kts index 4baab47..7f5d06b 100644 --- a/samples/client/build.gradle.kts +++ b/samples/client/build.gradle.kts @@ -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) diff --git a/samples/client/src/main/java/global/covesa/sdk/client/ActionEvent.kt b/samples/client/src/main/java/global/covesa/sdk/client/ActionEvent.kt new file mode 100644 index 0000000..281d927 --- /dev/null +++ b/samples/client/src/main/java/global/covesa/sdk/client/ActionEvent.kt @@ -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() + ) + } + } +} \ No newline at end of file diff --git a/samples/client/src/main/java/global/covesa/sdk/client/EventBus.kt b/samples/client/src/main/java/global/covesa/sdk/client/EventBus.kt new file mode 100644 index 0000000..1b67421 --- /dev/null +++ b/samples/client/src/main/java/global/covesa/sdk/client/EventBus.kt @@ -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() + val events = _events.asSharedFlow() + + suspend fun publish(event: Any) { + _events.emit(event) + } + + suspend inline fun subscribe(crossinline onEvent: (T) -> Unit) { + events.filterIsInstance() + .collectLatest { event -> + coroutineContext.ensureActive() + onEvent(event) + } + } +} \ No newline at end of file diff --git a/samples/client/src/main/java/global/covesa/sdk/client/MainActivity.kt b/samples/client/src/main/java/global/covesa/sdk/client/MainActivity.kt index 0d8dfff..ae2af4c 100644 --- a/samples/client/src/main/java/global/covesa/sdk/client/MainActivity.kt +++ b/samples/client/src/main/java/global/covesa/sdk/client/MainActivity.kt @@ -8,20 +8,18 @@ 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: MainViewModel = viewModel { MainViewModel( context = this@MainActivity, lightsServiceClient = LightsServiceClient(this@MainActivity), @@ -30,23 +28,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 { it.handleAction(this@MainActivity) } + } } } diff --git a/samples/client/src/main/java/global/covesa/sdk/client/MainViewModel.kt b/samples/client/src/main/java/global/covesa/sdk/client/MainViewModel.kt index 6855a03..e6d580b 100644 --- a/samples/client/src/main/java/global/covesa/sdk/client/MainViewModel.kt +++ b/samples/client/src/main/java/global/covesa/sdk/client/MainViewModel.kt @@ -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() { @@ -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 { @@ -59,8 +58,11 @@ class MainViewModel( } ) } - - refreshPushRegistration() + viewModelScope.launch { + EventBus.subscribe { + pushUiState = pushUiState.copy(registered = it.registered) + } + } } fun setInternalLight(lightState: LightState) { @@ -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) - } - } } diff --git a/samples/client/src/main/java/global/covesa/sdk/client/PushServiceImpl.kt b/samples/client/src/main/java/global/covesa/sdk/client/PushServiceImpl.kt index 89b2286..4357a3b 100644 --- a/samples/client/src/main/java/global/covesa/sdk/client/PushServiceImpl.kt +++ b/samples/client/src/main/java/global/covesa/sdk/client/PushServiceImpl.kt @@ -4,7 +4,9 @@ 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 @@ -12,7 +14,7 @@ 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) { @@ -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 { diff --git a/samples/client/src/main/java/global/covesa/sdk/client/PushSubscriptionEvent.kt b/samples/client/src/main/java/global/covesa/sdk/client/PushSubscriptionEvent.kt index ae7edc2..591bb83 100644 --- a/samples/client/src/main/java/global/covesa/sdk/client/PushSubscriptionEvent.kt +++ b/samples/client/src/main/java/global/covesa/sdk/client/PushSubscriptionEvent.kt @@ -1,4 +1,5 @@ package global.covesa.sdk.client -class PushSubscriptionEvent { -} \ No newline at end of file +data class PushSubscriptionEvent( + val registered: Boolean +) \ No newline at end of file diff --git a/samples/client/src/main/java/global/covesa/sdk/client/ui/PushUiState.kt b/samples/client/src/main/java/global/covesa/sdk/client/ui/PushUiState.kt index f7b3dcc..9f1b061 100644 --- a/samples/client/src/main/java/global/covesa/sdk/client/ui/PushUiState.kt +++ b/samples/client/src/main/java/global/covesa/sdk/client/ui/PushUiState.kt @@ -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 -) \ No newline at end of file +) { + constructor(context: Context) : this( + registered = UnifiedPush.getAckDistributor(context) != null + ) +} \ No newline at end of file