Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: 🧪 Run tests
run: xcodebuild test -scheme "swift-nibbles-Package" -testPlan "swift-nibbles-Package" -destination "OS=17.0,name=iPhone 15 Pro"
- name: 📊 Upload Coverage
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
swift: true
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
LastUpgradeVersion = "1600"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
buildImplicitDependencies = "YES"
buildArchitectures = "Automatic">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
Expand All @@ -14,9 +15,9 @@
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "HTTPNetworking"
BuildableName = "HTTPNetworking"
BlueprintName = "HTTPNetworking"
BlueprintIdentifier = "StateBinding"
BuildableName = "StateBinding"
BlueprintName = "StateBinding"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
Expand All @@ -26,13 +27,8 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<TestPlans>
<TestPlanReference
reference = "container:Tests/HTTPNetworkingTests/HTTPNetworking.xctestplan"
default = "YES">
</TestPlanReference>
</TestPlans>
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
Expand All @@ -54,9 +50,9 @@
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "HTTPNetworking"
BuildableName = "HTTPNetworking"
BlueprintName = "HTTPNetworking"
BlueprintIdentifier = "StateBinding"
BuildableName = "StateBinding"
BlueprintName = "StateBinding"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "StateBinding"
BuildableName = "StateBinding"
BlueprintName = "StateBinding"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
Expand Down Expand Up @@ -124,6 +138,16 @@
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "StateBindingTests"
BuildableName = "StateBindingTests"
BlueprintName = "StateBindingTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
Expand Down
6 changes: 5 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ let package = Package(
.library(name: "Identified", targets: ["Identified"]),
.library(name: "SharedState", targets: ["SharedState"]),
.library(name: "Stash", targets: ["Stash"]),
.library(name: "StateBinding", targets: ["StateBinding"]),
.plugin(name: "Create TCA Feature", targets: ["Create TCA Feature"])
],
dependencies: [
Expand All @@ -40,7 +41,10 @@ let package = Package(

.target(name: "Stash"),
.testTarget(name: "StashTests", dependencies: ["Stash"]),


.target(name: "StateBinding"),
.testTarget(name: "StateBindingTests", dependencies: ["StateBinding", .product(name: "ConcurrencyExtras", package: "swift-concurrency-extras")]),

.plugin(
name: "Create TCA Feature",
capability: .command(
Expand Down
50 changes: 25 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,9 @@ Nibbles are all broken down into their own targets, so you can choose which nibb

## Nibbles

### 🗄️ Stash
A simple cache that can be used to store objects.

Use a `Stash` to store objects of a given type in memory using an associated key.
You can then fetch attempt to retrieve from the `Stash` at a later time using the key.

### 📬 SharedState
A simple container that encapsulates an object allowing others to subscribe to and monitor changes to the state.

Frequently use in PointFree's TCA architecture to subscribe long-running effects to shared state changes.

### ⛓️ Extensions
A collection of useful extensions that I freqeuntly implement across multiple projects.

### ⚡️ Fuse
A collection of useful Combine nibbles.

- A variety of helpful sinks that allow for easier less verbose interactions with Combine publishers.
- A variety of helpful sinks that automatically cleanup after themselves by using a `DisposableBag`.
- `BuffableAsyncPublisher` and `BuffableAsyncThrowingPublisher` which both expose a `values(bufferingStrategy:)` on `Publisher`
- This is a more configurable and powerful version of `values` in Combine that allows converting Combine to an async/await syntax.

### 🛜 Exchange

#### HTTPClient

An HTTP client that creates and manages requests over the network.

The client provides support for sharing common functionality across all requests, but each request can also layer on additional functionality if needed.
Expand All @@ -65,15 +42,38 @@ have their own nuance and complexities, and encapsulating all of that in one pla
more scalable and testable way.

#### Socket

A websocket created from a URL that can listen to messages send through the connection using `AsyncStream`.

Sending messages and cancelling the connection is as easy as calling a few methods.

### 🏷️ Identified
### ⛓️ Extensions
A collection of useful extensions that I freqeuntly implement across multiple projects.

### ⚡️ Fuse
A collection of useful Combine nibbles.

- A variety of helpful sinks that allow for easier less verbose interactions with Combine publishers.
- A variety of helpful sinks that automatically cleanup after themselves by using a `DisposableBag`.
- `BuffableAsyncPublisher` and `BuffableAsyncThrowingPublisher` which both expose a `values(bufferingStrategy:)` on `Publisher`
- This is a more configurable and powerful version of `values` in Combine that allows converting Combine to an async/await syntax.

### 🏷️ Identified
A protocol for marking objects as identified and allowing interaction with their identifiers in a type-safe way.

### 📬 SharedState
A simple container that encapsulates an object allowing others to subscribe to and monitor changes to the state.

Frequently use in PointFree's TCA architecture to subscribe long-running effects to shared state changes.

### 🗄️ Stash
A simple cache that can be used to store objects.

Use a `Stash` to store objects of a given type in memory using an associated key.
You can then fetch attempt to retrieve from the `Stash` at a later time using the key.

### 🔗 StateBinding
A simple property wrapper that allows your views to optionally take a binding and default to internal state. Useful when creating reusable components.

## Contributing

[Learn more](https://github.com/connor-ricks/swift-nibbles/blob/main/CONTRIBUTING.md)
Expand Down
49 changes: 49 additions & 0 deletions Sources/StateBinding/StateBinding.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// MIT License
//
// Copyright (c) 2024 Connor Ricks
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import SwiftUI

@propertyWrapper
public struct StateBinding<Value>: DynamicProperty {

// MARK: Properties

@State private var state: Value

public var externalBinding: Binding<Value>? = nil

public var wrappedValue: Value {
get { projectedValue.wrappedValue }
nonmutating set { projectedValue.wrappedValue = newValue }
}

public var projectedValue: Binding<Value> {
externalBinding ?? $state
}

// MARK: Initializers

public init(wrappedValue: Value) {
_state = .init(initialValue: wrappedValue)
}
}
24 changes: 24 additions & 0 deletions Tests/StateBindingTests/StateBinding.xctestplan
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"configurations" : [
{
"id" : "F058B37F-5474-46C0-A432-D042453493AB",
"name" : "Configuration 1",
"options" : {

}
}
],
"defaultOptions" : {

},
"testTargets" : [
{
"target" : {
"containerPath" : "container:",
"identifier" : "StateBindingTests",
"name" : "StateBindingTests"
}
}
],
"version" : 1
}
59 changes: 59 additions & 0 deletions Tests/StateBindingTests/StateBindingTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// MIT License
//
// Copyright (c) 2024 Connor Ricks
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

@testable import StateBinding
import ConcurrencyExtras
import SwiftUI
import XCTest

@MainActor
class StateBindingTests: XCTestCase {
func test_stateBinding_whenProvidedExternalBinding_doesUseExternalBinding() async {
let getterExpectation = expectation(description: "Expected binding getter.")
getterExpectation.expectedFulfillmentCount = 3

let setterExpectation = expectation(description: "Expected binding setter.")
setterExpectation.expectedFulfillmentCount = 2

let count = LockIsolated(0)
let binding = Binding(
get: {
getterExpectation.fulfill()
return count.value
},
set: {
setterExpectation.fulfill()
count.setValue($0)
}
)
@StateBinding var counter = 5
_counter.externalBinding = binding
binding.wrappedValue = 10
XCTAssertEqual(counter, 10)

counter = 15
XCTAssertEqual(counter, 15)

await fulfillment(of: [setterExpectation, getterExpectation], enforceOrder: false)
}
}
21 changes: 14 additions & 7 deletions Tests/swift-nibbles-Package.xctestplan
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
"testTimeoutsEnabled" : true
},
"testTargets" : [
{
"parallelizable" : true,
"target" : {
"containerPath" : "container:",
"identifier" : "ExchangeTests",
"name" : "ExchangeTests"
}
},
{
"parallelizable" : true,
"target" : {
Expand Down Expand Up @@ -42,24 +50,23 @@
"parallelizable" : true,
"target" : {
"containerPath" : "container:",
"identifier" : "StashTests",
"name" : "StashTests"
"identifier" : "SharedStateTests",
"name" : "SharedStateTests"
}
},
{
"parallelizable" : true,
"target" : {
"containerPath" : "container:",
"identifier" : "ExchangeTests",
"name" : "ExchangeTests"
"identifier" : "StashTests",
"name" : "StashTests"
}
},
{
"parallelizable" : true,
"target" : {
"containerPath" : "container:",
"identifier" : "SharedStateTests",
"name" : "SharedStateTests"
"identifier" : "StateBindingTests",
"name" : "StateBindingTests"
}
}
],
Expand Down