LookupButton.swift (2337B)
1 /* 2 * This file is part of GNU Taler, ©2022-26 Taler Systems S.A. 3 * See LICENSE.md 4 */ 5 /** 6 * @author Marc Stibane 7 */ 8 import SwiftUI 9 import taler_swift 10 11 // MARK: - 12 enum LookupState: Equatable { 13 case unknown // not yet asked 14 case error // (Error) 15 case unregistered // 16 case registered(String, Duration) // we just (un)registered it, but it isn't confirmed yet 17 case confirmed(String, Duration) // lookup result 18 19 var hasTarget: Bool { 20 switch self { 21 case .registered, .confirmed: 22 return true 23 default: 24 return false 25 } 26 } 27 func isMine(address: String) -> Bool { 28 switch self { 29 case .registered(let targetUri, _): 30 return address == targetUri 31 case .confirmed(let targetUri, _): 32 return address == targetUri 33 default: 34 return false 35 } 36 } 37 } 38 // MARK: - 39 struct LookupButton: View { 40 let stack: CallStack 41 let alias: String 42 let aliasType: TaldirAliasType 43 @Binding var lookupState: LookupState 44 // @FocusState.Binding var isFocused: Bool 45 let focus: () -> Void 46 let action: () -> Void 47 48 @EnvironmentObject private var model: WalletModel 49 @State private var working: Bool = false 50 51 var body: some View { 52 let buttonTitle = String(localized: "Lookup Alias", comment: "button") 53 FeedbackButton(buttonTitle, disabled: working, haptic: false) { 54 working = true 55 focus() // isFocused = false // dismiss keyboard 56 let lookup = TaldirLookup(alias: alias, 57 aliasType: aliasType.name) 58 Task { 59 let lookup = try? await model.lookupAlias(lookup) 60 if let targetUri = lookup?.targetUri { 61 if targetUri.isEmpty { 62 lookupState = .unregistered 63 } else { 64 let validFor = lookup?.validFor 65 lookupState = .confirmed(targetUri, validFor ?? Duration.microseconds(0)) 66 } 67 } else { 68 lookupState = .error 69 } 70 self.working = false 71 action() 72 } 73 } 74 } 75 }