Skip to content

Commit

Permalink
[fix]: jumping logs list in selection mode
Browse files Browse the repository at this point in the history
  • Loading branch information
F0x1d committed Nov 3, 2024
1 parent d0ac7d6 commit 5eae9b4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.f0x1d.logfox.model.logline.LogLine

data class LogsState(
val logs: List<LogLine> = emptyList(),
val logsChanged: Boolean = true,
val paused: Boolean = false,
val query: String? = null,
val filters: List<UserFilter> = emptyList(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,12 @@ class LogsViewModel @Inject constructor(
val logsExpanded get() = appPreferences.logsExpanded
val logsFormat get() = appPreferences.showLogValues

val selectedItemsContent get() = currentState.selectedItems.joinToString("\n") {
originalOf(it)
}
val selectedItemsContent get() = currentState
.selectedItems
.sortedBy { it.dateAndTime }
.joinToString("\n") {
originalOf(it)
}

init {
load()
Expand All @@ -71,7 +74,11 @@ class LogsViewModel @Inject constructor(
state
.map { it.selectedItems }
.distinctUntilChanged()
.onEach { selectedLogLinesDataSource.updateSelectedLines(it.toList()) }
.onEach { lines ->
selectedLogLinesDataSource.updateSelectedLines(
selectedLines = lines.sortedBy { it.dateAndTime },
)
}
.launchIn(this)

combine(
Expand Down Expand Up @@ -119,7 +126,7 @@ class LogsViewModel @Inject constructor(
logs = data.logs.filterAndSearch(
filters = data.filters,
query = data.query,
)
),
)
}.flowOn(
defaultDispatcher,
Expand All @@ -129,6 +136,7 @@ class LogsViewModel @Inject constructor(
logs = data.logs,
query = data.query,
filters = data.filters,
logsChanged = true,
)
}
}.launchIn(this)
Expand All @@ -143,7 +151,8 @@ class LogsViewModel @Inject constructor(
) else remove(
logLine
)
}
},
logsChanged = false,
)
}

Expand All @@ -154,6 +163,7 @@ class LogsViewModel @Inject constructor(
} else {
logs.toSet()
},
logsChanged = false,
)
}

Expand All @@ -171,9 +181,9 @@ class LogsViewModel @Inject constructor(
}
}

fun switchState() = reduce { copy(paused = paused.not()) }
fun pause() = reduce { copy(paused = true) }
fun resume() = reduce { copy(paused = false) }
fun switchState() = reduce { copy(paused = paused.not(), logsChanged = false) }
fun pause() = reduce { copy(paused = true, logsChanged = false) }
fun resume() = reduce { copy(paused = false, logsChanged = false) }

fun originalOf(logLine: LogLine): String = appPreferences.originalOf(
logLine = logLine,
Expand All @@ -182,7 +192,10 @@ class LogsViewModel @Inject constructor(
)

fun clearSelection() = reduce {
copy(selectedItems = emptySet())
copy(
selectedItems = emptySet(),
logsChanged = false,
)
}

private data class LogsData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ class LogsFragment : BaseFragment<FragmentLogsBinding>() {
processSelectedItems(selectedItems = state.selectedItems)
processPaused(paused = state.paused)

updateLogsList(items = state.logs)
if (state.logsChanged) {
updateLogsList(items = state.logs)
}
}

requireActivity().onBackPressedDispatcher.apply {
Expand Down

0 comments on commit 5eae9b4

Please sign in to comment.