save categories size

This commit is contained in:
AmirHossein Abdolmotallebi 2024-09-12 08:18:58 +03:30
parent f10cc43ce0
commit 04a2e84486
3 changed files with 40 additions and 15 deletions

View File

@ -18,7 +18,9 @@ import com.abdownloadmanager.desktop.utils.action.simpleAction
import com.abdownloadmanager.desktop.utils.mvi.ContainsEffects
import com.abdownloadmanager.desktop.utils.mvi.supportEffects
import androidx.compose.runtime.*
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.coerceIn
import androidx.compose.ui.unit.dp
import com.arkivanov.decompose.ComponentContext
import ir.amirab.downloader.downloaditem.DownloadCredentials
@ -49,7 +51,7 @@ sealed interface HomeEffects {
data object BringToFront : HomeEffects
data class DeleteItems(
val list: List<Long>
val list: List<Long>,
) : HomeEffects
}
@ -272,9 +274,22 @@ class HomeComponent(
_windowSize.update { dpSize }
}
private val _categoriesWidth = homePageStateToPersist.mapTwoWayStateFlow(
map = {
it.categoriesWidth.dp.coerceIn(CATEGORIES_SIZE_RANGE)
},
unMap = {
copy(categoriesWidth = it.coerceIn(CATEGORIES_SIZE_RANGE).value)
}
)
val categoriesWidth = _categoriesWidth.asStateFlow()
fun setCategoriesWidth(updater: (Dp) -> Dp) {
_categoriesWidth.value = updater(_categoriesWidth.value)
}
private fun requestDelete(
downloadList: List<Long>
downloadList: List<Long>,
) {
sendEffect(HomeEffects.DeleteItems(downloadList))
}
@ -645,4 +660,8 @@ class HomeComponent(
"DELETE" to downloadActions.deleteAction
"ctrl I" to downloadActions.openDownloadDialogAction
}
companion object {
val CATEGORIES_SIZE_RANGE = 0.dp..500.dp
}
}

View File

@ -22,7 +22,6 @@ import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import com.abdownloadmanager.desktop.ui.widget.Text
import androidx.compose.runtime.*
import com.abdownloadmanager.desktop.utils.externaldraggable.*
import com.abdownloadmanager.desktop.utils.externaldraggable.onExternalDrag
import androidx.compose.ui.*
import androidx.compose.ui.draw.alpha
@ -111,21 +110,24 @@ fun HomePage(component: HomeComponent) {
Spacer(Modifier.height(4.dp))
TopBar(component)
Spacer(Modifier.height(6.dp))
Spacer(Modifier.fillMaxWidth()
.height(1.dp)
.background(myColors.surface)
Spacer(
Modifier.fillMaxWidth()
.height(1.dp)
.background(myColors.surface)
)
Row() {
var width by remember { mutableStateOf(170.dp) }
Categories(Modifier.padding(top = 8.dp)
.width(width), component
val categoriesWidth by component.categoriesWidth.collectAsState()
Categories(
Modifier.padding(top = 8.dp)
.width(categoriesWidth), component
)
Spacer(Modifier.size(8.dp))
//split pane
Handle(Modifier.width(5.dp)
.fillMaxHeight()
) {
width += it
Handle(
Modifier.width(5.dp)
.fillMaxHeight()
) { delta ->
component.setCategoriesWidth { it + delta }
}
Column(Modifier.weight(1f)) {
Row(
@ -262,7 +264,7 @@ private fun ShowDeletePrompts(
ActionButton(
text = "Delete",
onClick = onConfirm,
borderColor = SolidColor(myColors.error ),
borderColor = SolidColor(myColors.error),
contentColor = myColors.error,
)
Spacer(Modifier.width(8.dp))
@ -274,7 +276,7 @@ private fun ShowDeletePrompts(
@Stable
class DeletePromptState(
val downloadList: List<Long>
val downloadList: List<Long>,
) {
var alsoDeleteFile by mutableStateOf(false)
}

View File

@ -17,6 +17,7 @@ import org.koin.core.component.inject
data class HomePageStateToPersist(
val downloadListState: TableState.SerializableTableState? = null,
val windowSize: Pair<Float, Float> = 1000f to 500f,
val categoriesWidth: Float = 170f,
) {
class ConfigLens(prefix: String) : Lens<MapConfig, HomePageStateToPersist>,
KoinComponent {
@ -25,6 +26,7 @@ data class HomePageStateToPersist(
class Keys(prefix: String) {
val windowWidth = floatKeyOf("${prefix}window.width")
val windowHeight = floatKeyOf("${prefix}window.height")
val categoriesWidth = floatKeyOf("${prefix}categories.width")
val downloadListTableState = keyOfEncoded<TableState.SerializableTableState>("${prefix}downloadListState")
}
@ -34,6 +36,7 @@ data class HomePageStateToPersist(
return with(json) {
HomePageStateToPersist(
downloadListState = source.getDecoded(keys.downloadListTableState),
categoriesWidth = source.get(keys.categoriesWidth) ?: default.categoriesWidth,
windowSize = run {
val width = source.get(keys.windowWidth)
val height = source.get(keys.windowHeight)
@ -51,6 +54,7 @@ data class HomePageStateToPersist(
with(json) {
source.put(keys.windowWidth, focus.windowSize.first)
source.put(keys.windowHeight, focus.windowSize.second)
source.put(keys.categoriesWidth, focus.categoriesWidth)
source.putEncodedNullable(keys.downloadListTableState, focus.downloadListState)
}
return source