Skip to content

Commit

Permalink
[WEAV-137] 위젯 추가 handler 로 Toast show
Browse files Browse the repository at this point in the history
  • Loading branch information
jisu15-kim committed Nov 17, 2024
1 parent e9c53f4 commit 7b59bf0
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Projects/Core/CommonKit/Sources/AppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public final class AppCoordinator: ObservableObject {

//MARK: - Properties
public var authState: AuthState = .none
public var userInfo: UserInfo?
@Published public var userInfo: UserInfo?
public var needFadeTransition: Bool = false
@Published public var navigationStack: [PathType] = [.intro]
let authService = AuthService.shared
Expand Down
2 changes: 1 addition & 1 deletion Projects/DesignSystem/DesignCore/Sources/Toast/Toast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ final class ToastManager {
guard toastWindow == nil else { return }

let toastWindow = UIWindow(frame: UIScreen.main.bounds)
toastWindow.windowLevel = .alert + 1
toastWindow.windowLevel = .statusBar + 1
toastWindow.backgroundColor = .clear

let containerVC = UIViewController()
Expand Down
5 changes: 4 additions & 1 deletion Projects/Features/Home/Sources/HomeMain/HomeMainIntent.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 HomeMainIntent {
Expand Down Expand Up @@ -68,5 +69,7 @@ extension HomeMainIntent: HomeMainIntent.Intentable {
func task() async {}

// content
func onTapNextButton() {}
func onTapNextButton() {
ToastHelper.show(message: "토스트얍")
}
}
6 changes: 6 additions & 0 deletions Projects/Features/Home/Sources/HomeMain/HomeMainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ public struct HomeMainView: View {
// 첫 번째 탭 내용
VStack {
Text("첫 번째 탭")
Button {
intent.onTapNextButton()
} label: {
Text("토스트!")
}

}
.tag(HomeMainTab.home)

Expand Down
56 changes: 33 additions & 23 deletions Projects/Features/Home/Sources/Profile/ProfileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,33 +187,38 @@ 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)
}
}
}
// .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
isPresented: $container.model.isPresentedAddWidgetModal,
successHandler: {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
ToastHelper.show(message: "위젯이 추가되었어요")
}
}
)
}
})
Expand All @@ -227,7 +232,12 @@ public struct ProfileView: View {
isModalPresented: $container.model.isPresentedModifyWidgetView,
isPushed: .constant(false),
isEditing: true,
contentString: widget.content
contentString: widget.content,
successHandler: {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
ToastHelper.show(message: "위젯이 추가되었어요")
}
}
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class WidgetSelectionIntent {
) {
self.input = input
self.model = model
self.model?.setSuccessHandler(handler: input.successHandler)
}
}

Expand All @@ -38,7 +39,9 @@ extension WidgetSelectionIntent {
func task() async
}

struct DataModel {}
struct DataModel {
let successHandler: (() -> Void)?
}
}

//MARK: - Intentable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ final class WidgetSelectionModel: ObservableObject {
var isPushWriteContentView: Bool { get set }
var isValidated: Bool { get }
var selectedWidget: WidgetType? { get }
var successHandler: (() -> Void)? { get }

// default
var isLoading: Bool { get }
Expand All @@ -35,6 +36,7 @@ final class WidgetSelectionModel: ObservableObject {
@Published var isPushWriteContentView: Bool = false
@Published var isValidated: Bool = false
@Published var selectedWidget: WidgetType?
var successHandler: (() -> Void)?

// default
@Published var isLoading: Bool = false
Expand All @@ -53,6 +55,7 @@ protocol WidgetSelectionModelActionable: AnyObject {
func setPushWriteContentView(status: Bool)
func setValidation(value: Bool)
func setSelectedWidget(widget: WidgetType)
func setSuccessHandler(handler: (() -> Void)?)

// default
func setLoading(status: Bool)
Expand All @@ -77,6 +80,9 @@ extension WidgetSelectionModel: WidgetSelectionModelActionable {
func setValidation(value: Bool) {
isValidated = value
}
func setSuccessHandler(handler: (() -> Void)?) {
successHandler = handler
}

// default
func setLoading(status: Bool) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ import CommonKit
import Model

public struct WidgetSelectionView: View {

// @State private var isPushedWriteView: Bool = false

@Binding var isPresentedSelectionView: Bool
@StateObject var container: MVIContainer<WidgetSelectionIntent.Intentable, WidgetSelectionModel.Stateful>

Expand All @@ -27,11 +24,14 @@ public struct WidgetSelectionView: View {
GridItem(.flexible(), spacing: 16)
]

public init(isPresented: Binding<Bool>) {
public init(
isPresented: Binding<Bool>,
successHandler: (() -> Void)?
) {
let model = WidgetSelectionModel()
let intent = WidgetSelectionIntent(
model: model,
input: .init()
input: .init(successHandler: successHandler)
)
let container = MVIContainer(
intent: intent as WidgetSelectionIntent.Intentable,
Expand Down Expand Up @@ -80,7 +80,8 @@ public struct WidgetSelectionView: View {
WidgetWritingView(
widgetType: widget,
isModalPresented: $container.model.isModalPresented,
isPushed: $container.model.isPushWriteContentView
isPushed: $container.model.isPushWriteContentView,
successHandler: state.successHandler
)
}
}
Expand Down Expand Up @@ -111,6 +112,8 @@ public struct WidgetSelectionView: View {

#Preview {
NavigationStack {
WidgetSelectionView(isPresented: .constant(true))
WidgetSelectionView(isPresented: .constant(true)) {

}
}
}
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 WidgetWritingIntent {
Expand All @@ -28,6 +29,7 @@ class WidgetWritingIntent {
self.model = model
self.service = service
model.setWidgetType(input.widgetType)
model.setSuccessHandler(handler: input.successHandler)
if let contentString = input.content {
model.setContentString(contentString)
}
Expand All @@ -51,6 +53,7 @@ extension WidgetWritingIntent {
struct DataModel {
let widgetType: WidgetType
let content: String?
let successHandler: (() -> Void)?
}
}

Expand Down Expand Up @@ -80,13 +83,13 @@ extension WidgetWritingIntent: WidgetWritingIntent.Intentable {
Task {
model?.setLoading(status: true)
do {
try await requestPutProfileWidget(
widget: selectedWidget,
content: state.widgetBodyText
)
try await AppCoordinator.shared.refreshMyUserInfo()
// try await requestPutProfileWidget(
// widget: selectedWidget,
// content: state.widgetBodyText
// )
model?.setLoading(status: false)
model?.modalDismiss()
model?.doSuccessAction()
} catch {
// TODO: 에러처리
model?.setLoading(status: false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ final class WidgetWritingModel: ObservableObject {
var isFocused: Bool { get }
var isCTAButtonEnabled: Bool { get }

var successHandler: (() -> Void)? { get }

// default
var isLoading: Bool { get }

Expand All @@ -41,6 +43,7 @@ final class WidgetWritingModel: ObservableObject {
@Published var isPushedWriteContentView: Bool = true
@Published var isModalPresented: Bool = true
@Published var isFocused: Bool = false
var successHandler: (() -> Void)?
var textMaxCount: Int = 40

var isCTAButtonEnabled: Bool {
Expand All @@ -67,6 +70,8 @@ protocol WidgetWritingModelActionable: AnyObject {
func setValidation(value: Bool)
func setWidgetType(_ widget: WidgetType)
func setContentString(_ content: String)
func setSuccessHandler(handler: (() -> Void)?)
func doSuccessAction()
func navigationPop()
func modalDismiss()

Expand All @@ -93,6 +98,12 @@ extension WidgetWritingModel: WidgetWritingModelActionable {
func setWidgetType(_ widget: WidgetType) {
selectedWidgetType = widget
}
func setSuccessHandler(handler: (() -> Void)?) {
successHandler = handler
}
func doSuccessAction() {
successHandler?()
}
func navigationPop() {
isPushedWriteContentView = false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,17 @@ public struct WidgetWritingView: View {
isModalPresented: Binding<Bool>,
isPushed: Binding<Bool>,
isEditing: Bool = false,
contentString: String? = nil
contentString: String? = nil,
successHandler: (() -> Void)?
) {
let model = WidgetWritingModel()
let intent = WidgetWritingIntent(
model: model,
input: .init(widgetType: widgetType, content: contentString)
input: .init(
widgetType: widgetType,
content: contentString,
successHandler: successHandler
)
)
let container = MVIContainer(
intent: intent as WidgetWritingIntent.Intentable,
Expand Down Expand Up @@ -145,6 +150,8 @@ public struct WidgetWritingView: View {
widgetType: .body,
isModalPresented: .constant(false),
isPushed: .constant(false)
)
) {

}
}
}

0 comments on commit 7b59bf0

Please sign in to comment.