Skip to content

Commit

Permalink
[WEAV-137] widget 수정삭제 global toast 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
jisu15-kim committed Nov 20, 2024
1 parent 7b59bf0 commit e89cc51
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 58 deletions.
17 changes: 4 additions & 13 deletions Projects/Core/CommonKit/Sources/AppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public final class AppCoordinator: ObservableObject {
}
}
}
startRefreshMyUserInfo()
}

@MainActor
Expand Down Expand Up @@ -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
}
}
10 changes: 10 additions & 0 deletions Projects/DesignSystem/DesignCore/Sources/Toast/Toast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 15 additions & 1 deletion Projects/Features/Home/Sources/Profile/ProfileIntent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import CommonKit
import CoreKit
import Model
import NetworkKit
import DesignCore

//MARK: - Intent
class ProfileIntent {
Expand Down Expand Up @@ -40,6 +41,7 @@ extension ProfileIntent {
func deleteWidget(_ widget: ProfileWidget) async

func onTapNextButton()
func refreshUserInfo() async
func fetchUserInfo(_ userInfo: UserInfo)

// default
Expand Down Expand Up @@ -73,18 +75,29 @@ 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()
}
}
}

func onAppear() {
fetchUserInfo(input.userInfo)
}

func refreshUserInfo() async {
if let userInfo = try? await AppCoordinator.shared.refreshMyUserInfo() {
fetchUserInfo(userInfo)
}
}

func fetchUserInfo(_ userInfo: UserInfo) {
model?.setUserInfo(userInfo)
}
Expand All @@ -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)
}
}
50 changes: 10 additions & 40 deletions Projects/Features/Home/Sources/Profile/ProfileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import Model
public struct ProfileView: View {

@StateObject var container: MVIContainer<ProfileIntent.Intentable, ProfileModel.Stateful>
// @State var isPresentWidgetSelectionView = false

private var intent: ProfileIntent.Intentable { container.intent }
private var state: ProfileModel.Stateful { container.model }
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -187,36 +186,17 @@ 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: {
NavigationStack {
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("위젯이 추가되었어요")
}
}
)
Expand All @@ -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("위젯이 수정되었어요")
}
}
)
Expand All @@ -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
Expand Down Expand Up @@ -312,15 +294,3 @@ fileprivate struct DeleteWidgetConfirmView: View {
.padding(.vertical, 30)
}
}

#Preview {
DeleteWidgetConfirmView {

} cancelHandler: {

}

// NavigationView {
// HomeMainView(userInfo: .mock)
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit e89cc51

Please sign in to comment.