Skip to content

Commit

Permalink
Step 15.5: Use getContactsFromAddressbook in new-chat.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
darkbasic committed Jun 15, 2017
1 parent 56fc0d3 commit c7b393b
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/pages/chats/new-chat.ts
Expand Up @@ -5,6 +5,7 @@ import { AlertController, Platform, ViewController } from 'ionic-angular';
import { MeteorObservable } from 'meteor-rxjs';
import { _ } from 'meteor/underscore';
import { Observable, Subscription, BehaviorSubject } from 'rxjs';
import { PhoneService } from "../../services/phone";

@Component({
selector: 'new-chat',
Expand All @@ -15,18 +16,28 @@ export class NewChatComponent implements OnInit {
senderId: string;
users: Observable<User[]>;
usersSubscription: Subscription;
contacts: string[] = [];
contactsPromise: Promise<void>;

constructor(
private alertCtrl: AlertController,
private viewCtrl: ViewController,
private platform: Platform
private platform: Platform,
private phoneService: PhoneService
) {
this.senderId = Meteor.userId();
this.searchPattern = new BehaviorSubject(undefined);
}

ngOnInit() {
this.observeSearchBar();
this.contactsPromise = this.phoneService.getContactsFromAddressbook()
.then((phoneNumbers: string[]) => {
this.contacts = phoneNumbers;
})
.catch((e: Error) => {
console.error(e.message);
});
}

updateSubscription(newValue) {
Expand All @@ -42,7 +53,9 @@ export class NewChatComponent implements OnInit {
this.usersSubscription.unsubscribe();
}

this.usersSubscription = this.subscribeUsers();
this.contactsPromise.then(() => {
this.usersSubscription = this.subscribeUsers();
});
});
}

Expand All @@ -61,7 +74,7 @@ export class NewChatComponent implements OnInit {

subscribeUsers(): Subscription {
// Fetch all users matching search pattern
const subscription = MeteorObservable.subscribe('users', this.searchPattern.getValue());
const subscription = MeteorObservable.subscribe('users', this.searchPattern.getValue(), this.contacts);
const autorun = MeteorObservable.autorun();

return Observable.merge(subscription, autorun).subscribe(() => {
Expand Down

0 comments on commit c7b393b

Please sign in to comment.