Skip to content

Commit f505d2d

Browse files
committed
Removed commits limit and added lazy loading
Fixes #141
1 parent c6ce532 commit f505d2d

File tree

6 files changed

+74
-155
lines changed

6 files changed

+74
-155
lines changed

src/main/kotlin/com/jetpackduba/gitnuro/git/log/GetLogUseCase.kt

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,47 @@ import javax.inject.Inject
1515
class GetLogUseCase @Inject constructor() {
1616
private var graphWalkCached: GraphWalk? = null
1717

18-
suspend operator fun invoke(git: Git, currentBranch: Ref?, hasUncommittedChanges: Boolean, commitsLimit: Int) =
19-
withContext(Dispatchers.IO) {
20-
val commitList = GraphCommitList()
21-
val repositoryState = git.repository.repositoryState
22-
23-
if (currentBranch != null || repositoryState.isRebasing) { // Current branch is null when there is no log (new repo) or rebasing
24-
val logList = git.log().setMaxCount(1).call().toList()
25-
26-
val walk = GraphWalk(git.repository)
27-
28-
walk.use {
29-
// Without this, during rebase conflicts the graph won't show the HEAD commits (new commits created
30-
// by the rebase)
18+
suspend operator fun invoke(
19+
git: Git,
20+
currentBranch: Ref?,
21+
hasUncommittedChanges: Boolean,
22+
commitsLimit: Int,
23+
cachedCommitList: GraphCommitList? = null,
24+
) = withContext(Dispatchers.IO) {
25+
val repositoryState = git.repository.repositoryState
26+
val commitList: GraphCommitList = cachedCommitList ?: GraphCommitList()
27+
28+
if (currentBranch != null || repositoryState.isRebasing) { // Current branch is null when there is no log (new repo) or rebasing
29+
val logList = git.log().setMaxCount(1).call().toList()
30+
31+
val walk = GraphWalk(git.repository)
32+
33+
walk.use {
34+
// Without this, during rebase conflicts the graph won't show the HEAD commits (new commits created
35+
// by the rebase)
36+
if (cachedCommitList == null) {
3137
walk.markStart(walk.lookupCommit(logList.first()))
3238

3339
walk.markStartAllRefs(Constants.R_HEADS)
3440
walk.markStartAllRefs(Constants.R_REMOTES)
3541
walk.markStartAllRefs(Constants.R_TAGS)
36-
// walk.markStartAllRefs(Constants.R_STASH)
3742

3843
if (hasUncommittedChanges)
3944
commitList.addUncommittedChangesGraphCommit(logList.first())
4045

4146
commitList.source(walk)
42-
commitList.fillTo(commitsLimit)
4347
}
44-
45-
ensureActive()
46-
48+
commitList.fillTo(commitsLimit)
4749
}
4850

49-
commitList.calcMaxLine()
50-
51-
return@withContext commitList
51+
ensureActive()
5252
}
5353

54+
commitList.calcMaxLine()
55+
56+
return@withContext commitList
57+
}
58+
5459
private fun cachedGraphWalk(repository: Repository): GraphWalk {
5560
val graphWalkCached = this.graphWalkCached
5661

src/main/kotlin/com/jetpackduba/gitnuro/repositories/AppSettingsRepository.kt

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ private const val PREF_LATEST_REPOSITORY_TAB_SELECTED = "latestRepositoryTabSele
2525
private const val PREF_LAST_OPENED_REPOSITORIES_PATH = "lastOpenedRepositoriesList"
2626
private const val PREF_THEME = "theme"
2727
private const val PREF_LINE_HEIGHT_TYPE = "linesHeight"
28-
private const val PREF_COMMITS_LIMIT = "commitsLimit"
29-
private const val PREF_COMMITS_LIMIT_ENABLED = "commitsLimitEnabled"
3028
private const val PREF_WINDOW_PLACEMENT = "windowsPlacement"
3129
private const val PREF_CUSTOM_THEME = "customTheme"
3230
private const val PREF_UI_SCALE = "ui_scale"
@@ -46,15 +44,12 @@ private const val PREF_CACHE_CREDENTIALS_IN_MEMORY = "credentialsInMemory"
4644
private const val PREF_FIRST_PANE_WIDTH = "firstPaneWidth"
4745
private const val PREF_THIRD_PANE_WIDTH = "thirdPaneWidth"
4846

49-
5047
private const val PREF_GIT_FF_MERGE = "gitFFMerge"
5148
private const val PREF_GIT_PULL_REBASE = "gitPullRebase"
5249
private const val PREF_GIT_PUSH_WITH_LEASE = "gitPushWithLease"
5350

5451
private const val PREF_VERIFY_SSL = "verifySsl"
5552

56-
private const val DEFAULT_COMMITS_LIMIT = 1000
57-
private const val DEFAULT_COMMITS_LIMIT_ENABLED = true
5853
private const val DEFAULT_SWAP_UNCOMMITTED_CHANGES = false
5954
private const val DEFAULT_SHOW_CHANGES_AS_TREE = false
6055
private const val DEFAULT_CACHE_CREDENTIALS_IN_MEMORY = true
@@ -73,9 +68,6 @@ class AppSettingsRepository @Inject constructor() {
7368
private val _linesHeightTypeState = MutableStateFlow(linesHeightType)
7469
val linesHeightTypeState = _linesHeightTypeState.asStateFlow()
7570

76-
private val _commitsLimitEnabledFlow = MutableStateFlow(commitsLimitEnabled)
77-
val commitsLimitEnabledFlow = _commitsLimitEnabledFlow.asStateFlow()
78-
7971
private val _swapUncommittedChangesFlow = MutableStateFlow(swapUncommittedChanges)
8072
val swapUncommittedChangesFlow = _swapUncommittedChangesFlow.asStateFlow()
8173

@@ -97,9 +89,6 @@ class AppSettingsRepository @Inject constructor() {
9789
private val _pushWithLeaseFlow = MutableStateFlow(pushWithLease)
9890
val pushWithLeaseFlow: StateFlow<Boolean> = _pushWithLeaseFlow.asStateFlow()
9991

100-
private val _commitsLimitFlow = MutableSharedFlow<Int>()
101-
val commitsLimitFlow = _commitsLimitFlow.asSharedFlow()
102-
10392
private val _customThemeFlow = MutableStateFlow<ColorsScheme?>(null)
10493
val customThemeFlow = _customThemeFlow.asStateFlow()
10594

@@ -177,15 +166,6 @@ class AppSettingsRepository @Inject constructor() {
177166
_linesHeightTypeState.value = value
178167
}
179168

180-
var commitsLimitEnabled: Boolean
181-
get() {
182-
return preferences.getBoolean(PREF_COMMITS_LIMIT_ENABLED, DEFAULT_COMMITS_LIMIT_ENABLED)
183-
}
184-
set(value) {
185-
preferences.putBoolean(PREF_COMMITS_LIMIT_ENABLED, value)
186-
_commitsLimitEnabledFlow.value = value
187-
}
188-
189169
var swapUncommittedChanges: Boolean
190170
get() {
191171
return preferences.getBoolean(PREF_SWAP_UNCOMMITTED_CHANGES, DEFAULT_SWAP_UNCOMMITTED_CHANGES)
@@ -280,16 +260,6 @@ class AppSettingsRepository @Inject constructor() {
280260
_pushWithLeaseFlow.value = value
281261
}
282262

283-
val commitsLimit: Int
284-
get() {
285-
return preferences.getInt(PREF_COMMITS_LIMIT, DEFAULT_COMMITS_LIMIT)
286-
}
287-
288-
suspend fun setCommitsLimit(value: Int) {
289-
preferences.putInt(PREF_COMMITS_LIMIT, value)
290-
_commitsLimitFlow.emit(value)
291-
}
292-
293263
var windowPlacement: WindowsPlacementPreference
294264
get() {
295265
val placement = preferences.getInt(PREF_WINDOW_PLACEMENT, defaultWindowPlacement.value)

src/main/kotlin/com/jetpackduba/gitnuro/ui/dialogs/settings/SettingsDialog.kt

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ val settings = listOf(
4242
SettingsEntry.Entry(AppIcons.LAYOUT, "Layout") { Layout(it) },
4343

4444
SettingsEntry.Section("GIT"),
45-
SettingsEntry.Entry(AppIcons.LIST, "Commits history") { CommitsHistory(it) },
4645
SettingsEntry.Entry(AppIcons.BRANCH, "Branches") { Branches(it) },
4746
SettingsEntry.Entry(AppIcons.CLOUD, "Remote actions") { RemoteActions(it) },
4847

@@ -160,11 +159,6 @@ fun SettingsDialog(
160159
settingsViewModel: SettingsViewModel,
161160
onDismiss: () -> Unit,
162161
) {
163-
164-
LaunchedEffect(Unit) {
165-
settingsViewModel.resetInfo()
166-
}
167-
168162
var selectedCategory by remember {
169163
mutableStateOf<SettingsEntry.Entry>(
170164
settings.filterIsInstance(SettingsEntry.Entry::class.java).first()
@@ -174,8 +168,6 @@ fun SettingsDialog(
174168
MaterialDialog(
175169
background = MaterialTheme.colors.surface,
176170
onCloseRequested = {
177-
settingsViewModel.savePendingChanges()
178-
179171
onDismiss()
180172
},
181173
paddingHorizontal = 0.dp,
@@ -239,7 +231,6 @@ fun SettingsDialog(
239231
.padding(end = 16.dp, bottom = 16.dp)
240232
.align(Alignment.End),
241233
onClick = {
242-
settingsViewModel.savePendingChanges()
243234
onDismiss()
244235
},
245236
)
@@ -291,33 +282,6 @@ private fun Section(name: String) {
291282
)
292283
}
293284

294-
295-
@Composable
296-
private fun CommitsHistory(settingsViewModel: SettingsViewModel) {
297-
val commitsLimitEnabled by settingsViewModel.commitsLimitEnabledFlow.collectAsState()
298-
var commitsLimit by remember { mutableStateOf(settingsViewModel.commitsLimit) }
299-
300-
SettingToggle(
301-
title = "Limit log commits",
302-
subtitle = "Turning off this may affect the performance",
303-
value = commitsLimitEnabled,
304-
onValueChanged = { value ->
305-
settingsViewModel.commitsLimitEnabled = value
306-
}
307-
)
308-
309-
SettingIntInput(
310-
title = "Max commits",
311-
subtitle = "Increasing this value may affect the performance",
312-
value = commitsLimit,
313-
enabled = commitsLimitEnabled,
314-
onValueChanged = { value ->
315-
commitsLimit = value
316-
settingsViewModel.commitsLimit = value
317-
}
318-
)
319-
}
320-
321285
@Composable
322286
private fun RemoteActions(settingsViewModel: SettingsViewModel) {
323287
val pullRebase by settingsViewModel.pullRebaseFlow.collectAsState()

src/main/kotlin/com/jetpackduba/gitnuro/ui/log/Log.kt

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import com.jetpackduba.gitnuro.git.graph.GraphNode
4747
import com.jetpackduba.gitnuro.git.workspace.StatusSummary
4848
import com.jetpackduba.gitnuro.keybindings.KeybindingOption
4949
import com.jetpackduba.gitnuro.keybindings.matchesBinding
50+
import com.jetpackduba.gitnuro.logging.printLog
5051
import com.jetpackduba.gitnuro.theme.*
5152
import com.jetpackduba.gitnuro.ui.SelectedItem
5253
import com.jetpackduba.gitnuro.ui.components.AvatarImage
@@ -93,9 +94,6 @@ private const val DIVIDER_WIDTH = 8
9394
private const val LOG_BOTTOM_PADDING = 80
9495

9596
// TODO Min size for message column
96-
@OptIn(
97-
ExperimentalFoundationApi::class, ExperimentalComposeUiApi::class
98-
)
9997
@Composable
10098
fun Log(
10199
logViewModel: LogViewModel,
@@ -115,6 +113,7 @@ fun Log(
115113
selectedItem = selectedItem,
116114
repositoryState = repositoryState,
117115
changeDefaultUpstreamBranchViewModel = changeDefaultUpstreamBranchViewModel,
116+
onRequestMoreLogItems = { logViewModel.loadMoreLogItems() }
118117
)
119118

120119
LogStatus.Loading -> LogLoading()
@@ -147,6 +146,7 @@ private fun LogLoaded(
147146
selectedItem: SelectedItem,
148147
repositoryState: RepositoryState,
149148
changeDefaultUpstreamBranchViewModel: () -> ChangeDefaultUpstreamBranchViewModel,
149+
onRequestMoreLogItems: () -> Unit,
150150
) {
151151
val scope = rememberCoroutineScope()
152152
val hasUncommittedChanges = logStatus.hasUncommittedChanges
@@ -156,6 +156,24 @@ private fun LogLoaded(
156156
val searchFilter = logViewModel.logSearchFilterResults.collectAsState()
157157
val searchFilterValue = searchFilter.value
158158

159+
LaunchedEffect(verticalScrollState) {
160+
snapshotFlow { verticalScrollState.firstVisibleItemIndex }
161+
// .distinctUntilChanged()
162+
.collect {
163+
val commitsList = logStatus.plotCommitList
164+
165+
if (
166+
commitsList.isNotEmpty() &&
167+
commitsList.count() - it < 100 &&
168+
commitsList.last().parentCount > 0
169+
) {
170+
printLog("ABDE", "Should request more items!")
171+
onRequestMoreLogItems()
172+
}
173+
printLog("ABDE", "First visible item index is $it")
174+
}
175+
}
176+
159177
val selectedCommit = if (selectedItem is SelectedItem.CommitBasedItem) {
160178
selectedItem.revCommit
161179
} else {
@@ -250,7 +268,6 @@ private fun LogLoaded(
250268
selectedItem = selectedItem,
251269
commitList = commitList,
252270
graphWidth = graphWidth,
253-
commitsLimit = logStatus.commitsLimit,
254271
onMerge = { ref -> logViewModel.mergeBranch(ref) },
255272
onRebase = { ref -> logViewModel.rebaseBranch(ref) },
256273
onShowLogDialog = { dialog -> logViewModel.showDialog(dialog) },
@@ -469,7 +486,6 @@ fun CommitsList(
469486
repositoryState: RepositoryState,
470487
selectedItem: SelectedItem,
471488
commitList: GraphCommitList,
472-
commitsLimit: Int,
473489
onCheckoutCommit: (GraphNode) -> Unit,
474490
onRevertCommit: (GraphNode) -> Unit,
475491
onCherryPickCommit: (GraphNode) -> Unit,
@@ -571,25 +587,6 @@ fun CommitsList(
571587
)
572588
}
573589

574-
if (commitsLimit >= 0 && commitsLimit <= commitList.count()) {
575-
item {
576-
Box(
577-
modifier = Modifier
578-
.padding(start = graphWidth + 24.dp)
579-
.height(MaterialTheme.linesHeight.logCommitHeight),
580-
contentAlignment = Alignment.CenterStart,
581-
) {
582-
Text(
583-
text = "The commits list has been limited to $commitsLimit. Access the settings to change it.",
584-
color = MaterialTheme.colors.onBackground,
585-
fontStyle = FontStyle.Italic,
586-
style = MaterialTheme.typography.body2,
587-
maxLines = 1,
588-
)
589-
}
590-
}
591-
}
592-
593590
item {
594591
Box(modifier = Modifier.height(LOG_BOTTOM_PADDING.dp))
595592
}

0 commit comments

Comments
 (0)