77
88import SwiftUI
99
10- /// 오늘의 코인 시장/ 커뮤니티 분위기를 제공하는 뷰 모델입니다.
10+ /// 오늘의 코인 커뮤니티 분위기를 제공하는 뷰 모델입니다.
1111///
1212/// AI 또는 커뮤니티 기반의 분위기를 비동기적으로 불러오고,
1313/// 감정(`Sentiment`)과 요약(`summary`)을 제공합니다.
1414///
1515/// - Properties:
16- /// - overall: 오늘의 전체 시장 분위기(`FetchState<Insight>`)
1716/// - community: 커뮤니티 기반 분위기(`FetchState<Insight>`)
1817final class InsightViewModel : ObservableObject {
19- @AppStorage ( AppStorageKey . cacheBriefTodayTimestamp) private var cacheBriefTodayTimestamp : String = " "
2018 @AppStorage ( AppStorageKey . cacheBriefCommunityTimestamp) private var cacheBriefCommunityTimestamp : String = " "
2119
22- @Published var overall : FetchState < Insight > = . loading
2320 @Published var community : FetchState < Insight > = . loading
2421
2522 private let llmService = LLMAPIService ( )
2623 private let redditAPIService = RedditAPIService ( )
2724
28- private var overallTask : Task < Insight , Error > ?
2925 private var communityTask : Task < Insight , Error > ?
3026
3127 init ( ) {
@@ -36,14 +32,9 @@ final class InsightViewModel: ObservableObject {
3632 cancelAll ( )
3733
3834 Task { @MainActor in
39- overall = . loading
4035 community = . loading
4136 }
4237
43- overallTask = Task {
44- try await llmService. fetchTodayInsight ( )
45- }
46-
4738 communityTask = Task { [ weak self] in
4839 try await withTaskCancellationHandler (
4940 operation: {
@@ -58,8 +49,6 @@ final class InsightViewModel: ObservableObject {
5849 }
5950
6051 Task {
61- await updateOverallUI ( )
62- try ? await Task . sleep ( for: . milliseconds( 350 ) ) // UI가 순차적으로 적용되는 효과를 주기 위한 딜레이
6352 await updateCommunityUI ( )
6453 }
6554 }
@@ -71,20 +60,6 @@ final class InsightViewModel: ObservableObject {
7160 return try await llmService. fetchCommunityInsight ( from: communityData. communitySummary, ignoreCache: ignoreCache)
7261 }
7362
74- // overall만 다시 시도
75- func retryOverall( ) {
76- if overall. isLoading { return }
77- overallTask? . cancel ( )
78- overallTask = nil
79-
80- Task {
81- await MainActor . run { self . overall = . loading }
82- try ? await Task . sleep ( for: . milliseconds( 350 ) ) // 새로고침 효과를 주기 위한 딜레이
83- overallTask = Task { try await llmService. fetchTodayInsight ( ignoreCache: true ) }
84- await updateOverallUI ( )
85- }
86- }
87-
8863 // community만 다시 시도
8964 func retryCommunity( ) {
9065 if community. isLoading { return }
@@ -100,16 +75,11 @@ final class InsightViewModel: ObservableObject {
10075 }
10176 }
10277
103- func cancelOverall( ) {
104- overallTask? . cancel ( )
105- }
106-
10778 func cancelCommunity( ) {
10879 communityTask? . cancel ( )
10980 }
11081
11182 func cancelAll( ) {
112- overallTask? . cancel ( )
11383 communityTask? . cancel ( )
11484 }
11585
@@ -119,15 +89,6 @@ final class InsightViewModel: ObservableObject {
11989}
12090
12191extension InsightViewModel {
122- private func updateOverallUI( ) async {
123- await TaskResultHandler . apply (
124- of: overallTask,
125- update: { [ weak self] state in
126- self ? . overall = state
127- }
128- )
129- }
130-
13192 private func updateCommunityUI( ) async {
13293 await TaskResultHandler . apply (
13394 of: communityTask,
@@ -141,15 +102,6 @@ extension InsightViewModel {
141102extension InsightViewModel {
142103 var sectionDataSource : [ ReportSectionData < Insight > ] {
143104 [
144- // ReportSectionData(
145- // id: "overall",
146- // icon: "bitcoinsign.bank.building",
147- // title: "전반적인 시장의 분위기",
148- // state: overall,
149- // timestamp: Date.dateAndTimeFormatter.date(from: cacheBriefTodayTimestamp),
150- // onCancel: { [weak self] in self?.cancelOverall() },
151- // onRetry: { [weak self] in self?.retryOverall() }
152- // ),
153105 ReportSectionData (
154106 id: " community " ,
155107 icon: " shareplay " ,
0 commit comments