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

πŸ”€ :: (#140) - 주식 μ˜ˆμ•½ 맀도λ₯Ό ν•˜λŠ” 뢀뢄을 κ΅¬ν˜„ν–ˆμŠ΅λ‹ˆλ‹€ #141

Open
wants to merge 16 commits into
base: develop
Choose a base branch
from
Open
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 @@ -36,10 +36,10 @@ import com.jusiCool.presentation.search.screen.searchRoute
import com.jusiCool.presentation.splash.screen.splashRoute
import com.jusiCool.presentation.stockDetail.screen.navigateToStockDetail
import com.jusiCool.presentation.stockDetail.screen.stockDetailRoute
import com.jusiCool.presentation.stockBuying.screen.stockBuyingRoute
import com.jusiCool.presentation.stockReservationBuying.screen.stockReservationBuyingRoute
import com.jusiCool.presentation.stockBuy.screen.stockBuyingRoute
import com.jusiCool.presentation.stockReservationBuy.screen.stockReservationBuyRoute
import com.jusiCool.presentation.stockReservationSelling.screen.stockReservationSellingRoute
import com.jusiCool.presentation.stockReservationBuying.screen.stockSell.screen.stockSellingRoute
import com.jusiCool.presentation.stockSell.screen.stockSellingRoute

@Composable
fun JusiCool_Android_NavHost(
Expand Down Expand Up @@ -107,7 +107,7 @@ fun JusiCool_Android_NavHost(
navigateToOrderHistory = navController::navigateToOrderHistory,
)

stockReservationBuyingRoute(
stockReservationBuyRoute(
navigateToStockDetail = navController::navigateToStockDetail,
navigateToOrderHistory = navController::navigateToOrderHistory,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jusiCool.presentation.stockBuying.screen
package com.jusiCool.presentation.stockBuy.screen

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
package com.jusiCool.presentation.stockReservationBuy.screen

import androidx.activity.ComponentActivity
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.mutableLongStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusManager
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import com.example.design_system.component.button.ButtonState
import com.example.design_system.component.button.JDSButton
import com.example.design_system.component.modifier.clickableSingle.clickableSingle
import com.example.design_system.component.textfield.JDSTextField
import com.example.design_system.component.topbar.JDSArrowTopBar
import com.example.design_system.icon_image.icon.LeftArrowIcon
import com.example.design_system.icon_image.icon.RightArrowIcon
import com.example.design_system.icon_image.image.CostImage
import com.example.design_system.theme.JDSTypography
import com.example.design_system.theme.JusiCoolAndroidTheme
import com.example.design_system.theme.color.JDSColor
import com.jusiCool.presentation.main.component.EntireStocksData
import com.jusiCool.presentation.main.component.MyAccountData
import com.jusiCool.presentation.main.screen.tempMyAccountData
import com.jusiCool.presentation.stockReservationBuy.viewmodel.StockReservationBuyViewModel
import com.jusiCool.presentation.utill.formatStockPrice

const val stockReservationBuyRoute = "stockReservationBuyRoute"

fun NavController.navigationToStockReservationBuy(id: String) {
this.navigate("${stockReservationBuyRoute}/${id}")
}

fun NavGraphBuilder.stockReservationBuyRoute(
navigateToStockDetail: (String) -> Unit,
navigateToOrderHistory: () -> Unit,
) {
composable("$stockReservationBuyRoute}/{id}") { backStackEntry ->
val id = backStackEntry.arguments?.getString("id")
if (id != null) {
StockReservationBuyRoute(
id = id,
navigateToStockDetail = navigateToStockDetail,
navigateToOrderHistory = navigateToOrderHistory
)
}
}
}

@Composable
internal fun StockReservationBuyRoute(
modifier: Modifier = Modifier,
viewModel: StockReservationBuyViewModel = hiltViewModel(LocalContext.current as ComponentActivity),
id: String,
navigateToStockDetail: (String) -> Unit,
navigateToOrderHistory: () -> Unit,
) {
val focusManager = LocalFocusManager.current

StockReservationBuyScreen(
modifier = modifier,
id = id,
myAccountData = tempMyAccountData,
entireStocksData = EntireStocksData("", "", 10000, 8160, 6, 4.9f),
navigateToStockDetail = navigateToStockDetail,
navigateToOrderHistory = navigateToOrderHistory,
focusManager = focusManager,
postStock = viewModel.postStock.longValue,
postReserveStock = viewModel.postReserveStock.longValue,
postBuyStock = {num, goal_price ->
viewModel.postBuyStock(
stockId = id,
num = num,
goal_price = goal_price
)
viewModel.postStock.longValue = 0
viewModel.postReserveStock.longValue = 0
}
)
}

@Composable
internal fun StockReservationBuyScreen(
modifier: Modifier = Modifier,
id: String,
myAccountData: MyAccountData,
entireStocksData: EntireStocksData,
postBuyStock: (num: Long, goal_price: Long) -> Unit,
navigateToStockDetail: (String) -> Unit,
navigateToOrderHistory: () -> Unit,
focusManager: FocusManager,
postStock: Long,
postReserveStock: Long
) {
val (stockReservationTextState, setStockReservationTextState) = remember { mutableLongStateOf(postStock) }
val (stockTextState, setStockTextState) = remember { mutableLongStateOf(postReserveStock) }
val (pager, setPager) = remember { mutableStateOf(1) }

CompositionLocalProvider(LocalFocusManager provides focusManager) {
JusiCoolAndroidTheme { colors, _ ->
Column(
modifier = modifier
.fillMaxSize()
.background(color = colors.WHITE)
.pointerInput(Unit) {
detectTapGestures {
focusManager.clearFocus()
}
},
horizontalAlignment = Alignment.CenterHorizontally
) {
JDSArrowTopBar(
startIcon = {
LeftArrowIcon(modifier = Modifier.clickableSingle {
if (pager == 2) setPager(1)
else navigateToStockDetail(id)
})
},
betweenText = "주식 ꡬ맀"
)

when (pager) {
1 -> {
Spacer(modifier = Modifier.height(40.dp))

JDSTextField(
modifier = Modifier.padding(horizontal = 24.dp),
textState = stockReservationTextState.toString(),
placeHolder = "μ˜ˆμ•½ κΈˆμ•‘μ„ λ‹¬μ„±ν–ˆμ„ λ•Œ 주식을 κ΅¬λ§€ν•΄μš”",
label = "μ˜ˆμ•½ κΈˆμ•‘μ„ μž…λ ₯ν•˜μ„Έμš”",
helperText = "μ§€κΈˆ 주식 가격: ${entireStocksData.myStockPrice.formatStockPrice()} P",
onTextChange = { setStockReservationTextState(it.toLong()) }
)

Spacer(modifier = Modifier.weight(1f))

JDSButton(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 20.dp, vertical = 32.dp),
state = if (stockReservationTextState == 0L) ButtonState.Disable else ButtonState.Enable,
text = "λ‹€μŒ",
onClick = { setPager(2) }
)
}

2 -> {
Spacer(modifier = Modifier.height(40.dp))

JDSTextField(
modifier = Modifier.padding(horizontal = 24.dp),
textState = stockTextState.toString(),
placeHolder = "μ΅œλŒ€ Nμ£Ό ꡬ맀 κ°€λŠ₯",
label = "λͺ‡ μ£Ό κ΅¬λ§€ν• κΉŒμš”?",
helperText = "보유 포인트 ${myAccountData.point.formatStockPrice()} P",
placerHolderShare = true,
onTextChange = { setStockTextState(it.toLong()) }
)

Spacer(modifier = Modifier.weight(1f))

JDSButton(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 20.dp, vertical = 32.dp),
state = if (stockTextState == 0L) ButtonState.Disable else ButtonState.Enable,
text = "ꡬ맀 ν•˜κΈ°",
onClick = {
postBuyStock(
stockTextState,
stockReservationTextState
)
setPager(3)
}
)
}

3 -> {
Column(
modifier = modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally
) {
Spacer(modifier = Modifier.height(120.dp))

CostImage(modifier = Modifier.size(177.dp))

Text(
text = "${entireStocksData.stockName} ${stockTextState.toInt().formatStockPrice()}μ£Ό\n" +
"${(stockTextState.toInt() * stockReservationTextState.toInt()).formatStockPrice()}P ꡬ맀 μ˜ˆμ•½ 성곡",
style = JDSTypography.subTitle,
color = JDSColor.Black,
textAlign = TextAlign.Center
)

Spacer(modifier = Modifier.height(16.dp))

Row(
modifier = Modifier.clickableSingle { navigateToOrderHistory() },
horizontalArrangement = Arrangement.spacedBy(4.dp),
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = "ꡬ맀 λ‚΄μ—­ λ³΄λŸ¬κ°€κΈ°",
style = JDSTypography.label,
color = JDSColor.GRAY600
)

RightArrowIcon()
}
}
}
}
}
}
}
}


@Preview
@Composable
fun StockReservationBuyingScreenPreview() {
StockReservationBuyScreen(
id = "",
myAccountData = tempMyAccountData,
entireStocksData = EntireStocksData("", "", 10000, 8160, 23, 9.3f),
navigateToStockDetail = {},
navigateToOrderHistory = {},
focusManager = LocalFocusManager.current,
postStock = 0,
postReserveStock = 0,
postBuyStock = {_, _ ->}
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.jusiCool.presentation.stockReservationBuy.viewmodel

import androidx.compose.runtime.mutableLongStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.jusiCool.domain.model.stock.request.BuyStockRequestModel
import com.jusiCool.domain.usecase.stock.BuyStockReserveUseCase
import com.jusiCool.presentation.utill.Event
import com.jusiCool.presentation.utill.errorHandling
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class StockReservationBuyViewModel @Inject constructor(
private val buyStockReserveUseCase: BuyStockReserveUseCase
) : ViewModel() {

private val _buyStockReserveResponse = MutableStateFlow<Event<BuyStockReserveUseCase>>(Event.Loading)
val buyStockReserveResponse = _buyStockReserveResponse.asStateFlow()

var postStock = mutableLongStateOf(0)
private set

var postReserveStock = mutableLongStateOf(0)
private set

fun postBuyStock(
stockId: String,
num: Long,
goal_price: Long,
) = viewModelScope.launch {
buyStockReserveUseCase(
stockId = stockId,
body = BuyStockRequestModel(
num = num,
goal_price = goal_price
)
).onSuccess {
it.catch { remoteError ->
_buyStockReserveResponse.value = remoteError.errorHandling()
}.collect {
_buyStockReserveResponse.value = Event.Success()
}
}.onFailure { error ->
_buyStockReserveResponse.value = error.errorHandling()
}
}
}
Loading
Loading