Skip to content

Commit

Permalink
feat: register/unregister webhooks for tenants (#22)
Browse files Browse the repository at this point in the history
add a switch for each unit to register/unregister webhooks for tenants
remove APIClient

Refs # DP-300
  • Loading branch information
yingtao-butterflymx committed Jun 11, 2021
1 parent 1b74d55 commit d81dcaa
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 142 deletions.
4 changes: 0 additions & 4 deletions ButterflyMX Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
1A8DF4A2225CBAF000AB0393 /* Colors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A8DF4A1225CBAF000AB0393 /* Colors.swift */; };
1A8DF4A3225CBAF500AB0393 /* Colors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A8DF4A1225CBAF000AB0393 /* Colors.swift */; };
1AA6FBF9219B2A78002F7168 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1AA6FBF8219B2A78002F7168 /* Foundation.framework */; };
9CD3AF282656DE220000D8CE /* APIClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9CD3AF272656DE220000D8CE /* APIClient.swift */; };
C3689694234CD51F003DED8D /* AVFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C3689693234CD51F003DED8D /* AVFoundation.framework */; };
C3C2322C21F5C6B500A84483 /* (null) in Sources */ = {isa = PBXBuildFile; };
C3C2322D21F5C6B500A84483 /* LoginViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A1C1317219ADCEE003449F8 /* LoginViewController.swift */; };
Expand Down Expand Up @@ -94,7 +93,6 @@
1A8DF4A1225CBAF000AB0393 /* Colors.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Colors.swift; sourceTree = "<group>"; };
1AA6FBF8219B2A78002F7168 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
1ACCD9B121EF73FB00E97924 /* ButterflyMX Demo.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = "ButterflyMX Demo.entitlements"; sourceTree = "<group>"; };
9CD3AF272656DE220000D8CE /* APIClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIClient.swift; sourceTree = "<group>"; };
C3689693234CD51F003DED8D /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = System/Library/Frameworks/AVFoundation.framework; sourceTree = SDKROOT; };
C3C2324121F5C6B500A84483 /* ButterflyMX Demo Internal.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "ButterflyMX Demo Internal.app"; sourceTree = BUILT_PRODUCTS_DIR; };
CE11560B1A4691DFA2BADB9A /* Pods-ButterflyMX Demo.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ButterflyMX Demo.release.xcconfig"; path = "Target Support Files/Pods-ButterflyMX Demo/Pods-ButterflyMX Demo.release.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -186,7 +184,6 @@
1A8DF4A1225CBAF000AB0393 /* Colors.swift */,
1A1C1314219ADC62003449F8 /* Resources */,
1A1C130E219AD956003449F8 /* Info.plist */,
9CD3AF272656DE220000D8CE /* APIClient.swift */,
);
path = "ButterflyMX Demo";
sourceTree = "<group>";
Expand Down Expand Up @@ -402,7 +399,6 @@
1A715B0D22562E260002772C /* UnitTableViewCell.swift in Sources */,
1A715B0522561DDE0002772C /* DoorTableViewCell.swift in Sources */,
1A1C1303219AD954003449F8 /* AppDelegate.swift in Sources */,
9CD3AF282656DE220000D8CE /* APIClient.swift in Sources */,
1A05267521F0A60700F6D373 /* CallsService.swift in Sources */,
1A1C1316219ADC82003449F8 /* Extension.swift in Sources */,
1A8DF49F225CB26500AB0393 /* DoorsTableViewController.swift in Sources */,
Expand Down
47 changes: 0 additions & 47 deletions ButterflyMX Demo/APIClient.swift

This file was deleted.

46 changes: 31 additions & 15 deletions ButterflyMX Demo/Controllers/AccountTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,40 @@ class AccountTableViewController: UITableViewController {
if indexPath.section == 1 && indexPath.row == 0 {
SVProgressHUD.show()

APIClient.shared.unRegisterDevice { result in
switch result {
case .success:
// Will change this line when releasing a new SDK since I removed completion from logoutUser
BMXCoreKit.shared.logoutUser{ result in }

SVProgressHUD.dismiss()

let stb = UIStoryboard(name: "Main", bundle: nil)
let loginViewController = stb.instantiateViewController(withIdentifier: "LoginViewController")
loginViewController.modalPresentationStyle = .fullScreen
self.present(loginViewController, animated: true, completion: nil)
let tenants = BMXUser.shared.getTenants()

let dispatchGroup = DispatchGroup()

for tenant in tenants {
let key = "webhook-\(tenant.id)"
if let webhookId = UserDefaults.standard.string(forKey: key) {
dispatchGroup.enter()

case .failure(let error):
SVProgressHUD.dismiss()
self.alert(message: error.localizedDescription)
BMXCoreKit.shared.unregisterWebhook(withTenantId: tenant.id, webhookId: webhookId) { result in
switch result {
case .success:
print("remove webhook for key: \(key)")
UserDefaults.standard.removeObject(forKey: key)
case .failure(let error):
print(error.localizedDescription)
}

dispatchGroup.leave()
}
}
}

dispatchGroup.notify(queue: .main) {
print("Finished all requests.")

BMXCoreKit.shared.logoutUser()
SVProgressHUD.dismiss()

let stb = UIStoryboard(name: "Main", bundle: nil)
let loginViewController = stb.instantiateViewController(withIdentifier: "LoginViewController")
loginViewController.modalPresentationStyle = .fullScreen
self.present(loginViewController, animated: true, completion: nil)
}
}
}

Expand Down
10 changes: 2 additions & 8 deletions ButterflyMX Demo/Controllers/LoginViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,8 @@ class LoginViewController: UITableViewController {
guard let pushToken = CallsService.shared.pushkitToken else { return }
let token = pushToken.map { String(format: "%02.2hhx", $0) }.joined()

APIClient.shared.registerDevice(with: token) { result in
switch result {
case .success:
print("The device is registered.")
case .failure(let error):
print(error.localizedDescription)
}
}
UserDefaults.standard.set(token, forKey: "deviceToken")
UserDefaults.standard.synchronize()
})
case .failure(let error):
print(error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@

import UIKit
import BMXCore
import SVProgressHUD

class UnitTableViewCell: UITableViewCell {

@IBOutlet weak var unitLabel: UILabel!
@IBOutlet weak var statusLabel: UILabel!
@IBOutlet weak var registerWebhookSwitch: UISwitch!

var tenantModel: TenantModel? = nil

func setTenant(_ data: TenantModel) {
unitLabel.text = "Unit: " + (data.unit?.label ?? "")
if data.isOpenDoorEnabled {
func setTenant(_ tenantModel: TenantModel) {
self.tenantModel = tenantModel
unitLabel.text = "Unit: " + (tenantModel.unit?.label ?? "")
if tenantModel.isOpenDoorEnabled {
statusLabel.text = "Available"
statusLabel.textColor = Colors.lightGreen
accessoryType = .disclosureIndicator
Expand All @@ -24,5 +30,57 @@ class UnitTableViewCell: UITableViewCell {
statusLabel.textColor = Colors.lightGray
accessoryType = .none
}

registerWebhookSwitch.isOn = UserDefaults.standard.string(forKey: "webhook-\(tenantModel.id)") != nil
}

@IBAction func toggleRegisterWebhook(_ sender: Any) {
guard let tenantModel = tenantModel else {
return
}

let key = "webhook-\(tenantModel.id)"

if registerWebhookSwitch.isOn {
guard let deviceToken = UserDefaults.standard.string(forKey: "deviceToken") else {
return
}

let webhookBaseUrl = "https://a98fb4c79b25.ngrok.io/webhook/"
let webhookUrl = "\(webhookBaseUrl)?token=\(deviceToken)&platform=ios&type=voip"

SVProgressHUD.show()
BMXCoreKit.shared.registerWebhook(withTenantId: tenantModel.id, urlString: webhookUrl) { [weak self] result in
switch result {
case .success(let webhookId):
UserDefaults.standard.set(webhookId, forKey: key)
UserDefaults.standard.synchronize()
case .failure(let error):
switch error {
case .runtime(let error):
print(error.localizedDescription)
case .unableToCreateRequest(let message):
fallthrough
case .unableToProcessResponse(let message):
print(message)
@unknown default:
print("unknown error")
}
self?.registerWebhookSwitch.isOn = false
}
SVProgressHUD.dismiss()
}
} else if let webhookId = UserDefaults.standard.string(forKey: key) {
SVProgressHUD.show()
BMXCoreKit.shared.unregisterWebhook(withTenantId: tenantModel.id, webhookId: webhookId) { result in
switch result {
case .success:
UserDefaults.standard.removeObject(forKey: key)
case .failure(let error):
print(error.localizedDescription)
}
SVProgressHUD.dismiss()
}
}
}
}
11 changes: 3 additions & 8 deletions ButterflyMX Demo/Logic/CallsService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,9 @@ extension CallsService: PKPushRegistryDelegate, CXProviderDelegate {
pushkitToken = pushCredentials.token
if BMXCoreKit.shared.isUserLoggedIn {
let token = pushCredentials.token.map { String(format: "%02.2hhx", $0) }.joined()
APIClient.shared.registerDevice(with: token) { result in
switch result {
case .success:
print("The device is registered.")
case .failure(let error):
print(error.localizedDescription)
}
}

UserDefaults.standard.set(token, forKey: "deviceToken")
UserDefaults.standard.synchronize()
}
}

Expand Down
Loading

0 comments on commit d81dcaa

Please sign in to comment.