Skip to content

Commit

Permalink
Merge pull request #527 from NordicSemiconductor/message-callbacks-async
Browse files Browse the repository at this point in the history
Asynchronous message sending - with `async`-first approach
  • Loading branch information
philips77 authored Jun 27, 2023
2 parents 4ddb2a0 + b44d700 commit 2876660
Show file tree
Hide file tree
Showing 21 changed files with 887 additions and 282 deletions.
2 changes: 1 addition & 1 deletion Example/Podfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use_frameworks!

platform :ios, '12.0'
platform :ios, '13.0'

target 'nRF Mesh' do
pod 'nRFMeshProvision', :path => '../'
Expand Down
13 changes: 4 additions & 9 deletions Example/Pods/Local Podspecs/nRFMeshProvision.podspec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 12 additions & 8 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Example/Source/Utils/UIViewController+Alert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,17 @@ extension UIViewController {
/// - parameters:
/// - title: The alert title.
/// - message: The message below the title.
/// - onCancel:The Cancel button handler.
/// - handler: The Confirm button handler.
func confirm(title: String?, message: String?, handler: ((UIAlertAction) -> Void)? = nil) {
func confirm(title: String?, message: String?, onCancel: ((UIAlertAction) -> Void)? = nil, handler: ((UIAlertAction) -> Void)? = nil) {
// TODO: Should only iPad be handled differently? How about carPlay or Apple TV?
let ipad = UIDevice.current.userInterfaceIdiom == .pad
let style: UIAlertController.Style = ipad ? .alert : .actionSheet
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
let alert = UIAlertController(title: title, message: message, preferredStyle: style)
alert.addAction(UIAlertAction(title: "Confirm", style: .destructive, handler: handler))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: onCancel))
self.present(alert, animated: true)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,14 @@ class GroupsViewController: UITableViewController, Editable, UISearchBarDelegate
return group.isUsed ? .none : .delete
}

override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let group = filteredGroups[indexPath.row]
if group.isUsed {
return [UITableViewRowAction(style: .normal, title: "In Use", handler: { _,_ in })]
return UISwipeActionsConfiguration(actions: [
UIContextualAction(style: .normal, title: "In Use", handler: { _, _, completionHandler in
completionHandler(false)
})
])
}
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,13 +506,14 @@ class ModelViewController: ProgressViewController {
}
}

override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let section = sections[indexPath.section]
switch section {
case .appKeyBinding:
return [UITableViewRowAction(style: .destructive, title: "Unbind", handler: { _, indexPath in
let action = UIContextualAction(style: .destructive, title: "Unbind") { _, _, completionHandler in
guard indexPath.row < self.model.boundApplicationKeys.count else {
return
completionHandler(false)
return
}
let applicationKey = self.model.boundApplicationKeys[indexPath.row]

Expand Down Expand Up @@ -553,13 +554,18 @@ class ModelViewController: ProgressViewController {
message += "\nThe publication will be cancelled automatically."
}
}
self.confirm(title: "Key in use", message: message, handler: { _ in
self.confirm(title: "Key in use", message: message) { _ in
completionHandler(false)
} handler: { _ in
self.unbindApplicationKey(applicationKey)
})
completionHandler(true)
}
} else {
self.unbindApplicationKey(applicationKey)
completionHandler(true)
}
})]
}
return UISwipeActionsConfiguration(actions: [action])
default:
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,6 @@ class NodeAppKeysViewController: ProgressViewController, Editable {
tableView.deselectRow(at: indexPath, animated: true)
}

override func tableView(_ tableView: UITableView,
editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
// This is required to allow swipe to delete action.
return nil
}

override func tableView(_ tableView: UITableView,
commit editingStyle: UITableViewCell.EditingStyle,
forRowAt indexPath: IndexPath) {
Expand All @@ -134,9 +128,9 @@ class NodeAppKeysViewController: ProgressViewController, Editable {
if node.contains(modelBoundToApplicationKey: applicationKey) {
confirm(title: "Remove Key", message: "The selected key is bound to one or more " +
"models in the Node. When removed, it will be unbound automatically, and the " +
"models may stop working.") { _ in
"models may stop working.", handler: { _ in
self.delete(applicationKey: applicationKey)
}
})
} else {
// Otherwise, just try removing it.
delete(applicationKey: applicationKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,13 @@ class NodeNetworkKeysViewController: ProgressViewController, Editable {
return node.networkKeys.count == 1 ? .none : .delete
}

override func tableView(_ tableView: UITableView,
editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
if node.networkKeys.count == 1 {
return [UITableViewRowAction(style: .normal, title: "Last Key", handler: {_,_ in })]
return UISwipeActionsConfiguration(actions: [
UIContextualAction(style: .normal, title: "Last Key", handler: { _, _, completionHandler in
completionHandler(false)
})
])
}
return nil
}
Expand All @@ -139,9 +142,9 @@ class NodeNetworkKeysViewController: ProgressViewController, Editable {
if node.contains(applicationKeyBoundToNetworkKey: networkKey) {
confirm(title: "Remove Key", message: "The selected key is bound to one or more " +
"Application Keys in the Node. When removed, those keys will also be removed " +
"and all models bound to them will be unbound, which may cause them to stop working.") { _ in
"and all models bound to them will be unbound, which may cause them to stop working.", handler: { _ in
self.deleteNetworkKey(networkKey)
}
})
} else {
// Otherwise, just try removing it.
deleteNetworkKey(networkKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,17 @@ class NodeScenesViewController: ProgressViewController, Editable {
tableView.deselectRow(at: indexPath, animated: true)
}

override func tableView(_ tableView: UITableView,
editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
if !isSceneClientReadyToSetup {
return [UITableViewRowAction(style: .normal, title: "Client not ready", handler: {
[unowned self] _,_ in
_ = self.ensureClientReadyToDelete()
})]
return UISwipeActionsConfiguration(actions: [
UIContextualAction(style: .normal, title: "Client not ready", handler: { [weak self] _, _, completionHandler in
guard let self = self else {
completionHandler(false)
return
}
completionHandler(self.ensureClientReadyToDelete())
})
])
}
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,17 @@ class AppKeysViewController: UITableViewController, Editable {
return applicationKey.isUsed(in: network) ? .none : .delete
}

override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let network = MeshNetworkManager.instance.meshNetwork!
let applicationKey = network.applicationKeys[indexPath.keyIndex]

// It should not be possible to delete a key that is in use.
if applicationKey.isUsed(in: network) {
return [UITableViewRowAction(style: .normal, title: "Key in use", handler: {_,_ in })]
return UISwipeActionsConfiguration(actions: [
UIContextualAction(style: .normal, title: "Key in use", handler: { _, _, completionHandler in
completionHandler(false)
})
])
}
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,18 @@ class NetworkKeysViewController: UITableViewController, Editable {
return networkKey.isPrimary || networkKey.isUsed(in: network) ? .none : .delete
}

override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let network = MeshNetworkManager.instance.meshNetwork!
let networkKey = network.networkKeys[indexPath.keyIndex]

// It should not be possible to delete a key that is in use.
if networkKey.isPrimary || networkKey.isUsed(in: network) {
let title = networkKey.isPrimary ? "Primary Key" : "Key in use"
return [UITableViewRowAction(style: .normal, title: title, handler: {_,_ in })]
return UISwipeActionsConfiguration(actions: [
UIContextualAction(style: .normal, title: title, handler: { _, _, completionHandler in
completionHandler(false)
})
])
}
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ private extension EditProvisionerViewController {
let nodeAssigned = newAddress != nil || (node != nil && !disableConfigCapabilities)
let action = !nodeAssigned ? nil : UIAlertAction(title: "Unassign", style: .destructive) { action in
self.confirm(title: "Disable configuration capabilities",
message: "A Provisioner without the unicast address assigned is not able to perform configuration operations.") { [weak self] _ in
message: "A Provisioner without the unicast address assigned is not able to perform configuration operations.",
handler: { [weak self] _ in
guard let self = self else { return }
self.disableConfigCapabilities = true
self.newAddress = nil
Expand All @@ -243,7 +244,7 @@ private extension EditProvisionerViewController {
self.newTtl = nil
self.deviceKeyCell.detailTextLabel?.text = "N/A"
self.deviceKeyCell.detailTextLabel?.font = .systemFont(ofSize: 17)
}
})
}
presentTextAlert(title: "Unicast address", message: "Hexadecimal value in range\n0001 - 7FFF.",
text: address, placeHolder: "Address", type: .unicastAddressRequired,
Expand Down
Loading

0 comments on commit 2876660

Please sign in to comment.