Skip to content

Commit

Permalink
調整程式碼 (weakSelf 的部分)。
Browse files Browse the repository at this point in the history
  • Loading branch information
kyumdbot committed Aug 21, 2020
1 parent 75eb789 commit 33c18df
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 33 deletions.
15 changes: 6 additions & 9 deletions AutoPassApp/AutoPassApp/BLEDevicesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,9 @@ class BLEDevicesViewController: UIViewController, UITableViewDataSource, UITable

func searchBleDevices() {
print("Search BLE Devices:")

weak var weakSelf = self
bleDevice.scan() { device in
bleDevice.scan() { [weak self] (device) in
print("> Device: \(device.name ?? "") (\(device.identifier.uuidString))")
weakSelf?.addToDevices(device)
self?.addToDevices(device)
}
}

Expand All @@ -96,12 +94,11 @@ class BLEDevicesViewController: UIViewController, UITableViewDataSource, UITable
}

func connectToDevice(_ device: CBPeripheral) {
weak var weakSelf = self
bleDevice.connent(to: device, success: { _ in
bleDevice.connent(to: device, success: { [weak self] _ in
let deviceName = device.name ?? ""
weakSelf?.dismiss(animated: true, completion: { weakSelf?.callOnCloseCallbackWith(deviceName: deviceName) })
}, failure: { errorString in
weakSelf?.msgBox(title: "ERROR: ", message: errorString)
self?.dismiss(animated: true, completion: { self?.callOnCloseCallbackWith(deviceName: deviceName) })
}, failure: { [weak self] (errorString) in
self?.msgBox(title: "ERROR: ", message: errorString)
})
}

Expand Down
42 changes: 18 additions & 24 deletions AutoPassApp/AutoPassApp/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega
super.viewDidLoad()
setup()

weak var weakSelf = self
bleDevice.begin() { isReady in
bleDevice.begin() { [weak self] (isReady) in
print("BLE Ready: \(isReady)")
if isReady {
weakSelf?.showBLEDevicesViewController()
self?.showBLEDevicesViewController()
} else {
weakSelf?.msgBox(title: "訊息:", message: "請到系統設定裡開啟藍牙。")
self?.msgBox(title: "訊息:", message: "請到系統設定裡開啟藍牙。")
}
}
}
Expand Down Expand Up @@ -81,15 +80,13 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega
func showBLEDevicesViewController() {
let vc = storyboard?.instantiateViewController(withIdentifier: "BLEDevicesVC") as! BLEDevicesViewController
vc.bleDevice = bleDevice

weak var weakSelf = self
vc.onCloseCallback = { deviceName in
vc.onCloseCallback = { [weak self] (deviceName) in
print("Close BLEDevicesViewController...")
weakSelf?.removeScanBarButtonItem()
weakSelf?.msgLabelShowDeviceName(deviceName)
weakSelf?.setBLEDeviceValueChangedCallback()
weakSelf?.setBLEDeviceOnDisconnectCallback()
weakSelf?.reloadTable()
self?.removeScanBarButtonItem()
self?.msgLabelShowDeviceName(deviceName)
self?.setBLEDeviceValueChangedCallback()
self?.setBLEDeviceOnDisconnectCallback()
self?.reloadTable()
}
present(vc, animated: true, completion: nil)
}
Expand Down Expand Up @@ -118,10 +115,9 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega
}

func setBLEDeviceValueChangedCallback() {
weak var weakSelf = self
bleDevice.characteristicValueChanged(action: { uuid, data in
bleDevice.characteristicValueChanged(action: { [weak self] (uuid, data) in
print("BLEDevice's characteristic value changed:")
weakSelf?.bleValueChanged(uuid: uuid, data: data)
self?.bleValueChanged(uuid: uuid, data: data)
})
}

Expand All @@ -142,13 +138,12 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega
}

func setBLEDeviceOnDisconnectCallback() {
weak var weakSelf = self
bleDevice.onDisconnect(action: {
bleDevice.onDisconnect(action: { [weak self] in
print("BLEDevice disconnected.")
weakSelf?.showBLEDevicesViewController()
weakSelf?.msgLabelShowDeviceName("")
weakSelf?.clearItems()
weakSelf?.setupScanBarButtonItem()
self?.showBLEDevicesViewController()
self?.msgLabelShowDeviceName("")
self?.clearItems()
self?.setupScanBarButtonItem()
})
}

Expand All @@ -175,9 +170,8 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega
}

let vc = storyboard?.instantiateViewController(withIdentifier: "AddItemVC") as! AddItemViewController
weak var weakSelf = self
vc.onAddedCallback = { title, password, appendEnter in
weakSelf?.addItem(title: title, password: password, appendEnter: appendEnter)
vc.onAddedCallback = { [weak self] (title, password, appendEnter) in
self?.addItem(title: title, password: password, appendEnter: appendEnter)
}
present(vc, animated: true, completion: nil)
}
Expand Down

0 comments on commit 33c18df

Please sign in to comment.