Skip to content

Commit

Permalink
Apply TCA v1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
jiyimeta committed Jan 2, 2024
1 parent 0025b38 commit 01d9a39
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ opt_in_rules:
# - let_var_whitespace
- literal_expression_end_indentation
- lower_acl_than_parent
- missing_docs
# - missing_docs
- modifier_order
# - multiline_arguments
# - multiline_arguments_brackets
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let package = Package(
),
],
dependencies: [
.package(url: "https://github.com/pointfreeco/swift-composable-architecture", "1.4.2"..<"2.0.0"),
.package(url: "https://github.com/pointfreeco/swift-composable-architecture", "1.5.0" ..< "2.0.0"),
],
targets: [
.target(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ extension View {
public func textFieldAlert<ButtonAction>(
store: Store<PresentationState<TextFieldAlertState<ButtonAction>>, PresentationAction<ButtonAction>>
) -> some View {
self.textFieldAlert(store: store, state: { $0 }, action: { $0 })
textFieldAlert(store: store, state: { $0 }, action: { $0 })
}

public func textFieldAlert<State, Action, ButtonAction>(
store: Store<PresentationState<State>, PresentationAction<Action>>,
state toDestinationState: @escaping (_ state: State) -> TextFieldAlertState<ButtonAction>?,
action fromDestinationAction: @escaping (_ alertAction: ButtonAction) -> Action
) -> some View {
self.presentation(
presentation(
store: store,
state: toDestinationState,
action: fromDestinationAction
) { `self`, $isPresented, destination in
let textFieldAlertState = store.stateSubject.value.wrappedValue.flatMap(toDestinationState)
) { `self`, $isPresented, _ in
let textFieldAlertState = store.withState { $0.wrappedValue.flatMap(toDestinationState) }

self.textFieldAlert(
(textFieldAlertState?.title).map { String(state: $0) } ?? "",
isPresented: $isPresented,
Expand All @@ -32,11 +32,20 @@ extension View {
switch button.action.type {
case let .send(commitAction):
if let commitAction {
store.send(.presented(fromDestinationAction(commitAction.embed(text))))
store.send(
.presented(
fromDestinationAction(commitAction.embed(text))
)
)
}
case let .animatedSend(commitAction, animation):
if let commitAction {
store.send(.presented(fromDestinationAction(commitAction.embed(text))), animation: animation)
store.send(
.presented(
fromDestinationAction(commitAction.embed(text))
),
animation: animation
)
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import ComposableArchitecture
import SwiftUI

public struct TextFieldAlertState<Action>: Identifiable {
public typealias CommitAction = CasePath<Action, String>
public typealias CommitAction = AnyCasePath<Action, String>

public let id: UUID
public var buttons: [ButtonState<CommitAction>]
public var message: TextState?
public var title: TextState
public var placeholder: TextState?
public var defaultText: TextState?

init(
id: UUID,
buttons: [ButtonState<CommitAction>],
Expand All @@ -26,7 +26,7 @@ public struct TextFieldAlertState<Action>: Identifiable {
self.placeholder = placeholder
self.defaultText = defaultText
}

public init(
title: () -> TextState,
@ButtonStateBuilder<CommitAction> actions: () -> [ButtonState<CommitAction>] = { [] },
Expand All @@ -43,9 +43,9 @@ public struct TextFieldAlertState<Action>: Identifiable {
defaultText: defaultText?()
)
}

public func map<NewAction>(
_ transform: (CommitAction?) -> CasePath<NewAction, String>?
_ transform: (CommitAction?) -> AnyCasePath<NewAction, String>?
) -> TextFieldAlertState<NewAction> {
TextFieldAlertState<NewAction>(
id: id,
Expand All @@ -61,7 +61,7 @@ public struct TextFieldAlertState<Action>: Identifiable {
extension TextFieldAlertState: CustomDumpReflectable {
public var customDumpMirror: Mirror {
var children: [(label: String?, value: Any)] = [
("title", title)
("title", title),
]
if !buttons.isEmpty {
children.append(("actions", buttons))
Expand All @@ -83,30 +83,28 @@ extension TextFieldAlertState: CustomDumpReflectable {
}
}

extension CasePath: Equatable where Root: Equatable, Value == String {
extension AnyCasePath: Equatable where Root: Equatable, Value == String {
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs ~= rhs.embed("")
}
}

extension CasePath: Hashable where Root: Hashable, Value == String {
extension AnyCasePath: Hashable where Root: Hashable, Value == String {
public func hash(into hasher: inout Hasher) {
hasher.combine(embed(""))
}
}


extension TextFieldAlertState: Equatable where Action: Equatable {
public static func == (lhs: Self, rhs: Self) -> Bool {
lhs.title == rhs.title
&& lhs.message == rhs.message
&& lhs.buttons == rhs.buttons
&& lhs.placeholder == rhs.placeholder
&& lhs.defaultText == rhs.defaultText
&& lhs.message == rhs.message
&& lhs.buttons == rhs.buttons
&& lhs.placeholder == rhs.placeholder
&& lhs.defaultText == rhs.defaultText
}
}


extension TextFieldAlertState: Hashable where Action: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(title)
Expand All @@ -130,12 +128,12 @@ extension TextFieldAlert.Button.Role {

extension TextFieldAlert.Button {
init<Action>(
_ button: ButtonState<CasePath<Action, String>>,
action handler: @escaping (CasePath<Action, String>?) -> Void
_ button: ButtonState<AnyCasePath<Action, String>>,
action handler: @escaping (AnyCasePath<Action, String>?) -> Void
) {
self.init(
label: String(state: button.label),
action: { text in
action: { _ in
button.withAction(handler)
},
role: button.role.map(TextFieldAlert.Button.Role.init) ?? .default
Expand Down
17 changes: 9 additions & 8 deletions Sources/ComposablePopup/PopupState+Initializers.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Foundation
import ComposableArchitecture
import Foundation

public extension PopupState {
static func alert(
extension PopupState {
public static func alert(
title: () -> TextState,
@ButtonStateBuilder<Action> actions: () -> [ButtonState<Action>] = { [] },
message: (() -> TextState)? = nil
Expand All @@ -15,8 +15,8 @@ public extension PopupState {
)
)
}
static func confirmationDialog(

public static func confirmationDialog(
titleVisibility: ConfirmationDialogStateTitleVisibility = .automatic,
title: () -> TextState,
@ButtonStateBuilder<Action> actions: () -> [ButtonState<Action>] = { [] },
Expand All @@ -31,10 +31,11 @@ public extension PopupState {
)
)
}
static func textFieldAlert(

public static func textFieldAlert(
title: () -> TextState,
@ButtonStateBuilder<CasePath<Action, String>> actions: () -> [ButtonState<CasePath<Action, String>>] = { [] },
@ButtonStateBuilder<AnyCasePath<Action, String>>
actions: () -> [ButtonState<AnyCasePath<Action, String>>] = { [] },
message: (() -> TextState)? = nil,
placeholder: (() -> TextState)? = nil,
defaultText: (() -> TextState)? = nil
Expand Down

0 comments on commit 01d9a39

Please sign in to comment.