Use sealed class for AppAction type

This commit is contained in:
sim 2024-12-03 08:43:39 +00:00
parent 9c5bb9c818
commit 5cc002475a
5 changed files with 38 additions and 73 deletions

View File

@ -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<String, Any>? = 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<String>) : 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<String, Any>? = nu
UiAction.publish(UiAction.Type.Login)
}
private fun loginDirect(context: Context, argv: Map<String, Any>?) {
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<String, Any>? = nu
UiAction.publish(UiAction.Type.Logout)
}
private fun addChannel(context: Context, argv: Map<String, Any>?) {
(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<String, Any>? = nu
}
}
private fun copyEndpoint(context: Context, argv: Map<String, Any>?) {
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<String, Any>?) {
val registrations = argv?.get(ARG_REGISTRATIONS) as List<String>? ?: 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) {

View File

@ -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 {

View File

@ -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
}
)

View File

@ -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),

View File

@ -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
)
)
)