Skip to content

Commit

Permalink
Implement settings option to disable auto-scroll for notes in preview…
Browse files Browse the repository at this point in the history
… mode #143
  • Loading branch information
alialbaali committed Jun 20, 2023
1 parent cabf3e2 commit 488b9b0
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ object SettingsKeys {
val ScreenBrightnessLevel = floatPreferencesKey("ScreenBrightnessLevel")
val QuickExit = booleanPreferencesKey("QuickExit")
val ContinuousSearch = booleanPreferencesKey("ContinuousSearch")
val PreviewAutoScroll = booleanPreferencesKey("PreviewAutoScroll")
fun FilteredItemModel(model: FilteredItemModel) = intPreferencesKey("Filtered_Item_Model_${model.id}")

@Suppress("FunctionName")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ class SettingsRepositoryImpl(
.map { it ?: true }
.flowOn(dispatcher)

override val previewAutoScroll: Flow<Boolean> = storage.data
.map { preferences -> preferences[SettingsKeys.PreviewAutoScroll] }
.map { it ?: true }
.flowOn(dispatcher)

override fun getFilteredNotesScrollingPosition(model: FilteredItemModel): Flow<Int> = storage.data
.map { preferences -> preferences[SettingsKeys.FilteredItemModel(model)] }
.map { it ?: 0 }
Expand Down Expand Up @@ -467,5 +472,11 @@ class SettingsRepositoryImpl(
}
}

override suspend fun updatePreviewAutoScroll(isEnabled: Boolean) {
withContext(dispatcher) {
storage.edit { preferences -> preferences[SettingsKeys.PreviewAutoScroll] = isEnabled }
}
}

private fun String.toLongList() = split(", ").mapNotNull { it.toLongOrNull() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ interface SettingsRepository {

val continuousSearch: Flow<Boolean>

val previewAutoScroll: Flow<Boolean>

fun getFilteredNotesScrollingPosition(model: FilteredItemModel): Flow<Int>

fun getWidgetFolderId(widgetId: Int): Flow<Long>
Expand Down Expand Up @@ -142,4 +144,6 @@ interface SettingsRepository {

suspend fun updateContinuousSearch(isEnabled: Boolean)

suspend fun updatePreviewAutoScroll(isEnabled: Boolean)

}
50 changes: 10 additions & 40 deletions app/src/main/java/com/noto/app/folder/FolderViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,14 @@ package com.noto.app.folder
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.noto.app.UiState
import com.noto.app.domain.model.FilteringType
import com.noto.app.domain.model.Folder
import com.noto.app.domain.model.Font
import com.noto.app.domain.model.Grouping
import com.noto.app.domain.model.GroupingOrder
import com.noto.app.domain.model.Layout
import com.noto.app.domain.model.NewNoteCursorPosition
import com.noto.app.domain.model.Note
import com.noto.app.domain.model.NoteLabel
import com.noto.app.domain.model.NoteListSortingType
import com.noto.app.domain.model.NotoColor
import com.noto.app.domain.model.OpenNotesIn
import com.noto.app.domain.model.SortingOrder
import com.noto.app.domain.repository.FolderRepository
import com.noto.app.domain.repository.LabelRepository
import com.noto.app.domain.repository.NoteLabelRepository
import com.noto.app.domain.repository.NoteRepository
import com.noto.app.domain.repository.SettingsRepository
import com.noto.app.domain.model.*
import com.noto.app.domain.repository.*
import com.noto.app.getOrDefault
import com.noto.app.label.LabelItemModel
import com.noto.app.map
import com.noto.app.util.LineSeparator
import com.noto.app.util.SelectedLabelsComparator
import com.noto.app.util.filterBySearchTerm
import com.noto.app.util.filterSelected
import com.noto.app.util.filterByLabels
import com.noto.app.util.forEachRecursively
import com.noto.app.util.getOrCreateLabel
import com.noto.app.util.mapToNoteItemModel
import com.noto.app.util.*
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import kotlinx.datetime.Clock

Expand Down Expand Up @@ -132,6 +99,9 @@ class FolderViewModel(
.filter { it.isSelected }
.sortedBy { it.selectionOrder }

private val previewAutoScroll = settingsRepository.previewAutoScroll
.stateIn(viewModelScope, SharingStarted.Eagerly, true)

init {
combine(
folderRepository.getFolderById(folderId)
Expand Down Expand Up @@ -195,8 +165,8 @@ class FolderViewModel(
}
.launchIn(viewModelScope)

notes
.onEach { state ->
notes.combine(previewAutoScroll) { state, isEnabled ->
if (isEnabled) {
val models = state.getOrDefault(emptyList()).filter { it.isSelected }
if (models.isNotEmpty()) {
while (true) {
Expand All @@ -214,7 +184,7 @@ class FolderViewModel(
}
}
}
.launchIn(viewModelScope)
}.launchIn(viewModelScope)
}

suspend fun getFolderById(id: Long) = folderRepository.getFolderById(id).firstOrNull()
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/com/noto/app/settings/SettingsViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ class SettingsViewModel(
val continuousSearch = settingsRepository.continuousSearch
.stateIn(viewModelScope, SharingStarted.Eagerly, true)

val previewAutoScroll = settingsRepository.previewAutoScroll
.stateIn(viewModelScope, SharingStarted.Eagerly, true)

var folderIdType = FolderIdType.MainInterface
private set

Expand Down Expand Up @@ -196,6 +199,10 @@ class SettingsViewModel(
settingsRepository.updateContinuousSearch(!continuousSearch.value)
}

fun togglePreviewAutoScroll() = viewModelScope.launch {
settingsRepository.updatePreviewAutoScroll(!previewAutoScroll.value)
}

fun emitIsImportFinished() = viewModelScope.launch {
mutableIsImportFinished.emit(Unit)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class GeneralSettingsFragment : Fragment() {
.collect { value = it.getTitle(context) }
}
val continuousSearch by viewModel.continuousSearch.collectAsState()
val previewAutoScroll by viewModel.previewAutoScroll.collectAsState()

Screen(title = stringResource(id = R.string.general)) {
SettingsSection {
Expand Down Expand Up @@ -210,6 +211,15 @@ class GeneralSettingsFragment : Fragment() {
description = stringResource(id = R.string.continuous_search_description),
painter = painterResource(id = R.drawable.ic_round_continuous_search_24),
)


SettingsItem(
title = stringResource(id = R.string.preview_auto_scroll),
type = SettingsItemType.Switch(previewAutoScroll),
onClick = { viewModel.togglePreviewAutoScroll() },
description = stringResource(id = R.string.preview_auto_scroll_description),
painter = painterResource(id = R.drawable.ic_round_carousel_24),
)
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_round_carousel_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="?attr/notoPrimaryColor"
android:pathData="M3,7h2c0.55,0 1,0.45 1,1v8c0,0.55 -0.45,1 -1,1H3c-0.55,0 -1,-0.45 -1,-1V8C2,7.45 2.45,7 3,7zM8,19h8c0.55,0 1,-0.45 1,-1V6c0,-0.55 -0.45,-1 -1,-1H8C7.45,5 7,5.45 7,6v12C7,18.55 7.45,19 8,19zM19,7h2c0.55,0 1,0.45 1,1v8c0,0.55 -0.45,1 -1,1h-2c-0.55,0 -1,-0.45 -1,-1V8C18,7.45 18.45,7 19,7z" />
</vector>
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,10 @@
<string name="continuous_search">Continuous Search</string>
<!-- Description of the "Continuous Search" settings option. -->
<string name="continuous_search_description">Whether to pass the search term to the note screen.</string>
<!-- Whether to enable auto-scroll for notes in preview mode. -->
<string name="preview_auto_scroll">Preview auto-scroll</string>
<!-- Description of the "Preview auto-scroll" settings option. -->
<string name="preview_auto_scroll_description">Whether to enable auto-scroll for notes in preview mode.</string>
<string name="remember_scrolling_position">Remember scrolling position</string>
<!-- Description of the "Remember scrolling position" settings option. -->
<string name="remember_scrolling_position_description">Whether to store the scrolling position in folders, and notes.</string>
Expand Down

0 comments on commit 488b9b0

Please sign in to comment.