Skip to content

Commit 5940f6b

Browse files
committed
Cleanup: UIKit
1 parent 362b567 commit 5940f6b

File tree

11 files changed

+68
-220
lines changed

11 files changed

+68
-220
lines changed

Sources/DiffableTextKitXUIKit/DiffableTextField.swift

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public struct DiffableTextField<Style: DiffableTextStyle>: UIViewRepresentable {
6565
}
6666

6767
//=------------------------------------------------------------------------=
68-
// MARK: Cycle
68+
// MARK: Utilities
6969
//=------------------------------------------------------------------------=
7070

7171
@inlinable public func makeCoordinator() -> Coordinator {
@@ -93,7 +93,8 @@ public struct DiffableTextField<Style: DiffableTextStyle>: UIViewRepresentable {
9393
// MARK: State
9494
//=--------------------------------------------------------------------=
9595

96-
@usableFromInline let lock = Lock()
96+
@usableFromInline let lock = Lock()
97+
9798
@usableFromInline var cache: Cache!
9899
@usableFromInline var context: Context!
99100

@@ -102,15 +103,16 @@ public struct DiffableTextField<Style: DiffableTextStyle>: UIViewRepresentable {
102103
@usableFromInline var sidestream = Sidestream()
103104

104105
//=--------------------------------------------------------------------=
105-
// MARK: Cycle
106+
// MARK: Transformations
106107
//=--------------------------------------------------------------------=
107108

108-
@inlinable @inline(never)
109-
func setup(_ upstream: DiffableTextField, _ environment: EnvironmentValues) {
109+
@inlinable @inline(never) func setup(
110+
_ view:/*---*/ DiffableTextField,
111+
_ environment: EnvironmentValues) {
110112
//=----------------------------------=
111113
// Upstream
112114
//=----------------------------------=
113-
self.upstream = Upstream(upstream, environment)
115+
self.upstream = Upstream(view, environment)
114116
//=----------------------------------=
115117
// Downstream
116118
//=----------------------------------=
@@ -120,21 +122,22 @@ public struct DiffableTextField<Style: DiffableTextStyle>: UIViewRepresentable {
120122
// Synchronize
121123
//=----------------------------------=
122124
var update = Update()
123-
self.cache = upstream.style.cache()
124-
self.context = Context(pull(),with: &self.cache, then: &update)
125+
self.cache = self.upstream.style.cache()
126+
self.context = Context(self.pull(),with: &self.cache, then: &update)
125127
self.push(update)
126128
}
127129

128-
@inlinable @inline(never)
129-
func update(_ upstream: DiffableTextField, _ environment: EnvironmentValues) {
130+
@inlinable @inline(never) func update(
131+
_ view:/*---*/ DiffableTextField,
132+
_ environment: EnvironmentValues) {
130133
//=----------------------------------=
131134
// Upstream
132135
//=----------------------------------=
133-
self.upstream = Upstream(upstream, environment)
136+
self.upstream = Upstream(view, environment)
134137
//=----------------------------------=
135138
// Downstream
136139
//=----------------------------------=
137-
self.downstream.placeholder(upstream.title)
140+
self.downstream.placeholder(view.title)
138141
self.downstream.autocorrectionDisabled(environment)
139142
self.downstream.font(environment)
140143
self.downstream.foregroundColor(environment)
@@ -195,8 +198,7 @@ public struct DiffableTextField<Style: DiffableTextStyle>: UIViewRepresentable {
195198
return false
196199
}
197200

198-
@inlinable @inline(never)
199-
public func textFieldDidChangeSelection(_ textField: UITextField) {
201+
@inlinable @inline(never) public func textFieldDidChangeSelection(_ textField: UITextField) {
200202
//=----------------------------------=
201203
// Marked
202204
//=----------------------------------=

Sources/DiffableTextKitXUIKit/Support/NSTextAlignment.swift renamed to Sources/DiffableTextKitXUIKit/Models/Alignment.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import SwiftUI
1313
import UIKit
1414

1515
//*============================================================================*
16-
// MARK: * NSTextAlignment [...]
16+
// MARK: * Alignment [...]
1717
//*============================================================================*
1818

1919
extension NSTextAlignment {

Sources/DiffableTextKitXUIKit/Models/Base.swift renamed to Sources/DiffableTextKitXUIKit/Models/Field.swift

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,30 @@
99

1010
#if canImport(UIKit)
1111

12+
import DiffableTextKit
1213
import SwiftUI
1314
import UIKit
1415

1516
//*============================================================================*
16-
// MARK: * Base
17+
// MARK: * Field
1718
//*============================================================================*
1819

1920
/// An as-you-type formatting compatible UITextField.
2021
///
21-
/// UITextField has two selection methods: drag and drop and keyboard inputs.
22+
/// UITextField has two selection methods: drag and drop, and keyboard inputs.
2223
///
23-
/// - Use static selection for drag and drop.
24+
/// - Use static selection for drag and drop.
2425
/// - Use momentum selection for keyboard inputs.
2526
///
26-
@usableFromInline final class Base: UITextField {
27+
@usableFromInline final class Field: UITextField {
28+
29+
@usableFromInline typealias Offset = DiffableTextKit.Offset<UTF16>
2730

2831
//=------------------------------------------------------------------------=
2932
// MARK: State
3033
//=------------------------------------------------------------------------=
3134

35+
/// Use this value to determine selection method.
3236
@usableFromInline private(set) var intent = Intent()
3337

3438
//=------------------------------------------------------------------------=
@@ -44,9 +48,9 @@ import UIKit
4448
required init?(coder: NSCoder) {
4549
fatalError("init(coder:) has not been implemented")
4650
}
47-
51+
4852
//=------------------------------------------------------------------------=
49-
// MARK: Presses
53+
// MARK: Events
5054
//=------------------------------------------------------------------------=
5155

5256
override func pressesBegan(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
@@ -64,6 +68,30 @@ import UIKit
6468
override func pressesCancelled(_ presses: Set<UIPress>, with event: UIPressesEvent?) {
6569
intent.remove(presses); super.pressesCancelled(presses, with: event)
6670
}
71+
72+
//=------------------------------------------------------------------------=
73+
// MARK: Utilities
74+
//=------------------------------------------------------------------------=
75+
76+
@inlinable func distance(to position: UITextPosition) -> Offset {
77+
Offset(offset(from: beginningOfDocument, to: position))
78+
}
79+
80+
@inlinable func range(to positions: UITextRange) -> Range<Offset> {
81+
let lower = distance(to: positions.start)
82+
let count = offset(from: positions.start, to: positions.end)
83+
return lower ..< lower + Offset(count)
84+
}
85+
86+
@inlinable func position(at distance: Offset) -> UITextPosition {
87+
position(from: beginningOfDocument, offset: Int(distance))!
88+
}
89+
90+
@inlinable func range(at distances: Range<Offset>) -> UITextRange {
91+
let lower = position(at: distances.lowerBound)
92+
let upper = position(from: lower, offset: distances.count)!
93+
return textRange(from: lower, to: upper)!
94+
}
6795
}
6896

6997
#endif
File renamed without changes.

Sources/DiffableTextKitXUIKit/Models/Downstream.swift renamed to Sources/DiffableTextKitXUIKit/Streams/Downstream.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import UIKit
2525
// MARK: State
2626
//=------------------------------------------------------------------------=
2727

28-
@usableFromInline var view = Base()
28+
@usableFromInline var view = Field()
2929

3030
//=------------------------------------------------------------------------=
3131
// MARK: Accessors
@@ -68,13 +68,11 @@ import UIKit
6868
}
6969

7070
@inlinable func font(_ environment: EnvironmentValues) {
71-
let font = environment.diffableTextViews_font
72-
self.view.font = UIFont(font ?? .body)
71+
self.view.font = UIFont(environment.diffableTextViews_font ?? .body)
7372
}
7473

7574
@inlinable func foregroundColor(_ environment: EnvironmentValues) {
76-
let color = environment.diffableTextViews_foregroundColor
77-
self.view.textColor = UIColor(color ?? .primary)
75+
self.view.textColor = UIColor(environment.diffableTextViews_foregroundColor ?? .primary)
7876
}
7977

8078
@inlinable func keyboardType(_ environment: EnvironmentValues) {
@@ -105,8 +103,7 @@ import UIKit
105103
}
106104

107105
@inlinable func tint(_ environment: EnvironmentValues) {
108-
let color = environment.diffableTextViews_tint
109-
self.view.tintColor = UIColor(color ?? .accentColor)
106+
self.view.tintColor = UIColor(environment.diffableTextViews_tint ?? .accentColor)
110107
}
111108
}
112109

Sources/DiffableTextKitXUIKit/Models/Sidestream.swift renamed to Sources/DiffableTextKitXUIKit/Streams/Sidestream.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import SwiftUI
2626

2727
@inlinable init() { }
2828

29-
@inlinable init( _ environment: EnvironmentValues) {
29+
@inlinable init(_ environment: EnvironmentValues) {
3030
self.onSubmit = environment.diffableTextViews_onSubmit
3131
}
3232
}

Sources/DiffableTextKitXUIKit/Models/Upstream.swift renamed to Sources/DiffableTextKitXUIKit/Streams/Upstream.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import SwiftUI
2525

2626
//=------------------------------------------------------------------------=
2727

28-
@inlinable init(_ upstream: DiffableTextField<Style>, _ environment: EnvironmentValues) {
29-
self.style = upstream.style.locale(environment.locale); self.value = upstream.value
28+
@inlinable init(_ view: DiffableTextField<Style>, _ environment: EnvironmentValues) {
29+
self.style = view.style.locale(environment.locale); self.value = view.value
3030
}
3131
}
3232

Sources/DiffableTextKitXUIKit/Support/UITextField.swift

Lines changed: 0 additions & 48 deletions
This file was deleted.

Tests/DiffableTextKitXUIKitTests/Models/Base.swift renamed to Tests/DiffableTextKitXUIKitTests/Field.swift

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,41 @@
77
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
88
//=----------------------------------------------------------------------------=
99

10-
#if DEBUG
11-
#if canImport(UIKit)
10+
#if DEBUG && canImport(UIKit)
1211

1312
@testable import DiffableTextKitXUIKit
1413

1514
import XCTest
1615

1716
//*============================================================================*
18-
// MARK: * Base x Tests
17+
// MARK: * Field x Tests
1918
//*============================================================================*
2019

21-
final class BaseTests: XCTestCase {
20+
final class FieldTests: XCTestCase {
2221

2322
//=------------------------------------------------------------------------=
2423
// MARK: State
2524
//=------------------------------------------------------------------------=
2625

27-
lazy var view = Base()
26+
lazy var field = Field()
2827

2928
//=------------------------------------------------------------------------=
3029
// MARK: Tests
3130
//=------------------------------------------------------------------------=
3231

3332
func testForceUnwrappingTextIsOK() {
34-
view.text = nil
35-
XCTAssertNotNil(view.text)
33+
field.text = nil
34+
XCTAssertNotNil(field.text)
3635
}
3736

3837
func testForceUnwrappingSelectedTextRangeIsOK() {
39-
view.selectedTextRange = nil
40-
XCTAssertNotNil(view.selectedTextRange)
38+
field.selectedTextRange = nil
39+
XCTAssertNotNil(field.selectedTextRange)
4140
}
4241

4342
func testForceUnwrappingMarkedTextRangeIsBad() {
44-
XCTAssertNil(view.markedTextRange)
43+
XCTAssertNil(field.markedTextRange)
4544
}
4645
}
4746

4847
#endif
49-
#endif

Tests/DiffableTextKitXUIKitTests/Utilities/Font.swift renamed to Tests/DiffableTextKitXUIKitTests/Font.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
88
//=----------------------------------------------------------------------------=
99

10-
#if DEBUG
11-
#if canImport(UIKit)
10+
#if DEBUG && canImport(UIKit)
1211

1312
@testable import DiffableTextKitXUIKit
1413

@@ -50,9 +49,6 @@ final class FontTests: XCTestCase {
5049
//=------------------------------------------------------------------------=
5150

5251
func testDesignsCanBeForceUnwrapped() {
53-
//=--------------------------------------=
54-
// Fonts, Designs
55-
//=--------------------------------------=
5652
for font in fonts {
5753
for design in designs {
5854
XCTAssertNotNil(font.descriptor.withDesign(design))
@@ -62,4 +58,3 @@ final class FontTests: XCTestCase {
6258
}
6359

6460
#endif
65-
#endif

0 commit comments

Comments
 (0)