From c5fe7527758da4d0a775b6b23276da0962a1c1ff Mon Sep 17 00:00:00 2001 From: Filippos Sakellaropoulos Date: Fri, 27 Sep 2024 23:52:56 +0300 Subject: [PATCH] Add displayName property to DocElementsViewModel struct --- .../Services/PresentationSession.swift | 14 +++++++------- .../EudiWalletKit/Services/StorageManager.swift | 4 ++-- .../ViewModels/DocElementsViewModel.swift | 7 ++++--- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/Sources/EudiWalletKit/Services/PresentationSession.swift b/Sources/EudiWalletKit/Services/PresentationSession.swift index f736e07..b7fa68e 100644 --- a/Sources/EudiWalletKit/Services/PresentationSession.swift +++ b/Sources/EudiWalletKit/Services/PresentationSession.swift @@ -25,7 +25,7 @@ import LocalAuthentication /// This class wraps the ``PresentationService`` instance, providing bindable fields to a SwifUI view public class PresentationSession: ObservableObject { public var presentationService: any PresentationService - /// Reader certificate issuer (only for BLE flow wih verifier using reader authentication) + /// Reader certificate issuer (the Common Name (CN) from the verifier's certificate) @Published public var readerCertIssuer: String? /// Reader legal name (if provided) @Published public var readerLegalName: String? @@ -44,12 +44,12 @@ public class PresentationSession: ObservableObject { var handleSelected: ((Bool, RequestItems?) -> Void)? /// Device engagement data (QR data for the BLE flow) @Published public var deviceEngagement: String? - // map of document id to doc types - public var docIdAndTypes: [String: String] + // map of document id to (doc type, display name) pairs + public var docIdAndTypes: [String: (String, String?)] /// User authentication required var userAuthenticationRequired: Bool - public init(presentationService: any PresentationService, docIdAndTypes: [String: String], userAuthenticationRequired: Bool) { + public init(presentationService: any PresentationService, docIdAndTypes: [String: (String, String?)], userAuthenticationRequired: Bool) { self.presentationService = presentationService self.docIdAndTypes = docIdAndTypes self.userAuthenticationRequired = userAuthenticationRequired @@ -65,10 +65,10 @@ public class PresentationSession: ObservableObject { // show the items as checkboxes guard let validRequestItems = request[UserRequestKeys.valid_items_requested.rawValue] as? RequestItems else { return } disclosedDocuments = [DocElementsViewModel]() - for (docId, docType) in docIdAndTypes { - var tmp = validRequestItems.toDocElementViewModels(docId: docId, docType: docType, valid: true) + for (docId, (docType, displayName)) in docIdAndTypes { + var tmp = validRequestItems.toDocElementViewModels(docId: docId, docType: docType, displayName: displayName, valid: true) if let errorRequestItems = request[UserRequestKeys.error_items_requested.rawValue] as? RequestItems, errorRequestItems.count > 0 { - tmp = tmp.merging(with: errorRequestItems.toDocElementViewModels(docId: docId, docType: docType, valid: false)) + tmp = tmp.merging(with: errorRequestItems.toDocElementViewModels(docId: docId, docType: docType, displayName: displayName, valid: false)) } disclosedDocuments.append(contentsOf: tmp) } diff --git a/Sources/EudiWalletKit/Services/StorageManager.swift b/Sources/EudiWalletKit/Services/StorageManager.swift index dd3f629..26676a2 100644 --- a/Sources/EudiWalletKit/Services/StorageManager.swift +++ b/Sources/EudiWalletKit/Services/StorageManager.swift @@ -131,8 +131,8 @@ public class StorageManager: ObservableObject { return retModel } - public func getDocIdsToTypes() -> [String: String] { - Dictionary(uniqueKeysWithValues: mdocModels.map { m in (m.id, m.docType) }) + public func getDocIdsToTypes() -> [String: (String, String?)] { + Dictionary(uniqueKeysWithValues: mdocModels.map { m in (m.id, (m.docType, m.displayName) ) }) } /// Load documents from storage diff --git a/Sources/EudiWalletKit/ViewModels/DocElementsViewModel.swift b/Sources/EudiWalletKit/ViewModels/DocElementsViewModel.swift index 289dc7b..c3b8213 100644 --- a/Sources/EudiWalletKit/ViewModels/DocElementsViewModel.swift +++ b/Sources/EudiWalletKit/ViewModels/DocElementsViewModel.swift @@ -22,6 +22,7 @@ public struct DocElementsViewModel: Identifiable { public var id: String { docId } public var docId: String public let docType: String + public let displayName: String? public var isEnabled: Bool public var elements: [ElementViewModel] } @@ -47,10 +48,10 @@ extension DocElementsViewModel { } extension RequestItems { - func toDocElementViewModels(docId: String, docType: String, valid: Bool) -> [DocElementsViewModel] { + func toDocElementViewModels(docId: String, docType: String, displayName: String?, valid: Bool) -> [DocElementsViewModel] { compactMap { dType,nsItems in if dType != docType { nil } - else { DocElementsViewModel(docId: docId, docType: docType, isEnabled: valid, elements: DocElementsViewModel.fluttenItemViewModels(nsItems, valid: valid, mandatoryElementKeys: DocElementsViewModel.getMandatoryElementKeys(docType: docType))) } + else { DocElementsViewModel(docId: docId, docType: docType, displayName: displayName, isEnabled: valid, elements: DocElementsViewModel.fluttenItemViewModels(nsItems, valid: valid, mandatoryElementKeys: DocElementsViewModel.getMandatoryElementKeys(docType: docType))) } } } } @@ -63,7 +64,7 @@ extension Array where Element == DocElementsViewModel { for otherDE in other { if let exist = first(where: { $0.docId == otherDE.docId}) { let newElements = (exist.elements + otherDE.elements).sorted(by: { $0.isEnabled && $1.isDisabled }) - res.append(DocElementsViewModel(docId: exist.docId, docType: exist.docType, isEnabled: exist.isEnabled, elements: newElements)) + res.append(DocElementsViewModel(docId: exist.docId, docType: exist.docType, displayName: exist.displayName, isEnabled: exist.isEnabled, elements: newElements)) } else { res.append(otherDE) } }