Skip to content

Commit b0c2fd9

Browse files
dotansimhadarkbasic
authored andcommitted
Step 13.26: Implement pick, update and set of profile image
1 parent be2cf51 commit b0c2fd9

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

src/pages/profile/profile.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { Component, OnInit } from '@angular/core';
22
import { Profile } from 'api/models';
3-
import { AlertController, NavController } from 'ionic-angular';
3+
import { AlertController, NavController, Platform } from 'ionic-angular';
44
import { MeteorObservable } from 'meteor-rxjs';
55
import { ChatsPage } from '../chats/chats';
6+
import { PictureService } from '../../services/picture';
7+
import { Pictures } from 'api/collections';
68

79
@Component({
810
selector: 'profile',
@@ -14,13 +16,42 @@ export class ProfilePage implements OnInit {
1416

1517
constructor(
1618
private alertCtrl: AlertController,
17-
private navCtrl: NavController
19+
private navCtrl: NavController,
20+
private pictureService: PictureService,
21+
private platform: Platform
1822
) {}
1923

2024
ngOnInit(): void {
2125
this.profile = Meteor.user().profile || {
2226
name: ''
2327
};
28+
29+
MeteorObservable.subscribe('user').subscribe(() => {
30+
let platform = this.platform.is('android') ? "android" :
31+
this.platform.is('ios') ? "ios" : "";
32+
platform = this.platform.is('cordova') ? platform : "";
33+
34+
this.picture = Pictures.getPictureUrl(this.profile.pictureId, platform);
35+
});
36+
}
37+
38+
selectProfilePicture(): void {
39+
this.pictureService.select().then((blob) => {
40+
this.uploadProfilePicture(blob);
41+
})
42+
.catch((e) => {
43+
this.handleError(e);
44+
});
45+
}
46+
47+
uploadProfilePicture(blob: File): void {
48+
this.pictureService.upload(blob).then((picture) => {
49+
this.profile.pictureId = picture._id;
50+
this.picture = picture.url;
51+
})
52+
.catch((e) => {
53+
this.handleError(e);
54+
});
2455
}
2556

2657
updateProfile(): void {

0 commit comments

Comments
 (0)