|
1 | | -/* tslint:disable:no-unused-variable */ |
2 | | -import { ControlStateChangeListener as ControlStateChangeListenerDefinition } from '.'; |
| 1 | +import { ControlStateChangeListenerCallback, ControlStateChangeListener as ControlStateChangeListenerDefinition } from '.'; |
3 | 2 |
|
4 | 3 | @NativeClass |
5 | 4 | class ObserverClass extends NSObject { |
6 | | - // NOTE: Refactor this - use Typescript property instead of strings.... |
7 | | - observeValueForKeyPathOfObjectChangeContext(path: string) { |
8 | | - if (path === 'selected') { |
9 | | - this['_owner']._onSelectedChanged(); |
10 | | - } else if (path === 'enabled') { |
11 | | - this['_owner']._onEnabledChanged(); |
12 | | - } else if (path === 'highlighted') { |
13 | | - this['_owner']._onHighlightedChanged(); |
| 5 | + public callback: WeakRef<ControlStateChangeListenerCallback>; |
| 6 | + |
| 7 | + public static initWithCallback(callback: WeakRef<ControlStateChangeListenerCallback>): ObserverClass { |
| 8 | + const observer = <ObserverClass>ObserverClass.alloc().init(); |
| 9 | + observer.callback = callback; |
| 10 | + |
| 11 | + return observer; |
| 12 | + } |
| 13 | + |
| 14 | + public observeValueForKeyPathOfObjectChangeContext(path: string, object: UIControl) { |
| 15 | + const callback = this.callback?.deref(); |
| 16 | + |
| 17 | + if (callback) { |
| 18 | + callback(path, object[path]); |
14 | 19 | } |
15 | 20 | } |
16 | 21 | } |
17 | 22 |
|
18 | 23 | export class ControlStateChangeListener implements ControlStateChangeListenerDefinition { |
19 | 24 | private _observer: NSObject; |
20 | 25 | private _control: UIControl; |
21 | | - private _observing = false; |
| 26 | + private _observing: boolean = false; |
22 | 27 |
|
23 | | - private _callback: (state: string) => void; |
| 28 | + private readonly _states: string[]; |
24 | 29 |
|
25 | | - constructor(control: UIControl, callback: (state: string) => void) { |
26 | | - this._observer = ObserverClass.alloc().init(); |
27 | | - this._observer['_owner'] = this; |
| 30 | + constructor(control: UIControl, states: string[], callback: ControlStateChangeListenerCallback) { |
28 | 31 | this._control = control; |
29 | | - this._callback = callback; |
| 32 | + this._states = states; |
| 33 | + this._observer = ObserverClass.initWithCallback(new WeakRef(callback)); |
30 | 34 | } |
31 | 35 |
|
32 | 36 | public start() { |
33 | 37 | if (!this._observing) { |
34 | | - this._control.addObserverForKeyPathOptionsContext(this._observer, 'highlighted', NSKeyValueObservingOptions.New, null); |
35 | 38 | this._observing = true; |
36 | | - this._updateState(); |
| 39 | + |
| 40 | + for (const state of this._states) { |
| 41 | + this._control.addObserverForKeyPathOptionsContext(this._observer, state, NSKeyValueObservingOptions.New, null); |
| 42 | + } |
37 | 43 | } |
38 | 44 | } |
39 | 45 |
|
40 | 46 | public stop() { |
41 | 47 | if (this._observing) { |
42 | | - this._observing = false; |
43 | | - this._control.removeObserverForKeyPath(this._observer, 'highlighted'); |
44 | | - } |
45 | | - } |
46 | | - |
47 | | - //@ts-ignore |
48 | | - private _onEnabledChanged() { |
49 | | - this._updateState(); |
50 | | - } |
51 | | - |
52 | | - //@ts-ignore |
53 | | - private _onSelectedChanged() { |
54 | | - this._updateState(); |
55 | | - } |
56 | | - |
57 | | - //@ts-ignore |
58 | | - private _onHighlightedChanged() { |
59 | | - this._updateState(); |
60 | | - } |
| 48 | + for (const state of this._states) { |
| 49 | + this._control.removeObserverForKeyPath(this._observer, state); |
| 50 | + } |
61 | 51 |
|
62 | | - private _updateState() { |
63 | | - let state = 'normal'; |
64 | | - if (this._control.highlighted) { |
65 | | - state = 'highlighted'; |
| 52 | + this._observing = false; |
66 | 53 | } |
67 | | - this._callback(state); |
68 | 54 | } |
69 | 55 | } |
0 commit comments