Skip to content

Commit

Permalink
Refactor: Composable 분리 및 패키지 정리
Browse files Browse the repository at this point in the history
  • Loading branch information
SeongHoonC committed May 26, 2024
1 parent c067790 commit 597ee88
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 150 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sundaegukbap.banchango.presentation.reciperecommend
package com.sundaegukbap.banchango.model

data class Recipe(
val id: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,10 @@ import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.pager.PagerState
import androidx.compose.foundation.pager.VerticalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight.Companion.Bold
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.bumptech.glide.integration.compose.ExperimentalGlideComposeApi
import com.bumptech.glide.integration.compose.GlideImage
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import com.sundaegukbap.banchango.ui.theme.BanchangoTheme
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
Expand All @@ -63,115 +31,3 @@ class MainActivity : ComponentActivity() {
}
}
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun RecipesRecommendScreen(
modifier: Modifier = Modifier,
viewModel: RecipeRecommendViewModel = hiltViewModel(),
) {
val recipesUiState by viewModel.recipes.collectAsStateWithLifecycle()

val pagerState = rememberPagerState(
pageCount = {
recipesUiState.size
}
)
VerticalPager(
modifier = modifier,
state = pagerState,
contentPadding = PaddingValues(vertical = 200.dp, horizontal = 40.dp),
pageSpacing = 40.dp,
) { page ->
RecipeScreen(
pagerState = pagerState,
page = page,
recipesUiState[page],
)
}
}


@OptIn(ExperimentalFoundationApi::class)
@Composable
private fun RecipeScreen(
pagerState: PagerState,
page: Int,
recipe: Recipe,
) {
Box(
modifier = Modifier
.fillMaxSize()
.background(Color.Black),
contentAlignment = Alignment.Center,
) {
NetworkImage(
modifier = Modifier
.fillMaxSize(),
url = recipe.image,
)
Box {
Column {
Text(
recipe.name,
color = Color.White,
fontSize = 24.sp,
style = TextStyle(fontWeight = Bold),
textAlign = TextAlign.Center,
modifier = Modifier.fillMaxWidth(),
)
Text(
text = page.toString(),
textAlign = TextAlign.Center,
modifier = Modifier.fillMaxWidth(),
color = Color.White,
fontSize = 60.sp
)
val coroutineScope = rememberCoroutineScope()
Row(
modifier = Modifier
.align(Alignment.CenterHorizontally)
) {
Button(
modifier = Modifier.padding(end = 16.dp),
onClick = {
coroutineScope.launch {
pagerState.animateScrollToPage(page + 1)
}
}
) {
Text("싫어요")
}
Button(
onClick = {
coroutineScope.launch {
pagerState.animateScrollToPage(page + 1)
}
},
) {
Text("좋아요")
}
}
}
}
}
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
BanchangoTheme {
RecipesRecommendScreen()
}
}

@OptIn(ExperimentalGlideComposeApi::class)
@Composable
fun NetworkImage(modifier: Modifier, url: String) {
GlideImage(
model = url,
contentScale = ContentScale.Crop,
contentDescription = null,
modifier = modifier.fillMaxSize(),
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package com.sundaegukbap.banchango.presentation.reciperecommend

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight.Companion.Bold
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.sundaegukbap.banchango.core.designsystem.NetworkImage
import com.sundaegukbap.banchango.model.Recipe
import com.sundaegukbap.banchango.ui.theme.BanchangoTheme

@Composable
fun RecipeCard(
page: Int,
recipe: Recipe,
onHateClick: (page: Int) -> Unit = {},
onLikeClick: (page: Int) -> Unit = {},
) {
Box(
modifier = Modifier
.fillMaxSize()
.background(Color.Black),
contentAlignment = Alignment.Center,
) {
NetworkImage(
modifier = Modifier
.fillMaxSize(),
url = recipe.image,
)
RecipeInfo(recipe, page, onHateClick, onLikeClick)
}
}

@Composable
private fun RecipeInfo(
recipe: Recipe,
page: Int,
onHateClick: (page: Int) -> Unit,
onLikeClick: (page: Int) -> Unit
) {
Box {
Column {
Text(
recipe.name,
color = Color.White,
fontSize = 24.sp,
style = TextStyle(fontWeight = Bold),
textAlign = TextAlign.Center,
modifier = Modifier.fillMaxWidth(),
)
Text(
text = page.toString(),
textAlign = TextAlign.Center,
modifier = Modifier.fillMaxWidth(),
color = Color.White,
fontSize = 60.sp
)
Row(
modifier = Modifier
.align(Alignment.CenterHorizontally)
) {
Button(
modifier = Modifier.padding(end = 16.dp),
onClick = {
onHateClick(page + 1)
}
) {
Text("싫어요")
}
Button(
onClick = {
onLikeClick(page + 1)
},
) {
Text("좋아요")
}
}
}
}
}


@Preview(showBackground = true)
@Composable
fun RecipeCardPreview() {
BanchangoTheme {
RecipeCard(
page = 1, recipe = Recipe(
id = 1,
name = "간장계란볶음밥",
introduction = "아주 간단하면서 맛있는 계란간장볶음밥으로 한끼식사 만들어보세요. 아이들이 더 좋아할거예요.",
image = "https://recipe1.ezmember.co.kr/cache/recipe/2018/05/26/d0c6701bc673ac5c18183b47212a58571.jpg",
link = "https://www.10000recipe.com/recipe/6889616",
cookingTime = 10,
servings = 2,
difficulty = "Easy",
have = listOf(1, 2, 3, 4, 5),
need = listOf(6, 7, 8, 9, 10),
)
)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.sundaegukbap.banchango.presentation.reciperecommend

import androidx.lifecycle.ViewModel
import com.sundaegukbap.banchango.model.Recipe
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.sundaegukbap.banchango.presentation.reciperecommend

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.pager.VerticalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import kotlinx.coroutines.launch

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun RecipesRecommendScreen(
modifier: Modifier = Modifier,
viewModel: RecipeRecommendViewModel = hiltViewModel(),
) {
val recipesUiState by viewModel.recipes.collectAsStateWithLifecycle()

val pagerState = rememberPagerState(
pageCount = {
recipesUiState.size
}
)
val coroutineScope = rememberCoroutineScope()
VerticalPager(
modifier = modifier,
state = pagerState,
contentPadding = PaddingValues(vertical = 200.dp, horizontal = 40.dp),
pageSpacing = 40.dp,
) { page ->
RecipeCard(
page = page,
recipe = recipesUiState[page],
onLikeClick = {
coroutineScope.launch { pagerState.animateScrollToPage(it) }
},
onHateClick = {
coroutineScope.launch { pagerState.animateScrollToPage(it) }
},
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.sundaegukbap.banchango.repository

import com.sundaegukbap.banchango.model.Recipe

interface RecipeRepository {
fun getRecipeRecommendation(): List<Recipe>
}

0 comments on commit 597ee88

Please sign in to comment.