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

Add more extensions #8

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
28 changes: 26 additions & 2 deletions Sources/XUI/Binding/Binding+Change.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ extension Binding {
)
}

public func map<V>(get: @escaping (Value) -> V, set: @escaping (V) -> Value) -> Binding<V> {
public func map<V>(get: @escaping (Value) -> V,
set: @escaping (inout Value, V) -> Void) -> Binding<V> {
.init(
get: { get(self.wrappedValue) },
set: { self.wrappedValue = set($0) }
set: { set(&self.wrappedValue, $0) }
)
}

Expand All @@ -75,4 +76,27 @@ extension Binding {
)
}

public func onlySetOnChange(_ isEqual: @escaping (Value, Value) -> Bool) -> Binding {
.init(
get: { self.wrappedValue },
set: { newValue in
guard !isEqual(self.wrappedValue, newValue) else {
return
}
self.wrappedValue = newValue
}
)
}

public func onlySetOnChange<V>(
of keyPath: KeyPath<Value, V>
) -> Binding where V: Equatable {

onlySetOnChange { $0[keyPath: keyPath] == $1[keyPath: keyPath] }
}

public func onlySetOnChange() -> Binding where Value: Equatable {
onlySetOnChange(==)
}

}
7 changes: 7 additions & 0 deletions Sources/XUI/Binding/Binding+Force.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,12 @@ extension Binding {
)
}

public func `default`<Wrapped>(_ value: Wrapped) -> Binding<Wrapped> where Value == Wrapped? {
.init(
get: { self.wrappedValue ?? value },
set: { self.wrappedValue = $0 }
)
}

}

24 changes: 24 additions & 0 deletions Sources/XUI/Combine/AnyPublisher+Init.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// AnyPublisher+Init.swift
// XUI
//
// Created by Paul Kraft on 01.03.21.
// Copyright © 2021 QuickBird Studios. All rights reserved.
//

extension AnyPublisher {

// MARK: Factory Methods

public static func just(_ value: Output) -> AnyPublisher {
Just(value)
.mapError { _ -> Failure in }
.eraseToAnyPublisher()
}

public static func failure(_ error: Failure) -> AnyPublisher {
Fail(error: error)
.eraseToAnyPublisher()
}

}
8 changes: 8 additions & 0 deletions Sources/XUI/Combine/CancellableBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ extension Cancellable {

extension RangeReplaceableCollection where Element == AnyCancellable {

public init(@CancellableBuilder _ builder: () -> [AnyCancellable]) {
self.init(builder())
}

/// This method can be used to store multiple `Cancellable` objects
/// from different subscriptions in a collection of `AnyCancellable`.
public mutating func insert(@CancellableBuilder _ builder: () -> [AnyCancellable]) {
Expand All @@ -80,6 +84,10 @@ extension RangeReplaceableCollection where Element == AnyCancellable {

extension Set where Element == AnyCancellable {

public init(@CancellableBuilder _ builder: () -> [AnyCancellable]) {
self.init(builder())
}

/// This method can be used to store multiple `Cancellable` objects
/// from different subscriptions in a set of `AnyCancellable`.
public mutating func insert(@CancellableBuilder _ builder: () -> [AnyCancellable]) {
Expand Down
6 changes: 6 additions & 0 deletions Sources/XUI/Internal/ErasedObservableObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@

class ErasedObservableObject: ObservableObject {

// MARK: Stored Properties

let objectWillChange: AnyPublisher<Void, Never>

// MARK: Initialization

init(objectWillChange: AnyPublisher<Void, Never>) {
self.objectWillChange = objectWillChange
}

// MARK: Factory Methods

static func empty() -> ErasedObservableObject {
.init(objectWillChange: Empty().eraseToAnyPublisher())
}
Expand Down
4 changes: 4 additions & 0 deletions Sources/XUI/Internal/ObjectIdentifiable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@

struct ObjectIdentifiable: Identifiable {

// MARK: Stored Properties

var id: ObjectIdentifier

// MARK: Initialization

init(_ object: Any) {
self.id = ObjectIdentifier(object as AnyObject)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/XUI/Store/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@propertyWrapper
public struct Store<Model>: DynamicProperty {

// MARK: Nested types
// MARK: Nested Types

@dynamicMemberLookup
public struct Wrapper {
Expand All @@ -23,7 +23,7 @@ public struct Store<Model>: DynamicProperty {

}

// MARK: Stored properties
// MARK: Stored Properties

public let wrappedValue: Model

Expand Down
27 changes: 27 additions & 0 deletions Sources/XUI/ViewModifiers/CustomModifier.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// NavigationModifier.swift
// XUI
//
// Created by Paul Kraft on 01.03.21.
// Copyright © 2021 QuickBird Studios. All rights reserved.
//

public struct CustomModifier<Result: View>: ViewModifier {

// MARK: Stored Properties

private let _body: (Content) -> Result

// MARK: Initialization

public init(@ViewBuilder body: @escaping (Content) -> Result) {
self._body = body
}

// MARK: Methods

public func body(content: Content) -> some View {
_body(content)
}

}
2 changes: 2 additions & 0 deletions Sources/XUI/Views/NavigationLink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

extension NavigationLink {

// MARK: Initialization

public init<Model, _Destination: View>(
model: Binding<Model?>,
@ViewBuilder destination: (Model) -> _Destination,
Expand Down