Fixed log not refreshing when changing commits limit

This commit is contained in:
Abdelilah El Aissaoui 2022-11-12 19:53:18 +01:00
parent af3054342f
commit 76e5237cd5
8 changed files with 53 additions and 24 deletions

View File

@ -14,8 +14,6 @@ import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.DrawScope import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.painter.Painter import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyShortcut
import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.Density import androidx.compose.ui.unit.Density
@ -37,12 +35,10 @@ import com.jetpackduba.gitnuro.ui.AppTab
import com.jetpackduba.gitnuro.ui.components.RepositoriesTabPanel import com.jetpackduba.gitnuro.ui.components.RepositoriesTabPanel
import com.jetpackduba.gitnuro.ui.components.TabInformation import com.jetpackduba.gitnuro.ui.components.TabInformation
import com.jetpackduba.gitnuro.ui.components.emptyTabInformation import com.jetpackduba.gitnuro.ui.components.emptyTabInformation
import com.jetpackduba.gitnuro.ui.context_menu.Separator
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import javax.inject.Inject import javax.inject.Inject
import androidx.compose.ui.window.MenuBar
private const val TAG = "App" private const val TAG = "App"
@ -171,7 +167,7 @@ class App {
} }
} }
private fun removeTab(key: Int) = appStateManager.appStateScope.launch(Dispatchers.IO) { private fun removeTab(key: Int) = appStateManager.appScope.launch(Dispatchers.IO) {
// Stop any running jobs // Stop any running jobs
val tabs = tabsFlow.value val tabs = tabsFlow.value
val tabToRemove = tabs.firstOrNull { it.key == key } ?: return@launch val tabToRemove = tabs.firstOrNull { it.key == key } ?: return@launch
@ -184,7 +180,7 @@ class App {
tabsFlow.value = tabsFlow.value.filter { tab -> tab.key != key } tabsFlow.value = tabsFlow.value.filter { tab -> tab.key != key }
} }
fun addTab(tabInformation: TabInformation) = appStateManager.appStateScope.launch(Dispatchers.IO) { fun addTab(tabInformation: TabInformation) = appStateManager.appScope.launch(Dispatchers.IO) {
tabsFlow.value = tabsFlow.value.toMutableList().apply { add(tabInformation) } tabsFlow.value = tabsFlow.value.toMutableList().apply { add(tabInformation) }
} }

View File

@ -1,5 +1,6 @@
package com.jetpackduba.gitnuro package com.jetpackduba.gitnuro
import com.jetpackduba.gitnuro.di.qualifiers.AppCoroutineScope
import com.jetpackduba.gitnuro.preferences.AppSettings import com.jetpackduba.gitnuro.preferences.AppSettings
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
@ -12,6 +13,7 @@ import javax.inject.Singleton
@Singleton @Singleton
class AppStateManager @Inject constructor( class AppStateManager @Inject constructor(
private val appSettings: AppSettings, private val appSettings: AppSettings,
@AppCoroutineScope val appScope: CoroutineScope,
) { ) {
private val mutex = Mutex() private val mutex = Mutex()
@ -23,12 +25,10 @@ class AppStateManager @Inject constructor(
val latestOpenedRepositoriesPaths: List<String> val latestOpenedRepositoriesPaths: List<String>
get() = _latestOpenedRepositoriesPaths get() = _latestOpenedRepositoriesPaths
val appStateScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
val latestOpenedRepositoryPath: String val latestOpenedRepositoryPath: String
get() = _latestOpenedRepositoriesPaths.firstOrNull() ?: "" get() = _latestOpenedRepositoriesPaths.firstOrNull() ?: ""
fun repositoryTabChanged(key: Int, path: String) = appStateScope.launch(Dispatchers.IO) { fun repositoryTabChanged(key: Int, path: String) = appScope.launch(Dispatchers.IO) {
mutex.lock() mutex.lock()
try { try {
// Do not save already saved repos // Do not save already saved repos
@ -51,7 +51,7 @@ class AppStateManager @Inject constructor(
} }
} }
fun repositoryTabRemoved(key: Int) = appStateScope.launch(Dispatchers.IO) { fun repositoryTabRemoved(key: Int) = appScope.launch(Dispatchers.IO) {
_openRepositoriesPaths.remove(key) _openRepositoriesPaths.remove(key)
updateSavedRepositoryTabs() updateSavedRepositoryTabs()
@ -85,6 +85,6 @@ class AppStateManager @Inject constructor(
} }
fun cancelCoroutines() { fun cancelCoroutines() {
appStateScope.cancel("Closing com.jetpackduba.gitnuro.app") appScope.cancel("Closing com.jetpackduba.gitnuro.app")
} }
} }

View File

@ -3,13 +3,20 @@ package com.jetpackduba.gitnuro.di
import com.jetpackduba.gitnuro.App import com.jetpackduba.gitnuro.App
import com.jetpackduba.gitnuro.AppStateManager import com.jetpackduba.gitnuro.AppStateManager
import com.jetpackduba.gitnuro.credentials.CredentialsStateManager import com.jetpackduba.gitnuro.credentials.CredentialsStateManager
import com.jetpackduba.gitnuro.di.modules.AppModule
import com.jetpackduba.gitnuro.di.modules.NetworkModule
import com.jetpackduba.gitnuro.di.modules.TabModule
import com.jetpackduba.gitnuro.preferences.AppSettings import com.jetpackduba.gitnuro.preferences.AppSettings
import com.jetpackduba.gitnuro.viewmodels.SettingsViewModel import com.jetpackduba.gitnuro.viewmodels.SettingsViewModel
import dagger.Component import dagger.Component
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
@Component @Component(
modules = [
AppModule::class
]
)
interface AppComponent { interface AppComponent {
fun inject(main: App) fun inject(main: App)
fun appStateManager(): AppStateManager fun appStateManager(): AppStateManager

View File

@ -0,0 +1,15 @@
package com.jetpackduba.gitnuro.di.modules
import com.jetpackduba.gitnuro.di.qualifiers.AppCoroutineScope
import dagger.Module
import dagger.Provides
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
@Module
class AppModule {
@Provides
@AppCoroutineScope
fun provideAppScope(): CoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
}

View File

@ -0,0 +1,7 @@
package com.jetpackduba.gitnuro.di.qualifiers
import javax.inject.Qualifier
@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation class AppCoroutineScope

View File

@ -117,14 +117,15 @@ class AppSettings @Inject constructor() {
_ffMergeFlow.value = value _ffMergeFlow.value = value
} }
var commitsLimit: Int val commitsLimit: Int
get() { get() {
return preferences.getInt(PREF_COMMITS_LIMIT, DEFAULT_COMMITS_LIMIT) return preferences.getInt(PREF_COMMITS_LIMIT, DEFAULT_COMMITS_LIMIT)
} }
set(value) {
preferences.putInt(PREF_COMMITS_LIMIT, value) suspend fun setCommitsLimit(value: Int) {
_commitsLimitFlow.tryEmit(value) preferences.putInt(PREF_COMMITS_LIMIT, value)
} _commitsLimitFlow.emit(value)
}
var windowPlacement: WindowsPlacementPreference var windowPlacement: WindowsPlacementPreference
get() { get() {

View File

@ -101,12 +101,12 @@ class LogViewModel @Inject constructor(
init { init {
tabScope.launch { tabScope.launch {
appSettings.commitsLimitEnabledFlow.drop(1).collect { appSettings.commitsLimitEnabledFlow.drop(1).collectLatest {
tabState.refreshData(RefreshType.ONLY_LOG) tabState.refreshData(RefreshType.ONLY_LOG)
} }
} }
tabScope.launch { tabScope.launch {
appSettings.commitsLimitFlow.collect { appSettings.commitsLimitFlow.collectLatest {
tabState.refreshData(RefreshType.ONLY_LOG) tabState.refreshData(RefreshType.ONLY_LOG)
} }
} }

View File

@ -1,19 +1,22 @@
package com.jetpackduba.gitnuro.viewmodels package com.jetpackduba.gitnuro.viewmodels
import com.jetpackduba.gitnuro.di.qualifiers.AppCoroutineScope
import com.jetpackduba.gitnuro.preferences.AppSettings import com.jetpackduba.gitnuro.preferences.AppSettings
import com.jetpackduba.gitnuro.theme.Theme import com.jetpackduba.gitnuro.theme.Theme
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class SettingsViewModel @Inject constructor( class SettingsViewModel @Inject constructor(
val appSettings: AppSettings, private val appSettings: AppSettings,
@AppCoroutineScope private val appScope: CoroutineScope,
) { ) {
// Temporary values to detect changed variables // Temporary values to detect changed variables
var commitsLimit: Int = -1 var commitsLimit: Int = -1
val themeState = appSettings.themeState val themeState = appSettings.themeState
val customThemeFlow = appSettings.customThemeFlow
val ffMergeFlow = appSettings.ffMergeFlow val ffMergeFlow = appSettings.ffMergeFlow
val commitsLimitEnabledFlow = appSettings.commitsLimitEnabledFlow val commitsLimitEnabledFlow = appSettings.commitsLimitEnabledFlow
@ -50,11 +53,11 @@ class SettingsViewModel @Inject constructor(
commitsLimit = appSettings.commitsLimit commitsLimit = appSettings.commitsLimit
} }
fun savePendingChanges() { fun savePendingChanges() = appScope.launch {
val commitsLimit = this.commitsLimit val commitsLimit = this@SettingsViewModel.commitsLimit
if (appSettings.commitsLimit != commitsLimit) { if (appSettings.commitsLimit != commitsLimit) {
appSettings.commitsLimit = commitsLimit appSettings.setCommitsLimit(commitsLimit)
} }
} }
} }