This commit is contained in:
Михаил Капелько
2023-12-28 12:55:11 +03:00
parent eb29bb6d9e
commit ea09967ab6
13 changed files with 74 additions and 38 deletions

View File

@@ -1,8 +0,0 @@
/// Пропускаем лишь значения от UI
///
/// - Returns: Значение без префиксов "a:"/"u:"
func onlyUIText(_ s: String) -> String? {
guard s.hasPrefix("u:") else { return nil }
return String(s.dropFirst(2))
}

View File

@@ -1,17 +0,0 @@
import BusX
import Combine
extension Cord {
final class Button: ObservableObject {
let press = PassthroughSubject<Void, Never>()
var subscriptions = [AnyCancellable]()
init(_ key: String) {
Bus.send(
key,
press.eraseToAnyPublisher(),
sub: &subscriptions
)
}
}
}

View File

@@ -1,21 +0,0 @@
import BusX
import Combine
extension Cord {
final class Receive<T>: ObservableObject {
@Published var value: T
var subscriptions = [AnyCancellable]()
init(
_ key: String,
_ defaultValue: T
) {
value = defaultValue
Bus.receive(
[key],
{ [weak self] (_, v: T) in self?.value = v },
sub: &subscriptions
)
}
}
}

View File

@@ -1,27 +0,0 @@
import BusX
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.removeDuplicates().compactMap(onlyUIText).eraseToAnyPublisher(),
sub: &subscriptions
)
Bus.receive(
[textApp],
{ [weak self] (_, v: String) in self?.value = "a:\(v)" },
sub: &subscriptions
)
}
}
}

View File

@@ -1,2 +0,0 @@
public enum Cord { }

View File

@@ -29,6 +29,14 @@ enum MeetupId {
}
return r
}
/// Пропускаем лишь значения от UI
///
/// - Returns: Значение без префиксов "a:"/"u:"
func onlyUIText(_ s: String) -> String? {
guard s.hasPrefix("u:") else { return nil }
return String(s.dropFirst(2))
}
}

View File

@@ -1,17 +0,0 @@
import Foundation
class TextFieldValueOwner: Formatter {
override func string(for obj: Any?) -> String? {
guard let str = obj as? String else { return nil }
return String(str.dropFirst(2))
}
override func getObjectValue(
_ obj: AutoreleasingUnsafeMutablePointer<AnyObject?>?,
for string: String,
errorDescription error: AutoreleasingUnsafeMutablePointer<NSString?>?
) -> Bool {
obj?.pointee = "u:\(string)" as AnyObject
return true
}
}

View File

@@ -1,4 +1,5 @@
import BusX
import CordX
import SwiftUI
struct V: View {
@@ -15,7 +16,7 @@ struct V: View {
.fontWeight(.bold)
}
TextField("Binding-3", value: $textF.value, formatter: TextFieldValueOwner())
TextField("Binding-3", value: $textF.value, formatter: Cord.TextFieldValueOwner())
.padding(8)
.border(Color.blue, width: 2)