Михаил Капелько 1 anno fa
parent
commit
9a4f181795
5 ha cambiato i file con 37 aggiunte e 4 eliminazioni
  1. +1
    -1
      src/Cord.Aux.swift
  2. +26
    -0
      src/Cord.TextField.swift
  3. +2
    -0
      src/Cord.swift
  4. +3
    -2
      src/V.swift
  5. +5
    -1
      src/VM.swift

src/Aux.swift → src/Cord.Aux.swift Vedi File

@@ -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
- 0
src/Cord.TextField.swift Vedi 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
- 0
src/Cord.swift Vedi File

@@ -0,0 +1,2 @@

public enum Cord { }

+ 3
- 2
src/V.swift Vedi File

@@ -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)



+ 5
- 1
src/VM.swift Vedi File

@@ -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
)
*/
}
}

Loading…
Annulla
Salva