d
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
/// Пропускаем лишь значения от UI
|
||||
///
|
||||
/// - Returns: Значение без префиксов "a:"/"u:"
|
||||
func onlyAcceptUIText(_ s: String) -> String? {
|
||||
func onlyUIText(_ s: String) -> String? {
|
||||
guard s.hasPrefix("u:") else { return nil }
|
||||
return String(s.dropFirst(2))
|
||||
}
|
||||
26
src/Cord.TextField.swift
Normal file
26
src/Cord.TextField.swift
Normal file
@@ -0,0 +1,26 @@
|
||||
import Combine
|
||||
import SwiftUI
|
||||
|
||||
extension Cord {
|
||||
final class TextField: ObservableObject {
|
||||
@Published var value = "a:"
|
||||
var subscriptions = [AnyCancellable]()
|
||||
|
||||
init(
|
||||
_ textApp: String,
|
||||
_ textUI: String
|
||||
) {
|
||||
Bus.send(
|
||||
textUI,
|
||||
$value.compactMap(onlyUIText).eraseToAnyPublisher(),
|
||||
sub: &subscriptions
|
||||
)
|
||||
|
||||
Bus.receive(
|
||||
[textApp],
|
||||
{ [weak self] (_, v: String) in self?.value = "a:\(v)" },
|
||||
sub: &subscriptions
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
2
src/Cord.swift
Normal file
2
src/Cord.swift
Normal file
@@ -0,0 +1,2 @@
|
||||
|
||||
public enum Cord { }
|
||||
@@ -1,17 +1,18 @@
|
||||
import SwiftUI
|
||||
|
||||
struct V: View {
|
||||
@StateObject var tf = Cord.TextField("text.app", "text.ui")
|
||||
@StateObject var vm = VM()
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
HStack {
|
||||
Text("Check text field:")
|
||||
Text("'\(vm.text)'")
|
||||
Text("'\(tf.value)'")
|
||||
.fontWeight(.bold)
|
||||
}
|
||||
|
||||
TextField("Binding-3", value: $vm.text, formatter: TextFieldValueOwner())
|
||||
TextField("Binding-3", value: $tf.value, formatter: TextFieldValueOwner())
|
||||
.padding(8)
|
||||
.border(Color.blue, width: 2)
|
||||
|
||||
|
||||
@@ -2,23 +2,26 @@ import Combine
|
||||
import SwiftUI
|
||||
|
||||
final class VM: ObservableObject {
|
||||
/*
|
||||
@Published var text = "a:"
|
||||
let format = Bus.Processor(
|
||||
MeetupId.K.meetupIdTextUI.rawValue,
|
||||
MeetupId.K.meetupIdTextApp.rawValue,
|
||||
MeetupId.onlyFormat
|
||||
)
|
||||
*/
|
||||
var isJoinAvailable = true
|
||||
let join = PassthroughSubject<Void, Never>()
|
||||
var subscriptions = [AnyCancellable]()
|
||||
|
||||
init() {
|
||||
/*
|
||||
Bus.send(
|
||||
MeetupId.K.meetupIdTextUI.rawValue,
|
||||
$text
|
||||
// Исключаем конфликты от UI и App путём игнорирования спама.
|
||||
.debounce(for: .seconds(0.3), scheduler: DispatchQueue.main)
|
||||
.compactMap(onlyAcceptUIText)
|
||||
.compactMap(onlyUIText)
|
||||
.eraseToAnyPublisher(),
|
||||
sub: &subscriptions
|
||||
)
|
||||
@@ -29,5 +32,6 @@ final class VM: ObservableObject {
|
||||
opt: [.async],
|
||||
sub: &subscriptions
|
||||
)
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user