atrade/src/main/kotlin/ui/StockDetailArea.kt

248 lines
10 KiB
Kotlin
Raw Normal View History

2026-01-10 18:16:50 +09:00
package ui
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items // 반드시 수동 import 확인
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.runtime.*
import io.ktor.client.engine.cio.CIO
// 아래 두 import가 'delegate' 에러를 해결합니다.
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
2026-01-14 15:42:26 +09:00
import kotlinx.coroutines.coroutineScope
2026-01-10 18:16:50 +09:00
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import model.AppConfig
import model.BalanceSummary
import model.CandleData
import model.RankingStock
import model.StockHolding
import network.KisTradeService
import network.KisWebSocketManager
2026-01-22 16:21:18 +09:00
import service.TechnicalAnalyzer
import java.time.LocalTime
import java.time.format.DateTimeFormatter
2026-01-10 18:16:50 +09:00
import kotlin.collections.isNotEmpty
@Composable
2026-01-13 16:04:25 +09:00
fun StockDetailSection(
stockCode: String,
stockName: String,
2026-01-19 17:09:37 +09:00
holdingQuantity : String,
2026-01-13 16:04:25 +09:00
isDomestic: Boolean,
tradeService: KisTradeService,
2026-01-21 11:49:30 +09:00
wsManager: KisWebSocketManager,
onOrderSaved: (String) -> Unit
2026-01-10 18:16:50 +09:00
) {
2026-01-14 15:42:26 +09:00
var openPrice by remember { mutableStateOf("0") }
2026-01-10 18:16:50 +09:00
var chartData by remember { mutableStateOf<List<CandleData>>(emptyList()) }
var isLoading by remember { mutableStateOf(false) }
var resultMessage by remember { mutableStateOf("") }
var isSuccess by remember { mutableStateOf(true) }
2026-01-14 15:42:26 +09:00
var daySummary by remember { mutableStateOf<List<CandleData>>(emptyList()) }
var weekSummary by remember { mutableStateOf<List<CandleData>>(emptyList()) }
var monthSummary by remember { mutableStateOf<List<CandleData>>(emptyList()) }
var yearSummary by remember { mutableStateOf<List<CandleData>>(emptyList()) }
val todayOpen = remember(daySummary) {
daySummary.lastOrNull()?.stck_oprc ?: "0"
}
val previousClose = remember(daySummary) {
2026-01-22 16:21:18 +09:00
if (daySummary.size >= 2) daySummary[daySummary.size - 2].stck_prpr else "0"
2026-01-14 15:42:26 +09:00
}
fun calculateAvg(data: List<CandleData>): String {
if (data.isEmpty()) return "0"
2026-01-22 16:21:18 +09:00
val avg = data.map { it.stck_prpr.toDoubleOrNull() ?: 0.0 }.average()
2026-01-14 15:42:26 +09:00
return String.format("%,d", avg.toLong())
}
2026-01-10 18:16:50 +09:00
2026-01-13 16:04:25 +09:00
// 이전 종목 코드를 기억하기 위한 상태
var previousCode by remember { mutableStateOf("") }
// 종목 변경 시 데이터 로드 및 웹소켓 구독 관리
LaunchedEffect(stockCode) {
if (stockCode.isEmpty()) return@LaunchedEffect
2026-01-10 18:16:50 +09:00
isLoading = true
2026-01-13 16:04:25 +09:00
// 1. 웹소켓 구독 관리: 이전 종목 해제 -> 새 종목 구독
if (previousCode.isNotEmpty()) {
wsManager.unsubscribeStock(previousCode)
}
2026-01-14 15:42:26 +09:00
wsManager.clearData()
2026-01-13 16:04:25 +09:00
wsManager.subscribeStock(stockCode)
previousCode = stockCode
// 2. 차트 데이터 로드 (KisSession 기반으로 파라미터 간소화)
2026-01-10 18:16:50 +09:00
2026-01-14 15:42:26 +09:00
coroutineScope {
2026-01-22 16:26:29 +09:00
TechnicalAnalyzer.clear()
2026-01-14 15:42:26 +09:00
launch {tradeService.fetchChartData(stockCode, isDomestic)
.onSuccess { data ->
println("✅ 차트 데이터 로드 성공: ${data.size}") // ${} 사용하여 정확히 출력
chartData = data
2026-01-22 16:21:18 +09:00
TechnicalAnalyzer.min30 = chartData
2026-01-14 15:42:26 +09:00
}
.onFailure { error ->
println("❌ 차트 데이터 로드 실패: ${error.localizedMessage}")
chartData = emptyList()
}}
2026-01-22 16:21:18 +09:00
launch { tradeService.fetchPeriodChartData(stockCode, "D").onSuccess {
daySummary = it.takeLast(7) }
TechnicalAnalyzer.daily = daySummary
} // 최근 7일
launch { tradeService.fetchPeriodChartData(stockCode, "W").onSuccess { weekSummary = it.takeLast(4) }
TechnicalAnalyzer.weekly = weekSummary} // 최근 4주
2026-01-14 15:42:26 +09:00
launch { tradeService.fetchPeriodChartData(stockCode, "M").onSuccess {
monthSummary = it.takeLast(6) // 최근 6개월
yearSummary = it.takeLast(36) // 최근 3년
2026-01-22 16:21:18 +09:00
TechnicalAnalyzer.monthly = yearSummary
}}
2026-01-14 15:42:26 +09:00
}
2026-01-10 18:16:50 +09:00
isLoading = false
}
2026-01-13 16:04:25 +09:00
val latestPrice by wsManager.currentPrice // 웹소켓에서 업데이트되는 현재가
2026-01-10 18:16:50 +09:00
2026-01-13 16:04:25 +09:00
LaunchedEffect(latestPrice) {
if (chartData.isNotEmpty() && latestPrice != "0") {
val priceDouble = latestPrice.replace(",", "").toDoubleOrNull() ?: return@LaunchedEffect
val lastCandle = chartData.last()
2026-01-14 15:42:26 +09:00
// 현재 시간(분 단위) 확인
2026-01-22 16:21:18 +09:00
val currentMinute = LocalTime.now().format(DateTimeFormatter.ofPattern("HHmm00"))
2026-01-14 15:42:26 +09:00
if (lastCandle.stck_bsop_date != currentMinute) {
// [개선] 시간이 바뀌었으면 새로운 캔들 추가 (차트가 밀려나는 효과)
val newCandle = CandleData(
stck_bsop_date = currentMinute,
stck_oprc = latestPrice,
stck_hgpr = latestPrice,
stck_lwpr = latestPrice,
2026-01-22 16:21:18 +09:00
stck_prpr = latestPrice,
stck_cntg_hour = currentMinute,
cntg_vol = "1",
acml_tr_pbmn = "1",
2026-01-14 15:42:26 +09:00
)
// 최대 100개까지만 유지하여 성능 최적화
chartData = (chartData + newCandle).takeLast(100)
} else {
// 같은 분 내에서는 기존 마지막 캔들만 업데이트
val updatedCandle = lastCandle.copy(
2026-01-22 16:21:18 +09:00
stck_prpr = latestPrice,
2026-01-14 15:42:26 +09:00
stck_hgpr = if (priceDouble > (lastCandle.stck_hgpr.toDoubleOrNull() ?: 0.0)) latestPrice else lastCandle.stck_hgpr,
stck_lwpr = if (priceDouble < (lastCandle.stck_lwpr.toDoubleOrNull() ?: Double.MAX_VALUE)) latestPrice else lastCandle.stck_lwpr
)
chartData = chartData.dropLast(1) + updatedCandle
}
2026-01-10 18:16:50 +09:00
}
2026-01-13 16:04:25 +09:00
}
2026-01-10 18:16:50 +09:00
2026-01-13 16:04:25 +09:00
Column(modifier = Modifier.fillMaxSize().padding(16.dp)) {
// [상단] 종목명 및 상태 메시지
2026-01-14 15:42:26 +09:00
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
StockHeader(
name = stockName,
code = stockCode,
isDomestic = isDomestic,
previousClose = previousClose,
openPrice = openPrice,
resultMessage = resultMessage,
2026-01-19 17:09:37 +09:00
resultMessageClear = {resultMessage = ""},
2026-01-14 15:42:26 +09:00
isSuccess = isSuccess
)
// 실시간 가격 표시 (WebSocket 데이터)
Column(horizontalAlignment = Alignment.End) {
Text(
text = "${wsManager.currentPrice.value}",
style = MaterialTheme.typography.h4,
fontWeight = FontWeight.Bold,
color = if (wsManager.currentPrice.value.contains("-")) Color.Blue else Color.Red
)
Text("실시간 체결가", style = MaterialTheme.typography.caption, color = Color.Gray)
}
}
// 통합된 트렌드 카드 배치
Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(8.dp)) {
PeriodTrendCard("7일", daySummary, Modifier.weight(1f))
PeriodTrendCard("4주", weekSummary, Modifier.weight(1f))
PeriodTrendCard("6개월", monthSummary, Modifier.weight(1f))
PeriodTrendCard("3년", yearSummary, Modifier.weight(1f))
}
2026-01-10 18:16:50 +09:00
2026-01-22 16:21:18 +09:00
Spacer(modifier = Modifier.height(4.dp))
2026-01-13 16:04:25 +09:00
// [중앙] 캔들 차트 (Card 내부)
2026-01-10 18:16:50 +09:00
Card(
2026-01-22 16:21:18 +09:00
modifier = Modifier.fillMaxWidth().height(320.dp),
2026-01-10 18:16:50 +09:00
backgroundColor = Color(0xFF121212)
) {
if (isLoading) {
Box(contentAlignment = Alignment.Center) { CircularProgressIndicator(color = Color.White) }
} else {
2026-01-13 16:04:25 +09:00
CandleChart(data = chartData, modifier = Modifier.padding(16.dp))
2026-01-10 18:16:50 +09:00
}
}
2026-01-13 16:04:25 +09:00
2026-01-22 16:21:18 +09:00
Spacer(modifier = Modifier.height(4.dp))
2026-01-13 16:04:25 +09:00
// [하단] 실시간 체결 내역 및 주문 섹션
Row(modifier = Modifier.weight(1f)) {
// 실시간 체결 리스트
Column(modifier = Modifier.weight(1f)) {
Text("실시간 체결", style = MaterialTheme.typography.subtitle2, fontWeight = FontWeight.Bold)
RealTimeTradeList(wsManager.tradeLogs)
2026-01-10 18:16:50 +09:00
}
2026-01-13 16:04:25 +09:00
Spacer(modifier = Modifier.width(12.dp))
// 주문 섹션 (인자 간소화)
2026-01-14 15:42:26 +09:00
Column(modifier = Modifier.weight(0.6f)) {
IntegratedOrderSection(
stockCode = stockCode,
2026-01-19 17:09:37 +09:00
stockName = stockName,
isDomestic = isDomestic,
2026-01-14 15:42:26 +09:00
currentPrice = wsManager.currentPrice.value,
2026-01-19 17:09:37 +09:00
holdingQuantity = holdingQuantity,
2026-01-14 15:42:26 +09:00
tradeService = tradeService,
2026-01-21 11:49:30 +09:00
onOrderSaved = onOrderSaved,
2026-01-14 15:42:26 +09:00
onOrderResult = { msg, success ->
resultMessage = msg
isSuccess = success
}
)
}
}
}
}
@Composable
fun PeriodSummaryCard(label: String, avgPrice: String, modifier: Modifier = Modifier) {
Card(modifier = modifier, elevation = 2.dp, backgroundColor = Color.White) {
Column(modifier = Modifier.padding(8.dp), horizontalAlignment = Alignment.CenterHorizontally) {
Text(label, fontSize = 10.sp, color = Color.Gray)
Text(text = "${avgPrice}", fontSize = 13.sp, fontWeight = FontWeight.Bold, color = Color.Black)
2026-01-10 18:16:50 +09:00
}
}
}