Skip to content

Commit

Permalink
IOS-8946 Remove eye feature
Browse files Browse the repository at this point in the history
  • Loading branch information
tureck1y committed Jan 23, 2025
1 parent 23cdd76 commit bd0f7d1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 49 deletions.
12 changes: 1 addition & 11 deletions TangemSdk/TangemSdk/UI/Views/Common/FloatingTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ struct FloatingTextField: View {
var shouldBecomeFirstResponder: Bool = false

@EnvironmentObject private var style: TangemSdkStyle
@State private var isSecured: Bool = true

@ViewBuilder
private var textField: some View {
FocusableTextField(
isSecured: isSecured,
shouldBecomeFirstResponder: shouldBecomeFirstResponder,
text: text,
onCommit: onCommit
Expand All @@ -44,11 +42,7 @@ struct FloatingTextField: View {
.font(.system(size: 17))
.frame(height: 17)
}

Button(action: toggleSecured) {
Image(systemName: isSecured ? "eye" : "eye.slash")
.foregroundColor(.gray)
}

}

Color(UIColor.opaqueSeparator)
Expand All @@ -57,10 +51,6 @@ struct FloatingTextField: View {
.padding(.top, 20)
.animation(Animation.easeInOut(duration: 0.1), value: text.wrappedValue)
}

private func toggleSecured() {
isSecured.toggle()
}
}


Expand Down
60 changes: 22 additions & 38 deletions TangemSdk/TangemSdk/UI/Views/Common/FocusableTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,73 +11,57 @@ import SwiftUI
import Combine

struct FocusableTextField: View {
let isSecured: Bool
let shouldBecomeFirstResponder: Bool
let text: Binding<String>
var onCommit: () -> Void = {}

@FocusState private var focusedField: Field?
@StateObject private var model: FocusableTextFieldModel = .init()

var body: some View {
ZStack {
if isSecured {
SecureField("", text: text, onCommit: onCommit)
.focused($focusedField, equals: .secure)
} else {
TextField("", text: text, onCommit: onCommit)
.focused($focusedField, equals: .plain)
}
}
.keyboardType(.default)
.onAppear(perform: model.onAppear)
.onChange(of: isSecured) { newValue in
setFocus(for: newValue)
}
.onReceive(model.focusPublisher) { _ in
if shouldBecomeFirstResponder {
setFocus(for: isSecured)
SecureField("", text: text, onCommit: onCommit)
.textContentType(.oneTimeCode) // to prevent passwords suggestion. Tested on ios 15-18
.focused($focusedField, equals: .secure)
.keyboardType(.asciiCapable)
.onAppear(perform: model.onAppear)
.onReceive(model.focusPublisher) { _ in
if shouldBecomeFirstResponder {
focusedField = .secure
}
}
}
}

init(isSecured: Bool,
shouldBecomeFirstResponder: Bool,

init(shouldBecomeFirstResponder: Bool,
text: Binding<String>,
onCommit: @escaping () -> Void = {}) {
self.isSecured = isSecured
onCommit: @escaping () -> Void = {}
) {
self.shouldBecomeFirstResponder = shouldBecomeFirstResponder
self.text = text
self.onCommit = onCommit
}

private func setFocus(for value: Bool) {
focusedField = value ? .secure : .plain
}
}


private extension FocusableTextField {
enum Field: Hashable {
case secure
case plain
}
}

fileprivate class FocusableTextFieldModel: ObservableObject {
var focusPublisher: PassthroughSubject<Void, Never> = .init()

private var appearPublisher: CurrentValueSubject<Bool, Never> = .init(false)
private var activePublisher: CurrentValueSubject<Bool, Never> = .init(UIApplication.shared.isActive)
private var bag: Set<AnyCancellable> = .init()

private var becomeActivePublisher: AnyPublisher<Void, Never> {
NotificationCenter.default
.publisher(for: UIApplication.didBecomeActiveNotification)
.map { _ in () }
.eraseToAnyPublisher()
}

/// This is the minimum allowable delay, calculated empirically for all iOS versions prior 16.
private var appearDelay: Int {
if #available(iOS 16.0, *) {
Expand All @@ -86,22 +70,22 @@ fileprivate class FocusableTextFieldModel: ObservableObject {
return 500
}
}

init() {
bind()
}

func onAppear() {
appearPublisher.send(true)
}

private func bind() {
becomeActivePublisher
.sink { [weak self] _ in
self?.activePublisher.send(true)
}
.store(in: &bag)

appearPublisher
.filter { $0 }
.delay(for: .milliseconds(appearDelay), scheduler: DispatchQueue.main)
Expand Down

0 comments on commit bd0f7d1

Please sign in to comment.