Skip to content

Commit

Permalink
Pass id instead of serialized model
Browse files Browse the repository at this point in the history
  • Loading branch information
massivemadness committed Feb 5, 2025
1 parent fa04abf commit 2e68f8b
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 195 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fun Fragment.sendFragmentResult(resultKey: String, vararg pairs: Pair<String, An
)
}

fun Fragment.sendFragmentResult(resultKey: String, bundle: Bundle) {
fun Fragment.sendFragmentResult(resultKey: String, bundle: Bundle = Bundle.EMPTY) {
requireActivity().supportFragmentManager.setFragmentResult(resultKey, bundle)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,13 @@

package com.blacksquircle.ui.feature.servers.data.mapper

import android.os.Bundle
import com.blacksquircle.ui.core.storage.database.entity.server.ServerEntity
import com.blacksquircle.ui.filesystem.base.model.AuthMethod
import com.blacksquircle.ui.filesystem.base.model.FileServer
import com.blacksquircle.ui.filesystem.base.model.ServerConfig

internal object ServerMapper {

private const val KEY_UUID = "uuid"
private const val KEY_SCHEME = "scheme"
private const val KEY_NAME = "name"
private const val KEY_ADDRESS = "address"
private const val KEY_PORT = "port"
private const val KEY_INITIAL_DIR = "initial_dir"
private const val KEY_AUTH_METHOD = "auth_method"
private const val KEY_USERNAME = "username"
private const val KEY_PASSWORD = "password"
private const val KEY_PRIVATE_KEY = "private_key"
private const val KEY_PASSPHRASE = "passphrase"

fun toModel(serverEntity: ServerEntity): ServerConfig {
return ServerConfig(
uuid = serverEntity.uuid,
Expand Down Expand Up @@ -67,36 +54,4 @@ internal object ServerMapper {
passphrase = serverConfig.passphrase,
)
}

fun toBundle(serverConfig: ServerConfig): Bundle {
return Bundle().apply {
putString(KEY_UUID, serverConfig.uuid)
putString(KEY_SCHEME, serverConfig.scheme.value)
putString(KEY_NAME, serverConfig.name)
putString(KEY_ADDRESS, serverConfig.address)
putInt(KEY_PORT, serverConfig.port)
putString(KEY_INITIAL_DIR, serverConfig.initialDir)
putString(KEY_AUTH_METHOD, serverConfig.authMethod.value)
putString(KEY_USERNAME, serverConfig.username)
putString(KEY_PASSWORD, serverConfig.password)
putString(KEY_PRIVATE_KEY, serverConfig.privateKey)
putString(KEY_PASSPHRASE, serverConfig.passphrase)
}
}

fun fromBundle(bundle: Bundle): ServerConfig {
return ServerConfig(
uuid = bundle.getString(KEY_UUID).orEmpty(),
scheme = FileServer.of(bundle.getString(KEY_SCHEME).orEmpty()),
name = bundle.getString(KEY_NAME).orEmpty(),
address = bundle.getString(KEY_ADDRESS).orEmpty(),
port = bundle.getInt(KEY_PORT),
initialDir = bundle.getString(KEY_INITIAL_DIR).orEmpty(),
authMethod = AuthMethod.of(bundle.getString(KEY_AUTH_METHOD).orEmpty()),
username = bundle.getString(KEY_USERNAME).orEmpty(),
password = bundle.getString(KEY_PASSWORD),
privateKey = bundle.getString(KEY_PRIVATE_KEY),
passphrase = bundle.getString(KEY_PASSPHRASE),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import com.blacksquircle.ui.core.contract.OpenFileContract
import com.blacksquircle.ui.core.extensions.extractFilePath
import com.blacksquircle.ui.core.extensions.sendFragmentResult
import com.blacksquircle.ui.ds.SquircleTheme
import com.blacksquircle.ui.feature.servers.data.mapper.ServerMapper
import com.blacksquircle.ui.feature.servers.ui.fragment.CloudFragment
import com.blacksquircle.ui.feature.servers.ui.navigation.ServerViewEvent
import com.blacksquircle.ui.feature.servers.ui.viewmodel.ServerViewModel
Expand All @@ -50,7 +49,7 @@ internal class ServerDialog : DialogFragment() {
private val viewModel by viewModels<ServerViewModel>(
extrasProducer = {
defaultViewModelCreationExtras.withCreationCallback<ServerViewModel.Factory> { factory ->
factory.create(serverData = navArgs.data)
factory.create(serverId = navArgs.id)
}
}
)
Expand Down Expand Up @@ -92,17 +91,11 @@ internal class ServerDialog : DialogFragment() {
.onEach { event ->
when (event) {
is ServerViewEvent.SendSaveResult -> {
sendFragmentResult(
resultKey = CloudFragment.KEY_SAVE,
bundle = ServerMapper.toBundle(event.serverConfig)
)
sendFragmentResult(CloudFragment.KEY_SAVE)
navController.popBackStack()
}
is ServerViewEvent.SendDeleteResult -> {
sendFragmentResult(
resultKey = CloudFragment.KEY_DELETE,
bundle = ServerMapper.toBundle(event.serverConfig)
)
sendFragmentResult(CloudFragment.KEY_DELETE)
navController.popBackStack()
}
is ServerViewEvent.ChooseFile -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import com.blacksquircle.ui.filesystem.base.model.ServerConfig
@Immutable
internal data class ServerViewState(
val isEditMode: Boolean = false,
val uuid: String = "",
val scheme: FileServer = FileServer.FTP,
val name: String = "",
val address: String = "",
Expand All @@ -44,41 +43,6 @@ internal data class ServerViewState(
val invalidAddress: Boolean = false,
) : ViewState() {

fun toServerConfig(): ServerConfig {
return ServerConfig(
uuid = uuid,
scheme = scheme,
name = name,
address = address,
port = port.toIntOrNull() ?: when (scheme) {
FileServer.FTP,
FileServer.FTPS,
FileServer.FTPES -> DEFAULT_FTP_PORT
FileServer.SFTP -> DEFAULT_SFTP_PORT
},
initialDir = initialDir,
authMethod = authMethod,
username = username,
password = if (
authMethod == AuthMethod.PASSWORD &&
passwordAction == PasswordAction.SAVE_PASSWORD
) {
password
} else {
null
},
privateKey = if (authMethod == AuthMethod.KEY) privateKey else null,
passphrase = if (
authMethod == AuthMethod.KEY &&
passphraseAction == PassphraseAction.SAVE_PASSPHRASE
) {
passphrase
} else {
null
},
)
}

companion object {
const val DEFAULT_FTP_PORT = 21
const val DEFAULT_SFTP_PORT = 22
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,11 @@ internal class CloudFragment : Fragment() {
}
.launchIn(viewLifecycleOwner.lifecycleScope)

observeFragmentResult(KEY_SAVE) { bundle ->
val serverConfig = ServerMapper.fromBundle(bundle)
viewModel.onSaveClicked(serverConfig)
observeFragmentResult(KEY_SAVE) {
viewModel.loadServers()
}
observeFragmentResult(KEY_DELETE) { bundle ->
val serverConfig = ServerMapper.fromBundle(bundle)
viewModel.onDeleteClicked(serverConfig)
observeFragmentResult(KEY_DELETE) {
viewModel.loadServers()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
package com.blacksquircle.ui.feature.servers.ui.navigation

import com.blacksquircle.ui.core.mvi.ViewEvent
import com.blacksquircle.ui.filesystem.base.model.ServerConfig

internal sealed class ServerViewEvent : ViewEvent() {

class SendSaveResult(val serverConfig: ServerConfig) : ServerViewEvent()
class SendDeleteResult(val serverConfig: ServerConfig) : ServerViewEvent()
data object SendSaveResult : ServerViewEvent()
data object SendDeleteResult : ServerViewEvent()
data object ChooseFile : ServerViewEvent()
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@

package com.blacksquircle.ui.feature.servers.ui.navigation

import com.blacksquircle.ui.core.extensions.toJsonEncoded
import com.blacksquircle.ui.core.navigation.Screen
import com.blacksquircle.ui.filesystem.base.model.ServerConfig
import com.google.gson.Gson

internal sealed class ServersScreen(route: String) : Screen<String>(route) {

class EditServer(serverConfig: ServerConfig) : ServersScreen(
route = "blacksquircle://settings/cloud/edit?data=${serverConfig.toJsonEncoded()}",
class EditServer(serverId: String) : ServersScreen(
route = "blacksquircle://settings/cloud/edit/$serverId",
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject
import kotlin.coroutines.cancellation.CancellationException

@HiltViewModel
internal class CloudViewModel @Inject constructor(
Expand All @@ -54,7 +55,7 @@ internal class CloudViewModel @Inject constructor(

fun onServerClicked(serverConfig: ServerConfig) {
viewModelScope.launch {
val screen = ServersScreen.EditServer(serverConfig)
val screen = ServersScreen.EditServer(serverConfig.uuid)
_viewEvent.send(ViewEvent.Navigation(screen))
}
}
Expand All @@ -66,34 +67,17 @@ internal class CloudViewModel @Inject constructor(
}
}

fun onSaveClicked(serverConfig: ServerConfig) {
fun loadServers() {
viewModelScope.launch {
try {
serversRepository.upsertServer(serverConfig)
loadServers()
val servers = serversRepository.loadServers()
_viewState.value = CloudViewState(servers)
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
Timber.e(e, e.message)
_viewEvent.send(ViewEvent.Toast(e.message.orEmpty()))
}
}
}

fun onDeleteClicked(serverConfig: ServerConfig) {
viewModelScope.launch {
try {
serversRepository.deleteServer(serverConfig)
loadServers()
} catch (e: Exception) {
Timber.e(e, e.message)
_viewEvent.send(ViewEvent.Toast(e.message.orEmpty()))
}
}
}

private fun loadServers() {
viewModelScope.launch {
val servers = serversRepository.loadServers()
_viewState.value = CloudViewState(servers)
}
}
}
Loading

0 comments on commit 2e68f8b

Please sign in to comment.