mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-15 09:57:04 +05:30
refactor(android): rework sorting mechanism and saved posts month header
This commit is contained in:
parent
a23037ce70
commit
c5a3562608
3 changed files with 27 additions and 20 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright © 2021-2023 Harsh Shandilya.
|
* Copyright © 2021-2024 Harsh Shandilya.
|
||||||
* Use of this source code is governed by an MIT-style
|
* Use of this source code is governed by an MIT-style
|
||||||
* license that can be found in the LICENSE file or at
|
* license that can be found in the LICENSE file or at
|
||||||
* https://opensource.org/licenses/MIT.
|
* https://opensource.org/licenses/MIT.
|
||||||
|
@ -28,13 +28,12 @@ import androidx.compose.ui.unit.dp
|
||||||
import dev.msfjarvis.claw.common.posts.PostActions
|
import dev.msfjarvis.claw.common.posts.PostActions
|
||||||
import dev.msfjarvis.claw.common.ui.decorations.MonthHeader
|
import dev.msfjarvis.claw.common.ui.decorations.MonthHeader
|
||||||
import dev.msfjarvis.claw.database.local.SavedPost
|
import dev.msfjarvis.claw.database.local.SavedPost
|
||||||
import java.time.Month
|
|
||||||
import kotlinx.collections.immutable.ImmutableMap
|
import kotlinx.collections.immutable.ImmutableMap
|
||||||
|
|
||||||
@OptIn(ExperimentalFoundationApi::class)
|
@OptIn(ExperimentalFoundationApi::class)
|
||||||
@Composable
|
@Composable
|
||||||
fun DatabasePosts(
|
fun DatabasePosts(
|
||||||
items: ImmutableMap<Month, List<SavedPost>>,
|
items: ImmutableMap<String, List<SavedPost>>,
|
||||||
listState: LazyListState,
|
listState: LazyListState,
|
||||||
postActions: PostActions,
|
postActions: PostActions,
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
|
@ -53,7 +52,7 @@ fun DatabasePosts(
|
||||||
} else {
|
} else {
|
||||||
LazyColumn(state = listState) {
|
LazyColumn(state = listState) {
|
||||||
items.forEach { (month, posts) ->
|
items.forEach { (month, posts) ->
|
||||||
stickyHeader(contentType = "month-header") { MonthHeader(month = month) }
|
stickyHeader(contentType = "month-header") { MonthHeader(label = month) }
|
||||||
items(
|
items(
|
||||||
items = posts,
|
items = posts,
|
||||||
key = { it.shortId },
|
key = { it.shortId },
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright © 2021-2023 Harsh Shandilya.
|
* Copyright © 2021-2024 Harsh Shandilya.
|
||||||
* Use of this source code is governed by an MIT-style
|
* Use of this source code is governed by an MIT-style
|
||||||
* license that can be found in the LICENSE file or at
|
* license that can be found in the LICENSE file or at
|
||||||
* https://opensource.org/licenses/MIT.
|
* https://opensource.org/licenses/MIT.
|
||||||
|
@ -11,6 +11,8 @@ import android.content.Context
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
|
import androidx.compose.ui.text.capitalize
|
||||||
|
import androidx.compose.ui.text.intl.Locale
|
||||||
import androidx.glance.appwidget.updateAll
|
import androidx.glance.appwidget.updateAll
|
||||||
import androidx.lifecycle.AndroidViewModel
|
import androidx.lifecycle.AndroidViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
|
@ -36,7 +38,6 @@ import java.io.InputStream
|
||||||
import java.io.OutputStream
|
import java.io.OutputStream
|
||||||
import java.net.HttpURLConnection
|
import java.net.HttpURLConnection
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
import java.time.Month
|
|
||||||
import java.time.ZoneId
|
import java.time.ZoneId
|
||||||
import java.time.format.DateTimeFormatter
|
import java.time.format.DateTimeFormatter
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
@ -87,10 +88,7 @@ constructor(
|
||||||
get() = newestPostsPager.flow
|
get() = newestPostsPager.flow
|
||||||
|
|
||||||
val savedPosts
|
val savedPosts
|
||||||
get() =
|
get() = savedPostsRepository.savedPosts
|
||||||
savedPostsRepository.savedPosts.map {
|
|
||||||
it.sortedByDescending { post -> post.createdAt.toLocalDateTime() }
|
|
||||||
}
|
|
||||||
|
|
||||||
val savedPostsByMonth
|
val savedPostsByMonth
|
||||||
get() = savedPosts.map(::mapSavedPosts)
|
get() = savedPosts.map(::mapSavedPosts)
|
||||||
|
@ -111,12 +109,25 @@ constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun mapSavedPosts(items: List<SavedPost>): ImmutableMap<Month, List<SavedPost>> {
|
private fun mapSavedPosts(items: List<SavedPost>): ImmutableMap<String, List<SavedPost>> {
|
||||||
val sorted =
|
val sorted =
|
||||||
items.sortedWith { post1, post2 ->
|
items.sortedWith { post1, post2 ->
|
||||||
post2.createdAt.toLocalDateTime().compareTo(post1.createdAt.toLocalDateTime())
|
val post1Date = post1.createdAt.toLocalDateTime()
|
||||||
|
val post2Date = post2.createdAt.toLocalDateTime()
|
||||||
|
if (post2Date.isBefore(post1Date)) {
|
||||||
|
-1
|
||||||
|
} else if (post2Date.isAfter(post1Date)) {
|
||||||
|
1
|
||||||
|
} else {
|
||||||
|
0
|
||||||
}
|
}
|
||||||
return sorted.groupBy { post -> post.createdAt.toLocalDateTime().month }.toImmutableMap()
|
}
|
||||||
|
return sorted
|
||||||
|
.groupBy { post ->
|
||||||
|
val time = post.createdAt.toLocalDateTime()
|
||||||
|
"${time.month.name.lowercase().capitalize(Locale.current)} ${time.year}"
|
||||||
|
}
|
||||||
|
.toImmutableMap()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isPostSaved(post: SavedPost): Boolean {
|
fun isPostSaved(post: SavedPost): Boolean {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright © 2022 Harsh Shandilya.
|
* Copyright © 2022-2024 Harsh Shandilya.
|
||||||
* Use of this source code is governed by an MIT-style
|
* Use of this source code is governed by an MIT-style
|
||||||
* license that can be found in the LICENSE file or at
|
* license that can be found in the LICENSE file or at
|
||||||
* https://opensource.org/licenses/MIT.
|
* https://opensource.org/licenses/MIT.
|
||||||
|
@ -15,18 +15,15 @@ import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.text.capitalize
|
|
||||||
import androidx.compose.ui.text.intl.Locale
|
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import dev.msfjarvis.claw.common.theme.LobstersTheme
|
import dev.msfjarvis.claw.common.theme.LobstersTheme
|
||||||
import dev.msfjarvis.claw.common.ui.preview.DevicePreviews
|
import dev.msfjarvis.claw.common.ui.preview.DevicePreviews
|
||||||
import dev.msfjarvis.claw.common.ui.preview.ThemePreviews
|
import dev.msfjarvis.claw.common.ui.preview.ThemePreviews
|
||||||
import dev.msfjarvis.claw.common.ui.surfaceColorAtNavigationBarElevation
|
import dev.msfjarvis.claw.common.ui.surfaceColorAtNavigationBarElevation
|
||||||
import java.time.Month
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun MonthHeader(
|
fun MonthHeader(
|
||||||
month: Month,
|
label: String,
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
) {
|
) {
|
||||||
Box(
|
Box(
|
||||||
|
@ -36,7 +33,7 @@ fun MonthHeader(
|
||||||
.background(MaterialTheme.colorScheme.surfaceColorAtNavigationBarElevation())
|
.background(MaterialTheme.colorScheme.surfaceColorAtNavigationBarElevation())
|
||||||
) {
|
) {
|
||||||
Text(
|
Text(
|
||||||
text = month.name.lowercase().capitalize(Locale.current),
|
text = label,
|
||||||
style = MaterialTheme.typography.headlineSmall,
|
style = MaterialTheme.typography.headlineSmall,
|
||||||
modifier = Modifier.padding(horizontal = 12.dp, vertical = 12.dp),
|
modifier = Modifier.padding(horizontal = 12.dp, vertical = 12.dp),
|
||||||
)
|
)
|
||||||
|
@ -47,5 +44,5 @@ fun MonthHeader(
|
||||||
@ThemePreviews
|
@ThemePreviews
|
||||||
@Composable
|
@Composable
|
||||||
fun MonthHeaderPreview() {
|
fun MonthHeaderPreview() {
|
||||||
LobstersTheme { MonthHeader(month = Month.APRIL) }
|
LobstersTheme { MonthHeader("April 2023") }
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue