Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix mailer #10

Merged
merged 2 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Examples/HoodsApp/HoodsApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@
BUNDLE_LOADER = "$(TEST_HOST)";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = SWWYZ4DZ7N;
DEVELOPMENT_TEAM = N77R86QZUG;
GENERATE_INFOPLIST_FILE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 17.0;
MARKETING_VERSION = 1.0;
Expand All @@ -487,7 +487,7 @@
BUNDLE_LOADER = "$(TEST_HOST)";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_TEAM = SWWYZ4DZ7N;
DEVELOPMENT_TEAM = N77R86QZUG;
GENERATE_INFOPLIST_FILE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 17.0;
MARKETING_VERSION = 1.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,34 @@ import MessageUI
@Reducer
struct MailButtonDemoFeature {
struct State: Equatable {
@BindingState var recipient: String = "foo@bar.tld"
@BindingState var subject: String = "Hello from the ’hoods"
@BindingState var body: String = "Look at this awesome TCA mailer"
var canSendMail: Bool = MFMailComposeViewController.canSendMail()
@BindingState var subject: String
@BindingState var body: String
var errorDescription: String?

var mailContent: MailButtonFeature.State {
get {
MailButtonFeature.State(
mailtoComponents: MailtoComponents(recipient: recipient, subject: subject, body: body),
canSendEmail: canSendMail
)
}
set {
// Unidirectional data flow, do nothing
}
var mailContent: MailButtonFeature.State

init(recipient: String = "foo@bar.tld",
subject: String = "Hello from the ’hoods",
body: String = "Look at this awesome TCA mailer") {
self.subject = subject
self.body = body

mailContent = .init(
mailtoComponents: MailtoComponents(
recipient: recipient,
subject: subject,
body: body
),
canSendEmail: MFMailComposeViewController.canSendMail()
)
}
}

enum Action: BindableAction {
case binding(BindingAction<State>)

case recipientChanged(String)

case mailButton(MailButtonFeature.Action)
}

Expand All @@ -38,6 +45,14 @@ struct MailButtonDemoFeature {
}
Reduce { state, action in
switch action {
case .binding:
// Keep fields up to date
state.mailContent.mailtoComponents.subject = state.subject
state.mailContent.mailtoComponents.body = state.body
return .none
case let .recipientChanged(recipient):
state.mailContent.mailtoComponents.recipient = recipient
return .none
case .mailButton(.buttonTapped):
state.errorDescription = nil
return .none
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ struct MailButtonDemoView: View {
WithViewStore(store, observe: { $0 }) { viewStore in
Form {
Section(header: Text("Mail Content")) {
TextField("Recipient", text: viewStore.$recipient)
.textInputAutocapitalization(.never)
.textContentType(.emailAddress)
.keyboardType(.emailAddress)
TextField("Recipient", text: viewStore.binding(
get: { $0.mailContent.mailtoComponents.recipient ?? "" },
send: { .recipientChanged($0) }
))
.textInputAutocapitalization(.never)
.textContentType(.emailAddress)
.keyboardType(.emailAddress)
TextField("Subject", text: viewStore.$subject)
TextField("Body", text: viewStore.$body)
}
Expand Down
7 changes: 4 additions & 3 deletions Examples/HoodsApp/HoodsAppTests/MailButtonDemoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,23 @@ import XCTest
@MainActor
final class MailButtonDemoTests: XCTestCase {
func testMailButtonDemo() async throws {
// isRecording = true
let openURL = TestDependenciesFactory.OpenURL()

let store = TestStore(initialState: MailButtonDemoFeature.State()) {
MailButtonDemoFeature()
} withDependencies: {
$0.openURL = openURL.effect
}
await store.send(.set(\.$recipient, "john.doe@example.com")) {
$0.recipient = "john.doe@example.com"
await store.send(.recipientChanged("john.doe@example.com")) {
$0.mailContent.mailtoComponents.recipient = "john.doe@example.com"
}
await store.send(.set(\.$subject, "Hey Joe")) {
$0.subject = "Hey Joe"
$0.mailContent.mailtoComponents.subject = "Hey Joe"
}
await store.send(.set(\.$body, "Have you heard of Jimi Hendrix?")) {
$0.body = "Have you heard of Jimi Hendrix?"
$0.mailContent.mailtoComponents.body = "Have you heard of Jimi Hendrix?"
}

await store.send(.mailButton(.buttonTapped))
Expand Down
34 changes: 31 additions & 3 deletions Sources/Hoods/Mailer/MailButtonFeature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,51 @@ import Blocks
import ComposableArchitecture
import Foundation

/// A SwiftUI Reducer representing a mail button feature.
///
///
@Reducer
public struct MailButtonFeature {
// MARK: - State

public struct State: Equatable {
/// The state of the destination screen, if applicable.
@PresentationState var destination: Destination.State?
var mailtoComponents: MailtoComponents
var canSendEmail: Bool

/// The components used to the mail to be sent.
public var mailtoComponents: MailtoComponents

/// A boolean indicating whether the device can send *in-app* emails.
///
/// Typically, it should be set to the value of `MFMailComposeViewController.canSendMail()`
public var canSendEmail: Bool

/// Initializes the state with the given parameters.
///
/// - Parameters:
/// - mailtoComponents: The components used to construct a 'mailto' URL.
/// - canSendEmail: A boolean indicating whether the device can send emails.
public init(mailtoComponents: MailtoComponents, canSendEmail: Bool) {
self.mailtoComponents = mailtoComponents
self.canSendEmail = canSendEmail
}
}

// MARK: - Action

public enum Action {
// MARK: - Main Actions

/// Action triggered when the mail button is tapped.
case buttonTapped

// MARK: - Navigation-related actions

/// Actions related to the destination screen.
case destination(PresentationAction<Destination.Action>)

/// Delegate actions for handling specific scenarios.
case delegate(Delegate)
case buttonTapped

public enum Delegate: Equatable {
case shouldPresentError(MailButtonError)
Expand Down
Loading