d
This commit is contained in:
17
Modules/CordX/src/Cord.Button.swift
Normal file
17
Modules/CordX/src/Cord.Button.swift
Normal file
@@ -0,0 +1,17 @@
|
||||
import BusX
|
||||
import Combine
|
||||
|
||||
extension Cord {
|
||||
public final class Button: ObservableObject {
|
||||
public let press = PassthroughSubject<Void, Never>()
|
||||
var subscriptions = [AnyCancellable]()
|
||||
|
||||
public init(_ key: String) {
|
||||
Bus.send(
|
||||
key,
|
||||
press.eraseToAnyPublisher(),
|
||||
sub: &subscriptions
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
9
Modules/CordX/src/Cord.Onlys.swift
Normal file
9
Modules/CordX/src/Cord.Onlys.swift
Normal file
@@ -0,0 +1,9 @@
|
||||
extension Cord {
|
||||
/// Пропускаем лишь значения от UI
|
||||
///
|
||||
/// - Returns: Значение без префиксов "a:"/"u:"
|
||||
static func onlyUIText(_ s: String) -> String? {
|
||||
guard s.hasPrefix("u:") else { return nil }
|
||||
return String(s.dropFirst(2))
|
||||
}
|
||||
}
|
||||
21
Modules/CordX/src/Cord.Receive.swift
Normal file
21
Modules/CordX/src/Cord.Receive.swift
Normal file
@@ -0,0 +1,21 @@
|
||||
import BusX
|
||||
import Combine
|
||||
|
||||
extension Cord {
|
||||
public final class Receive<T>: ObservableObject {
|
||||
@Published public var value: T
|
||||
var subscriptions = [AnyCancellable]()
|
||||
|
||||
public init(
|
||||
_ key: String,
|
||||
_ defaultValue: T
|
||||
) {
|
||||
value = defaultValue
|
||||
Bus.receive(
|
||||
[key],
|
||||
{ [weak self] (_, v: T) in self?.value = v },
|
||||
sub: &subscriptions
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
30
Modules/CordX/src/Cord.TextField.swift
Normal file
30
Modules/CordX/src/Cord.TextField.swift
Normal file
@@ -0,0 +1,30 @@
|
||||
import BusX
|
||||
import Combine
|
||||
import SwiftUI
|
||||
|
||||
extension Cord {
|
||||
public final class TextField: ObservableObject {
|
||||
@Published public var value = "a:"
|
||||
var subscriptions = [AnyCancellable]()
|
||||
|
||||
public 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
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
19
Modules/CordX/src/Cord.TextFieldValueOwner.swift
Normal file
19
Modules/CordX/src/Cord.TextFieldValueOwner.swift
Normal file
@@ -0,0 +1,19 @@
|
||||
import Foundation
|
||||
|
||||
extension Cord {
|
||||
public final class TextFieldValueOwner: Formatter {
|
||||
public override func string(for obj: Any?) -> String? {
|
||||
guard let str = obj as? String else { return nil }
|
||||
return String(str.dropFirst(2))
|
||||
}
|
||||
|
||||
public override func getObjectValue(
|
||||
_ obj: AutoreleasingUnsafeMutablePointer<AnyObject?>?,
|
||||
for string: String,
|
||||
errorDescription error: AutoreleasingUnsafeMutablePointer<NSString?>?
|
||||
) -> Bool {
|
||||
obj?.pointee = "u:\(string)" as AnyObject
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
1
Modules/CordX/src/Cord.swift
Normal file
1
Modules/CordX/src/Cord.swift
Normal file
@@ -0,0 +1 @@
|
||||
public enum Cord { }
|
||||
Reference in New Issue
Block a user