Skip to content

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed May 9, 2022
1 parent 788a7d7 commit c15861e
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 56 deletions.
Binary file modified Assets/DiffableTextAppXUIKitXNumber.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Assets/DiffableTextAppXUIKitXPattern.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion Examples/DiffableTextAppXUIKit/App/Views/Example.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ struct Example<Style: DiffableTextStyle>: View {
}

var diffableTextField: some View {
DiffableTextField("Much wow. Very empty.", value: value, style: style)
DiffableTextField("Much wow. Such empty.", value: value, style: style)
.focused($focus).padding()
.background(Color(uiColor: .tertiarySystemBackground).ignoresSafeArea(.container, edges: []))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import SwiftUI
// MARK: Declaration
//*============================================================================*

@dynamicMemberLookup @usableFromInline final class Context {
@usableFromInline @dynamicMemberLookup final class Context {

//=------------------------------------------------------------------------=
// MARK: State
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ struct DiffableTextFieldXPhone: View {
}
```

## [WrapperTextStyle(s)](Notes/WrapperTextStyle.md) ([Source](Sources/DiffableTextStylesXWrapper/), [Tests](Tests/DiffableTextStylesXWrapperTests))
## [WrapperTextStyle(s)](Notes/WrapperTextStyle.md) ([Source](Sources/DiffableTextStylesXWrappers/), [Tests](Tests/DiffableTextStylesXWrappersTests))

Decorative styles that modify the behavior of their content.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/// - Autocorrects selection when the snapshot changes.
/// - Autocorrects selection when the selection changes.
///
@usableFromInline struct Field {
@usableFromInline struct Layout {

//=------------------------------------------------------------------------=
// MARK: State
Expand Down Expand Up @@ -50,7 +50,7 @@
// MARK: Transformations
//=----------------------------------------------------------------------------=

extension Field {
extension Layout {

//=------------------------------------------------------------------------=
// MARK: Snapshot
Expand Down
76 changes: 37 additions & 39 deletions Sources/DiffableTextKit/Models/Context.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
///
public struct Context<Style: DiffableTextStyle> {
public typealias Value = Style.Value
public typealias State = DiffableTextKit.Remote<Style>
public typealias Status = DiffableTextKit.Status<Style>
public typealias Commit = DiffableTextKit.Commit<Value>

//=------------------------------------------------------------------------=
Expand All @@ -30,14 +30,14 @@ public struct Context<Style: DiffableTextStyle> {
// MARK: Initializers
//=------------------------------------------------------------------------=

@inlinable init(_ state: State, _ field: Field) {
self._storage = Storage(state, field)
@inlinable init(_ status: Status, _ layout: Layout) {
self._storage = Storage(status, layout)
}

@inlinable public init(_ state: State) {
switch state.focus.wrapped {
case true: self = .focused(state.style, state.value)
case false: self = .unfocused(state.style, state.value)
@inlinable public init(_ status: Status) {
switch status.focus.wrapped {
case true: self = .focused(status.style, status.value)
case false: self = .unfocused(status.style, status.value)
}
}

Expand All @@ -50,11 +50,11 @@ public struct Context<Style: DiffableTextStyle> {
}

@inlinable static func focused(_ style: Style, _ commit: Commit) -> Self {
Self(State(style, commit.value, true), Field((commit.snapshot)))
Self(Status(style, commit.value, true), Layout((commit.snapshot)))
}

@inlinable static func unfocused(_ style: Style, _ value: Value) -> Self {
Self(State(style, value, false), Field(Snapshot(style.format(value), as: .phantom)))
Self(Status(style, value, false), Layout(Snapshot(style.format(value), as: .phantom)))
}

//=------------------------------------------------------------------------=
Expand Down Expand Up @@ -85,24 +85,24 @@ public struct Context<Style: DiffableTextStyle> {
// MARK: State
//=--------------------------------------------------------------------=

@usableFromInline var state: State
@usableFromInline var field: Field
@usableFromInline var status: Status
@usableFromInline var layout: Layout

//=--------------------------------------------------------------------=
// MARK: Initializers
//=--------------------------------------------------------------------=

@inlinable init(_ state: State, _ field: Field) {
self.state = state
self.field = field
@inlinable init(_ status: Status, _ layout: Layout) {
self.status = status
self.layout = layout
}

//=--------------------------------------------------------------------=
// MARK: Utilities
//=--------------------------------------------------------------------=

@inlinable func copy() -> Self {
Self(state, field)
Self(status, layout)
}
}
}
Expand All @@ -117,45 +117,45 @@ public extension Context {
// MARK: 1st
//=------------------------------------------------------------------------=

@inlinable internal var state: State {
_storage.state
@inlinable internal var status: Status {
_storage.status
}

@inlinable internal var field: Field {
_storage.field
@inlinable internal var layout: Layout {
_storage.layout
}

//=------------------------------------------------------------------------=
// MARK: 2nd
//=------------------------------------------------------------------------=

@inlinable var style: Style {
state.style
status.style
}

@inlinable var value: Value {
state.value
status.value
}

@inlinable var focus: Focus {
state.focus
status.focus
}

//=------------------------------------------------------------------------=
// MARK: 3rd
//=------------------------------------------------------------------------=

@inlinable var snapshot: Snapshot {
field.snapshot
layout.snapshot
}

@inlinable var text: String {
field.snapshot.characters
layout.snapshot.characters
}

@inlinable func selection<T>(as type: Position<T>.Type =
Position<T>.self) -> Range<T.Position> where T: Offset {
field.positions(at: field.selection).bounds
layout.positions(at: layout.selection).bounds
}
}

Expand All @@ -175,8 +175,8 @@ public extension Context {
//=--------------------------------------=
if other.focus.wrapped {
self.write {
$0.state = other.state
$0.field.update(snapshot: other.snapshot)
$0.status = other.status
$0.layout.update(snapshot: other.snapshot)
}
//=--------------------------------------=
// Unfocused
Expand All @@ -185,22 +185,20 @@ public extension Context {
}

//=------------------------------------------------------------------------=
// MARK: Remote
// MARK: Status
//=------------------------------------------------------------------------=

@inlinable mutating func merge(_ remote: State) -> Bool {
@inlinable mutating func merge(_ status: Status) -> Bool {
//=--------------------------------------=
// Update
//=--------------------------------------=
let (result, update) = self.state + remote
let (result, update) = self.status + status
//=--------------------------------------=
// At Least On Value Must Be Unique
// At Least One Must Be Different
//=--------------------------------------=
guard !update.isEmpty else { return false }
//=--------------------------------------=
// Insert
//=--------------------------------------=
self.merge(Self(result)); return true
return (!update.isEmpty).on(true) {
self.merge(Self.init(result))
}
}

//=------------------------------------------------------------------------=
Expand All @@ -212,7 +210,7 @@ public extension Context {
//=--------------------------------------=
// Values
//=--------------------------------------=
let carets = field.indices(at: Carets(range))
let carets = layout.indices(at: Carets(range))
let commit = try style.merge(Proposal(
snapshot, with: characters, in: carets.bounds))
//=--------------------------------------=
Expand All @@ -227,10 +225,10 @@ public extension Context {
//=------------------------------------------------------------------------=

@inlinable internal mutating func set(selection: Index) {
self.write({ $0.field.selection = Carets(selection) })
self.write({ $0.layout.selection = Carets(selection) })
}

@inlinable mutating func update<T>(selection: Range<T.Position>, momentum: Bool) where T: Offset {
self.write({ $0.field .update(selection: Carets(selection), momentum: momentum) })
self.write({ $0.layout .update(selection: Carets(selection), momentum: momentum) })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//*===========================================================================

/// A model used to collect upstream and downstream values.
public struct Remote<Style: DiffableTextStyle> {
public struct Status<Style: DiffableTextStyle> {
public typealias Value = Style.Value

//=------------------------------------------------------------------------=
Expand Down
24 changes: 24 additions & 0 deletions Sources/DiffableTextKit/Support/Bool.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//=----------------------------------------------------------------------------=
// This source file is part of the DiffableTextViews open source project.
//
// Copyright (c) 2022 Oscar Byström Ericsson
// Licensed under Apache License, Version 2.0
//
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
//=----------------------------------------------------------------------------=

//*============================================================================*
// MARK: Extension
//*============================================================================*

public extension Bool {

//=------------------------------------------------------------------------=
// MARK: Utilities
//=------------------------------------------------------------------------=

@inlinable @discardableResult func on(_ value: Bool,
perform action: () throws -> Void) rethrows -> Bool {
if value { try action() }; return self
}
}
14 changes: 7 additions & 7 deletions Sources/DiffableTextViewsXUIKit/DiffableTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import SwiftUI
///
public struct DiffableTextField<Style: DiffableTextStyle>: UIViewRepresentable {
public typealias Value = Style.Value

//=------------------------------------------------------------------------=
// MARK: State
//=------------------------------------------------------------------------=
Expand Down Expand Up @@ -69,7 +69,7 @@ public struct DiffableTextField<Style: DiffableTextStyle>: UIViewRepresentable {
@inlinable @inline(__always) func locale(_ locale: Locale) -> Self {
Self(title, value: value, style: style.locale(locale))
}

//=------------------------------------------------------------------------=
// MARK: View Life Cycle
//=------------------------------------------------------------------------=
Expand All @@ -86,7 +86,7 @@ public struct DiffableTextField<Style: DiffableTextStyle>: UIViewRepresentable {
@inlinable public func updateUIView(_ view: UITextField, context: Self.Context) {
context.coordinator.update(self, context.environment)
}

//*========================================================================*
// MARK: Coordinator
//*========================================================================*
Expand All @@ -95,7 +95,7 @@ public struct DiffableTextField<Style: DiffableTextStyle>: UIViewRepresentable {
@usableFromInline typealias Upstream = DiffableTextField
@usableFromInline typealias Environment = EnvironmentValues
@usableFromInline typealias Position = Unicode.UTF16.Position
@usableFromInline typealias Remote = DiffableTextKit.Remote<Style>
@usableFromInline typealias Status = DiffableTextKit.Status<Style>
@usableFromInline typealias Context = DiffableTextKit.Context<Style>

//=--------------------------------------------------------------------=
Expand Down Expand Up @@ -150,7 +150,7 @@ public struct DiffableTextField<Style: DiffableTextStyle>: UIViewRepresentable {
self.downstream.setTextContentType(environment)
self.downstream.setTextInputAutocapitalization(environment)
//=----------------------------------=
// Miscellaneous
// Coordinator
//=----------------------------------=
self.onSubmit = environment.diffableTextViews_onSubmit
//=----------------------------------=
Expand Down Expand Up @@ -259,11 +259,11 @@ public struct DiffableTextField<Style: DiffableTextStyle>: UIViewRepresentable {
// MARK: Synchronization
//=--------------------------------------------------------------------=

@inlinable func pull() -> Remote {
@inlinable func pull() -> Status {
//=----------------------------------=
// Upstream, Downstream
//=----------------------------------=
Remote(upstream.style, upstream.value.wrappedValue, downstream.focus)
Status(upstream.style, upstream.value.wrappedValue, downstream.focus)
}

@inlinable func push() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import SwiftUI
//*============================================================================*

@usableFromInline enum DiffableTextViews_TextFieldStyle: EnvironmentKey {
@usableFromInline static let defaultValue: UITextField.BorderStyle = .none // enum
@usableFromInline static let defaultValue: UITextField.BorderStyle = .none
}

//*============================================================================*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import SwiftUI
//*============================================================================*

@usableFromInline enum DiffableTextViews_TextInputAutocapitalization: EnvironmentKey {
@usableFromInline static let defaultValue: UITextAutocapitalizationType? = nil // enum
@usableFromInline static let defaultValue: UITextAutocapitalizationType? = nil
}

//*============================================================================*
Expand Down
3 changes: 1 addition & 2 deletions Sources/DiffableTextViewsXUIKit/Models/Font.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public struct DiffableTextFont {
// MARK: Instances
//=------------------------------------------------------------------------=


public static let standard: Self = .body.monospaced()
#if !os(tvOS)
public static let largeTitle: Self = .preferred(.largeTitle)
#endif
Expand All @@ -36,7 +36,6 @@ public struct DiffableTextFont {
public static let footnote: Self = .preferred(.footnote)
public static let caption1: Self = .preferred(.caption1)
public static let caption2: Self = .preferred(.caption2)
public static let standard: Self = .body.monospaced()

//=------------------------------------------------------------------------=
// MARK: State
Expand Down

0 comments on commit c15861e

Please sign in to comment.