Skip to content
Merged
90 changes: 61 additions & 29 deletions presentation/src/main/java/view/main/Screen/MainScreen.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package view.main.Screen

import androidx.activity.ComponentActivity
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
Expand All @@ -10,46 +11,76 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import model.rank.response.RankResponseModel
import view.main.component.MyRankingComponent
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import view.main.component.MyPageButton
import view.main.component.MyRanking
import view.main.component.RankingList
import view.main.component.TimeComponent
import java.util.UUID
import viewmodel.homes.HomesViewModel
import viewmodel.homes.uistate.HomesMyRankUiState
import viewmodel.homes.uistate.HomesUiState

@Composable
internal fun MainRoute(
navigateToBack: () -> Unit,
onErrorToast: (throwable: Throwable?, message: Int?) -> Unit,
viewModel: HomesViewModel = hiltViewModel(LocalContext.current as ComponentActivity)
) {
val homesUiState by viewModel.homesUiState.collectAsStateWithLifecycle()
val homesMyRankUiState by viewModel.homesMyRankUiState.collectAsStateWithLifecycle()


MainScreen(
homesUiState = homesUiState,
rankListCallBack = viewModel::getRank,
homesMyRankUiState = homesMyRankUiState,
onErrorToast = onErrorToast,
navigateToBack = navigateToBack,
myRankCallBack = viewModel::getMyRank,
)


}

@Composable
fun MainScreen(
modifier: Modifier = Modifier,
rank: Int,
homesUiState: HomesUiState,
myRankCallBack: () -> Unit,
rankListCallBack: () -> Unit,
homesMyRankUiState: HomesMyRankUiState,
onErrorToast: (throwable: Throwable?, message: Int?) -> Unit,
navigateToBack: () -> Unit

) {
LaunchedEffect(Unit) {
myRankCallBack()
rankListCallBack()
}


Column(
modifier = modifier
.fillMaxSize()
.background(color = Color(0xFF1E1E1E)),
verticalArrangement = Arrangement.spacedBy(24.dp, Alignment.Top),
horizontalAlignment = Alignment.CenterHorizontally,
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(start = 316.dp, top = 4.dp, end = 10.dp, bottom = 4.dp),
horizontalArrangement = Arrangement.spacedBy(10.dp, Alignment.Start),
verticalAlignment = Alignment.CenterVertically,

) {
MyPageButton() {}
}
Row(
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically,
Expand All @@ -70,14 +101,15 @@ fun MainScreen(
horizontalAlignment = Alignment.Start,

) {
MyRankingComponent(
rank = rank,
MyRanking(
homesMyRankUiState = homesMyRankUiState,
onErrorToast = onErrorToast
)
}
Row(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 20.dp,),
.padding(horizontal = 20.dp),
horizontalArrangement = Arrangement.spacedBy(10.dp, Alignment.Start),
verticalAlignment = Alignment.CenterVertically,

Expand Down Expand Up @@ -137,16 +169,10 @@ fun MainScreen(
)
)
}
LazyColumn(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 10.dp,),
verticalArrangement = Arrangement.spacedBy(12.dp, Alignment.Top),
horizontalAlignment = Alignment.Start,

) {
//item viewmodel ν•œ ν›„ κ΅¬ν˜„ ν•˜κ² μŠ΅λ‹ˆλ‹€.
}
RankingList(
homeUiState = homesUiState,
onErrorToast = onErrorToast
)
}
}
}
Expand All @@ -157,6 +183,12 @@ fun MainScreen(
@Preview
fun PreviewMainScreen() {
MainScreen(
rank = 1,
homesUiState = HomesUiState.Loading,
rankListCallBack = {},
homesMyRankUiState = HomesMyRankUiState.Loading,
onErrorToast = { _, _ -> },
navigateToBack = {},
myRankCallBack = {},

)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,46 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.kim.presentation.R
import model.myrank.response.MyRankResponseModel
import model.rank.response.RankResponseModel
import viewmodel.homes.uistate.HomesMyRankUiState
import java.util.UUID
@Composable
internal fun MyRanking(
modifier: Modifier = Modifier,
homesMyRankUiState: HomesMyRankUiState,
onErrorToast: (throwable: Throwable?, message: Int?) -> Unit,

){
when(homesMyRankUiState){
HomesMyRankUiState.Loading -> {
Text(text = "λ‘œλ”©μ€‘")
}
is HomesMyRankUiState.Success -> {
val data = homesMyRankUiState.data
MyRankingComponent(
modifier = modifier,
data = data
)
}
is HomesMyRankUiState.Fail -> {
onErrorToast(homesMyRankUiState.throwable,R.string.error)

}
is HomesMyRankUiState.Empty -> {

}
}

}




@Composable
fun MyRankingComponent(
modifier: Modifier = Modifier,
rank: Int
data: MyRankResponseModel
) {
Column(
modifier = modifier
Expand Down Expand Up @@ -55,7 +88,7 @@ fun MyRankingComponent(
Text(
text = stringResource(
R.string.Rank,
rank
data.rank
),
style = TextStyle(
fontSize = 20.sp,
Expand All @@ -70,8 +103,5 @@ fun MyRankingComponent(
@Composable
@Preview
fun PreviewMyRankingComponent() {
MyRankingComponent(
rank = 3

)
}
74 changes: 70 additions & 4 deletions presentation/src/main/java/view/main/component/Rankingcomponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
Expand All @@ -21,9 +24,43 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.kim.presentation.R
import kotlinx.collections.immutable.PersistentList
import kotlinx.collections.immutable.toPersistentList
import model.rank.response.RankResponseModel
import viewmodel.homes.uistate.HomesMyRankUiState
import viewmodel.homes.uistate.HomesUiState
import java.util.UUID

@Composable
internal fun RankingList(
modifier: Modifier = Modifier,
homeUiState: HomesUiState,
onErrorToast: (throwable: Throwable?, message: Int?) -> Unit
) {
when (homeUiState) {
HomesUiState.Loading -> {
Text(text = "λ‘œλ”©μ€‘")
}

is HomesUiState.Success -> {
val list = homeUiState.data

RankingListItem(
modifier = modifier,
list = list.toPersistentList()
)
}

is HomesUiState.Fail -> {
onErrorToast(homeUiState.exception, R.string.error)
}

is HomesUiState.Empty -> {

}
}
}

@Composable
fun RankingComponent(
Expand All @@ -50,12 +87,12 @@ fun RankingComponent(
verticalAlignment = Alignment.CenterVertically,
) {
Text(
text = data.rank.toString(),
text = "${data.rank}μœ„",
style = TextStyle(
fontSize = 16.sp,
fontWeight = FontWeight(700),
color = Color(0xFFFFFFFF),
)
)
)
Spacer(modifier = Modifier.width(20.dp))
Text(
Expand All @@ -64,11 +101,11 @@ fun RankingComponent(
fontSize = 16.sp,
fontWeight = FontWeight(600),
color = Color(0xFFFFFFFF),
)
)
)
Spacer(modifier = Modifier.width(152.dp))
Text(
text = data.penaltyPoint.toString(),
text = "${data.penaltyPoint}점",
style = TextStyle(
fontSize = 14.sp,
fontWeight = FontWeight(600),
Expand All @@ -79,6 +116,35 @@ fun RankingComponent(
}
}

@Composable
internal fun RankingListItem(
modifier: Modifier = Modifier,
list: PersistentList<RankResponseModel>,

) {
Column(modifier = modifier.fillMaxWidth()) {
LazyColumn(
modifier = Modifier
.fillMaxWidth()
.heightIn(max = 10_000.dp)
) {
items(
items = list,
key = { data ->
data.userId
}
) { data ->
RankingComponent(
modifier = Modifier.fillMaxWidth(),
data = data
)
}

}

}
}
Comment on lines +119 to +146
Copy link

Choose a reason for hiding this comment

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

πŸ› οΈ Refactor suggestion

LazyColumn의 높이 μ œν•œμ„ μž¬κ²€ν† ν•΄μ£Όμ„Έμš”.

ν˜„μž¬ μ„€μ •λœ heightIn(max = 10_000.dp)λŠ” λ‹€μŒκ³Ό 같은 λ¬Έμ œκ°€ μžˆμ„ 수 μžˆμŠ΅λ‹ˆλ‹€:

  1. λΆˆν•„μš”ν•˜κ²Œ 큰 κ°’μœΌλ‘œ μ„€μ •λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€
  2. λ©”λͺ¨λ¦¬ μ‚¬μš©λŸ‰μ— 영ν–₯을 쀄 수 μžˆμŠ΅λ‹ˆλ‹€

λ‹€μŒκ³Ό 같이 μˆ˜μ •ν•˜λŠ” 것을 μ œμ•ˆλ“œλ¦½λ‹ˆλ‹€:

 LazyColumn(
     modifier = Modifier
-        .fillMaxWidth()
-        .heightIn(max = 10_000.dp)
+        .fillMaxWidth()
+        .heightIn(max = 300.dp)
 ) {

λ˜λŠ” 슀크둀이 ν•„μš”ν•œ 경우라면:

 LazyColumn(
     modifier = Modifier
-        .fillMaxWidth()
-        .heightIn(max = 10_000.dp)
+        .fillMaxWidth()
+        .weight(1f)
 ) {
πŸ“ Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
@Composable
internal fun RankingListItem(
modifier: Modifier = Modifier,
list: PersistentList<RankResponseModel>,
) {
Column(modifier = modifier.fillMaxWidth()) {
LazyColumn(
modifier = Modifier
.fillMaxWidth()
.heightIn(max = 10_000.dp)
) {
items(
items = list,
key = { data ->
data.userId
}
) { data ->
RankingComponent(
modifier = Modifier.fillMaxWidth(),
data = data
)
}
}
}
}
@Composable
internal fun RankingListItem(
modifier: Modifier = Modifier,
list: PersistentList<RankResponseModel>,
) {
Column(modifier = modifier.fillMaxWidth()) {
LazyColumn(
modifier = Modifier
.fillMaxWidth()
.heightIn(max = 300.dp)
) {
items(
items = list,
key = { data ->
data.userId
}
) { data ->
RankingComponent(
modifier = Modifier.fillMaxWidth(),
data = data
)
}
}
}
}
Suggested change
@Composable
internal fun RankingListItem(
modifier: Modifier = Modifier,
list: PersistentList<RankResponseModel>,
) {
Column(modifier = modifier.fillMaxWidth()) {
LazyColumn(
modifier = Modifier
.fillMaxWidth()
.heightIn(max = 10_000.dp)
) {
items(
items = list,
key = { data ->
data.userId
}
) { data ->
RankingComponent(
modifier = Modifier.fillMaxWidth(),
data = data
)
}
}
}
}
@Composable
internal fun RankingListItem(
modifier: Modifier = Modifier,
list: PersistentList<RankResponseModel>,
) {
Column(modifier = modifier.fillMaxWidth()) {
LazyColumn(
modifier = Modifier
.fillMaxWidth()
.weight(1f)
) {
items(
items = list,
key = { data ->
data.userId
}
) { data ->
RankingComponent(
modifier = Modifier.fillMaxWidth(),
data = data
)
}
}
}
}



@Composable
@Preview
Expand Down
25 changes: 25 additions & 0 deletions presentation/src/main/java/view/main/navigation/MainNavigation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package view.main.navigation

import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import view.main.Screen.MainRoute
import view.main.Screen.MainScreen

const val mainRoute = "main_route"

fun NavController.navigationToMain(){
this.navigate(mainRoute)
}
fun NavGraphBuilder.mainScreen(
navigateToBack: () -> Unit,
onErrorToast: (throwable: Throwable?, message: Int?) -> Unit

){
composable(mainRoute){
MainRoute(
navigateToBack = navigateToBack,
onErrorToast = onErrorToast
)
}
}
5 changes: 3 additions & 2 deletions presentation/src/main/java/viewModel/homes/HomesViewmodel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class HomesViewModel @Inject constructor(
}

is Result.Error -> {
_homesUiState.value = HomesUiState.Fail
_homesUiState.value = HomesUiState.Fail(result.exception)

}

is Result.Success -> {
Expand All @@ -57,7 +58,7 @@ class HomesViewModel @Inject constructor(
}

is Result.Error -> {
_homesMyRankUiState.value = HomesMyRankUiState.Fail
_homesMyRankUiState.value = HomesMyRankUiState.Fail(result.exception,null)
}

is Result.Success -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ sealed interface HomesMyRankUiState {
object Loading : HomesMyRankUiState
object Empty : HomesMyRankUiState
data class Success(val data: MyRankResponseModel) : HomesMyRankUiState
object Fail : HomesMyRankUiState
data class Fail(val throwable: Throwable?, val message: Int?) : HomesMyRankUiState
}
Loading
Loading