-
Notifications
You must be signed in to change notification settings - Fork 373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added initial state improvement & a minor fix for BluetoothState methods. #371
Added initial state improvement & a minor fix for BluetoothState methods. #371
Conversation
…we should report unknown if manager doesn't report anything yet, and not unsupported.
ping? :( |
Sorry for such a late reply. I am trying to find someone willing to look into this and opinionate. As far as I remember the lack of initial state was picked because it is cleaner to add it to the beginning of the stream than to skip the first value (as it is an assumption about the internals of the observable) – merely not having this situation needed in the examples does not mean that noone has it and there was such a situation (I do not remember details now though). I am not sure about |
Hello @dariuszseweryn ! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry that you had to wait for so long. Can you apply changes which I've suggested? Please, add also tests for new code.
@@ -92,11 +91,19 @@ public class CentralManager: ManagerType { | |||
// MARK: State | |||
|
|||
public var state: BluetoothState { | |||
return BluetoothState(rawValue: manager.state.rawValue) ?? .unsupported | |||
return BluetoothState(rawValue: manager.state.rawValue) ?? .unknown |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Thanks for suggesting this changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return BluetoothState(rawValue: manager.state.rawValue) ?? .unknown | |
return BluetoothState(rawValue: manager.state.rawValue).unsafelyUnwrapped |
} | ||
|
||
public func observeState() -> Observable<BluetoothState> { | ||
return self.delegateWrapper.didUpdateState.asObservable() | ||
return Observable.deferred { [weak self] in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather add a new method which will return also current state than change current implementation. This will require less changes for users who already use our library. Can you create create new method for observing state and put there you code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done :). Let me know what you think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ping :D
This PR will add 1 fix and 1 improvement for BluetoothState methods:
unknown
if CBCentralManager doesn't have a state yet. This feels the right state to report in this case, since we can't say for sure that Bluetooth is unsupported.observeState()
. This will also be emitted upon subscription, using deferred syntax. The main reason behind this is that perhaps all use-cases when observing the BluetoothState you will also want to check the initial state. All the examples in the code also use this and any app that wants to present BT state will probably also need an initial value. Moreover, the examples in the code currently usestartWith
, but they don't take into consideration when the subscription happened. If the subscription happens at a later time, that startWith might have a different value, and it becomes useless. In order to fix this, the user will always need to use thedeferred
syntax to be sure that he actually has the latest bluetooth state. This feels like an improvement that we can do for the users.