From e89cc51ac28f7d18a4ebfe9515ad35425666965e Mon Sep 17 00:00:00 2001 From: Jisu Kim Date: Wed, 20 Nov 2024 16:41:04 +0900 Subject: [PATCH] =?UTF-8?q?[WEAV-137]=20widget=20=EC=88=98=EC=A0=95?= =?UTF-8?q?=EC=82=AD=EC=A0=9C=20global=20toast=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CommonKit/Sources/AppCoordinator.swift | 17 ++----- .../DesignCore/Sources/Toast/Toast.swift | 10 ++++ .../Home/Sources/Profile/ProfileIntent.swift | 16 +++++- .../Home/Sources/Profile/ProfileView.swift | 50 ++++--------------- .../WidgetWriting/WidgetWritingIntent.swift | 8 +-- 5 files changed, 43 insertions(+), 58 deletions(-) diff --git a/Projects/Core/CommonKit/Sources/AppCoordinator.swift b/Projects/Core/CommonKit/Sources/AppCoordinator.swift index 5a0de61..a34311b 100644 --- a/Projects/Core/CommonKit/Sources/AppCoordinator.swift +++ b/Projects/Core/CommonKit/Sources/AppCoordinator.swift @@ -43,7 +43,6 @@ public final class AppCoordinator: ObservableObject { } } } - startRefreshMyUserInfo() } @MainActor @@ -102,24 +101,16 @@ public final class AppCoordinator: ObservableObject { } } - public func refreshMyUserInfo() async throws { + public func refreshMyUserInfo() async throws -> UserInfo? { if TokenManager.accessToken == nil || TokenManager.accessToken == "" { - return + AuthState.change(.loggedOut) + return nil } let userInfo = try await authService.requestMyUserInfo() await MainActor.run { self.userInfo = userInfo AuthState.change(.login) } - } - - // 20초마다 한번씩 refreshMyUserInfo() 를 호출 - private func startRefreshMyUserInfo() { - Task { - while true { - try? await Task.sleep(for: .seconds(20)) - try? await refreshMyUserInfo() - } - } + return userInfo } } diff --git a/Projects/DesignSystem/DesignCore/Sources/Toast/Toast.swift b/Projects/DesignSystem/DesignCore/Sources/Toast/Toast.swift index 0a69c7c..826b797 100644 --- a/Projects/DesignSystem/DesignCore/Sources/Toast/Toast.swift +++ b/Projects/DesignSystem/DesignCore/Sources/Toast/Toast.swift @@ -36,6 +36,16 @@ public class ToastHelper { ) { manager.showToast(message: message, style: style) } + + public static func show(_ message: String) { + manager.showToast(message: message, style: .normal) + } + + public static func showErrorMessage( + _ message: String = "에러가 발생했어요 다시 시도해주세요" + ) { + manager.showToast(message: message, style: .error) + } } final class ToastManager { diff --git a/Projects/Features/Home/Sources/Profile/ProfileIntent.swift b/Projects/Features/Home/Sources/Profile/ProfileIntent.swift index 6d4cb3a..dc36f1a 100644 --- a/Projects/Features/Home/Sources/Profile/ProfileIntent.swift +++ b/Projects/Features/Home/Sources/Profile/ProfileIntent.swift @@ -11,6 +11,7 @@ import CommonKit import CoreKit import Model import NetworkKit +import DesignCore //MARK: - Intent class ProfileIntent { @@ -40,6 +41,7 @@ extension ProfileIntent { func deleteWidget(_ widget: ProfileWidget) async func onTapNextButton() + func refreshUserInfo() async func fetchUserInfo(_ userInfo: UserInfo) // default @@ -73,11 +75,16 @@ extension ProfileIntent: ProfileIntent.Intentable { do { model?.setLoading(status: true) try await requestDeleteWidget(widget) - try await AppCoordinator.shared.refreshMyUserInfo() + await refreshUserInfo() model?.setLoading(status: false) + try await Task.sleep(for: .milliseconds(500)) + ToastHelper.show("위젯이 삭제되었어요") } catch { print(error) model?.setLoading(status: false) + DispatchQueue.main.asyncAfter(wallDeadline: .now() + 1.0) { + ToastHelper.showErrorMessage() + } } } @@ -85,6 +92,12 @@ extension ProfileIntent: ProfileIntent.Intentable { fetchUserInfo(input.userInfo) } + func refreshUserInfo() async { + if let userInfo = try? await AppCoordinator.shared.refreshMyUserInfo() { + fetchUserInfo(userInfo) + } + } + func fetchUserInfo(_ userInfo: UserInfo) { model?.setUserInfo(userInfo) } @@ -95,6 +108,7 @@ extension ProfileIntent: ProfileIntent.Intentable { func onTapNextButton() {} func requestDeleteWidget(_ widget: ProfileWidget) async throws { + throw NSError(domain: "33", code: 33) try await profileService.requestDeleteProfileWidget(widgetType: widget.widgetType.toDto) } } diff --git a/Projects/Features/Home/Sources/Profile/ProfileView.swift b/Projects/Features/Home/Sources/Profile/ProfileView.swift index 25bc110..840caac 100644 --- a/Projects/Features/Home/Sources/Profile/ProfileView.swift +++ b/Projects/Features/Home/Sources/Profile/ProfileView.swift @@ -15,7 +15,6 @@ import Model public struct ProfileView: View { @StateObject var container: MVIContainer -// @State var isPresentWidgetSelectionView = false private var intent: ProfileIntent.Intentable { container.intent } private var state: ProfileModel.Stateful { container.model } @@ -112,7 +111,7 @@ public struct ProfileView: View { } } else { LazyVGrid(columns: columns, spacing: 16) { - ForEach(userInfo.profileWidgets, id: \.self) { widget in + ForEach(userInfo.profileWidgets, id: \.widgetType.toDto) { widget in ZStack { RoundedRectangle(cornerRadius: 24) .fill(.white) @@ -187,27 +186,6 @@ public struct ProfileView: View { ProgressView() } } -// .onChange(of: state.isPresentedAddWidgetModal) { -// if !state.isPresentedAddWidgetModal { -// if let userInfo = AppCoordinator.shared.userInfo { -// intent.fetchUserInfo(userInfo) -// } -// } -// } -// .onChange(of: state.isPresentedModifyWidgetView) { -// if !state.isPresentedModifyWidgetView { -// if let userInfo = AppCoordinator.shared.userInfo { -// intent.fetchUserInfo(userInfo) -// } -// } -// } -// .onChange(of: state.isPresentedDeleteConfirmSheet) { -// if !state.isPresentedDeleteConfirmSheet { -// if let userInfo = AppCoordinator.shared.userInfo { -// intent.fetchUserInfo(userInfo) -// } -// } -// } .sheet( isPresented: $container.model.isPresentedAddWidgetModal, content: { @@ -215,8 +193,10 @@ public struct ProfileView: View { WidgetSelectionView( isPresented: $container.model.isPresentedAddWidgetModal, successHandler: { - DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { - ToastHelper.show(message: "위젯이 추가되었어요") + Task { + await intent.refreshUserInfo() + try await Task.sleep(for: .seconds(1)) + ToastHelper.show("위젯이 추가되었어요") } } ) @@ -234,8 +214,10 @@ public struct ProfileView: View { isEditing: true, contentString: widget.content, successHandler: { - DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { - ToastHelper.show(message: "위젯이 추가되었어요") + Task { + await intent.refreshUserInfo() + try await Task.sleep(for: .seconds(1)) + ToastHelper.show("위젯이 수정되었어요") } } ) @@ -249,10 +231,10 @@ public struct ProfileView: View { if let widget = state.selectedWidgetType { DeleteWidgetConfirmView { Task { - await intent.deleteWidget(widget) await MainActor.run { container.model.isPresentedDeleteConfirmSheet = false } + await intent.deleteWidget(widget) } } cancelHandler: { container.model.isPresentedDeleteConfirmSheet = false @@ -312,15 +294,3 @@ fileprivate struct DeleteWidgetConfirmView: View { .padding(.vertical, 30) } } - -#Preview { - DeleteWidgetConfirmView { - - } cancelHandler: { - - } - -// NavigationView { -// HomeMainView(userInfo: .mock) -// } -} diff --git a/Projects/Features/Home/Sources/Widget/WidgetWriting/WidgetWritingIntent.swift b/Projects/Features/Home/Sources/Widget/WidgetWriting/WidgetWritingIntent.swift index 50ff539..d21bbc0 100644 --- a/Projects/Features/Home/Sources/Widget/WidgetWriting/WidgetWritingIntent.swift +++ b/Projects/Features/Home/Sources/Widget/WidgetWriting/WidgetWritingIntent.swift @@ -83,10 +83,10 @@ extension WidgetWritingIntent: WidgetWritingIntent.Intentable { Task { model?.setLoading(status: true) do { -// try await requestPutProfileWidget( -// widget: selectedWidget, -// content: state.widgetBodyText -// ) + try await requestPutProfileWidget( + widget: selectedWidget, + content: state.widgetBodyText + ) model?.setLoading(status: false) model?.modalDismiss() model?.doSuccessAction()