fix related to error message when authentication failed

This commit is contained in:
Johan Degraeve 2019-10-18 23:04:41 +02:00
parent 7b8b890bb6
commit 09349f6912
7 changed files with 97 additions and 59 deletions

View File

@ -255,7 +255,9 @@ class M5StackManager: NSObject {
}
// MARK: - conform to M5StackManaging
// MARK: - extensions
// MARK: extension M5StackManaging
extension M5StackManager: M5StackManaging {
@ -282,11 +284,6 @@ extension M5StackManager: M5StackManaging {
}
}
/// will call coreDataManager.saveChanges
func save() {
coreDataManager.saveChanges()
}
/// try to connect to the M5Stack
func connect(toM5Stack m5Stack: M5Stack) {
@ -324,7 +321,7 @@ extension M5StackManager: M5StackManaging {
m5Stack.shouldconnect = false
// save in coredata
save()
coreDataManager.saveChanges()
if let bluetoothTransmitter = m5StacksBlueToothTransmitters[m5Stack] {
if let bluetoothTransmitter = bluetoothTransmitter {
@ -376,13 +373,19 @@ extension M5StackManager: M5StackManaging {
}
/// sets flag m5StacksParameterUpdateNeeded for m5Stack to true
public func updateNeeded(forM5Stack m5Stack: M5Stack) {
func updateNeeded(forM5Stack m5Stack: M5Stack) {
m5Stack.parameterUpdateNeeded = true
}
/// bluetoothtransmitter for this m5Stack will be deleted, as a result this will also disconnect the M5Stack
func setBluetoothTransmitterToNil(forM5Stack m5Stack: M5Stack) {
self.m5StacksBlueToothTransmitters[m5Stack] = (M5StackBluetoothTransmitter?).none
}
}
// MARK: - conform to M5StackBluetoothDelegate
// MARK: extensions M5StackBluetoothDelegate
extension M5StackManager: M5StackBluetoothDelegate {
@ -493,29 +496,39 @@ extension M5StackManager: M5StackBluetoothDelegate {
// if authentication not successful then disconnect and don't reconnect, user should verify password or reset the M5Stack, disconnect and set shouldconnect to false, permenantly (ie store in core data)
// disconnection is done because maybe another device is trying to connect to the M5Stack, need to make it free
// also set shouldConnect to false (note that this is also done in M5StackViewController if an instance of that exists, no issue, shouldConnect will be set to false two times
if !success {
m5Stack.shouldconnect = false
coreDataManager.saveChanges()
// disconnect
disconnect(fromM5stack: m5Stack)
}
}
/// there's no ble password set, user should set it in the settings - disconnect will be called
/// there's no ble password set, user should set it in the settings - disconnect will be called, shouldconnect is set to false
func blePasswordMissing(forM5Stack m5Stack: M5Stack) {
trace("in blePasswordMissing", log: self.log, type: .info)
m5Stack.shouldconnect = false
coreDataManager.saveChanges()
// disconnect
disconnect(fromM5stack: m5Stack)
}
/// it's an M5Stack without password configured in the ini file. xdrip app has been requesting temp password to M5Stack but this was already done once. M5Stack needs to be reset. - disconnect will be called
/// it's an M5Stack without password configured in the ini file. xdrip app has been requesting temp password to M5Stack but this was already done once. M5Stack needs to be reset. - disconnect will be called, shouldconnect is set to false
func m5StackResetRequired(forM5Stack m5Stack:M5Stack) {
trace("in m5StackResetRequired", log: self.log, type: .info)
m5Stack.shouldconnect = false
coreDataManager.saveChanges()
// disconnect
disconnect(fromM5stack: m5Stack)

View File

@ -9,9 +9,6 @@ protocol M5StackManaging: AnyObject {
/// will stop scanning, this is again for the case where scanning for a new M5Stack has started
func stopScanningForNewDevice()
/// will call coreDataManager.saveChanges
func save()
/// try to connect to the M5Stack
func connect(toM5Stack m5Stack: M5Stack)
@ -33,4 +30,7 @@ protocol M5StackManaging: AnyObject {
/// sets flag m5StacksParameterUpdateNeeded for m5Stack to true
func updateNeeded(forM5Stack m5Stack: M5Stack)
/// bluetoothtransmitter for this m5Stack will be deleted, as a result this will also disconnect the M5Stack
func setBluetoothTransmitterToNil(forM5Stack m5Stack: M5Stack)
}

View File

@ -378,6 +378,7 @@ final class M5StackBluetoothTransmitter: BluetoothTransmitter, BluetoothTransmit
case .readBlePassWordError2Rx:
if let m5Stack = m5Stack {
m5StackBluetoothTransmitterDelegateFixed?.m5StackResetRequired(forM5Stack: m5Stack)
m5StackBluetoothTransmitterDelegateVariable?.m5StackResetRequired(forM5Stack: m5Stack)
}

View File

@ -91,11 +91,6 @@ final class M5StackViewController: UIViewController {
/// temp storage of value while user is editing the M5Stack attributes
private var userDefinedNameTemporaryValue: String?
/// should the app try to connect automatically to the M5Stack or not, setting to false because compiler needs to have a value. It's set to the correct value in configure
///
/// temp storage of value while user is editing the M5Stack attributes
private var shouldConnectTemporaryValue: Bool = false
/// textColor to be used in M5Stack
///
/// temp storage of value while user is editing the M5Stack attributes
@ -117,14 +112,13 @@ final class M5StackViewController: UIViewController {
if let m5StackAsNSObject = m5StackAsNSObject {
// set self as delegate in bluetoothTransmitter
m5StackManager.m5StackBluetoothTransmitter(forM5stack: m5StackAsNSObject, createANewOneIfNecesssary: false)?.m5StackBluetoothTransmitterDelegateVariable = self
if let bluetoothTransmitter = m5StackManager.m5StackBluetoothTransmitter(forM5stack: m5StackAsNSObject, createANewOneIfNecesssary: false) {
bluetoothTransmitter.m5StackBluetoothTransmitterDelegateVariable = self
}
// temporary store the userDefinedName, user can change this name via the view, it will be stored back in the m5StackAsNSObject only after clicking 'done' button
userDefinedNameTemporaryValue = m5StackAsNSObject.m5StackName?.userDefinedName
// temporary store the value of shouldConnect, user can change this via the view, it will be stored back in the m5StackAsNSObject only after clicking 'done' button
shouldConnectTemporaryValue = m5StackAsNSObject.shouldconnect
// temporary store the value of textColor, user can change this via the view, it will be stored back in the m5StackAsNSObject only after clicking 'done' button
textColorTemporaryValue = M5StackTextColor(forUInt16: UInt16(m5StackAsNSObject.textcolor))
@ -238,9 +232,6 @@ final class M5StackViewController: UIViewController {
m5StackAsNSObject.m5StackName = m5Stackname
}
// store value of shouldconnect
m5StackAsNSObject.shouldconnect = shouldConnectTemporaryValue
// store value of textcolor
if let textColor = textColorTemporaryValue {
@ -248,7 +239,7 @@ final class M5StackViewController: UIViewController {
}
// save all changes now
m5StackManager?.save()
coreDataManager.saveChanges()
}
@ -273,7 +264,6 @@ final class M5StackViewController: UIViewController {
self.m5StackAsNSObject = m5Stack
// assign local variables
self.shouldConnectTemporaryValue = m5Stack.shouldconnect
self.userDefinedNameTemporaryValue = m5Stack.m5StackName?.userDefinedName //should be nil anyway
self.textColorTemporaryValue = M5StackTextColor(forUInt16: UInt16(m5Stack.textcolor))
@ -338,28 +328,44 @@ final class M5StackViewController: UIViewController {
// let's first check if m5stack exists, it should because otherwise connectButton should be disabled
guard let m5StackAsNSObject = m5StackAsNSObject else {return}
if shouldConnectTemporaryValue {
if m5StackAsNSObject.shouldconnect {
// device should not automaticaly connect, which means, each time the app restarts, it will not try to connect to this M5Stack
// if user clicks cancel button (ie goes back to previous view controller without clicking done, then this value will not be saved
shouldConnectTemporaryValue = false
m5StackAsNSObject.shouldconnect = false
// save the update in coredata
coreDataManager?.saveChanges()
// update the connect button text
setConnectButtonLabelText()
// normally there should be a bluetoothTransmitter
if let bluetoothTransmitter = m5StackManager?.m5StackBluetoothTransmitter(forM5stack: m5StackAsNSObject, createANewOneIfNecesssary: false) {
// disconnect, even if not connected for the moment
bluetoothTransmitter.disconnect(reconnectAfterDisconnect: false)
// set delegate in bluetoothtransmitter to nil, as we're going to disconnect permenantly, so not interested anymore to receive info
bluetoothTransmitter.m5StackBluetoothTransmitterDelegateVariable = nil
// this will also set bluetoothTransmitter to nil and also disconnect the M5Stack
m5StackManager?.setBluetoothTransmitterToNil(forM5Stack: m5StackAsNSObject)
}
} else {
// device should automatically connect, this will be stored in coredata (only after clicking done button), which means, each time the app restarts, it will try to connect to this M5Stack
// if user clicks cancel button (ie goes back to previous view controller without clicking done, then this value will not be saved
shouldConnectTemporaryValue = true
// device should automatically connect, this will be stored in coredata
m5StackAsNSObject.shouldconnect = true
coreDataManager?.saveChanges()
// connect,
m5StackManager?.m5StackBluetoothTransmitter(forM5stack: m5StackAsNSObject, createANewOneIfNecesssary: true)?.connect()
// get bluetoothTransmitter
if let bluetoothTransmitter = m5StackManager?.m5StackBluetoothTransmitter(forM5stack: m5StackAsNSObject, createANewOneIfNecesssary: true) {
// set delegate
bluetoothTransmitter.m5StackBluetoothTransmitterDelegateVariable = self
// connect
bluetoothTransmitter.connect()
}
}
@ -386,17 +392,21 @@ final class M5StackViewController: UIViewController {
private func setConnectButtonLabelText() {
// if M5Stack is nil, then set text to "Always Connect", it's disabled anyway - if m5Stack not nil, then set depending on value of shouldconnect
if m5StackAsNSObject == nil {
connectButtonOutlet.setTitle(Texts_M5StackView.alwaysConnect, for: .normal)
} else {
if let m5StackAsNSObject = m5StackAsNSObject {
// set label of connect button, according to curren status
connectButtonOutlet.setTitle(shouldConnectTemporaryValue ? Texts_M5StackView.donotconnect:Texts_M5StackView.alwaysConnect, for: .normal)
connectButtonOutlet.setTitle(m5StackAsNSObject.shouldconnect ? Texts_M5StackView.donotconnect:Texts_M5StackView.alwaysConnect, for: .normal)
} else {
connectButtonOutlet.setTitle(Texts_M5StackView.alwaysConnect, for: .normal)
}
}
/// user clicked cancel button
public func cancelButtonAction() {
private func cancelButtonAction() {
// just in case scanning for a new device is still ongoing, call stopscanning
m5StackManager?.stopScanningForNewDevice()
@ -406,10 +416,22 @@ final class M5StackViewController: UIViewController {
}
/// sets m5Stack.shouldconnect to false, saves in coredata, calls setConnectButtonLabelText
private func setShouldConnectToFalse(forM5Stack m5Stack: M5Stack) {
m5Stack.shouldconnect = false
coreDataManager?.saveChanges()
self.setConnectButtonLabelText()
}
}
// MARK: - extensions
// MARK: extension UITableViewDataSource, UITableViewDelegate
extension M5StackViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
@ -585,6 +607,8 @@ extension M5StackViewController: UITableViewDataSource, UITableViewDelegate {
}
// MARK: extension M5StackBluetoothDelegate
extension M5StackViewController: M5StackBluetoothDelegate {
func isAskingForAllParameters(m5Stack: M5Stack) {
@ -612,10 +636,8 @@ extension M5StackViewController: M5StackBluetoothDelegate {
let alert = UIAlertController(title: Texts_Common.warning, message: Texts_M5StackView.authenticationFailureWarning + " " + Texts_M5StackView.alwaysConnect, actionHandler: {
// by the time user clicks 'ok', the M5stack will be disconnected by the M5StackManager (see authentication in M5StackManager)
self.shouldConnectTemporaryValue = m5Stack.shouldconnect
self.setConnectButtonLabelText()
self.setShouldConnectToFalse(forM5Stack: m5Stack)
})
self.present(alert, animated: true, completion: nil)
@ -628,10 +650,8 @@ extension M5StackViewController: M5StackBluetoothDelegate {
let alert = UIAlertController(title: Texts_Common.warning, message: Texts_M5StackView.authenticationFailureWarning + " " + Texts_M5StackView.alwaysConnect, actionHandler: {
// by the time user clicks 'ok', the M5stack will be disconnected by the M5StackManager (see authentication in M5StackManager)
self.shouldConnectTemporaryValue = m5Stack.shouldconnect
self.setConnectButtonLabelText()
self.setShouldConnectToFalse(forM5Stack: m5Stack)
})
self.present(alert, animated: true, completion: nil)
@ -644,10 +664,8 @@ extension M5StackViewController: M5StackBluetoothDelegate {
let alert = UIAlertController(title: Texts_Common.warning, message: Texts_M5StackView.m5StackResetRequiredWarning + " " + Texts_M5StackView.alwaysConnect, actionHandler: {
// by the time user clicks 'ok', the M5stack will be disconnected by the M5StackManager (see authentication in M5StackManager)
self.shouldConnectTemporaryValue = m5Stack.shouldconnect
self.setConnectButtonLabelText()
self.setShouldConnectToFalse(forM5Stack: m5Stack)
})
self.present(alert, animated: true, completion: nil)
@ -678,6 +696,8 @@ extension M5StackViewController: M5StackBluetoothDelegate {
}
// MARK: extension M5StackBluetoothDelegate
/// defines perform segue identifiers used within M5StackViewController
extension M5StackViewController {
public enum SegueIdentifiers:String {

View File

@ -135,7 +135,9 @@ final class M5StacksViewController: UIViewController {
}
// MARK: - UITableViewDataSource and UITableViewDelegate protocol Methods
// MARK: - extensions
// MARK: extension UITableViewDataSource and UITableViewDelegate
extension M5StacksViewController: UITableViewDataSource, UITableViewDelegate {
@ -207,6 +209,8 @@ extension M5StacksViewController: UITableViewDataSource, UITableViewDelegate {
}
// MARK: extension M5StackBluetoothDelegate
extension M5StacksViewController: M5StackBluetoothDelegate {
func isAskingForAllParameters(m5Stack: M5Stack) {

View File

@ -1188,7 +1188,7 @@ extension RootViewController:CGMTransmitterDelegate {
UserDefaults.standard.lastdisConnectTimestamp = Date()
case .poweredOn:
// user changes device bluetooth status to on
debuglogging("in deviceDidUpdateBluetoothState, status = poweredon")
if UserDefaults.standard.cgmTransmitterDeviceAddress == nil, let cgmTransmitter = cgmTransmitter, let transmitterType = UserDefaults.standard.transmitterType, transmitterType.startScanningAfterInit() {
// bluetoothDeviceAddress = nil, means app hasn't connected before to the transmitter
// cgmTransmitter != nil, means user has configured transmitter type and transmitterid

View File

@ -10,7 +10,7 @@ fileprivate enum Setting:Int, CaseIterable {
struct SettingsViewM5StackBluetoothSettingsViewModel: SettingsViewModelProtocol {
func sectionTitle() -> String? {
return Texts_SettingsView.m5StackSettingsViewScreenTitle
return Texts_SettingsView.m5StackSectionTitleBluetooth
}
func settingsRowText(index: Int) -> String {