The app now remembers the window state.

The resolution won't be saved to avoid issues when adding or removing screens with different resolution.

Fixes https://github.com/JetpackDuba/Gitnuro/issues/8
This commit is contained in:
Abdelilah El Aissaoui 2022-06-17 03:14:21 +02:00
parent d79095533d
commit a43c13462a
11 changed files with 72 additions and 15 deletions

View File

@ -20,6 +20,9 @@ import androidx.compose.ui.window.WindowPlacement
import androidx.compose.ui.window.application
import androidx.compose.ui.window.rememberWindowState
import app.di.DaggerAppComponent
import app.extensions.preferenceValue
import app.extensions.toWindowPlacement
import app.preferences.AppPreferences
import app.theme.AppTheme
import app.theme.primaryTextColor
import app.theme.secondaryTextColor
@ -47,12 +50,21 @@ class App {
private val tabsFlow = MutableStateFlow<List<TabInformation>>(emptyList())
fun start() {
val windowPlacement = appPreferences.windowPlacement.toWindowPlacement
appStateManager.loadRepositoriesTabs()
loadTabs()
application {
var isOpen by remember { mutableStateOf(true) }
val theme by appPreferences.themeState.collectAsState()
val windowState = rememberWindowState(
placement = windowPlacement,
size = DpSize(1280.dp, 720.dp)
)
// Save window state for next time the Window is started
appPreferences.windowPlacement = windowState.placement.preferenceValue
if (isOpen) {
Window(
@ -60,10 +72,7 @@ class App {
onCloseRequest = {
isOpen = false
},
state = rememberWindowState(
placement = WindowPlacement.Maximized,
size = DpSize(1280.dp, 720.dp)
),
state = windowState,
icon = painterResource("logo.svg"),
) {
var showSettingsDialog by remember { mutableStateOf(false) }

View File

@ -1,5 +1,6 @@
package app
import app.preferences.AppPreferences
import kotlinx.coroutines.*
import kotlinx.coroutines.sync.Mutex
import kotlinx.serialization.decodeFromString

View File

@ -1,7 +1,7 @@
package app.di
import app.App
import app.AppPreferences
import app.preferences.AppPreferences
import app.AppStateManager
import dagger.Component
import javax.inject.Singleton

View File

@ -1,6 +1,5 @@
package app.di
import app.AppPreferences
import app.di.modules.NetworkModule
import app.ui.components.TabInformation
import dagger.Component

View File

@ -0,0 +1,31 @@
package app.extensions
import androidx.compose.ui.window.WindowPlacement
import app.preferences.WindowsPlacementPreference
private val windowPlacementFloating = WindowsPlacementPreference(0)
private val windowPlacementMaximized = WindowsPlacementPreference(1)
private val windowPlacementFullscreen = WindowsPlacementPreference(2)
val defaultWindowPlacement = windowPlacementMaximized
val WindowPlacement.preferenceValue: WindowsPlacementPreference
get() {
return when (this) {
WindowPlacement.Floating -> windowPlacementFloating
WindowPlacement.Maximized -> windowPlacementMaximized
WindowPlacement.Fullscreen -> windowPlacementFullscreen
}
}
val WindowsPlacementPreference.toWindowPlacement: WindowPlacement
get() {
return when (this) {
windowPlacementFloating -> WindowPlacement.Floating
windowPlacementFullscreen -> WindowPlacement.Fullscreen
else -> WindowPlacement.Maximized
}
}

View File

@ -1,5 +1,6 @@
package app
package app.preferences
import app.extensions.defaultWindowPlacement
import app.theme.Themes
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
@ -14,6 +15,7 @@ private const val PREF_LAST_OPENED_REPOSITORIES_PATH = "lastOpenedRepositoriesLi
private const val PREF_THEME = "theme"
private const val PREF_COMMITS_LIMIT = "commitsLimit"
private const val PREF_COMMITS_LIMIT_ENABLED = "commitsLimitEnabled"
private const val PREF_WINDOW_PLACEMENT = "windowsPlacement"
private const val DEFAULT_COMMITS_LIMIT = 1000
private const val DEFAULT_COMMITS_LIMIT_ENABLED = true
@ -75,4 +77,14 @@ class AppPreferences @Inject constructor() {
preferences.putInt(PREF_COMMITS_LIMIT, value)
_commitsLimitFlow.value = value
}
var windowPlacement: WindowsPlacementPreference
get() {
val placement = preferences.getInt(PREF_WINDOW_PLACEMENT, defaultWindowPlacement.value)
return WindowsPlacementPreference(placement)
}
set(placement) {
preferences.putInt(PREF_WINDOW_PLACEMENT, placement.value)
}
}

View File

@ -0,0 +1,4 @@
package app.preferences
@JvmInline
value class WindowsPlacementPreference(val value: Int)

View File

@ -10,7 +10,7 @@ import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import app.AppPreferences
import app.preferences.AppPreferences
import app.DropDownOption
import app.theme.outlinedTextFieldColors
import app.theme.primaryTextColor

View File

@ -79,6 +79,8 @@ private const val MARGIN_GRAPH_LANES = 2
private const val LANE_WIDTH = 30f
private const val DIVIDER_WIDTH = 8
private const val LINE_HEIGHT = 40
// TODO Min size for message column
@OptIn(
ExperimentalFoundationApi::class, ExperimentalComposeUiApi::class
@ -363,7 +365,7 @@ fun MessagesList(
Box(
modifier = Modifier
.padding(start = graphWidth + 24.dp)
.height(40.dp),
.height(LINE_HEIGHT.dp),
contentAlignment = Alignment.CenterStart,
) {
Text(
@ -425,7 +427,7 @@ fun GraphList(
if (hasUncommitedChanges) {
item {
Row(
modifier = Modifier.height(40.dp).fillMaxWidth(),
modifier = Modifier.height(LINE_HEIGHT.dp).fillMaxWidth(),
) {
UncommitedChangesGraphNode(
modifier = Modifier.fillMaxSize(),
@ -441,7 +443,7 @@ fun GraphList(
Row(
modifier = Modifier
.height(40.dp)
.height(LINE_HEIGHT.dp)
.fillMaxWidth(),
) {
CommitsGraphLine(
@ -458,7 +460,7 @@ fun GraphList(
item {
Box(
modifier = Modifier
.height(40.dp),
.height(LINE_HEIGHT.dp),
)
}
}
@ -712,7 +714,7 @@ fun CommitLine(
modifier = Modifier
.clickable { onRevCommitSelected() }
.padding(start = graphWidth)
.height(40.dp)
.height(LINE_HEIGHT.dp)
.backgroundIf(isSelected, MaterialTheme.colors.backgroundSelected)
) {

View File

@ -1,7 +1,6 @@
package app.updates
import app.AppConstants
import app.AppPreferences
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.serialization.decodeFromString

View File

@ -1,7 +1,7 @@
package app.viewmodels
import androidx.compose.foundation.lazy.LazyListState
import app.AppPreferences
import app.preferences.AppPreferences
import app.extensions.delayedStateChange
import app.git.*
import app.git.graph.GraphCommitList