52 lines
1.1 KiB
Swift
52 lines
1.1 KiB
Swift
//
|
|
// TextFieldViewModel.swift
|
|
//
|
|
//
|
|
// Created by Juraldinio on 2/19/23.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
import Combine
|
|
|
|
public final class TextFieldViewModel: ObservableObject {
|
|
|
|
@Published
|
|
public var text = ""
|
|
|
|
@Published
|
|
public var isFirstResponder = false
|
|
|
|
@Published
|
|
public var shouldClear: Bool = false
|
|
|
|
public let placeholder: String
|
|
public let keyboardType: UIKeyboardType
|
|
|
|
public var isCloseButtonVisible: Bool { !self.text.isEmpty }
|
|
|
|
private var canclellables = Set<AnyCancellable>()
|
|
|
|
// MARK: - Init
|
|
|
|
public init(placeholder: String = "", keyboardType: UIKeyboardType = .default) {
|
|
self.placeholder = placeholder
|
|
self.keyboardType = keyboardType
|
|
|
|
self.$text
|
|
.receive(on: DispatchQueue.main)
|
|
.sink { [weak self] value in
|
|
self?.shouldClear = value == ""
|
|
}
|
|
.store(in: &self.canclellables)
|
|
}
|
|
|
|
// MARK: - Methods
|
|
|
|
public func clearButtonAction() {
|
|
self.text = ""
|
|
self.isFirstResponder = false
|
|
self.shouldClear = false
|
|
}
|
|
}
|