Skip to content

Commit cdd437b

Browse files
committed
Mini-rework: cleanup.
1 parent a8621ce commit cdd437b

File tree

2 files changed

+51
-47
lines changed

2 files changed

+51
-47
lines changed

Sources/DiffableTextKit/Models/Context.swift

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -136,111 +136,115 @@ public extension Context {
136136
public extension Context {
137137

138138
//=------------------------------------------------------------------------=
139-
// MARK: Static
139+
// MARK: Remote
140140
//=------------------------------------------------------------------------=
141141

142-
@inlinable static func focused(_ style: Style, _ value: Value) -> Self {
142+
@inlinable init(_ remote: Remote) {
143+
switch remote.focus.value {
144+
case true: self = .focused(remote.style, remote.value)
145+
case false: self = .unfocused(remote.style, remote.value)
146+
}
147+
}
148+
149+
//=------------------------------------------------------------------------=
150+
// MARK: Primitives
151+
//=------------------------------------------------------------------------=
152+
153+
@inlinable internal static func focused(_ style: Style, _ value: Value) -> Self {
143154
Self.focused(style, style.interpret(value))
144155
}
145156

146-
@inlinable static func focused(_ style: Style, _ commit: Commit) -> Self {
157+
@inlinable internal static func focused(_ style: Style, _ commit: Commit) -> Self {
147158
Self(Storage(true, style, commit.value, Field((commit.snapshot))))
148159
}
149160

150-
@inlinable static func unfocused(_ style: Style, _ value: Value) -> Self {
161+
@inlinable internal static func unfocused(_ style: Style, _ value: Value) -> Self {
151162
Self(Storage(false, style, value, Field(Snapshot(style.format(value), as: .phantom))))
152163
}
153-
154-
@inlinable static func remote(_ remote: Remote) -> Self {
155-
switch remote.focus.value {
156-
case true: return .focused(remote.style, remote.value)
157-
case false: return .unfocused(remote.style, remote.value)
158-
}
159-
}
160164
}
161165

162166
//=----------------------------------------------------------------------------=
163167
// MARK: + Transformations
164168
//=----------------------------------------------------------------------------=
165169

166170
public extension Context {
167-
171+
168172
//=------------------------------------------------------------------------=
169-
// MARK: Other
173+
// MARK: Context
170174
//=------------------------------------------------------------------------=
171-
172-
@inlinable mutating func merge(_ other: Self) {
175+
176+
@inlinable internal mutating func merge(_ context: Self) {
173177
//=--------------------------------------=
174178
// MARK: Focused
175179
//=--------------------------------------=
176-
if other.focus.value {
180+
if context.focus.value {
177181
self.write {
178-
$0.focus = other.focus
179-
$0.style = other.style
180-
$0.value = other.value
181-
$0.field.update(snapshot: other.snapshot)
182+
$0.focus = context.focus
183+
$0.style = context.style
184+
$0.value = context.value
185+
$0.field.update(snapshot: context.snapshot)
182186
}
183187
//=--------------------------------------=
184188
// MARK: Unfocused
185189
//=--------------------------------------=
186-
} else { self = other }
190+
} else { self = context }
187191
}
188192

193+
//=------------------------------------------------------------------------=
194+
// MARK: Characters
195+
//=------------------------------------------------------------------------=
196+
197+
@inlinable mutating func merge<T>(_ characters: String,
198+
in range: Range<T.Position>) throws where T: Offset {
199+
//=--------------------------------------=
200+
// MARK: Values
201+
//=--------------------------------------=
202+
let range = snapshot.indices(at: range)
203+
let commit = try style.merge(Changes(
204+
snapshot, with: characters, in: range))
205+
//=--------------------------------------=
206+
// MARK: Update
207+
//=--------------------------------------=
208+
self.set(selection: range.upperBound)
209+
self.merge(Self.focused(style, commit))
210+
}
211+
189212
//=------------------------------------------------------------------------=
190213
// MARK: Remote
191214
//=------------------------------------------------------------------------=
192215

193216
@inlinable mutating func merge(_ remote: Remote) -> Bool {
194217
//=--------------------------------------=
195-
// MARK: Comparisons
218+
// MARK: Values
196219
//=--------------------------------------=
197220
let changeInStyle = remote.style != style
198221
let changeInValue = remote.value != value
199222
let changeInFocus = remote.focus != focus
200223
//=--------------------------------------=
201-
// MARK: At Least One Value Must Change
224+
// MARK: At Least One Must Have Changed
202225
//=--------------------------------------=
203226
guard changeInStyle
204227
|| changeInValue
205228
|| changeInFocus else { return false }
206229
//=--------------------------------------=
207-
// MARK: Yes
230+
// MARK: Update
208231
//=--------------------------------------=
209-
self.merge(Self.remote(Remote(
232+
self.merge(Self(Remote(
210233
focus: changeInFocus ? remote.focus : focus,
211234
style: changeInStyle ? remote.style : style,
212235
value: changeInValue ? remote.value : value)))
213236
return true
214237
}
215238

216-
//=------------------------------------------------------------------------=
217-
// MARK: Characters
218-
//=------------------------------------------------------------------------=
219-
220-
@inlinable mutating func merge<T>(_ characters: String,
221-
in range: Range<T.Position>) throws where T: Offset {
222-
//=--------------------------------------=
223-
// MARK: Values
224-
//=--------------------------------------=
225-
let range = snapshot.indices(at: range)
226-
let commit = try style.merge(Changes(
227-
snapshot, with: characters, in: range))
228-
//=--------------------------------------=
229-
// MARK: Update
230-
//=--------------------------------------=
231-
self.set(selection: range.upperBound)
232-
self.merge(Self.focused(style, commit))
233-
}
234-
235239
//=------------------------------------------------------------------------=
236240
// MARK: Selection
237241
//=------------------------------------------------------------------------=
238242

239243
@inlinable internal mutating func set(selection: Index) {
240-
self.write { $0.field.selection = .empty(selection) }
244+
self.write({ $0.field.selection = .empty(selection) })
241245
}
242246

243247
@inlinable mutating func update<T>(selection: Range<T.Position>, momentum: Bool) where T: Offset {
244-
self.write { $0.field.update(selection: selection, momentum: momentum) }
248+
self.write({ $0.field.update(selection: selection, momentum: momentum) })
245249
}
246250
}

Sources/DiffableTextViewsXUIKit/DiffableTextField.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public struct DiffableTextField<Style: DiffableTextStyle>: UIViewRepresentable {
100100
@inlinable func setup(_ values: (Upstream, Environment, Downstream)) {
101101
(upstream, environment, downstream) = values
102102
self.downstream.wrapped.delegate = self
103-
self.context = .remote(pull()); self.write()
103+
self.context = .init(self.pull()); self.write()
104104
}
105105

106106
@inlinable func update(_ values: (Upstream, Environment)) {

0 commit comments

Comments
 (0)