Skip to content

Commit

Permalink
feat : Tab에 viewModel 상태 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
chws0508 committed Mar 18, 2024
1 parent 419f456 commit 94865c7
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.withpeace.withpeace.core.domain.model.post

import java.time.LocalDate
import java.time.LocalDateTime

data class Post(
val postId: Long,
val title: String,
val content: String,
val postTopic: PostTopic,
val createDate: LocalDate,
val createDate: LocalDateTime,
val postImageUrl: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,59 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.withpeace.withpeace.core.designsystem.theme.WithpeaceTheme
import com.withpeace.withpeace.core.domain.model.post.Post
import com.withpeace.withpeace.core.domain.model.post.PostTopic
import java.time.LocalDateTime

@Composable
fun PostListRoute(
viewModel: PostListViewModel = hiltViewModel(),
onShowSnackBar: (String) -> Unit,
) {
PostListScreen()
val postList = viewModel.postList.collectAsStateWithLifecycle().value
val currentTopic = viewModel.currentTopic.collectAsStateWithLifecycle().value
PostListScreen(
currentTopic = currentTopic,
postList,
onTopicChanged = viewModel::onTopicChanged,
)
}

@Composable
fun PostListScreen(

currentTopic: PostTopic,
postList: List<Post>,
onTopicChanged: (PostTopic) -> Unit = {},
) {
Column(modifier = Modifier.fillMaxSize()) {
Spacer(modifier = Modifier.height(8.dp))
TopicTabs {}
TopicTabs(
currentTopic = currentTopic,
onClick = onTopicChanged,
tabPosition = PostTopic.findIndex(currentTopic),
)
}
}

@Preview(showBackground = true)
@Composable
private fun PostListScreenPreview() {
WithpeaceTheme {
PostListScreen()
PostListScreen(
currentTopic = PostTopic.FREEDOM,
postList = List(10) {
Post(
postId = 2049,
title = "periculis",
content = "pellentesque",
postTopic = PostTopic.INFORMATION,
createDate = LocalDateTime.now(),
postImageUrl = "https://duckduckgo.com/?q=verterem",
)
},
)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,42 @@
package com.withpeace.withpeace.feature.postlist

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.withpeace.withpeace.core.domain.model.post.Post
import com.withpeace.withpeace.core.domain.model.post.PostTopic
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import javax.inject.Inject

class PostListViewModel @Inject constructor(
class PostListViewModel @Inject constructor() : ViewModel() {

) : ViewModel() {
private val _currentTopic = MutableStateFlow(PostTopic.FREEDOM)
val currentTopic = _currentTopic.asStateFlow()

val postList = currentTopic.map {
getPost(it)
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), emptyList())

fun onTopicChanged(postTopic: PostTopic) {
_currentTopic.update { postTopic }
}

// API 나오기 전까지 임시로 이렇게 하겠습니다!
private fun getPost(postTopic: PostTopic) = List(10) {
Post(
postId = 1746,
title = postTopic.toString(),
content = postTopic.toString(),
postTopic = postTopic,
createDate = LocalDateTime.of(LocalDate.of(2024, 3, 18), LocalTime.of(12, 0, 0)),
postImageUrl = "http://withpeace.s3-website.kr.object.ncloudstorage.com/userProfile/1",
)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.withpeace.withpeace.feature.postlist

import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material3.Icon
Expand All @@ -9,7 +10,6 @@ import androidx.compose.material3.TabRowDefaults
import androidx.compose.material3.TabRowDefaults.tabIndicatorOffset
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
Expand All @@ -20,18 +20,20 @@ import com.withpeace.withpeace.core.ui.PostTopicUiState

@Composable
fun TopicTabs(
currentTopic: PostTopic = PostTopic.FREEDOM,
currentTopic: PostTopic,
tabPosition: Int,
onClick: (PostTopic) -> Unit,
) {
val index = rememberSaveable { PostTopic.findIndex(currentTopic) }
TabRow(
modifier = Modifier.wrapContentSize(),
selectedTabIndex = index,
selectedTabIndex = tabPosition,
containerColor = WithpeaceTheme.colors.SystemWhite,
indicator = { tabPositions ->
TabRowDefaults.PrimaryIndicator(
modifier = Modifier.tabIndicatorOffset(tabPositions[index]),
color = WithpeaceTheme.colors.MainPink
TabRowDefaults.SecondaryIndicator(
modifier = Modifier
.tabIndicatorOffset(tabPositions[tabPosition])
.padding(horizontal = 16.dp),
color = WithpeaceTheme.colors.MainPink,
)
},
) {
Expand Down Expand Up @@ -59,3 +61,4 @@ fun TopicTabs(
}
}
}

0 comments on commit 94865c7

Please sign in to comment.