Skip to content

Commit

Permalink
Reorganize client sample, remove EventBus
Browse files Browse the repository at this point in the history
  • Loading branch information
p1gp1g committed Dec 11, 2024
1 parent e06e39b commit 1c10979
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 51 deletions.
2 changes: 1 addition & 1 deletion samples/client/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="global.covesa.sdk.client.PushServiceImpl"
<service android:name="global.covesa.sdk.client.push.PushServiceImpl"
android:exported="false">
<intent-filter>
<action android:name="global.covesa.sdk.PUSH_EVENT"/>
Expand Down
26 changes: 0 additions & 26 deletions samples/client/src/main/java/global/covesa/sdk/client/EventBus.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import androidx.activity.compose.setContent
import androidx.lifecycle.viewmodel.compose.viewModel
import global.covesa.sdk.api.client.LightsServiceClient
import global.covesa.sdk.api.client.ServicesCatalogClient
import global.covesa.sdk.client.push.ActionEvent
import global.covesa.sdk.client.ui.MainUi
import global.covesa.sdk.client.ui.theme.CovesaSDKTheme
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch


Expand All @@ -36,7 +38,9 @@ class MainActivity : ComponentActivity() {

private fun subscribeActions() {
CoroutineScope(Dispatchers.IO).launch {
EventBus.subscribe<ActionEvent> { it.handleAction(this@MainActivity) }
ActionEvent.events.collect {
it.handleAction(this@MainActivity)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import androidx.lifecycle.viewModelScope
import global.covesa.sdk.api.client.LightsServiceClient
import global.covesa.sdk.api.client.ServicesCatalogClient
import global.covesa.sdk.api.lights.LightState
import global.covesa.sdk.client.push.ActionEvent
import global.covesa.sdk.client.push.PushServiceImpl
import global.covesa.sdk.client.ui.InstalledService
import global.covesa.sdk.client.ui.InstalledServicesUiState
import global.covesa.sdk.client.ui.LightServiceVersion
Expand All @@ -20,7 +22,7 @@ import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch

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

var pushUiState by mutableStateOf(PushUiState(context))
var pushUiState by mutableStateOf(context?.let { PushUiState(it) } ?: PushUiState())
private set

init {
Expand Down Expand Up @@ -59,7 +61,7 @@ class MainViewModel(
)
}
viewModelScope.launch {
EventBus.subscribe<PushSubscriptionEvent> {
PushServiceImpl.events.collect {
pushUiState = pushUiState.copy(registered = it.registered)
}
}
Expand All @@ -73,19 +75,19 @@ class MainViewModel(

fun sendPushNotification() {
viewModelScope.launch {
EventBus.publish(ActionEvent(ActionEvent.Type.SendNotification))
ActionEvent.emit(ActionEvent(ActionEvent.Type.SendNotification))
}
}

fun registerPushService() {
viewModelScope.launch {
EventBus.publish(ActionEvent(ActionEvent.Type.RegisterPush))
ActionEvent.emit(ActionEvent(ActionEvent.Type.RegisterPush))
}
}

fun unregisterPushService() {
viewModelScope.launch {
EventBus.publish(ActionEvent(ActionEvent.Type.UnregisterPush))
ActionEvent.emit(ActionEvent(ActionEvent.Type.UnregisterPush))
pushUiState = pushUiState.copy(registered = false)
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package global.covesa.sdk.client
package global.covesa.sdk.client.push

import android.app.Activity
import global.covesa.sdk.api.client.push.PushManager
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow

class ActionEvent(private val type: Type) {
enum class Type {
Expand All @@ -28,4 +30,12 @@ class ActionEvent(private val type: Type) {
)
}
}

companion object {
private val _events = MutableSharedFlow<ActionEvent>()
val events = _events.asSharedFlow()
suspend fun emit(event: ActionEvent) {
_events.emit(event)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package global.covesa.sdk.client
package global.covesa.sdk.client.push

import android.content.Context
import android.security.keystore.KeyGenParameterSpec
Expand All @@ -10,7 +10,7 @@ import com.android.volley.VolleyError
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import com.google.crypto.tink.apps.webpush.WebPushHybridEncrypt
import global.covesa.sdk.api.client.push.PushEndpoint
import global.covesa.sdk.api.client.push.data.PushEndpoint
import org.json.JSONObject
import java.net.URL
import java.security.KeyPair
Expand All @@ -22,7 +22,8 @@ import java.security.interfaces.ECPublicKey
import java.security.spec.ECGenParameterSpec

/**
* This class emulates an application server
* This class emulates an application server, this should not be implemented
* by real applications.
*/
class MockApplicationServer(private val context: Context) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package global.covesa.sdk.client
package global.covesa.sdk.client.push

import android.util.Log
import global.covesa.sdk.api.client.push.FailedReason
Expand All @@ -8,12 +8,16 @@ import global.covesa.sdk.api.client.push.PushService
import global.covesa.sdk.client.ui.Notification
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch

class PushServiceImpl: PushService() {
class NewRegistrationState(val registered: Boolean)

override fun onNewEndpoint(endpoint: PushEndpoint, instance: String) {
MockApplicationServer(this).MockApi().storePushEndpoint(endpoint)
publishEvent(true)
updateRegistrationState(true)
}

override fun onMessage(message: PushMessage, instance: String) {
Expand All @@ -22,24 +26,27 @@ class PushServiceImpl: PushService() {
}

override fun onRegistrationFailed(reason: FailedReason, instance: String) {
Log.d(TAG, "Registration failed: $reason")
Notification(this).showNotification("Registration failed", "Can't register to the service: $reason")
}

override fun onUnregistered(instance: String) {
MockApplicationServer(this).MockApi().storePushEndpoint(null)
publishEvent(false)
updateRegistrationState(false)
}

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

private companion object {
const val TAG = "PushServiceImpl"
companion object {
private const val TAG = "PushServiceImpl"
private val _events = MutableSharedFlow<NewRegistrationState>()
val events = _events.asSharedFlow()
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package global.covesa.sdk.client
package global.covesa.sdk.client.push

import android.util.Base64
import com.google.crypto.tink.subtle.EllipticCurves
Expand Down

0 comments on commit 1c10979

Please sign in to comment.