Skip to content

Commit

Permalink
WIP #304 getting the status wired up to actually change icon accordin…
Browse files Browse the repository at this point in the history
…g to pushtracker status.
  • Loading branch information
finger563 committed Nov 29, 2018
1 parent 0d3dc97 commit 46d2e24
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<NavigationButton *ngIf="allowNav" android:visibility="collapse"></NavigationButton>
<ng-content></ng-content>
<ActionItem ios.position="right" (tap)="onPTConnTap()">
<Image [visibility]="status==0 ? 'visible':'collapse'" class="pt-status" src="~/assets/images/pt-conn-red.png"></Image>
<Image [visibility]="status==1 ? 'visible':'collapse'" class="pt-status" src="~/assets/images/pt-conn-grey.png"></Image>
<Image [visibility]="status==2 ? 'visible':'collapse'" class="pt-status" src="~/assets/images/pt-conn-grey.png"></Image>
<Image [visibility]="status==3 ? 'visible':'collapse'" class="pt-status" src="~/assets/images/pt-conn-yellow.png"></Image>
<Image [visibility]="status==4 ? 'visible':'collapse'" class="pt-status" src="~/assets/images/pt-conn-green.png"></Image>
<Image *ngIf="status==0" class="pt-status" src="~/assets/images/pt-conn-red.png"></Image>
<Image *ngIf="status==1" class="pt-status" src="~/assets/images/pt-conn-grey.png"></Image>
<Image *ngIf="status==2" class="pt-status" src="~/assets/images/pt-conn-grey.png"></Image>
<Image *ngIf="status==3" class="pt-status" src="~/assets/images/pt-conn-yellow.png"></Image>
<Image *ngIf="status==4" class="pt-status" src="~/assets/images/pt-conn-green.png"></Image>
</ActionItem>
<ActionItem ios.position="right">
<label text="" padding="5"></label>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { ActionBar, ActionItem } from 'ui/action-bar';

import { BluetoothService } from '@maxmobility/mobile';

import { NavigationStart, Router } from '@angular/router';
import { Page } from 'tns-core-modules/ui/page';

import { Observable, fromObject } from 'tns-core-modules/data/observable';

@Component({
Expand All @@ -16,24 +19,34 @@ export class ActionbarComponent extends ActionBar {
@Input() title: string;
@Input() allowNav: boolean = true;

status = 0;
status = BluetoothService.pushTrackerStatus;

constructor(private _zone: NgZone) {
super();
private routeSub: Subscription; // subscription to route observer

this.onPushTrackerStateChange();
constructor(
private _page: Page,
private _router: Router,
private _zone: NgZone
) {
super();
this.onPushTrackerStateChange(null);
this.register();
}

ngOnInit() {
BluetoothService.pushTrackerStatus.removeEventListener(
Observable.propertyChangeEvent,
this.onPushTrackerStateChange
);
BluetoothService.pushTrackerStatus.addEventListener(
Observable.propertyChangeEvent,
this.onPushTrackerStateChange,
this
);
/*
// see https://github.com/NativeScript/nativescript-angular/issues/1049
this.routeSub = this._router.events.subscribe(event => {
if (event instanceof NavigationStart) {
this.unregister();
}
});
this._page.on(Page.navigatingFromEvent, event => {
this.unregister();
});
*/
}

onPTConnTap(): void {
Expand All @@ -43,67 +56,23 @@ export class ActionbarComponent extends ActionBar {
onPushTrackerStateChange(args: any) {
this._zone.run(() => {
this.status = BluetoothService.pushTrackerStatus.get('state');
console.log('status', this.status);
//console.log('status', this.status);
});
}

/*
// Connectivity Events
onPushTrackerPaired() {
this.statusImage = '~/assets/images/pt-conn-grey.png'
}
onPushTrackerConnected() {
this.statusImage = '~/assets/images/pt-conn-yellow.png'
}
onPushTrackerDisconnected() {
this.statusImage = '~/assets/images/pt-conn-grey.png'
}
onPushTrackerData() {
this.statusImage = '~/assets/images/pt-conn-green.png'
}
unregister(): void {
console.log('actionbar: unregistering for events!');
BluetoothService.PushTrackers.off(ObservableArray.changeEvent);
BluetoothService.PushTrackers.map(pt => {
pt.off(PushTracker.pushtracker_paired_event);
pt.off(PushTracker.pushtracker_connect_event);
pt.off(PushTracker.pushtracker_disconnect_event);
pt.off(PushTracker.pushtracker_settings_event);
});
}
register(): void {
this.unregister();
// handle pushtracker pairing events for existing pushtrackers
console.log('actionbar: registering for events!');
BluetoothService.PushTrackers.map(pt => {
pt.on(PushTracker.pushtracker_paired_event, this.onPushTrackerPaired, this);
pt.on(PushTracker.pushtracker_connect_event, this.onPushTrackerConnected, this);
pt.on(PushTracker.pushtracker_disconnect_event, this.onPushTrackerDisconnected, this);
pt.on(PushTracker.pushtracker_version_event, this.onPushTrackerData, this);
});
unregister(): void {
BluetoothService.pushTrackerStatus.removeEventListener(
Observable.propertyChangeEvent,
this.onPushTrackerStateChange
);
}

// listen for completely new pusthrackers (that we haven't seen before)
BluetoothService.PushTrackers.on(
ObservableArray.changeEvent,
(args: ChangedData<number>) => {
if (args.action === 'add') {
const pt = BluetoothService.PushTrackers.getItem(
BluetoothService.PushTrackers.length - 1
);
if (pt) {
pt.on(PushTracker.pushtracker_paired_event, this.onPushTrackerPaired, this);
pt.on(PushTracker.pushtracker_connect_event, this.onPushTrackerConnected, this);
pt.on(PushTracker.pushtracker_disconnect_event, this.onPushTrackerDisconnected, this);
pt.on(PushTracker.pushtracker_version_event, this.onPushTrackerData, this);
}
}
}
);
}
*/
register(): void {
this.unregister();
BluetoothService.pushTrackerStatus.addEventListener(
Observable.propertyChangeEvent,
this.onPushTrackerStateChange,
this
);
}
}
21 changes: 13 additions & 8 deletions maxmobility/xplat/mobile/core/services/bluetooth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -701,21 +701,26 @@ export class BluetoothService {
}

private updatePushTrackerState(pt: PushTracker): void {
if (pt.version !== 0xff) {
BluetoothService.pushTrackerStatus.set('state', PushTrackerState.ready);
} else if (pt.connected) {
BluetoothService.pushTrackerStatus.set(
'state',
PushTrackerState.connected
);
} else if (pt.paired) {
if (pt && pt.connected) {
if (pt.version !== 0xff) {
BluetoothService.pushTrackerStatus.set('state', PushTrackerState.ready);
} else {
BluetoothService.pushTrackerStatus.set(
'state',
PushTrackerState.connected
);
}
} else if (pt && pt.paired) {
BluetoothService.pushTrackerStatus.set(
'state',
PushTrackerState.disconnected
);
} else if (pt) {
BluetoothService.pushTrackerStatus.set('state', PushTrackerState.unknown);
} else {
BluetoothService.pushTrackerStatus.set('state', PushTrackerState.unknown);
}
//console.log('update pt state!', BluetoothService.pushTrackerStatus.get('state'));
}

private getOrMakePushTracker(device: any): PushTracker {
Expand Down

0 comments on commit 46d2e24

Please sign in to comment.