-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* added ability to import public key in settings page * feat: added ui test * fix: pr reviews
- Loading branch information
Showing
19 changed files
with
490 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
FlowCrypt/Controllers/Settings/Contacts/Contact Add/ContactAddViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// | ||
// ContactAddViewController.swift | ||
// FlowCrypt | ||
// | ||
// Created by Ioan Moldovan on 9/18/23 | ||
// Copyright © 2017-present FlowCrypt a. s. All rights reserved. | ||
// | ||
|
||
import AsyncDisplayKit | ||
import FlowCryptCommon | ||
import FlowCryptUI | ||
|
||
final class ContactAddViewController: TableNodeViewController { | ||
private let appContext: AppContext | ||
private let filesManager: FilesManagerType | ||
|
||
init(appContext: AppContext) { | ||
self.appContext = appContext | ||
self.filesManager = FilesManager() | ||
super.init(node: TableNode()) | ||
} | ||
|
||
@available(*, unavailable) | ||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
setupUI() | ||
} | ||
} | ||
|
||
// MARK: - ASTableDelegate, ASTableDataSource | ||
extension ContactAddViewController: ASTableDelegate, ASTableDataSource { | ||
func tableNode(_: ASTableNode, numberOfRowsInSection _: Int) -> Int { | ||
return 1 | ||
} | ||
|
||
func tableNode(_ node: ASTableNode, nodeBlockForRowAt indexPath: IndexPath) -> ASCellNodeBlock { | ||
return { [weak self] in | ||
guard let self else { return ASCellNode() } | ||
let node = ContactAddNode() | ||
node.onImportFromFile = { | ||
Task { | ||
await self.importFromFile() | ||
} | ||
} | ||
node.onImportFromClipboard = { | ||
Task { | ||
await self.importFromClipboard() | ||
} | ||
} | ||
return node | ||
} | ||
} | ||
|
||
func importFromFile() async { | ||
await filesManager.selectFromFilesApp(from: self) | ||
} | ||
|
||
func importFromClipboard() async { | ||
let data = Data((UIPasteboard.general.string ?? "").utf8) | ||
await processKeys(for: data) | ||
} | ||
|
||
func processKeys(for attachmentData: Data) async { | ||
do { | ||
let parsedKeys = try await Core.shared.parseKeys(armoredOrBinary: attachmentData) | ||
if parsedKeys.keyDetails.isEmpty { | ||
showAlert(message: "no_pubkeys_found".localized) | ||
return | ||
} | ||
let viewController = ContactPublicKeyListViewController(appContext: appContext, keyDetailsList: parsedKeys.keyDetails) | ||
navigationController?.pushViewController(viewController, animated: true) | ||
} catch { | ||
showAlert(message: error.errorMessage) | ||
} | ||
} | ||
} | ||
|
||
extension ContactAddViewController: FilesManagerPresenter {} | ||
|
||
// MARK: - UI | ||
extension ContactAddViewController { | ||
private func setupUI() { | ||
node.delegate = self | ||
node.dataSource = self | ||
} | ||
} | ||
|
||
// MARK: - UIDocumentPickerDelegate | ||
extension ContactAddViewController: UIDocumentPickerDelegate { | ||
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) { | ||
guard let fileUrl = urls.first, | ||
let attachment = MessageAttachment(fileURL: fileUrl), | ||
let attachmentData = attachment.data | ||
else { | ||
showAlert(message: "files_picking_files_error_message".localized) | ||
return | ||
} | ||
Task { | ||
await processKeys(for: attachmentData) | ||
} | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
FlowCrypt/Controllers/Settings/Contacts/Contact Add/ContactPublicKeyListViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// | ||
// ContactPublicKeyListViewController.swift | ||
// FlowCrypt | ||
// | ||
// Created by Ioan Moldovan on 9/18/23 | ||
// Copyright © 2017-present FlowCrypt a. s. All rights reserved. | ||
// | ||
|
||
import AsyncDisplayKit | ||
import FlowCryptCommon | ||
import FlowCryptUI | ||
|
||
final class ContactPublicKeyListViewController: TableNodeViewController { | ||
private let appContext: AppContext | ||
var keyDetailsList: [KeyDetails] = [] | ||
let localContactsProvider: LocalContactsProviderType | ||
|
||
init(appContext: AppContext, keyDetailsList: [KeyDetails]) { | ||
self.appContext = appContext | ||
self.localContactsProvider = LocalContactsProvider( | ||
encryptedStorage: appContext.encryptedStorage | ||
) | ||
self.keyDetailsList = keyDetailsList | ||
super.init(node: TableNode()) | ||
} | ||
|
||
@available(*, unavailable) | ||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
setupUI() | ||
} | ||
} | ||
|
||
// MARK: - ASTableDelegate, ASTableDataSource | ||
extension ContactPublicKeyListViewController: ASTableDelegate, ASTableDataSource { | ||
func tableNode(_: ASTableNode, numberOfRowsInSection _: Int) -> Int { | ||
return keyDetailsList.count | ||
} | ||
|
||
func tableNode(_ node: ASTableNode, nodeBlockForRowAt indexPath: IndexPath) -> ASCellNodeBlock { | ||
return { [weak self] in | ||
guard let self else { return ASCellNode() } | ||
let keyDetails = keyDetailsList[indexPath.row] | ||
let node = PublicKeyDetailNode( | ||
input: ThreadDetailsViewController.getPublicKeyDetailInput( | ||
for: keyDetails, | ||
localContactsProvider: localContactsProvider | ||
) | ||
) | ||
node.onImportKey = { | ||
self.importPublicKey(indexPath: indexPath, keyDetails: keyDetails) | ||
} | ||
return node | ||
} | ||
} | ||
|
||
private func importPublicKey(indexPath: IndexPath, keyDetails: KeyDetails) { | ||
guard let email = keyDetails.pgpUserEmails.first else { | ||
return | ||
} | ||
try? localContactsProvider.updateKey(for: email, pubKey: .init(keyDetails: keyDetails)) | ||
node.reloadRows(at: [indexPath], with: .automatic) | ||
reloadContactList() | ||
} | ||
|
||
private func reloadContactList() { | ||
if let contactsVC = navigationController?.viewControllers.first( | ||
where: { $0 is ContactsListViewController } | ||
) as? ContactsListViewController { | ||
contactsVC.fetchContacts() | ||
} | ||
} | ||
} | ||
|
||
// MARK: - UI | ||
extension ContactPublicKeyListViewController { | ||
private func setupUI() { | ||
node.delegate = self | ||
node.dataSource = self | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.