11package dev.dettmer.simplenotes.ui.main.components
22
33import androidx.compose.foundation.layout.Arrangement
4+ import androidx.compose.foundation.layout.BoxWithConstraints
45import androidx.compose.foundation.layout.Column
56import androidx.compose.foundation.layout.PaddingValues
67import androidx.compose.foundation.layout.Row
@@ -13,6 +14,7 @@ import androidx.compose.foundation.lazy.staggeredgrid.StaggeredGridItemSpan
1314import androidx.compose.foundation.lazy.staggeredgrid.items
1415import androidx.compose.runtime.Composable
1516import androidx.compose.runtime.remember
17+ import kotlin.math.max
1618import androidx.compose.ui.Modifier
1719import androidx.compose.ui.res.stringResource
1820import androidx.compose.ui.unit.dp
@@ -80,6 +82,8 @@ fun NotesStaggeredGrid(
8082 item(key = " pinned_notes_body" , contentType = " PinnedSection" , span = StaggeredGridItemSpan .FullLine ) {
8183 PinnedNotesGrid (
8284 notes = pinnedNotes,
85+ adaptiveScaling = adaptiveScaling,
86+ manualColumns = manualColumns,
8387 showSyncStatus = showSyncStatus,
8488 selectedNoteIds = selectedNoteIds,
8589 isSelectionMode = isSelectionMode,
@@ -140,43 +144,38 @@ fun NotesStaggeredGrid(
140144@Composable
141145private fun PinnedNotesGrid (
142146 notes : List <Note >,
147+ adaptiveScaling : Boolean ,
148+ manualColumns : Int ,
143149 showSyncStatus : Boolean ,
144150 selectedNoteIds : Set <String >,
145151 isSelectionMode : Boolean ,
146152 timestampTicker : Long ,
147153 onNoteClick : (Note ) -> Unit ,
148154 onNoteLongClick : (Note ) -> Unit
149155) {
150- val leftNotes = remember(notes) { notes.filterIndexed { i, _ -> i % 2 == 0 } }
151- val rightNotes = remember(notes) { notes.filterIndexed { i, _ -> i % 2 == 1 } }
152- Row (
153- modifier = Modifier .fillMaxWidth(),
154- horizontalArrangement = Arrangement .spacedBy(12 .dp)
155- ) {
156- Column (modifier = Modifier .weight(1f ), verticalArrangement = Arrangement .spacedBy(12 .dp)) {
157- leftNotes.forEach { note ->
158- NoteCardGrid (
159- note = note,
160- showSyncStatus = showSyncStatus,
161- isSelected = selectedNoteIds.contains(note.id),
162- isSelectionMode = isSelectionMode,
163- timestampTicker = timestampTicker,
164- onClick = { onNoteClick(note) },
165- onLongClick = { onNoteLongClick(note) }
166- )
167- }
156+ BoxWithConstraints (modifier = Modifier .fillMaxWidth()) {
157+ val columnCount = if (adaptiveScaling) max(1 , (maxWidth / 150 .dp).toInt()) else manualColumns
158+ val columnedNotes = remember(notes, columnCount) {
159+ (0 until columnCount).map { col -> notes.filterIndexed { i, _ -> i % columnCount == col } }
168160 }
169- Column (modifier = Modifier .weight(1f ), verticalArrangement = Arrangement .spacedBy(12 .dp)) {
170- rightNotes.forEach { note ->
171- NoteCardGrid (
172- note = note,
173- showSyncStatus = showSyncStatus,
174- isSelected = selectedNoteIds.contains(note.id),
175- isSelectionMode = isSelectionMode,
176- timestampTicker = timestampTicker,
177- onClick = { onNoteClick(note) },
178- onLongClick = { onNoteLongClick(note) }
179- )
161+ Row (
162+ modifier = Modifier .fillMaxWidth(),
163+ horizontalArrangement = Arrangement .spacedBy(12 .dp)
164+ ) {
165+ columnedNotes.forEach { columnNotes ->
166+ Column (modifier = Modifier .weight(1f ), verticalArrangement = Arrangement .spacedBy(12 .dp)) {
167+ columnNotes.forEach { note ->
168+ NoteCardGrid (
169+ note = note,
170+ showSyncStatus = showSyncStatus,
171+ isSelected = selectedNoteIds.contains(note.id),
172+ isSelectionMode = isSelectionMode,
173+ timestampTicker = timestampTicker,
174+ onClick = { onNoteClick(note) },
175+ onLongClick = { onNoteLongClick(note) }
176+ )
177+ }
178+ }
180179 }
181180 }
182181 }
0 commit comments