Skip to content

Commit

Permalink
Merge pull request #5 from Miiha/reducer-protocol-plus-dependency-update
Browse files Browse the repository at this point in the history
Updates to reducer protocol and dependency keys
  • Loading branch information
Miiha authored Nov 7, 2022
2 parents 7c91ad4 + 0dd8427 commit 94b1a3b
Show file tree
Hide file tree
Showing 19 changed files with 689 additions and 798 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,71 @@
"repositoryURL": "https://github.com/pointfreeco/combine-schedulers",
"state": {
"branch": null,
"revision": "ff42ec9061d864de7982162011321d3df5080c10",
"version": "0.1.2"
"revision": "882ac01eb7ef9e36d4467eb4b1151e74fcef85ab",
"version": "0.9.1"
}
},
{
"package": "swift-case-paths",
"repositoryURL": "https://github.com/pointfreeco/swift-case-paths",
"state": {
"branch": null,
"revision": "ed1838ab4fa5d47db8aa640736dd5b7672548873",
"version": "0.1.2"
"revision": "bb436421f57269fbcfe7360735985321585a86e5",
"version": "0.10.1"
}
},
{
"package": "swift-clocks",
"repositoryURL": "https://github.com/pointfreeco/swift-clocks",
"state": {
"branch": null,
"revision": "692ec4f5429a667bdd968c7260dfa2b23adfeffc",
"version": "0.1.4"
}
},
{
"package": "swift-collections",
"repositoryURL": "https://github.com/apple/swift-collections",
"state": {
"branch": null,
"revision": "f504716c27d2e5d4144fa4794b12129301d17729",
"version": "1.0.3"
}
},
{
"package": "swift-composable-architecture",
"repositoryURL": "https://github.com/pointfreeco/swift-composable-architecture",
"state": {
"branch": null,
"revision": "b67569f69813140cd9c984db33ee959d9711a008",
"version": "0.9.0"
"revision": "1fcd53fc875bade47d850749ea53c324f74fd64d",
"version": "0.45.0"
}
},
{
"package": "swift-custom-dump",
"repositoryURL": "https://github.com/pointfreeco/swift-custom-dump",
"state": {
"branch": null,
"revision": "819d9d370cd721c9d87671e29d947279292e4541",
"version": "0.6.0"
}
},
{
"package": "swift-identified-collections",
"repositoryURL": "https://github.com/pointfreeco/swift-identified-collections",
"state": {
"branch": null,
"revision": "bfb0d43e75a15b6dfac770bf33479e8393884a36",
"version": "0.4.1"
}
},
{
"package": "xctest-dynamic-overlay",
"repositoryURL": "https://github.com/pointfreeco/xctest-dynamic-overlay",
"state": {
"branch": null,
"revision": "16e6409ee82e1b81390bdffbf217b9c08ab32784",
"version": "0.5.0"
}
}
]
Expand Down
8 changes: 4 additions & 4 deletions Examples/Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
4AA682B2254D824B00031ADC /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4AA682B1254D824B00031ADC /* Assets.xcassets */; };
4AA682C0254D824B00031ADC /* ExampleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AA682BF254D824B00031ADC /* ExampleTests.swift */; };
4AA682E0254D827C00031ADC /* ComposableUserNotifications in Frameworks */ = {isa = PBXBuildFile; productRef = 4AA682DF254D827C00031ADC /* ComposableUserNotifications */; };
4AA682E7254D870C00031ADC /* AppState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AA682E6254D870C00031ADC /* AppState.swift */; };
4AA682E7254D870C00031ADC /* App.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AA682E6254D870C00031ADC /* App.swift */; };
4AFBD623254EC40900977967 /* Helper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AFBD622254EC40900977967 /* Helper.swift */; };
/* End PBXBuildFile section */

Expand All @@ -41,7 +41,7 @@
4AA682BB254D824B00031ADC /* ExampleTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = ExampleTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
4AA682BF254D824B00031ADC /* ExampleTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExampleTests.swift; sourceTree = "<group>"; };
4AA682C1254D824B00031ADC /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
4AA682E6254D870C00031ADC /* AppState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppState.swift; sourceTree = "<group>"; };
4AA682E6254D870C00031ADC /* App.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = App.swift; sourceTree = "<group>"; };
4AFBD622254EC40900977967 /* Helper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Helper.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -86,7 +86,7 @@
4AA682AC254D824900031ADC /* Example */ = {
isa = PBXGroup;
children = (
4AA682E6254D870C00031ADC /* AppState.swift */,
4AA682E6254D870C00031ADC /* App.swift */,
4A19F314254D8B9300E429F0 /* BackgroundNotification.swift */,
4AA682AF254D824900031ADC /* ContentView.swift */,
4AFBD622254EC40900977967 /* Helper.swift */,
Expand Down Expand Up @@ -216,7 +216,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
4AA682E7254D870C00031ADC /* AppState.swift in Sources */,
4AA682E7254D870C00031ADC /* App.swift in Sources */,
4AFBD623254EC40900977967 /* Helper.swift in Sources */,
4AA682B0254D824900031ADC /* ContentView.swift in Sources */,
4A19F319254D8BB700E429F0 /* UserNotification.swift in Sources */,
Expand Down
121 changes: 121 additions & 0 deletions Examples/Example/Example/App.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import Foundation
import ComposableArchitecture
import ComposableUserNotifications
import UIKit

struct App: ReducerProtocol {
struct State: Equatable {
var count: Int?
}

enum Action: Equatable {
case addNotificationResponse(TaskResult<Unit>)
case didFinishLaunching(notification: UserNotification?)
case didReceiveBackgroundNotification(BackgroundNotification)
case remoteCountResponse(TaskResult<Int>)
case requestAuthorizationResponse(TaskResult<Bool>)
case tappedScheduleButton
case userNotifications(UserNotificationClient.DeletegateAction)
}

@Dependency(\.remote) var remote
@Dependency(\.userNotifications) var userNotifications

func reduce(into state: inout State, action: Action) -> EffectTask<Action> {
switch action {
case let .didFinishLaunching(notification):
if case let .count(value) = notification {
state.count = value
}

return .run { send in
await withThrowingTaskGroup(of: Void.self) { group in
group.addTask {
for await event in self.userNotifications.delegate() {
await send(.userNotifications(event))
}
}

group.addTask {
await send(
.requestAuthorizationResponse(
TaskResult {
try await self.userNotifications.requestAuthorization([.alert, .badge, .sound])
}
)
)
}
}
}

case let .didReceiveBackgroundNotification(backgroundNotification):
let fetchCompletionHandler = backgroundNotification.fetchCompletionHandler
guard backgroundNotification.content == .countAvailable else {
return .fireAndForget {
backgroundNotification.fetchCompletionHandler(.noData)
}
}

return .task {
do {
let count = try await self.remote.fetchRemoteCount()
fetchCompletionHandler(.newData)
return .remoteCountResponse(.success(count))
} catch {
fetchCompletionHandler(.failed)
return .remoteCountResponse(.failure(error))
}
}

case let .remoteCountResponse(.success(count)):
state.count = count
return .none

case .remoteCountResponse(.failure):
return .none

case let .userNotifications(.willPresentNotification(_, completion)):
return .fireAndForget {
completion([.list, .banner, .sound])
}

case let .userNotifications(.didReceiveResponse(response, completion)):
let notification = UserNotification(userInfo: response.notification.request.content.userInfo())
if case let .count(value) = notification {
state.count = value
}

return .fireAndForget(completion)

case .userNotifications(.openSettingsForNotification):
return .none

case .requestAuthorizationResponse:
return .none

case .addNotificationResponse:
return .none

case .tappedScheduleButton:
let content = UNMutableNotificationContent()
content.title = "Example title"
content.body = "Example body"

let request = UNNotificationRequest(
identifier: "example_notification",
content: content,
trigger: UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
)

return .task {
await self.userNotifications
.removePendingNotificationRequestsWithIdentifiers(["example_notification"])
return await .addNotificationResponse(
TaskResult {
Unit(try await self.userNotifications.add(request))
}
)
}
}
}
}
118 changes: 0 additions & 118 deletions Examples/Example/Example/AppState.swift

This file was deleted.

7 changes: 0 additions & 7 deletions Examples/Example/Example/BackgroundNotification.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// BackgroundNotification.swift
// Example
//
// Created by Michael Kao on 31.10.20.
//

import Foundation
import UIKit

Expand Down
17 changes: 3 additions & 14 deletions Examples/Example/Example/ContentView.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
//
// ContentView.swift
// Example
//
// Created by Michael Kao on 31.10.20.
//

import ComposableArchitecture
import ComposableUserNotifications
import SwiftUI

struct ContentView: View {
let store: Store<AppState, AppAction>
let store: StoreOf<App>

var body: some View {
WithViewStore(self.store) { viewStore in
Expand All @@ -30,12 +23,8 @@ struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(
store: Store(
initialState: AppState(),
reducer: .empty,
environment: AppEnvironment(
remoteClient: RemoteClient(fetchRemoteCount: { Effect(value: 1) }),
userNotificationClient: .mock()
)
initialState: .init(),
reducer: App()
)
)
}
Expand Down
Loading

0 comments on commit 94b1a3b

Please sign in to comment.