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

15
mod/CordX/CordX.podspec Normal file
View File

@@ -0,0 +1,15 @@
Pod::Spec.new do |s|
s.name = 'CordX'
s.version = '2023.12.28'
s.license = 'IVCS'
s.summary = 'Упрощённое общение с шиной из SwiftUI'
s.homepage = 'IVCS'
s.author = 'IVCS'
s.source = { :git => 'https://fake.com/FAKE.git', :tag => s.version }
s.source_files = 'src/**/*.swift'
s.swift_version = '5.2'
s.ios.deployment_target = '14.0'
s.dependency 'BusX'
end

View 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
)
}
}
}

View 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))
}
}

View 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
)
}
}
}

View 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
)
}
}
}

View 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
mod/CordX/src/Cord.swift Normal file
View File

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