refactor: use immutable collections where flagged by Lint

This commit is contained in:
Harsh Shandilya 2023-03-08 02:23:49 +05:30
parent d7cc55ed53
commit b969b5f0bf
No known key found for this signature in database
11 changed files with 22 additions and 43 deletions

View file

@ -66,6 +66,7 @@ dependencies {
implementation(libs.androidx.work.runtime.ktx)
implementation(libs.coil)
implementation(libs.copydown)
implementation(libs.kotlinx.collections.immutable)
implementation(libs.kotlinx.coroutines.core)
implementation(libs.sqldelight.extensions.coroutines)
}

View file

@ -53,26 +53,4 @@
file="src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png"/>
</issue>
<issue
id="ComposeUnstableCollections"
message="The Compose Compiler cannot infer the stability of a parameter if a List&lt;NavigationItem> is used in it, even if the item type is stable.&#xA;You should use Kotlinx Immutable Collections instead: `items: ImmutableList&lt;NavigationItem>` or create an `@Immutable` wrapper for this class: `@Immutable data class ItemsList(val items: List&lt;NavigationItem>)`&#xA;See https://slackhq.github.io/compose-lints/rules/#avoid-using-unstable-collections for more information."
errorLine1=" items: List&lt;NavigationItem>,"
errorLine2=" ~~~~~~~~~~~~~~~~~~~~">
<location
file="src/main/kotlin/dev/msfjarvis/claw/android/ui/decorations/ClawNavigationBar.kt"
line="32"
column="10"/>
</issue>
<issue
id="ComposeUnstableCollections"
message="The Compose Compiler cannot infer the stability of a parameter if a Map&lt;Month, List&lt;SavedPost>> is used in it, even if the item type is stable.&#xA;You should use Kotlinx Immutable Collections instead: `items: ImmutableMap&lt;Month, List&lt;SavedPost>>` or create an `@Immutable` wrapper for this class: `@Immutable data class ItemsMap(val items: Map&lt;Month, List&lt;SavedPost>>)`&#xA;See https://slackhq.github.io/compose-lints/rules/#avoid-using-unstable-collections for more information."
errorLine1=" items: Map&lt;Month, List&lt;SavedPost>>,"
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~">
<location
file="src/main/kotlin/dev/msfjarvis/claw/android/ui/lists/DatabasePosts.kt"
line="24"
column="10"/>
</issue>
</issues>

View file

@ -55,6 +55,8 @@ import dev.msfjarvis.claw.common.ui.decorations.ClawAppBar
import dev.msfjarvis.claw.common.ui.surfaceColorAtNavigationBarElevation
import dev.msfjarvis.claw.common.urllauncher.UrlLauncher
import dev.msfjarvis.claw.common.user.UserProfile
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.persistentMapOf
import kotlinx.coroutines.launch
@OptIn(
@ -82,7 +84,7 @@ fun LobstersApp(
val hottestPosts = viewModel.hottestPosts.collectAsLazyPagingItems()
val newestPosts = viewModel.newestPosts.collectAsLazyPagingItems()
val savedPosts by viewModel.savedPosts.collectAsState(emptyMap())
val savedPosts by viewModel.savedPosts.collectAsState(persistentMapOf())
LobstersTheme(
dynamicColor = true,
@ -92,7 +94,7 @@ fun LobstersApp(
val systemBarsColor = MaterialTheme.colorScheme.surfaceColorAtNavigationBarElevation()
val backgroundColor = MaterialTheme.colorScheme.background
val navItems =
listOf(
persistentListOf(
NavigationItem(
label = "Hottest",
route = Destinations.Hottest.route,

View file

@ -1,5 +1,5 @@
/*
* Copyright © 2022 Harsh Shandilya.
* Copyright © 2022-2023 Harsh Shandilya.
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
@ -23,13 +23,14 @@ import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.platform.testTag
import androidx.navigation.NavController
import dev.msfjarvis.claw.android.ui.navigation.Destinations
import kotlinx.collections.immutable.ImmutableList
private const val AnimationDuration = 100
@Composable
fun ClawNavigationBar(
navController: NavController,
items: List<NavigationItem>,
items: ImmutableList<NavigationItem>,
isVisible: Boolean,
modifier: Modifier = Modifier,
) {

View file

@ -17,11 +17,12 @@ import dev.msfjarvis.claw.common.posts.PostActions
import dev.msfjarvis.claw.common.ui.decorations.MonthHeader
import dev.msfjarvis.claw.database.local.SavedPost
import java.time.Month
import kotlinx.collections.immutable.ImmutableMap
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun DatabasePosts(
items: Map<Month, List<SavedPost>>,
items: ImmutableMap<Month, List<SavedPost>>,
listState: LazyListState,
postActions: PostActions,
modifier: Modifier = Modifier,

View file

@ -25,6 +25,8 @@ import java.io.IOException
import java.net.HttpURLConnection
import java.time.Month
import javax.inject.Inject
import kotlinx.collections.immutable.ImmutableMap
import kotlinx.collections.immutable.toImmutableMap
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
@ -62,9 +64,9 @@ constructor(
val savedPosts
get() = savedPostsFlow.map(::mapSavedPosts)
private fun mapSavedPosts(items: List<SavedPost>): Map<Month, List<SavedPost>> {
private fun mapSavedPosts(items: List<SavedPost>): ImmutableMap<Month, List<SavedPost>> {
val sorted = items.sortedByDescending { post -> post.createdAt.toLocalDateTime() }
return sorted.groupBy { post -> post.createdAt.toLocalDateTime().month }
return sorted.groupBy { post -> post.createdAt.toLocalDateTime().month }.toImmutableMap()
}
suspend fun isPostSaved(post: SavedPost): Boolean {

View file

@ -36,6 +36,7 @@ dependencies {
implementation(libs.compose.richtext.markdown)
implementation(libs.compose.richtext.material3)
implementation(libs.compose.richtext.ui)
implementation(libs.kotlinx.collections.immutable)
implementation(libs.kotlinx.coroutines.core)
testImplementation(kotlin("test-junit"))
testImplementation(libs.testparameterinjector)

View file

@ -1,15 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<issues format="6" by="lint 7.4.2" type="baseline" client="gradle" dependencies="false" name="AGP (7.4.2)" variant="all" version="7.4.2">
<issue
id="ComposeUnstableCollections"
message="The Compose Compiler cannot infer the stability of a parameter if a List&lt;String> is used in it, even if the item type is stable.&#xA;You should use Kotlinx Immutable Collections instead: `tags: ImmutableList&lt;String>` or create an `@Immutable` wrapper for this class: `@Immutable data class TagsList(val items: List&lt;String>)`&#xA;See https://slackhq.github.io/compose-lints/rules/#avoid-using-unstable-collections for more information."
errorLine1=" tags: List&lt;String>,"
errorLine2=" ~~~~~~~~~~~~">
<location
file="src/main/kotlin/dev/msfjarvis/claw/common/posts/LobstersCard.kt"
line="204"
column="9"/>
</issue>
</issues>

View file

@ -48,6 +48,7 @@ import dev.msfjarvis.claw.model.LinkMetadata
import dev.msfjarvis.claw.model.LobstersPostDetails
import java.time.Instant
import java.time.temporal.TemporalAccessor
import kotlinx.collections.immutable.toImmutableList
@Composable
internal fun CommentsHeader(
@ -71,7 +72,7 @@ internal fun CommentsHeader(
verticalArrangement = Arrangement.spacedBy(8.dp),
) {
PostTitle(title = postDetails.title)
TagRow(tags = postDetails.tags)
TagRow(tags = postDetails.tags.toImmutableList())
Spacer(Modifier.height(4.dp))
if (linkMetadata.url.isNotBlank()) {

View file

@ -47,6 +47,8 @@ import com.google.accompanist.flowlayout.FlowRow
import dev.msfjarvis.claw.common.res.ClawIcons
import dev.msfjarvis.claw.common.ui.NetworkImage
import dev.msfjarvis.claw.database.local.SavedPost
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toImmutableList
@Composable
@OptIn(ExperimentalFoundationApi::class)
@ -104,7 +106,7 @@ fun LobstersCard(
fun PostDetails(post: SavedPost, modifier: Modifier = Modifier) {
Column(modifier = modifier, verticalArrangement = Arrangement.spacedBy(8.dp)) {
PostTitle(title = post.title)
TagRow(tags = post.tags)
TagRow(tags = post.tags.toImmutableList())
Spacer(Modifier.height(4.dp))
Submitter(
text = AnnotatedString("Submitted by ${post.submitterName}"),
@ -201,7 +203,7 @@ private fun CommentsButton(
@Composable
internal fun TagRow(
tags: List<String>,
tags: ImmutableList<String>,
modifier: Modifier = Modifier,
) {
FlowRow(

View file

@ -69,6 +69,7 @@ javax-inject = "javax.inject:javax.inject:1"
jsoup = "org.jsoup:jsoup:1.15.4"
kotest-assertions-core = { module = "io.kotest:kotest-assertions-core", version.ref = "kotest" }
kotest-runner-junit5 = { module = "io.kotest:kotest-runner-junit5", version.ref = "kotest" }
kotlinx-collections-immutable = "org.jetbrains.kotlinx:kotlinx-collections-immutable:0.3.5"
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" }
kotlinx-serialization-core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "serialization" }
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "serialization" }