Skip to content

Commit 1c039af

Browse files
committed
[refactor]: 저장페이지 -> 뷰모델 상태관리 일관성 있게끔 수정, 네이밍 수정(#26)
1 parent 1e22cd1 commit 1c039af

3 files changed

Lines changed: 66 additions & 61 deletions

File tree

app/src/main/java/com/texthip/thip/ui/myPage/SavedScreen.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ fun FeedContent(viewModel: SavedFeedViewModel = viewModel()) {
144144

145145
@Composable
146146
fun BookContent(viewModel: SavedBookViewModel = viewModel()) {
147-
val books = viewModel.bookList
147+
val books by viewModel.bookList.collectAsState()
148148

149149
LazyColumn {
150150
items(items = books, key = { it.id }) { book ->

app/src/main/java/com/texthip/thip/ui/myPage/viewmodel/SavedBookViewModel.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@ import androidx.compose.runtime.mutableStateOf
55
import androidx.compose.runtime.setValue
66
import androidx.lifecycle.ViewModel
77
import com.texthip.thip.ui.myPage.mock.BookItem
8+
import kotlinx.coroutines.flow.MutableStateFlow
9+
import kotlinx.coroutines.flow.StateFlow
10+
import kotlinx.coroutines.flow.map
811

912
class SavedBookViewModel : ViewModel() {
1013

11-
var bookList by mutableStateOf<List<BookItem>>(emptyList())
12-
private set
14+
private val _bookList = MutableStateFlow<List<BookItem>>(emptyList())
15+
val bookList: StateFlow<List<BookItem>> = _bookList
16+
1317
init {
1418
loadMockBooks()
1519
}
1620

1721
private fun loadMockBooks() {
18-
bookList = listOf(
22+
_bookList.value = listOf(
1923
BookItem(
2024
id = 1,
2125
title = "이기적 유전자",
@@ -84,7 +88,7 @@ class SavedBookViewModel : ViewModel() {
8488
}
8589

8690
fun toggleBookmark(id: Int) {
87-
bookList = bookList.map {
91+
_bookList.value = _bookList.value.map {
8892
if (it.id == id) it.copy(isSaved = !it.isSaved) else it
8993
}
9094
}

app/src/main/java/com/texthip/thip/ui/myPage/viewmodel/SavedFeedViewModel.kt

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -11,75 +11,76 @@ class SavedFeedViewModel: ViewModel() {
1111
listOf(
1212
FeedItem(
1313
id = 1,
14-
user_profile_image = R.drawable.character_art,
15-
user_name = "user",
16-
user_role = "학생",
17-
book_title = "라랄ㄹ라라",
18-
auth_name = "야야야",
19-
time_ago = 15,
14+
userProfileImage = R.drawable.character_art,
15+
userName = "user",
16+
userRole = "학생",
17+
bookTitle = "라랄ㄹ라라",
18+
authName = "야야야",
19+
timeAgo = 15,
2020
content = "진짜최공진짜최공진차최공진짜최공진짜최공진차최공진짜최공진짜최공진차최공진짜최공진짜최공진차최공진짜최공진짜최공진차최공진짜최공진짜최공진차최공진짜최공진짜최공진차최공",
21-
like_count = 25,
22-
comment_count = 4,
23-
is_liked = false,
24-
is_saved = true
21+
likeCount = 25,
22+
commentCount = 4,
23+
isLiked = false,
24+
isSaved = true
2525
),
2626
FeedItem(
2727
id = 2,
28-
user_profile_image = R.drawable.character_art,
29-
user_name = "user",
30-
user_role = "학생",
31-
book_title = "라랄ㄹ라라",
32-
auth_name = "야야야",
33-
time_ago = 15,
34-
content = "진짜최공진짜최공진차최공",
35-
like_count = 25,
36-
comment_count = 4,
37-
is_liked = false,
38-
is_saved = true,
28+
userProfileImage = R.drawable.character_art,
29+
userName = "user",
30+
userRole = "학생",
31+
bookTitle = "라랄ㄹ라라",
32+
authName = "야야야",
33+
timeAgo = 15,
34+
content = "너무 재밌네요..",
35+
likeCount = 25,
36+
commentCount = 4,
37+
isLiked = false,
38+
isSaved = true,
3939
imageUrl = R.drawable.bookcover_sample
4040
),
4141
FeedItem(
4242
id = 3,
43-
user_profile_image = R.drawable.character_art,
44-
user_name = "user",
45-
user_role = "학생",
46-
book_title = "라랄ㄹ라라",
47-
auth_name = "야야야",
48-
time_ago = 15,
49-
content = "진짜최공진짜최공진차최공",
50-
like_count = 25,
51-
comment_count = 4,
52-
is_liked = false,
53-
is_saved = true
43+
userProfileImage = R.drawable.character_art,
44+
userName = "user",
45+
userRole = "학생",
46+
bookTitle = "라랄ㄹ라라",
47+
authName = "야야야",
48+
timeAgo = 15,
49+
content = "너무 재밌네요..",
50+
likeCount = 25,
51+
commentCount = 4,
52+
isLiked = false,
53+
isSaved = true,
54+
imageUrl = R.drawable.bookcover_sample
5455
),
5556
FeedItem(
5657
id = 4,
57-
user_profile_image = R.drawable.character_literature,
58-
user_name = "user",
59-
user_role = "학생",
60-
book_title = "라랄ㄹ라라",
61-
auth_name = "야야야",
62-
time_ago = 15,
58+
userProfileImage = R.drawable.character_art,
59+
userName = "user",
60+
userRole = "학생",
61+
bookTitle = "책이름책이름",
62+
authName = "저자이름저자이름",
63+
timeAgo = 25,
6364
content = "진짜최공진짜최공진차최공진짜최공진짜최공진차최공진짜최공진짜최공진차최공진짜최공진짜최공진차최공진짜최공진짜최공진차최공진짜최공진짜최공진차최공진짜최공진짜최공진차최공",
64-
like_count = 25,
65-
comment_count = 4,
66-
is_liked = false,
67-
is_saved = true
65+
likeCount = 25,
66+
commentCount = 4,
67+
isLiked = false,
68+
isSaved = true,
69+
imageUrl = R.drawable.bookcover_sample
6870
),
6971
FeedItem(
7072
id = 5,
71-
user_profile_image = R.drawable.character_sociology,
72-
user_name = "user",
73-
user_role = "학생",
74-
book_title = "라랄ㄹ라라",
75-
auth_name = "야야야",
76-
time_ago = 15,
77-
content = "진짜최공진짜최공진차최공",
78-
like_count = 25,
79-
comment_count = 4,
80-
is_liked = false,
81-
is_saved = true,
82-
imageUrl = R.drawable.bookcover_sample
73+
userProfileImage = R.drawable.character_art,
74+
userName = "user",
75+
userRole = "학생",
76+
bookTitle = "책이름책이름",
77+
authName = "저자이름저자이름",
78+
timeAgo = 25,
79+
content = "진짜최공진짜최공진차최공진짜최공진짜최공진차최공진짜최공진짜최공진차최공진짜최공진짜최공진차최공진짜최공진짜최공진차최공진짜최공진짜최공진차최공진짜최공진짜최공진차최공",
80+
likeCount = 25,
81+
commentCount = 4,
82+
isLiked = true,
83+
isSaved = true
8384
),
8485

8586

@@ -89,13 +90,13 @@ class SavedFeedViewModel: ViewModel() {
8990

9091
fun toggleBookmark(id: Int) {
9192
_feeds.value = _feeds.value.map {
92-
if (it.id == id) it.copy(is_saved = !it.is_saved) else it
93+
if (it.id == id) it.copy(isSaved = !it.isSaved) else it
9394
}
9495
}
9596

9697
fun toggleLike(id: Int) {
9798
_feeds.value = _feeds.value.map {
98-
if (it.id == id) it.copy(is_liked = !it.is_liked) else it
99+
if (it.id == id) it.copy(isLiked = !it.isLiked) else it
99100
}
100101
}
101102
}

0 commit comments

Comments
 (0)