88import SwiftUI
99
1010final class ArticleViewModel : ObservableObject {
11+ @Published var toastMessage : ToastEntity ?
1112 @Published var selectedInterests : [ InterestEntity ] = [ ]
12- @Published var articles : [ ArticleEntity ] = [ ]
13- @Published var isLoading : Bool = false
13+ @Published var articles : [ String ] = [ ]
14+ @Published var isLoading : Bool = true
1415
1516 private let alanUsecase : AlanUsecase
1617 private let storageUsecase : InterestsUsecase
@@ -22,32 +23,43 @@ final class ArticleViewModel: ObservableObject {
2223 ]
2324
2425 init (
25- alanUsecase: AlanUsecase = AlanUsecaseImpl ( repository : NetworkRepositoryImpl ( ) ) ,
26- storageUsecase: InterestsUsecase /* = InterestsUsecaseImpl(repository: AnotherStorageRepositoryImpl<InterestsDTO>() */
26+ alanUsecase: AlanUsecase ,
27+ storageUsecase: InterestsUsecase
2728 ) {
2829 self . alanUsecase = alanUsecase
2930 self . storageUsecase = storageUsecase
3031
3132 Task {
3233 do {
3334 let result = try await storageUsecase. fetchInterests ( )
34- await MainActor . run { self . selectedInterests = result }
35+ await MainActor . run {
36+ self . selectedInterests = result
37+ loadArticles ( )
38+ }
3539 }
3640 catch {
37- #warning("여기에 에러핸들링 토스트 팝업 등 넣기" )
41+ await callToastMessage ( type : . error , title : " 관심사 로드 실패 " , message : error . localizedDescription )
3842 }
3943 }
4044 }
4145
4246 // MARK: - 아티클 로드
4347 func loadArticles( ) {
48+ isLoading = true
4449 Task {
4550 do {
46- let result = try await alanUsecase. fetchArticle ( topics: selectedInterests)
47- await MainActor . run { articles = result }
51+ // let result = try await alanUsecase.fetchArticle(topics: selectedInterests)
52+ // await MainActor.run{ articles = result }
53+ // MARK: 임시 로직
54+ try await Task . sleep ( for: . seconds( 5 ) )
55+ await MainActor . run { [ weak self] in
56+ self ? . articles = Array ( ( self ? . dummyData. shuffled ( ) . prefix ( 5 ) ) !)
57+ self ? . isLoading = false
58+ self ? . toastMessage = ToastEntity ( type: . success, title: " 아티클 업데이트 완료 " , message: " " )
59+ }
4860 }
4961 catch {
50- #warning("여기에 에러핸들링 토스트 팝업 등 넣기" )
62+ await callToastMessage ( type : . error , title : " 아티클 로드 실패 " , message : error . localizedDescription )
5163 }
5264 }
5365
@@ -70,18 +82,53 @@ final class ArticleViewModel: ObservableObject {
7082 if selectedInterests. contains ( where: { $0. title == title } ) {
7183 removeInterest ( title)
7284 Task {
73- do { try await storageUsecase. deleteInterests ( InterestEntity ( title: title) ) }
74- catch { }
75- #warning("여기에 에러핸들링 토스트 팝업 등 넣기")
85+ do {
86+ try await storageUsecase. deleteInterests ( InterestEntity ( title: title) )
87+ }
88+ catch {
89+ await callToastMessage ( type: . error, title: " 관심사 삭제 실패 " , message: error. localizedDescription)
90+ }
7691 }
7792 } else {
7893 addInterest ( title)
7994 Task {
80- do { try await storageUsecase. insertInterests ( InterestEntity ( title: title) ) }
81- catch { }
82- #warning("여기에 에러핸들링 토스트 팝업 등 넣기")
95+ do {
96+ try await storageUsecase. insertInterests ( InterestEntity ( title: title) )
97+ }
98+ catch {
99+ await callToastMessage ( type: . error, title: " 관심사 추가 실패 " , message: error. localizedDescription)
100+ }
83101 }
84102
85103 }
86104 }
105+
106+ private func callToastMessage( type: ToastStyle , title: String , message: String ) async {
107+ await MainActor . run { [ weak self] in
108+ self ? . toastMessage = ToastEntity ( type: type, title: title, message: message)
109+ }
110+ }
111+
112+ let dummyData = [
113+ " https://www.youtube.com/watch?v=jSuxMiRxnZg " ,
114+ " https://www.youtube.com/watch?v=2lDheJzSYeo " ,
115+ " https://www.youtube.com/watch?v=TEHS9dzSTZY " ,
116+ " https://www.youtube.com/watch?v=MjMkBaqimFo " ,
117+ " https://www.youtube.com/watch?v=Obu25eMlr4A " ,
118+ " https://www.youtube.com/watch?v=KFbeFLLJbWo " ,
119+ " https://www.youtube.com/watch?v=BHY0FxzoKZE " ,
120+ " https://www.youtube.com/watch?v=8so1WZ4j1oQ " ,
121+ " https://www.youtube.com/watch?v=zlaZGuXvL04 " ,
122+ " https://www.youtube.com/watch?v=1D9ASR6gSc0 " ,
123+ " https://www.youtube.com/watch?v=Cg_GW7yhq20 " ,
124+ " https://www.youtube.com/watch?v=QWF9mGtjju4 " ,
125+ " https://www.youtube.com/watch?v=YMnIhyWjrb4 " ,
126+ " https://www.youtube.com/watch?v=FoRku07ShZM " ,
127+ " https://www.youtube.com/watch?v=YNsuneGBsMY " ,
128+ " https://www.youtube.com/watch?v=3DZqX_2YLiw " ,
129+ " https://www.youtube.com/watch?v=CSjRBBqfhko " ,
130+ " https://www.youtube.com/watch?v=wWGulLAa0O0 " ,
131+ " https://www.youtube.com/watch?v=4WV7kAUGrgI " ,
132+ " https://www.youtube.com/watch?v=8so1WZ4j1oQ "
133+ ]
87134}
0 commit comments