Skip to content

Commit a17dbc3

Browse files
committed
feat: add note list appearance settings (#15)
Settings gain an Appearance dialog with an optional two-line list header (listed category and account), header color, note background color, and highlighted category labels with a configurable color. Text and status bar icon colors follow the chosen surfaces. for #15
1 parent f5bcdac commit a17dbc3

7 files changed

Lines changed: 449 additions & 54 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
1919
color, note-row color, and an optional frame. It applies to the note-list and single-note
2020
widgets, and text color adapts to the chosen colors for readability (for
2121
[#15](https://github.com/qownnotes/qownnotes-android/issues/15)).
22+
- Settings now include an Appearance section for the note list: an optional two-line header with
23+
the listed category and account name, header color, note background color, and highlighted
24+
category labels with a configurable color (for
25+
[#15](https://github.com/qownnotes/qownnotes-android/issues/15)).
2226

2327
### Changed
2428

‎app/src/androidTest/kotlin/org/qownnotes/mobile/AppLaunchTest.kt‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ import android.view.InputDevice
66
import android.view.KeyEvent
77
import android.view.MotionEvent
88
import android.widget.TextView
9+
import androidx.compose.ui.graphics.asAndroidBitmap
910
import androidx.compose.ui.test.assertIsDisplayed
1011
import androidx.compose.ui.test.assertIsEnabled
1112
import androidx.compose.ui.test.assertIsNotEnabled
1213
import androidx.compose.ui.test.assertIsOff
1314
import androidx.compose.ui.test.assertIsOn
1415
import androidx.compose.ui.test.assertIsSelected
16+
import androidx.compose.ui.test.assertTextEquals
17+
import androidx.compose.ui.test.captureToImage
1518
import androidx.compose.ui.test.junit4.createAndroidComposeRule
1619
import androidx.compose.ui.test.longClick
1720
import androidx.compose.ui.test.onAllNodesWithTag
@@ -583,6 +586,61 @@ class AppLaunchTest {
583586
assertEquals(compactHeight, rowHeight())
584587
}
585588

589+
@Test
590+
fun appearanceColorsTheListHeaderRowsAndCategoriesAndPersists() {
591+
val account = importAccount("alice", "Colored note", "etag-a", 10)
592+
val localId = runBlocking {
593+
application.component.noteRepository.observeNotes(account.localAccountId())
594+
.first().single().localId
595+
}
596+
val displayName = runBlocking {
597+
application.component.accountRepository.observeAccounts().first().single().displayName
598+
}
599+
composeRule.onNodeWithTag("note-list-header").assertDoesNotExist()
600+
601+
listAction("settings")
602+
composeRule.onNodeWithTag("toggle-category").performClick()
603+
composeRule.onNodeWithTag("open-appearance").performScrollTo().performClick()
604+
composeRule.onNodeWithTag("appearance-dialog").assertIsDisplayed()
605+
composeRule.onNodeWithTag("toggle-list-header").performClick()
606+
composeRule.onNodeWithTag("app-header-blue").performScrollTo().performClick()
607+
composeRule.onNodeWithTag("app-note-background-black").performScrollTo().performClick()
608+
composeRule.onNodeWithTag("app-category-highlight-default").assertDoesNotExist()
609+
composeRule.onNodeWithTag("toggle-highlight-categories").performScrollTo().performClick()
610+
composeRule.onNodeWithTag("app-category-highlight-amber").performScrollTo().performClick()
611+
composeRule.onNodeWithTag("close-appearance").performClick()
612+
composeRule.waitForIdle()
613+
614+
fun assertAppearance() {
615+
composeRule.onNodeWithTag("note-list-header-title")
616+
.assertTextEquals("Uncategorized")
617+
composeRule.onNodeWithTag("note-list-header-account").assertTextEquals(displayName)
618+
assertEquals(0xFF1565C0.toInt(), pixel("note-list-header"))
619+
assertEquals(0xFF000000.toInt(), pixel("note-$localId"))
620+
assertEquals(
621+
0xFFFFB300.toInt(),
622+
pixel("note-category-$localId", useUnmergedTree = true)
623+
)
624+
}
625+
assertAppearance()
626+
627+
composeRule.activityRule.scenario.recreate()
628+
composeRule.waitForText("Colored note")
629+
assertAppearance()
630+
631+
listAction("settings")
632+
composeRule.onNodeWithTag("open-appearance").performScrollTo().performClick()
633+
composeRule.onNodeWithTag("reset-appearance").performClick()
634+
composeRule.onNodeWithTag("close-appearance").performClick()
635+
composeRule.onNodeWithTag("note-list-header").assertDoesNotExist()
636+
assertEquals(AppAppearance(), application.component.settings.appearance.value)
637+
}
638+
639+
/** Color near the top-left corner of a node, clear of text glyphs. */
640+
private fun pixel(tag: String, useUnmergedTree: Boolean = false): Int =
641+
composeRule.onNodeWithTag(tag, useUnmergedTree).captureToImage().asAndroidBitmap()
642+
.getPixel(1, 1)
643+
586644
@Test
587645
fun switchingAmongThreeAccountsLetsTheUserChooseTheAccount() {
588646
val alice = importAccount("alice", "Alice note", "etag-a", 10)

‎app/src/androidTest/kotlin/org/qownnotes/mobile/TestQOwnNotesApplication.kt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class TestQOwnNotesApplication : QOwnNotesApplication() {
8080
component.settings.setSwipeNoteActions(false)
8181
component.settings.setHideCreateButtonOnScroll(true)
8282
component.settings.setCompactNoteList(false)
83+
component.settings.setAppearance(AppAppearance())
8384
fakeBackend.reset()
8485
fakeAccountImporter.reset()
8586
fakeSyncScheduler.reset()

‎app/src/main/kotlin/org/qownnotes/mobile/AppSettings.kt‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,42 @@ class AppSettings(context: Context, name: String = PREFERENCES) {
8989
mutableCompactNoteList.value = enabled
9090
}
9191

92+
private val mutableAppearance = MutableStateFlow(readAppearance())
93+
94+
/** Note-list colors and header; custom colors are fixed across light and dark themes. */
95+
val appearance: StateFlow<AppAppearance> = mutableAppearance.asStateFlow()
96+
97+
fun setAppearance(appearance: AppAppearance) {
98+
if (appearance == mutableAppearance.value) return
99+
preferences.edit().apply {
100+
putColor(HEADER_COLOR, appearance.headerColor)
101+
putColor(NOTE_BACKGROUND_COLOR, appearance.noteBackground)
102+
putBoolean(HIGHLIGHT_CATEGORIES, appearance.highlightCategories)
103+
putColor(CATEGORY_HIGHLIGHT_COLOR, appearance.categoryHighlight)
104+
putBoolean(SHOW_LIST_HEADER, appearance.showListHeader)
105+
}.apply()
106+
mutableAppearance.value = appearance
107+
}
108+
109+
private fun readAppearance(): AppAppearance {
110+
fun color(key: String): Int? = if (preferences.contains(key)) {
111+
AppearanceColors.opaque(preferences.getInt(key, 0))
112+
} else {
113+
null
114+
}
115+
return AppAppearance(
116+
headerColor = color(HEADER_COLOR),
117+
noteBackground = color(NOTE_BACKGROUND_COLOR),
118+
highlightCategories = preferences.getBoolean(HIGHLIGHT_CATEGORIES, false),
119+
categoryHighlight = color(CATEGORY_HIGHLIGHT_COLOR),
120+
showListHeader = preferences.getBoolean(SHOW_LIST_HEADER, false)
121+
)
122+
}
123+
124+
private fun android.content.SharedPreferences.Editor.putColor(key: String, color: Int?) {
125+
if (color == null) remove(key) else putInt(key, AppearanceColors.opaque(color))
126+
}
127+
92128
private val mutableShowCategories = mutableMapOf<String, MutableStateFlow<Boolean>>()
93129
private val mutableBookmarksPaths = mutableMapOf<String, MutableStateFlow<String>>()
94130

@@ -178,6 +214,11 @@ class AppSettings(context: Context, name: String = PREFERENCES) {
178214
const val SWIPE_NOTE_ACTIONS = "swipeNoteActions"
179215
const val HIDE_CREATE_BUTTON_ON_SCROLL = "hideCreateButtonOnScroll"
180216
const val COMPACT_NOTE_LIST = "compactNoteList"
217+
const val HEADER_COLOR = "headerColor"
218+
const val NOTE_BACKGROUND_COLOR = "noteBackgroundColor"
219+
const val HIGHLIGHT_CATEGORIES = "highlightCategories"
220+
const val CATEGORY_HIGHLIGHT_COLOR = "categoryHighlightColor"
221+
const val SHOW_LIST_HEADER = "showListHeader"
181222

182223
// Legacy global key migrated to existing accounts when the application starts.
183224
const val SHOW_CATEGORY = "showCategory"
@@ -187,6 +228,21 @@ class AppSettings(context: Context, name: String = PREFERENCES) {
187228
}
188229
}
189230

231+
/**
232+
* Note-list presentation choices. A `null` color follows the Material theme, including dark mode.
233+
*/
234+
data class AppAppearance(
235+
/** Top bar and optional list header. */
236+
val headerColor: Int? = null,
237+
/** Unselected note rows. */
238+
val noteBackground: Int? = null,
239+
/** Draw shown categories as tinted labels instead of plain text. */
240+
val highlightCategories: Boolean = false,
241+
val categoryHighlight: Int? = null,
242+
/** Two-line header below the search bar: listed category, then account name. */
243+
val showListHeader: Boolean = false
244+
)
245+
190246
/** Stable preference encoding of a category scope, shared by app and widget preferences. */
191247
internal object NoteCategoryScopeCodec {
192248
private const val UNDEFINED_CATEGORY = "undefined"

‎app/src/main/kotlin/org/qownnotes/mobile/AppearanceControls.kt‎

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package org.qownnotes.mobile
22

3+
import android.app.Activity
4+
import android.content.Context
5+
import android.content.ContextWrapper
36
import androidx.compose.foundation.background
47
import androidx.compose.foundation.border
8+
import androidx.compose.foundation.isSystemInDarkTheme
59
import androidx.compose.foundation.layout.Arrangement
610
import androidx.compose.foundation.layout.Box
711
import androidx.compose.foundation.layout.Column
@@ -25,11 +29,13 @@ import androidx.compose.material3.Slider
2529
import androidx.compose.material3.Switch
2630
import androidx.compose.material3.Text
2731
import androidx.compose.runtime.Composable
32+
import androidx.compose.runtime.DisposableEffect
2833
import androidx.compose.ui.Alignment
2934
import androidx.compose.ui.Modifier
3035
import androidx.compose.ui.draw.clip
3136
import androidx.compose.ui.graphics.Color
3237
import androidx.compose.ui.graphics.toArgb
38+
import androidx.compose.ui.platform.LocalView
3339
import androidx.compose.ui.platform.testTag
3440
import androidx.compose.ui.res.colorResource
3541
import androidx.compose.ui.semantics.Role
@@ -39,6 +45,7 @@ import androidx.compose.ui.text.font.FontWeight
3945
import androidx.compose.ui.text.style.TextOverflow
4046
import androidx.compose.ui.unit.dp
4147
import androidx.compose.ui.unit.sp
48+
import androidx.core.view.WindowCompat
4249
import kotlin.math.roundToInt
4350

4451
/** Test tag suffix for a palette color, such as `light-blue`. */
@@ -147,6 +154,165 @@ internal fun SwitchRow(
147154
}
148155
}
149156

157+
/** Editor for the note list's header, row, and category colors. */
158+
@Composable
159+
internal fun AppAppearanceEditor(
160+
appearance: AppAppearance,
161+
onChange: (AppAppearance) -> Unit,
162+
modifier: Modifier = Modifier
163+
) {
164+
Column(modifier = modifier.fillMaxWidth()) {
165+
SwitchRow(
166+
label = "Show list header",
167+
checked = appearance.showListHeader,
168+
onCheckedChange = { onChange(appearance.copy(showListHeader = it)) },
169+
testTag = "toggle-list-header"
170+
)
171+
Text(
172+
"Shows the listed category and the account name below the search bar.",
173+
style = MaterialTheme.typography.bodySmall,
174+
color = MaterialTheme.colorScheme.onSurfaceVariant
175+
)
176+
ColorChoice(
177+
label = "Header color",
178+
selected = appearance.headerColor,
179+
onSelect = { onChange(appearance.copy(headerColor = it)) },
180+
testTag = "app-header",
181+
defaultLabel = "Default header",
182+
defaultColor = MaterialTheme.colorScheme.surface
183+
)
184+
ColorChoice(
185+
label = "Note background",
186+
selected = appearance.noteBackground,
187+
onSelect = { onChange(appearance.copy(noteBackground = it)) },
188+
testTag = "app-note-background",
189+
defaultLabel = "Default note background",
190+
defaultColor = MaterialTheme.colorScheme.surface
191+
)
192+
SwitchRow(
193+
label = "Highlight categories",
194+
checked = appearance.highlightCategories,
195+
onCheckedChange = { onChange(appearance.copy(highlightCategories = it)) },
196+
testTag = "toggle-highlight-categories"
197+
)
198+
Text(
199+
"Applies when \"Show category\" is on.",
200+
style = MaterialTheme.typography.bodySmall,
201+
color = MaterialTheme.colorScheme.onSurfaceVariant
202+
)
203+
if (appearance.highlightCategories) {
204+
ColorChoice(
205+
label = "Category highlight color",
206+
selected = appearance.categoryHighlight,
207+
onSelect = { onChange(appearance.copy(categoryHighlight = it)) },
208+
testTag = "app-category-highlight",
209+
defaultLabel = "Default category highlight",
210+
defaultColor = MaterialTheme.colorScheme.secondaryContainer
211+
)
212+
}
213+
Text(
214+
"Custom colors stay the same in light and dark mode. Text color adjusts " +
215+
"automatically for readability.",
216+
style = MaterialTheme.typography.bodySmall,
217+
color = MaterialTheme.colorScheme.onSurfaceVariant,
218+
modifier = Modifier.padding(top = 8.dp)
219+
)
220+
}
221+
}
222+
223+
/**
224+
* A note's category, either as plain text or as a tinted label. The label uses
225+
* [highlightColor] when set and the theme's secondary container otherwise.
226+
*/
227+
@Composable
228+
internal fun NoteCategoryLabel(
229+
category: String,
230+
highlight: Boolean,
231+
highlightColor: Int?,
232+
testTag: String
233+
) {
234+
if (!highlight) {
235+
Text(
236+
category,
237+
style = MaterialTheme.typography.bodySmall,
238+
modifier = Modifier.testTag(testTag)
239+
)
240+
return
241+
}
242+
val background = highlightColor?.let(::Color) ?: MaterialTheme.colorScheme.secondaryContainer
243+
val content = highlightColor?.let { Color(AppearanceColors.contentColor(it)) }
244+
?: MaterialTheme.colorScheme.onSecondaryContainer
245+
Text(
246+
category,
247+
style = MaterialTheme.typography.labelMedium,
248+
color = content,
249+
maxLines = 1,
250+
overflow = TextOverflow.Ellipsis,
251+
modifier = Modifier.padding(vertical = 2.dp)
252+
.clip(RoundedCornerShape(6.dp))
253+
.background(background)
254+
.padding(horizontal = 6.dp, vertical = 1.dp)
255+
.testTag(testTag)
256+
)
257+
}
258+
259+
/** Optional two-line header under the search bar: what is listed, then whose notes they are. */
260+
@Composable
261+
internal fun NoteListHeader(
262+
title: String,
263+
accountName: String,
264+
contentColor: Color,
265+
secondaryColor: Color
266+
) {
267+
Column(
268+
modifier = Modifier.fillMaxWidth()
269+
.padding(start = 20.dp, end = 16.dp, bottom = 8.dp)
270+
.testTag("note-list-header")
271+
) {
272+
Text(
273+
title,
274+
style = MaterialTheme.typography.titleMedium,
275+
fontWeight = FontWeight.Bold,
276+
color = contentColor,
277+
maxLines = 1,
278+
overflow = TextOverflow.Ellipsis,
279+
modifier = Modifier.testTag("note-list-header-title")
280+
)
281+
Text(
282+
accountName,
283+
style = MaterialTheme.typography.bodySmall,
284+
color = secondaryColor,
285+
maxLines = 1,
286+
overflow = TextOverflow.Ellipsis,
287+
modifier = Modifier.testTag("note-list-header-account")
288+
)
289+
}
290+
}
291+
292+
/**
293+
* Keeps status bar icons readable over a custom header color and restores the theme default when
294+
* the screen leaves or the color is cleared.
295+
*/
296+
@Composable
297+
internal fun StatusBarIconsFor(headerColor: Int?) {
298+
val view = LocalView.current
299+
val darkTheme = isSystemInDarkTheme()
300+
DisposableEffect(view, headerColor, darkTheme) {
301+
val window = view.context.findActivity()?.window
302+
val controller = window?.let { WindowCompat.getInsetsController(it, view) }
303+
controller?.isAppearanceLightStatusBars = headerColor
304+
?.let { AppearanceColors.contentColor(it) == AppearanceColors.DARK_CONTENT }
305+
?: !darkTheme
306+
onDispose { controller?.isAppearanceLightStatusBars = !darkTheme }
307+
}
308+
}
309+
310+
private tailrec fun Context.findActivity(): Activity? = when (this) {
311+
is Activity -> this
312+
is ContextWrapper -> baseContext.findActivity()
313+
else -> null
314+
}
315+
150316
/** Editor for one widget's colors, opacity, and frame, with a live approximation above it. */
151317
@Composable
152318
internal fun WidgetAppearanceEditor(

0 commit comments

Comments
 (0)