diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/AppAction.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/AppAction.kt index bee4c95..b65c507 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/AppAction.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/AppAction.kt @@ -24,28 +24,28 @@ import org.unifiedpush.distributor.nextpush.services.RestartWorker import org.unifiedpush.distributor.nextpush.services.StartService import org.unifiedpush.distributor.nextpush.utils.TAG -class AppAction(private val type: Type, private val argv: Map? = null) { - enum class Type { - RestartService, - Logout, - AddChannel, - DisableBatteryOptimisation, - CopyEndpoint, - DeleteRegistration, - LoginSSO, - LoginDirect +class AppAction(private val type: Type) { + sealed class Type { + data object RestartService : Type() + data object Logout : Type() + class AddChannel(val title: String) : Type() + data object DisableBatteryOptimisation : Type() + class CopyEndpoint(val token: String) : Type() + class DeleteRegistration(val registrations: List) : Type() + data object LoginSSO : Type() + class LoginDirect(val username: String, val password: String, val url: String) : Type() } fun handle(context: Context) { when (type) { - Type.LoginSSO -> loginSSO(context) - Type.LoginDirect -> loginDirect(context, argv) - Type.RestartService -> restartService(context) - Type.Logout -> logout(context) - Type.AddChannel -> addChannel(context, argv) - Type.DisableBatteryOptimisation -> disableBatteryOptimisation(context) - Type.CopyEndpoint -> copyEndpoint(context, argv) - Type.DeleteRegistration -> deleteRegistration(context, argv) + is Type.LoginSSO -> loginSSO(context) + is Type.LoginDirect -> loginDirect(context, type) + is Type.RestartService -> restartService(context) + is Type.Logout -> logout(context) + is Type.AddChannel -> addChannel(context, type) + is Type.DisableBatteryOptimisation -> disableBatteryOptimisation(context) + is Type.CopyEndpoint -> copyEndpoint(context, type) + is Type.DeleteRegistration -> deleteRegistration(context, type) } } @@ -54,11 +54,8 @@ class AppAction(private val type: Type, private val argv: Map? = nu UiAction.publish(UiAction.Type.Login) } - private fun loginDirect(context: Context, argv: Map?) { - val username = argv?.get(ARG_USERNAME) as String? ?: return - val password = argv?.get(ARG_PASSWORD) as String? ?: return - val url = argv?.get(ARG_URL) as String? ?: return - AccountFactory.setTypeDirect(context, url, username, password) + private fun loginDirect(context: Context, action: Type.LoginDirect) { + AccountFactory.setTypeDirect(context, action.url, action.username, action.password) UiAction.publish(UiAction.Type.Login) } @@ -80,15 +77,10 @@ class AppAction(private val type: Type, private val argv: Map? = nu UiAction.publish(UiAction.Type.Logout) } - private fun addChannel(context: Context, argv: Map?) { - (argv?.get(ARG_NEW_CHANNEL_TITLE) as String?)?.let { - LocalNotification.createChannel( - context, - it - ) { - Log.d(TAG, "Channel \"$it\" created") - UiAction.publish(UiAction.Type.UpdateRegistrations) - } + private fun addChannel(context: Context, action: Type.AddChannel) { + LocalNotification.createChannel(context, action.title) { + Log.d(TAG, "Channel \"${action.title}\" created") + UiAction.publish(UiAction.Type.UpdateRegistrations) } } @@ -111,31 +103,20 @@ class AppAction(private val type: Type, private val argv: Map? = nu } } - private fun copyEndpoint(context: Context, argv: Map?) { - val token = argv?.get(ARG_TOKEN) as String? ?: return + private fun copyEndpoint(context: Context, action: Type.CopyEndpoint) { val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager val clip: ClipData = ClipData.newPlainText( "Endpoint", - Distributor.getEndpoint(context, token) + Distributor.getEndpoint(context, action.token) ) clipboard.setPrimaryClip(clip) } - private fun deleteRegistration(context: Context, argv: Map?) { - val registrations = argv?.get(ARG_REGISTRATIONS) as List? ?: return - registrations.forEach { + private fun deleteRegistration(context: Context, action: Type.DeleteRegistration) { + action.registrations.forEach { deleteApp(context, it) {} } } - - companion object { - const val ARG_USERNAME = "username" - const val ARG_PASSWORD = "password" - const val ARG_URL = "url" - const val ARG_NEW_CHANNEL_TITLE = "title" - const val ARG_TOKEN = "token" - const val ARG_REGISTRATIONS = "registrations" - } } fun ViewModel.publishAction(action: AppAction) { diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainViewModel.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainViewModel.kt index f7e9c09..cf36c84 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainViewModel.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/MainViewModel.kt @@ -81,12 +81,7 @@ class MainViewModel( viewModelScope.launch { val tokenList = registrationsState.list.filter { it.selected }.map { it.token } publishAction( - AppAction( - AppAction.Type.DeleteRegistration, - mapOf( - AppAction.ARG_REGISTRATIONS to tokenList - ) - ) + AppAction(AppAction.Type.DeleteRegistration(tokenList)) ) registrationsState = RegistrationListState( list = registrationsState.list.filter { diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/ui/AppBarUi.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/ui/AppBarUi.kt index 3e0222e..c975b16 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/ui/AppBarUi.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/ui/AppBarUi.kt @@ -57,7 +57,7 @@ fun AppBarUi(viewModel: ViewModel) { Dropdown( expanded, onRestart = { - viewModel.publishAction(AppAction(AppAction.Type.RestartService, null)) + viewModel.publishAction(AppAction(AppAction.Type.RestartService)) expanded = false }, onLogout = { @@ -76,14 +76,9 @@ fun AppBarUi(viewModel: ViewModel) { onDismissRequest = { showNotificationDialog = false }, - onConfirmation = { + onConfirmation = { title -> viewModel.publishAction( - AppAction( - AppAction.Type.AddChannel, - mapOf( - AppAction.ARG_NEW_CHANNEL_TITLE to it - ) - ) + AppAction(AppAction.Type.AddChannel(title)) ) showNotificationDialog = false } @@ -95,7 +90,7 @@ fun AppBarUi(viewModel: ViewModel) { showLogoutDialog = false }, onConfirmation = { - viewModel.publishAction(AppAction(AppAction.Type.Logout, null)) + viewModel.publishAction(AppAction(AppAction.Type.Logout)) showLogoutDialog = false } ) diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/ui/MainUi.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/ui/MainUi.kt index 00e5739..883194d 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/ui/MainUi.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/ui/MainUi.kt @@ -171,12 +171,7 @@ fun MainUiContent(viewModel: MainViewModel, innerPadding: PaddingValues) { modifier = Modifier .clickable { viewModel.publishAction( - AppAction( - AppAction.Type.CopyEndpoint, - mapOf( - AppAction.ARG_TOKEN to app.token - ) - ) + AppAction(AppAction.Type.CopyEndpoint(app.token)) ) } .align(Alignment.CenterVertically), diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/ui/StartUi.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/ui/StartUi.kt index 378b481..e18b1c1 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/ui/StartUi.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/activities/ui/StartUi.kt @@ -152,11 +152,10 @@ fun StartUi(viewModel: StartViewModel, showManualLogin: Boolean = false) { onClick = { viewModel.publishAction( AppAction( - AppAction.Type.LoginDirect, - mapOf( - AppAction.ARG_USERNAME to usernameValue, - AppAction.ARG_PASSWORD to passwordValue, - AppAction.ARG_URL to urlValue + AppAction.Type.LoginDirect( + usernameValue, + passwordValue, + urlValue ) ) )