Support changing download directory for internal server

This commit is contained in:
Syer10
2022-05-07 11:39:35 -04:00
parent 676d691814
commit bcf51dc9bc
5 changed files with 72 additions and 2 deletions

View File

@@ -48,6 +48,10 @@ class ServerHostPreferences(private val preferenceStore: PreferenceStore) {
fun systemTrayEnabled(): Preference<Boolean> {
return systemTrayEnabled.preference()
}
private val downloadPath = ServerHostPreference.DownloadPath(preferenceStore)
fun downloadPath(): Preference<String> {
return downloadPath.preference()
}
// WebUI
private val webUIEnabled = ServerHostPreference.WebUIEnabled(preferenceStore)
@@ -82,6 +86,7 @@ class ServerHostPreferences(private val preferenceStore: PreferenceStore) {
socksProxyPort,
debugLogsEnabled,
systemTrayEnabled,
downloadPath,
webUIEnabled,
openInBrowserEnabled,
basicAuthEnabled,

View File

@@ -106,6 +106,11 @@ sealed class ServerHostPreference<T : Any> {
false,
true
)
class DownloadPath(preferenceStore: PreferenceStore) : StringServerHostPreference(
preferenceStore,
"downloadsPath",
""
)
// WebUI
class WebUIEnabled(preferenceStore: PreferenceStore) : BooleanServerHostPreference(

View File

@@ -263,6 +263,9 @@
<string name="host_debug_logging_sub">Output debug logs from the server to JUI</string>
<string name="host_system_tray">Server system tray icon</string>
<string name="host_system_tray_sub">Use the system tray icon to view whether the server is running</string>
<string name="host_download_path">Download path</string>
<string name="host_download_path_sub">Current download path: %1$s</string>
<string name="host_download_path_sub_empty">Using default download path</string>
<string name="host_webui">Server WebUI</string>
<string name="host_webui_sub">Whether the server\'s default WebUI is enabled, makes you able to use Tachidesk in your browser</string>
<string name="host_open_in_browser">Open Server WebUI on startup</string>

View File

@@ -23,6 +23,7 @@ import ca.gosyer.jui.i18n.MR
import ca.gosyer.jui.ui.base.prefs.EditTextPreference
import ca.gosyer.jui.ui.base.prefs.PreferenceRow
import ca.gosyer.jui.ui.base.prefs.SwitchPreference
import ca.gosyer.jui.ui.util.system.folderPicker
import ca.gosyer.jui.uicore.prefs.PreferenceMutableStateFlow
import ca.gosyer.jui.uicore.prefs.asStateIn
import ca.gosyer.jui.uicore.prefs.asStringStateIn
@@ -60,6 +61,7 @@ actual fun getServerHostItems(viewModel: @Composable () -> SettingsServerHostVie
socksProxyPort = serverVm.socksProxyPort,
debugLogsEnabled = serverVm.debugLogsEnabled,
systemTrayEnabled = serverVm.systemTrayEnabled,
downloadPath = serverVm.downloadPath,
webUIEnabled = serverVm.webUIEnabled,
openInBrowserEnabled = serverVm.openInBrowserEnabled,
basicAuthEnabled = serverVm.basicAuthEnabled,
@@ -87,6 +89,7 @@ actual class SettingsServerHostViewModel @Inject constructor(
// Misc
val debugLogsEnabled = serverHostPreferences.debugLogsEnabled().asStateIn(scope)
val systemTrayEnabled = serverHostPreferences.systemTrayEnabled().asStateIn(scope)
val downloadPath = serverHostPreferences.downloadPath().asStateIn(scope)
// WebUI
val webUIEnabled = serverHostPreferences.webUIEnabled().asStateIn(scope)
@@ -141,6 +144,7 @@ fun LazyListScope.ServerHostItems(
socksProxyPort: PreferenceMutableStateFlow<String>,
debugLogsEnabled: PreferenceMutableStateFlow<Boolean>,
systemTrayEnabled: PreferenceMutableStateFlow<Boolean>,
downloadPath: PreferenceMutableStateFlow<String>,
webUIEnabled: PreferenceMutableStateFlow<Boolean>,
openInBrowserEnabled: PreferenceMutableStateFlow<Boolean>,
basicAuthEnabled: PreferenceMutableStateFlow<Boolean>,
@@ -217,6 +221,27 @@ fun LazyListScope.ServerHostItems(
changeListener = serverSettingChanged
)
}
item {
val downloadPathValue by downloadPath.collectAsState()
PreferenceRow(
title = stringResource(MR.strings.host_download_path),
subtitle = if (downloadPathValue.isEmpty()) {
stringResource(MR.strings.host_download_path_sub_empty)
} else {
stringResource(MR.strings.host_download_path_sub, downloadPathValue)
},
onClick = {
folderPicker {
downloadPath.value = it.toString()
serverSettingChanged()
}
},
onLongClick = {
downloadPath.value = ""
serverSettingChanged()
}
)
}
item {
SwitchPreference(
preference = webUIEnabled,

View File

@@ -19,7 +19,29 @@ fun filePicker(
onCancel: () -> Unit = {},
onError: () -> Unit = {},
onApprove: (Path) -> Unit
) = fileChooser(false, onCancel, onError, onApprove, extensions = extensions)
) = fileChooser(
saving = false,
selectFolders = false,
selectFiles = true,
onCancel = onCancel,
onError = onError,
onApprove = onApprove,
defaultFileName = "",
extensions = extensions
)
fun folderPicker(
onCancel: () -> Unit = {},
onError: () -> Unit = {},
onApprove: (Path) -> Unit
) = fileChooser(
saving = false,
selectFolders = true,
selectFiles = false,
onCancel = onCancel,
onError = onError,
onApprove = onApprove
)
fun fileSaver(
defaultFileName: String,
@@ -28,6 +50,8 @@ fun fileSaver(
onError: () -> Unit = {},
onApprove: (Path) -> Unit
) = fileChooser(
true,
false,
true,
onCancel,
onError,
@@ -45,8 +69,10 @@ fun fileSaver(
* @param onApprove the listener that is called when picking a file is completed
*/
@OptIn(DelicateCoroutinesApi::class)
internal fun fileChooser(
private fun fileChooser(
saving: Boolean = false,
selectFolders: Boolean = false,
selectFiles: Boolean = true,
onCancel: () -> Unit = {},
onError: () -> Unit = {},
onApprove: (Path) -> Unit,
@@ -63,6 +89,12 @@ internal fun fileChooser(
if (saving) {
selectedFile = Path(defaultFileName).toFile()
}
fileSelectionMode = when {
selectFiles && selectFolders -> JFileChooser.FILES_AND_DIRECTORIES
selectFiles -> JFileChooser.FILES_ONLY
selectFolders -> JFileChooser.DIRECTORIES_ONLY
else -> fileSelectionMode
}
}
val result = fileChooser.let {