Skip to content

Commit

Permalink
[WEAV-122] Widget 뷰 MVI 로직에 맞춰 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
jisu15-kim committed Nov 15, 2024
1 parent 6351006 commit a12c699
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public struct WritableProfileWidgetView: View {
PlainTextEditorStyle()
)
.focused($isfocused)
.flatTextFieldOption()
.flatTextFieldOption(keyboardType: .namePhonePad)
.typography(.regular_14)
.foregroundStyle(bodyColor)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import Foundation
import CommonKit
import CoreKit
import Model

//MARK: - Intent
class WidgetSelectionIntent {
Expand All @@ -29,6 +30,7 @@ class WidgetSelectionIntent {
extension WidgetSelectionIntent {
protocol Intentable {
// content
func onTapWidget(_ widget: WidgetType)
func onTapNextButton()

// default
Expand All @@ -42,6 +44,10 @@ extension WidgetSelectionIntent {
//MARK: - Intentable
extension WidgetSelectionIntent: WidgetSelectionIntent.Intentable {
// default
func onTapWidget(_ widget: WidgetType) {
model?.setSelectedWidget(widget: widget)
model?.setPushWriteContentView(status: true)
}
func onAppear() {}

func task() async {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
import Foundation
import CommonKit
import CoreKit
import Model

final class WidgetSelectionModel: ObservableObject {

//MARK: Stateful
protocol Stateful {
// content
var isModalPresented: Bool { get set }
var isPushWriteContentView: Bool { get set }
var isValidated: Bool { get }
var selectedWidget: WidgetType? { get }

// default
var isLoading: Bool { get }
Expand All @@ -27,7 +31,10 @@ final class WidgetSelectionModel: ObservableObject {

//MARK: State Properties
// content
@Published var isModalPresented: Bool = false
@Published var isPushWriteContentView: Bool = false
@Published var isValidated: Bool = false
@Published var selectedWidget: WidgetType?

// default
@Published var isLoading: Bool = false
Expand All @@ -42,7 +49,10 @@ extension WidgetSelectionModel: WidgetSelectionModel.Stateful {}
//MARK: - Actionable
protocol WidgetSelectionModelActionable: AnyObject {
// content
func setModalPresented(status: Bool)
func setPushWriteContentView(status: Bool)
func setValidation(value: Bool)
func setSelectedWidget(widget: WidgetType)

// default
func setLoading(status: Bool)
Expand All @@ -55,6 +65,15 @@ protocol WidgetSelectionModelActionable: AnyObject {

extension WidgetSelectionModel: WidgetSelectionModelActionable {
// content
func setModalPresented(status: Bool) {
isModalPresented = status
}
func setPushWriteContentView(status: Bool) {
isPushWriteContentView = status
}
func setSelectedWidget(widget: WidgetType) {
selectedWidget = widget
}
func setValidation(value: Bool) {
isValidated = value
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import Model

public struct WidgetSelectionView: View {

@State private var isPushedWriteView: Bool = false
@State private var selectedWidget: WidgetType?
// @State private var isPushedWriteView: Bool = false

@Binding var isPresentedSelectionView: Bool
@StateObject var container: MVIContainer<WidgetSelectionIntent.Intentable, WidgetSelectionModel.Stateful>
Expand All @@ -41,6 +40,7 @@ public struct WidgetSelectionView: View {
)
self._container = StateObject(wrappedValue: container)
self._isPresentedSelectionView = isPresented
model.setModalPresented(status: isPresented.wrappedValue)
}

public var body: some View {
Expand All @@ -66,24 +66,28 @@ public struct WidgetSelectionView: View {
iconType: .add
)
.onTapGesture {
selectedWidget = widget
isPushedWriteView = true
intent.onTapWidget(widget)
}
}
}
.padding()
}
}
.navigationDestination(
isPresented: $isPushedWriteView,
isPresented: $container.model.isPushWriteContentView,
destination: {
WidgetWritingView(
widgetType: selectedWidget ?? .book,
isModalPresented: $isPresentedSelectionView,
isPushed: $isPushedWriteView
)
if let widget = state.selectedWidget {
WidgetWritingView(
widgetType: widget,
isModalPresented: $container.model.isModalPresented,
isPushed: $container.model.isPushWriteContentView
)
}
}
)
.onChange(of: state.isModalPresented) {
isPresentedSelectionView = state.isModalPresented
}
.task {
await intent.task()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ extension WidgetWritingIntent {
// content
func onChangedBodyText(_ text: String, maxCount: Int)
func onTapNextButton(state: WidgetWritingModel.Stateful)
func onTapBackButton()
func onTapDismissButton()

// default
func onAppear()
Expand All @@ -59,6 +61,12 @@ extension WidgetWritingIntent: WidgetWritingIntent.Intentable {
let formattedText = text.clipMaxCount(maxCount)
model?.setBodyText(formattedText)
}
func onTapBackButton() {
model?.navigationPop()
}
func onTapDismissButton() {
model?.modalDismiss()
}
func onAppear() {
model?.setFocusState(true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ final class WidgetWritingModel: ObservableObject {
var isValidated: Bool { get }
var textMaxCount: Int { get }

var isPushedWriteContentView: Bool { get set }
var isModalPresented: Bool { get }
var isFocused: Bool { get }
var isCTAButtonEnabled: Bool { get }
Expand All @@ -37,6 +38,7 @@ final class WidgetWritingModel: ObservableObject {
// content
@Published var selectedWidgetType: WidgetType?
@Published var widgetBodyText = String()
@Published var isPushedWriteContentView: Bool = true
@Published var isModalPresented: Bool = true
@Published var isFocused: Bool = false
var textMaxCount: Int = 40
Expand Down Expand Up @@ -65,6 +67,7 @@ protocol WidgetWritingModelActionable: AnyObject {
func setValidation(value: Bool)
func setWidgetType(_ widget: WidgetType)
func setContentString(_ content: String)
func navigationPop()
func modalDismiss()

// default
Expand All @@ -90,6 +93,9 @@ extension WidgetWritingModel: WidgetWritingModelActionable {
func setWidgetType(_ widget: WidgetType) {
selectedWidgetType = widget
}
func navigationPop() {
isPushedWriteContentView = false
}
func modalDismiss() {
isModalPresented = false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public struct WidgetWritingView: View {

@Binding var isPushed: Bool
@Binding var isModalPresented: Bool

@FocusState var isFocused: Bool
let isEditingMode: Bool
let contentString: String?
Expand Down Expand Up @@ -107,6 +108,9 @@ public struct WidgetWritingView: View {
.onChange(of: state.isModalPresented) {
isModalPresented = state.isModalPresented
}
.onChange(of: state.isPushedWriteContentView) {
isPushed = state.isPushedWriteContentView
}
.task {
await intent.task()
}
Expand All @@ -119,13 +123,13 @@ public struct WidgetWritingView: View {
.setNavigation(
showLeftBackButton: isEditingMode ? false : true,
handler: {
isPushed = false
intent.onTapBackButton()
})
.toolbar {
if isEditingMode {
ToolbarItem {
Button("닫기") {
isModalPresented = false
intent.onTapDismissButton()
}
.typography(.medium_16)
}
Expand Down
33 changes: 33 additions & 0 deletions Projects/Features/Home/UnitTest/WidgetUnitTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// WidgetUnitTest.swift
// Home-UnitTest
//
// Created by 김지수 on 11/15/24.
// Copyright © 2024 com.weave. All rights reserved.
//

import Testing
@testable import Home

struct WidgetUnitTest {

let widgetSelectionState: WidgetSelectionModel
let widgetSelectionIntent: WidgetSelectionIntent

init () {
self.widgetSelectionState = WidgetSelectionModel()
self.widgetSelectionIntent = WidgetSelectionIntent(
model: widgetSelectionState,
input: .init()
)
}

@Test func someFunctions() async throws {
// Write your test here and use APIs like `#expect(...)` to check expected conditions.
#expect(widgetSelectionState.isValidated == false)
let a = 0
let b = 1
#expect(a + b == 1)

}
}
9 changes: 9 additions & 0 deletions Projects/Features/Home/UnitTest/temp.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//
// temp.swift
// DesignPreview
//
// Created by 김지수 on 11/15/24.
// Copyright © 2024 com.weave. All rights reserved.
//

import Foundation
6 changes: 6 additions & 0 deletions Projects/Features/Project.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ let project: Project = .make(
.project(target: .commonKit),
.project(target: .designCore)
]
),
.makeUnitTest(
target: .home,
dependencies: [
.project(target: .home)
]
)
]
)

0 comments on commit a12c699

Please sign in to comment.