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

Prepare 0.39.0 #58

Merged
merged 3 commits into from
Dec 21, 2022
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
15 changes: 4 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,8 @@ jobs:
name: SwiftPM Linux
runs-on: ubuntu-latest
steps:
- name: Install Swift
run: |
eval "$(curl -sL https://swiftenv.fuller.li/install.sh)"
- name: Checkout
uses: actions/checkout@v2
- name: Pull dependencies
run: |
swift package resolve
- uses: actions/checkout@v2
- name: Swift version
run: swift --version
- name: Test via SwiftPM
run: |
swift --version
swift test --enable-test-discovery
run: swift test --enable-test-discovery
5 changes: 5 additions & 0 deletions Examples/CaseStudies/CaseStudies.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@
DC4C6EA82450DD380066A05D /* UIKitCaseStudies */,
DC4C6EBF2450DD390066A05D /* UIKitCaseStudiesTests */,
);
indentWidth = 2;
sourceTree = "<group>";
};
DC89C41424460F95006900B9 /* Products */ = {
Expand Down Expand Up @@ -930,6 +931,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
OTHER_SWIFT_FLAGS = "-Xfrontend -warn-concurrency -Xfrontend -enable-actor-data-race-checks";
PRODUCT_BUNDLE_IDENTIFIER = co.pointfree.UIKitCaseStudies;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
Expand All @@ -949,6 +951,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
OTHER_SWIFT_FLAGS = "-Xfrontend -warn-concurrency -Xfrontend -enable-actor-data-race-checks";
PRODUCT_BUNDLE_IDENTIFIER = co.pointfree.UIKitCaseStudies;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
Expand Down Expand Up @@ -1121,6 +1124,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
OTHER_SWIFT_FLAGS = "-Xfrontend -warn-concurrency -Xfrontend -enable-actor-data-race-checks";
PRODUCT_BUNDLE_IDENTIFIER = co.pointfree.SwiftUICaseStudies;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
Expand All @@ -1139,6 +1143,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
OTHER_SWIFT_FLAGS = "-Xfrontend -warn-concurrency -Xfrontend -enable-actor-data-race-checks";
PRODUCT_BUNDLE_IDENTIFIER = co.pointfree.SwiftUICaseStudies;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
Expand Down
38 changes: 21 additions & 17 deletions Examples/CaseStudies/SwiftUICaseStudies/00-Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,31 @@ enum RootAction {
}

struct RootEnvironment {
var date: () -> Date
var date: @Sendable () -> Date
var downloadClient: DownloadClient
var fact: FactClient
var favorite: (UUID, Bool) -> Effect<Bool, Error>
var fetchNumber: () -> Effect<Int, Never>
var favorite: @Sendable (UUID, Bool) async throws -> Bool
var fetchNumber: @Sendable () async throws -> Int
var mainQueue: DateScheduler
var notificationCenter: NotificationCenter
var uuid: () -> UUID
var screenshots: @Sendable () async -> AsyncStream<Void>
var uuid: @Sendable () -> UUID
var webSocket: WebSocketClient

static let live = Self(
date: Date.init,
date: { Date() },
downloadClient: .live,
fact: .live,
favorite: favorite(id:isFavorite:),
fetchNumber: liveFetchNumber,
mainQueue: QueueScheduler.main,
notificationCenter: .default,
uuid: UUID.init,
screenshots: { @MainActor in
AsyncStream(
NotificationCenter.default
.notifications(named: UIApplication.userDidTakeScreenshotNotification)
.map { _ in }
)
},
uuid: { UUID() },
webSocket: .live
)
}
Expand Down Expand Up @@ -146,13 +152,13 @@ let rootReducer = Reducer<RootState, RootAction, RootEnvironment>.combine(
.pullback(
state: \.effectsCancellation,
action: /RootAction.effectsCancellation,
environment: { .init(fact: $0.fact, mainQueue: $0.mainQueue) }
environment: { .init(fact: $0.fact) }
),
episodesReducer
.pullback(
state: \.episodes,
action: /RootAction.episodes,
environment: { .init(favorite: $0.favorite, mainQueue: $0.mainQueue) }
environment: { .init(favorite: $0.favorite) }
),
focusDemoReducer
.pullback(
Expand Down Expand Up @@ -188,7 +194,7 @@ let rootReducer = Reducer<RootState, RootAction, RootEnvironment>.combine(
.pullback(
state: \.longLivingEffects,
action: /RootAction.longLivingEffects,
environment: { .init(notificationCenter: $0.notificationCenter) }
environment: { .init(screenshots: $0.screenshots) }
),
mapAppReducer
.pullback(
Expand Down Expand Up @@ -243,9 +249,7 @@ let rootReducer = Reducer<RootState, RootAction, RootEnvironment>.combine(
.pullback(
state: \.refreshable,
action: /RootAction.refreshable,
environment: {
.init(fact: $0.fact, mainQueue: $0.mainQueue)
}
environment: { .init(fact: $0.fact, mainQueue: $0.mainQueue) }
),
sharedStateReducer
.pullback(
Expand Down Expand Up @@ -275,7 +279,7 @@ let rootReducer = Reducer<RootState, RootAction, RootEnvironment>.combine(
.debug()
.signpost()

private func liveFetchNumber() -> Effect<Int, Never> {
Effect.deferred { Effect(value: Int.random(in: 1...1_000)) }
.delay(1, on: QueueScheduler.main)
@Sendable private func liveFetchNumber() async throws -> Int {
try await Task.sleep(nanoseconds: NSEC_PER_SEC)
return Int.random(in: 1...1_000)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ComposableArchitecture
import ReactiveSwift
import SwiftUI
@preconcurrency import SwiftUI // NB: SwiftUI.Color and SwiftUI.Animation are not Sendable yet.

private let readMe = """
This screen demonstrates how changes to application state can drive animations. Because the \
Expand All @@ -19,32 +19,14 @@ private let readMe = """
toggle at the bottom of the screen.
"""

extension Effect where Error == Never {
public static func keyFrames(
values: [(output: Value, duration: TimeInterval)],
scheduler: DateScheduler
) -> Self {
.concatenate(
values
.enumerated()
.map { index, animationState in
index == 0
? Effect(value: animationState.output)
: Effect(value: animationState.output)
.delay(values[index - 1].duration, on: scheduler)
}
)
}
}

struct AnimationsState: Equatable {
var alert: AlertState<AnimationsAction>?
var circleCenter: CGPoint?
var circleColor = Color.black
var isCircleScaled = false
}

enum AnimationsAction: Equatable {
enum AnimationsAction: Equatable, Sendable {
case alertDismissed
case circleScaleToggleChanged(Bool)
case rainbowButtonTapped
Expand Down Expand Up @@ -72,11 +54,12 @@ let animationsReducer = Reducer<AnimationsState, AnimationsAction, AnimationsEnv
return .none

case .rainbowButtonTapped:
return .keyFrames(
values: [Color.red, .blue, .green, .orange, .pink, .purple, .yellow, .black]
.map { (output: .setColor($0), duration: 1) },
scheduler: environment.mainQueue.animation(.linear)
)
return .run { send in
for color in [Color.red, .blue, .green, .orange, .pink, .purple, .yellow, .black] {
await send(.setColor(color), animation: .linear)
try await environment.mainQueue.sleep(for: .seconds(1))
}
}
.cancellable(id: CancelID.self)

case .resetButtonTapped:
Expand Down
28 changes: 23 additions & 5 deletions Examples/CaseStudies/SwiftUICaseStudies/02-Effects-Basics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ struct EffectsBasicsState: Equatable {

enum EffectsBasicsAction: Equatable {
case decrementButtonTapped
case decrementDelayResponse
case incrementButtonTapped
case numberFactButtonTapped
case numberFactResponse(Result<String, FactClient.Failure>)
case numberFactResponse(TaskResult<String>)
}

struct EffectsBasicsEnvironment {
Expand All @@ -47,25 +48,42 @@ let effectsBasicsReducer = Reducer<
EffectsBasicsAction,
EffectsBasicsEnvironment
> { state, action, environment in
enum DelayID {}

switch action {
case .decrementButtonTapped:
state.count -= 1
state.numberFact = nil
// Return an effect that re-increments the count after 1 second if the count is negative
return state.count >= 0
? .none
: .task {
try await environment.mainQueue.sleep(for: .seconds(1))
return .decrementDelayResponse
}
.cancellable(id: DelayID.self)

case .decrementDelayResponse:
if state.count < 0 {
state.count += 1
}
return .none

case .incrementButtonTapped:
state.count += 1
state.numberFact = nil
return .none
return state.count >= 0
? .cancel(id: DelayID.self)
: .none

case .numberFactButtonTapped:
state.isNumberFactRequestInFlight = true
state.numberFact = nil
// Return an effect that fetches a number fact from the API and returns the
// value back to the reducer's `numberFactResponse` action.
return environment.fact.fetch(state.count)
.observe(on: environment.mainQueue)
.catchToEffect(EffectsBasicsAction.numberFactResponse)
return .task { [count = state.count] in
await .numberFactResponse(TaskResult { try await environment.fact.fetch(count) })
}

case let .numberFactResponse(.success(response)):
state.isNumberFactRequestInFlight = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@ private let readMe = """

struct EffectsCancellationState: Equatable {
var count = 0
var currentTrivia: String?
var isTriviaRequestInFlight = false
var currentFact: String?
var isFactRequestInFlight = false
}

enum EffectsCancellationAction: Equatable {
case cancelButtonTapped
case stepperChanged(Int)
case triviaButtonTapped
case triviaResponse(Result<String, FactClient.Failure>)
case factButtonTapped
case factResponse(TaskResult<String>)
}

struct EffectsCancellationEnvironment {
var fact: FactClient
var mainQueue: DateScheduler
}

// MARK: - Business logic
Expand All @@ -40,35 +39,35 @@ let effectsCancellationReducer = Reducer<
EffectsCancellationState, EffectsCancellationAction, EffectsCancellationEnvironment
> { state, action, environment in

enum TriviaRequestId {}
enum NumberFactRequestID {}

switch action {
case .cancelButtonTapped:
state.isTriviaRequestInFlight = false
return .cancel(id: TriviaRequestId.self)
state.isFactRequestInFlight = false
return .cancel(id: NumberFactRequestID.self)

case let .stepperChanged(value):
state.count = value
state.currentTrivia = nil
state.isTriviaRequestInFlight = false
return .cancel(id: TriviaRequestId.self)

case .triviaButtonTapped:
state.currentTrivia = nil
state.isTriviaRequestInFlight = true

return environment.fact.fetch(state.count)
.observe(on: environment.mainQueue)
.catchToEffect(EffectsCancellationAction.triviaResponse)
.cancellable(id: TriviaRequestId.self)

case let .triviaResponse(.success(response)):
state.isTriviaRequestInFlight = false
state.currentTrivia = response
state.currentFact = nil
state.isFactRequestInFlight = false
return .cancel(id: NumberFactRequestID.self)

case .factButtonTapped:
state.currentFact = nil
state.isFactRequestInFlight = true

return .task { [count = state.count] in
await .factResponse(TaskResult { try await environment.fact.fetch(count) })
}
.cancellable(id: NumberFactRequestID.self)

case let .factResponse(.success(response)):
state.isFactRequestInFlight = false
state.currentFact = response
return .none

case .triviaResponse(.failure):
state.isTriviaRequestInFlight = false
case .factResponse(.failure):
state.isFactRequestInFlight = false
return .none
}
}
Expand All @@ -91,7 +90,7 @@ struct EffectsCancellationView: View {
value: viewStore.binding(get: \.count, send: EffectsCancellationAction.stepperChanged)
)

if viewStore.isTriviaRequestInFlight {
if viewStore.isFactRequestInFlight {
HStack {
Button("Cancel") { viewStore.send(.cancelButtonTapped) }
Spacer()
Expand All @@ -101,11 +100,11 @@ struct EffectsCancellationView: View {
.id(UUID())
}
} else {
Button("Number fact") { viewStore.send(.triviaButtonTapped) }
.disabled(viewStore.isTriviaRequestInFlight)
Button("Number fact") { viewStore.send(.factButtonTapped) }
.disabled(viewStore.isFactRequestInFlight)
}

viewStore.currentTrivia.map {
viewStore.currentFact.map {
Text($0).padding(.vertical, 8)
}
}
Expand Down Expand Up @@ -134,8 +133,7 @@ struct EffectsCancellation_Previews: PreviewProvider {
initialState: EffectsCancellationState(),
reducer: effectsCancellationReducer,
environment: EffectsCancellationEnvironment(
fact: .live,
mainQueue: QueueScheduler.main
fact: .live
)
)
)
Expand Down
Loading