Skip to content

Commit

Permalink
0.18.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dankinsoid committed Mar 4, 2024
1 parent b2eb638 commit b4faacd
Show file tree
Hide file tree
Showing 18 changed files with 601 additions and 605 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ jobs:
runs-on: macos-latest

steps:
- uses: swift-actions/setup-swift@v1
with:
swift-version: '5.9'
- name: Get swift version
run: swift --version
- uses: actions/checkout@v3
- name: Build
run: swift build -v
Expand Down
36 changes: 18 additions & 18 deletions Examples/Search/Search/Search.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ import Foundation
// MARK: - Search feature domain

struct Search: Equatable {

var results: [GeocodingSearch.Result] = []
var resultForecastRequestInFlight: GeocodingSearch.Result?
var searchQuery = ""
var weather: Weather?

struct Weather: Equatable {

var id: GeocodingSearch.Result.ID
var days: [Day]

struct Day: Equatable {
var date: Date
var temperatureMax: Double
var temperatureMaxUnit: String
var temperatureMin: Double
var temperatureMinUnit: String
}
}
var results: [GeocodingSearch.Result] = []
var resultForecastRequestInFlight: GeocodingSearch.Result?
var searchQuery = ""
var weather: Weather?

struct Weather: Equatable {

var id: GeocodingSearch.Result.ID
var days: [Day]

struct Day: Equatable {
var date: Date
var temperatureMax: Double
var temperatureMaxUnit: String
var temperatureMin: Double
var temperatureMinUnit: String
}
}
}
84 changes: 42 additions & 42 deletions Examples/Search/Search/SearchActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,52 @@ import VDStore
@Actions
extension Store<Search> {

func searchQueryChanged(query: String) {
state.searchQuery = query
cancel(Self.searchQueryChangeDebounced)
guard query.isEmpty else { return }
state.results = []
state.weather = nil
}
func searchQueryChanged(query: String) {
state.searchQuery = query
cancel(Self.searchQueryChangeDebounced)
guard query.isEmpty else { return }
state.results = []
state.weather = nil
}

func searchQueryChangeDebounced() async {
try? await Task.sleep(nanoseconds: NSEC_PER_SEC / 3)
guard !state.searchQuery.isEmpty, !Task.isCancelled else {
return
}
do {
let response = try await di.weatherClient.search(state.searchQuery)
guard !Task.isCancelled else { return }
state.results = response.results
} catch {
guard !Task.isCancelled, !(error is CancellationError) else { return }
state.results = []
}
}
func searchQueryChangeDebounced() async {
try? await Task.sleep(nanoseconds: NSEC_PER_SEC / 3)
guard !state.searchQuery.isEmpty, !Task.isCancelled else {
return
}
do {
let response = try await di.weatherClient.search(state.searchQuery)
guard !Task.isCancelled else { return }
state.results = response.results
} catch {
guard !Task.isCancelled, !(error is CancellationError) else { return }
state.results = []
}
}
}

@Actions
extension Store<Search> {

func searchResultTapped(location: GeocodingSearch.Result) async {
state.resultForecastRequestInFlight = location
defer { state.resultForecastRequestInFlight = nil }
do {
let forecast = try await di.weatherClient.forecast(location)
state.weather = State.Weather(
id: location.id,
days: forecast.daily.time.indices.map {
State.Weather.Day(
date: forecast.daily.time[$0],
temperatureMax: forecast.daily.temperatureMax[$0],
temperatureMaxUnit: forecast.dailyUnits.temperatureMax,
temperatureMin: forecast.daily.temperatureMin[$0],
temperatureMinUnit: forecast.dailyUnits.temperatureMin
)
}
)
} catch {
state.weather = nil
}
}
func searchResultTapped(location: GeocodingSearch.Result) async {
state.resultForecastRequestInFlight = location
defer { state.resultForecastRequestInFlight = nil }
do {
let forecast = try await di.weatherClient.forecast(location)
state.weather = State.Weather(
id: location.id,
days: forecast.daily.time.indices.map {
State.Weather.Day(
date: forecast.daily.time[$0],
temperatureMax: forecast.daily.temperatureMax[$0],
temperatureMaxUnit: forecast.dailyUnits.temperatureMax,
temperatureMin: forecast.daily.temperatureMin[$0],
temperatureMinUnit: forecast.dailyUnits.temperatureMin
)
}
)
} catch {
state.weather = nil
}
}
}
26 changes: 13 additions & 13 deletions Examples/Search/Search/SearchApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ struct SearchApp: App {
var body: some Scene {
WindowGroup {
SearchView()
.storeDIValues {
$0.middleware(LoggerMiddleware())
}
.storeDIValues {
$0.middleware(LoggerMiddleware())
}
}
}
}

struct LoggerMiddleware: StoreMiddleware {
func execute<State, Args, Res>(
_ args: Args,
context: Store<State>.Action<Args, Res>.Context,
dependencies: StoreDIValues,
next: (Args) -> Res
) -> Res {
print("\(context.actionID) called from \(context.file):\(context.line) \(context.function)")
return next(args)
}

func execute<State, Args, Res>(
_ args: Args,
context: Store<State>.Action<Args, Res>.Context,
dependencies: StoreDIValues,
next: (Args) -> Res
) -> Res {
print("\(context.actionID) called from \(context.file):\(context.line) \(context.function)")
return next(args)
}
}
10 changes: 5 additions & 5 deletions Examples/Search/Search/SearchView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct SearchView: View {
@ViewStore var state = Search()

var body: some View {
NavigationStack {
NavigationStack {
VStack(alignment: .leading) {
Text(readMe)
.padding()
Expand All @@ -39,9 +39,9 @@ struct SearchView: View {
ForEach(state.results) { location in
VStack(alignment: .leading) {
Button {
Task {
await $state.searchResultTapped(location: location)
}
Task {
await $state.searchResultTapped(location: location)
}
} label: {
HStack {
Text(location.name)
Expand All @@ -68,7 +68,7 @@ struct SearchView: View {
.navigationTitle("Search")
}
.task(id: state.searchQuery) {
await $state.searchQueryChangeDebounced()
await $state.searchQueryChangeDebounced()
}
}

Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ let package = Package(
],
targets: [
.target(name: "VDStore", dependencies: []),
.testTarget(name: "VDStoreTests", dependencies: ["VDStore"]),
.testTarget(name: "VDStoreTests", dependencies: ["VDStore"]),
]
)
20 changes: 10 additions & 10 deletions [email protected]
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// swift-tools-version:5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
import CompilerPluginSupport
import PackageDescription

let package = Package(
name: "VDStore",
Expand All @@ -16,17 +16,17 @@ let package = Package(
.library(name: "VDStore", targets: ["VDStore"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.2")
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.2"),
],
targets: [
.target(name: "VDStore", dependencies: ["VDStoreMacros"]),
.macro(
name: "VDStoreMacros",
dependencies: [
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftCompilerPlugin", package: "swift-syntax")
]
),
.testTarget(name: "VDStoreTests", dependencies: ["VDStore"]),
.macro(
name: "VDStoreMacros",
dependencies: [
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
]
),
.testTarget(name: "VDStoreTests", dependencies: ["VDStore"]),
]
)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ import PackageDescription
let package = Package(
name: "SomeProject",
dependencies: [
.package(url: "https://github.com/dankinsoid/VDStore.git", from: "0.17.4")
.package(url: "https://github.com/dankinsoid/VDStore.git", from: "0.18.0")
],
targets: [
.target(name: "SomeProject", dependencies: ["VDStore"])
Expand Down
8 changes: 4 additions & 4 deletions Sources/VDStore/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public struct Store<State> {
}

/// The publisher that emits before the state is going to be changed. Required by `SwiftUI`.
nonisolated var willSet: AnyPublisher<Void, Never> {
nonisolated var willSet: AnyPublisher<Void, Never> {
box.willSet.eraseToAnyPublisher()
}

Expand All @@ -141,7 +141,7 @@ public struct Store<State> {
/// Creates a new `Store` with the initial state.
public nonisolated init(_ state: State) {
self.init(
box: StoreBox(state),
box: StoreBox(state),
di: StoreDIValues()
)
}
Expand All @@ -150,7 +150,7 @@ public struct Store<State> {
box: StoreBox<State>,
di: StoreDIValues
) {
self.box = box
self.box = box
diStorage = di
}

Expand Down Expand Up @@ -187,7 +187,7 @@ public struct Store<State> {
set setter: @escaping (inout State, ChildState) -> Void
) -> Store<ChildState> {
Store<ChildState>(
box: StoreBox<ChildState>(parent: box, get: getter, set: setter),
box: StoreBox<ChildState>(parent: box, get: getter, set: setter),
di: di
)
}
Expand Down
Loading

0 comments on commit b4faacd

Please sign in to comment.