Skip to content

Commit b33de39

Browse files
committed
perf: use immutable collections in HomeViewModel and HomeState
- Update `HomeState` to use `ImmutableList` instead of `List` for `repos` and `installedApps` to improve stability and performance in Compose. - Refactor `HomeViewModel` to use `persistentListOf()` for initial states and `toImmutableList()` when updating repository lists. - Ensure all repository list transformations (filtering, mapping, and pagination) return immutable collections.
1 parent 0609157 commit b33de39

2 files changed

Lines changed: 28 additions & 21 deletions

File tree

feature/home/presentation/src/commonMain/kotlin/zed/rainxch/home/presentation/HomeState.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package zed.rainxch.home.presentation
22

3+
import kotlinx.collections.immutable.ImmutableList
4+
import kotlinx.collections.immutable.persistentListOf
35
import zed.rainxch.core.domain.model.InstalledApp
46
import zed.rainxch.core.presentation.model.DiscoveryRepositoryUi
57
import zed.rainxch.home.domain.model.HomeCategory
68

79
data class HomeState(
8-
val repos: List<DiscoveryRepositoryUi> = emptyList(),
9-
val installedApps: List<InstalledApp> = emptyList(),
10+
val repos: ImmutableList<DiscoveryRepositoryUi> = persistentListOf(),
11+
val installedApps: ImmutableList<InstalledApp> = persistentListOf(),
1012
val isLoading: Boolean = false,
1113
val isLoadingMore: Boolean = false,
1214
val errorMessage: String? = null,

feature/home/presentation/src/commonMain/kotlin/zed/rainxch/home/presentation/HomeViewModel.kt

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package zed.rainxch.home.presentation
22

33
import androidx.lifecycle.ViewModel
44
import androidx.lifecycle.viewModelScope
5+
import kotlinx.collections.immutable.persistentListOf
6+
import kotlinx.collections.immutable.toImmutableList
57
import kotlinx.coroutines.CancellationException
68
import kotlinx.coroutines.Job
79
import kotlinx.coroutines.channels.Channel
@@ -92,13 +94,14 @@ class HomeViewModel(
9294
_state.update { current ->
9395
current.copy(
9496
repos =
95-
current.repos.map { homeRepo ->
96-
val app = installedMap[homeRepo.repository.id]
97-
homeRepo.copy(
98-
isInstalled = app != null,
99-
isUpdateAvailable = app?.isUpdateAvailable ?: false,
100-
)
101-
},
97+
current.repos
98+
.map { homeRepo ->
99+
val app = installedMap[homeRepo.repository.id]
100+
homeRepo.copy(
101+
isInstalled = app != null,
102+
isUpdateAvailable = app?.isUpdateAvailable ?: false,
103+
)
104+
}.toImmutableList(),
102105
isUpdateAvailable = installedMap.any { it.value.isUpdateAvailable },
103106
)
104107
}
@@ -133,7 +136,7 @@ class HomeViewModel(
133136
isLoadingMore = !isInitial,
134137
errorMessage = null,
135138
currentCategory = targetCategory,
136-
repos = if (isInitial) emptyList() else it.repos,
139+
repos = if (isInitial) persistentListOf() else it.repos,
137140
)
138141
}
139142

@@ -200,7 +203,7 @@ class HomeViewModel(
200203
val uniqueList = rawList.distinctBy { it.repository.fullName }
201204

202205
currentState.copy(
203-
repos = uniqueList,
206+
repos = uniqueList.toImmutableList(),
204207
hasMorePages = paginatedRepos.hasMore,
205208
errorMessage =
206209
if (uniqueList.isEmpty() && !paginatedRepos.hasMore) {
@@ -323,11 +326,12 @@ class HomeViewModel(
323326
_state.update { current ->
324327
current.copy(
325328
repos =
326-
current.repos.map { homeRepo ->
327-
homeRepo.copy(
328-
isFavourite = favouritesMap.containsKey(homeRepo.repository.id),
329-
)
330-
},
329+
current.repos
330+
.map { homeRepo ->
331+
homeRepo.copy(
332+
isFavourite = favouritesMap.containsKey(homeRepo.repository.id),
333+
)
334+
}.toImmutableList(),
331335
)
332336
}
333337
}
@@ -341,11 +345,12 @@ class HomeViewModel(
341345
_state.update { current ->
342346
current.copy(
343347
repos =
344-
current.repos.map { homeRepo ->
345-
homeRepo.copy(
346-
isStarred = starredReposById.containsKey(homeRepo.repository.id),
347-
)
348-
},
348+
current.repos
349+
.map { homeRepo ->
350+
homeRepo.copy(
351+
isStarred = starredReposById.containsKey(homeRepo.repository.id),
352+
)
353+
}.toImmutableList(),
349354
)
350355
}
351356
}

0 commit comments

Comments
 (0)