Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(FEAT)[#294] UI 코드 정리 #295

Merged
merged 3 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ import com.droidknights.app.core.designsystem.theme.LocalDarkTheme
fun rememberPainterResource(
@DrawableRes lightId: Int,
@DrawableRes darkId: Int = lightId,
): Painter = painterResource(id = if (LocalDarkTheme.current) darkId else lightId)
): Painter =
painterResource(id = if (LocalDarkTheme.current) darkId else lightId)
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ import com.droidknights.app.core.designsystem.theme.DuskGray
import com.droidknights.app.core.designsystem.theme.KnightsTheme
import com.droidknights.app.core.designsystem.theme.PaleGray
import com.droidknights.app.core.designsystem.theme.Purple01
import com.droidknights.app.feature.bookmark.component.BookmarkCard
import com.droidknights.app.feature.bookmark.component.BookmarkItem
import com.droidknights.app.feature.bookmark.component.BookmarkTimelineItem
import com.droidknights.app.feature.bookmark.model.BookmarkItemUiState
import com.droidknights.app.feature.bookmark.model.BookmarkUiState
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.flow.collectLatest
Expand Down Expand Up @@ -109,6 +114,7 @@ private fun BookmarkScreen(
items = bookmarkItems,
key = { item -> item.session.id }
) { itemState ->
/** 편집모드 나타내는 Trailing 컨텐츠를 이곳에 구현하세요 */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

BookmarkItem(
leadingContent = @Composable {
BookmarkTimelineItem(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,29 @@ package com.droidknights.app.feature.bookmark
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.droidknights.app.core.domain.usecase.GetBookmarkedSessionsUseCase
import com.droidknights.app.feature.bookmark.model.BookmarkItemUiState
import com.droidknights.app.feature.bookmark.model.BookmarkUiState
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import kotlinx.collections.immutable.toPersistentList
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class BookmarkViewModel @Inject constructor(
private val getBookmarkedSessionsUseCase: GetBookmarkedSessionsUseCase,
) : ViewModel() {

private val _errorFlow = MutableSharedFlow<Throwable>()
val errorFlow: SharedFlow<Throwable> get() = _errorFlow
val errorFlow = _errorFlow.asSharedFlow()

private val _bookmarkUiState = MutableStateFlow<BookmarkUiState>(BookmarkUiState.Loading)
val bookmarkUiState: StateFlow<BookmarkUiState> = _bookmarkUiState
val bookmarkUiState = _bookmarkUiState.asStateFlow()

init {
viewModelScope.launch {
Expand All @@ -42,6 +45,7 @@ class BookmarkViewModel @Inject constructor(
isEditMode = false
)
}
.toPersistentList()
)
}

Expand All @@ -55,6 +59,7 @@ class BookmarkViewModel @Inject constructor(
isEditMode = bookmarkUiState.isEditButtonSelected
)
}
.toPersistentList()
)
}
}
Expand All @@ -72,7 +77,11 @@ class BookmarkViewModel @Inject constructor(

_bookmarkUiState.value = state.copy(
isEditButtonSelected = state.isEditButtonSelected.not(),
bookmarks = state.bookmarks.map { it.copy(isEditMode = !it.isEditMode.not()) }
bookmarks = state.bookmarks
.map {
it.copy(isEditMode = !it.isEditMode.not())
}
.toPersistentList()
)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.droidknights.app.feature.bookmark
package com.droidknights.app.feature.bookmark.component

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.droidknights.app.feature.bookmark
package com.droidknights.app.feature.bookmark.component

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.animateContentSize
Expand Down Expand Up @@ -47,13 +47,19 @@ internal fun BookmarkItem(
drawLine(
color = LightGray,
start = Offset(x = leadingContentWidth.toPx() / 2, y = 0F),
end = Offset(x = leadingContentWidth.toPx() / 2, y = (this.size.height / 2) - (leadingContentHeight.toPx() / 2)),
end = Offset(
x = leadingContentWidth.toPx() / 2,
y = (this.size.height / 2) - (leadingContentHeight.toPx() / 2)
),
strokeWidth = 1.dp.toPx()
)

drawLine(
color = LightGray,
start = Offset(x = leadingContentWidth.toPx() / 2, y = (this.size.height / 2) + (leadingContentHeight.toPx() / 2)),
start = Offset(
x = leadingContentWidth.toPx() / 2,
y = (this.size.height / 2) + (leadingContentHeight.toPx() / 2)
),
end = Offset(x = leadingContentWidth.toPx() / 2, y = this.size.height),
strokeWidth = 1.dp.toPx()
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.droidknights.app.feature.bookmark
package com.droidknights.app.feature.bookmark.component

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
Expand All @@ -21,6 +21,7 @@ import androidx.compose.ui.unit.dp
import com.droidknights.app.core.designsystem.theme.KnightsTheme
import com.droidknights.app.core.designsystem.theme.Purple01
import com.droidknights.app.core.designsystem.theme.White
import com.droidknights.app.feature.bookmark.R
import java.time.LocalTime
import java.time.format.DateTimeFormatter

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.droidknights.app.feature.bookmark
package com.droidknights.app.feature.bookmark.model

import com.droidknights.app.core.model.Session
import java.time.LocalTime
Expand All @@ -11,6 +11,7 @@ data class BookmarkItemUiState(
val session: Session,
val isEditMode: Boolean,
) {

val sequence: Int
get() = index + 1

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.droidknights.app.feature.bookmark
package com.droidknights.app.feature.bookmark.model

import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf

@Stable
sealed interface BookmarkUiState {
Expand All @@ -12,6 +14,6 @@ sealed interface BookmarkUiState {
@Immutable
data class Success(
val isEditButtonSelected: Boolean = false,
val bookmarks: List<BookmarkItemUiState> = listOf(),
val bookmarks: ImmutableList<BookmarkItemUiState> = persistentListOf(),
) : BookmarkUiState
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import com.droidknights.app.core.model.Contributor
import com.droidknights.app.feature.contributor.model.ContributorsUiState
import kotlinx.collections.immutable.persistentListOf
import org.junit.Before
import org.junit.Rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import com.droidknights.app.core.designsystem.theme.LocalDarkTheme
import com.droidknights.app.core.designsystem.theme.Neon01
import com.droidknights.app.core.designsystem.theme.Neon05
import com.droidknights.app.core.model.Contributor
import com.droidknights.app.feature.contributor.model.ContributorsUiState
import com.valentinilk.shimmer.shimmer
import kotlinx.collections.immutable.persistentListOf
import kotlinx.coroutines.flow.collectLatest
Expand Down Expand Up @@ -161,6 +162,7 @@ private fun ContributorList(
item {
TopBanner()
}

when (uiState) {
ContributorsUiState.Loading -> {
items(SHIMMERING_ITEM_COUNT) {
Expand All @@ -181,6 +183,7 @@ private fun ContributorList(
}
}
}

item {
Footer(modifier = Modifier.padding(bottom = 16.dp))
}
Expand All @@ -193,15 +196,15 @@ private fun ContributorItem(
modifier: Modifier = Modifier,
) {
val uriHandler = LocalUriHandler.current
val shimmerModifier =
if (contributor == null) {
Modifier
.clip(RoundedCornerShape(10.dp))
.shimmer()
.background(color = MaterialTheme.colorScheme.outline)
} else {
Modifier
}
val shimmerModifier = if (contributor == null) {
Modifier
.clip(RoundedCornerShape(10.dp))
.shimmer()
.background(color = MaterialTheme.colorScheme.outline)
} else {
Modifier
}

val placeholder = rememberPainterResource(
lightId = R.drawable.ic_contributor_placeholder_lightmode,
darkId = R.drawable.ic_contributor_placeholder_darkmode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,33 @@ package com.droidknights.app.feature.contributor
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.droidknights.app.core.domain.usecase.GetContributorsUseCase
import com.droidknights.app.feature.contributor.model.ContributorsUiState
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import kotlinx.collections.immutable.toPersistentList
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import javax.inject.Inject

@HiltViewModel
class ContributorViewModel @Inject constructor(
getContributorsUseCase: GetContributorsUseCase,
) : ViewModel() {

private val _errorFlow = MutableSharedFlow<Throwable>()
val errorFlow: SharedFlow<Throwable> get() = _errorFlow
val errorFlow = _errorFlow.asSharedFlow()

val uiState: StateFlow<ContributorsUiState> =
flow { emit(getContributorsUseCase().toPersistentList()) }
.map(ContributorsUiState::Contributors)
.catch { throwable -> _errorFlow.emit(throwable) }
.catch { throwable ->
_errorFlow.emit(throwable)
}
.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5_000),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.droidknights.app.feature.contributor
package com.droidknights.app.feature.contributor.model

import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import com.droidknights.app.core.model.Contributor
import kotlinx.collections.immutable.PersistentList
import kotlinx.collections.immutable.ImmutableList

@Stable
sealed interface ContributorsUiState {
Expand All @@ -13,6 +13,6 @@ sealed interface ContributorsUiState {

@Immutable
data class Contributors(
val contributors: PersistentList<Contributor>,
val contributors: ImmutableList<Contributor>,
) : ContributorsUiState
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import app.cash.turbine.test
import com.droidknights.app.core.domain.usecase.GetContributorsUseCase
import com.droidknights.app.core.model.Contributor
import com.droidknights.app.core.testing.rule.MainDispatcherRule
import com.droidknights.app.feature.contributor.model.ContributorsUiState
import io.mockk.coEvery
import io.mockk.mockk
import kotlin.test.assertIs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import com.droidknights.app.core.model.Sponsor
import com.droidknights.app.feature.home.component.SponsorCard
import com.droidknights.app.feature.home.model.SponsorsUiState
import kotlinx.collections.immutable.persistentListOf
import org.junit.Before
import org.junit.Rule
import org.junit.Test
Expand Down Expand Up @@ -60,7 +63,7 @@ class SponsorScreenTest {

companion object {
private val sponsors =
listOf(
persistentListOf(
Sponsor(
name = "Sponsor1",
homepage = "https://www.instagram.com/droid_knights",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.droidknights.app.feature.home.component.ContributorCard
import com.droidknights.app.feature.home.component.SessionCard
import com.droidknights.app.feature.home.component.SponsorCard
import com.droidknights.app.feature.home.model.SponsorsUiState
import kotlinx.coroutines.flow.collectLatest

@Composable
Expand Down Expand Up @@ -58,3 +64,17 @@ private fun HomeScreen(
SponsorCard(uiState = sponsorsUiState)
}
}

@Preview
@Composable
private fun PreviewHomeScreen(
@PreviewParameter(SponsorsUiStatePreviewParameterProvider::class)
sponsorsUiState: SponsorsUiState,
) {
HomeScreen(
padding = PaddingValues(),
sponsorsUiState = sponsorsUiState,
onSessionClick = {},
onContributorClick = {},
)
}
Loading
Loading