import { Component } from '@angular/core'; import { NavController, NavParams, PopoverController, ToastController } from 'ionic-angular'; import { UserListPage } from '../user-list/user-list'; import { ProfileMenu } from './profile-menu' import * as pnut from 'pnut-butter'; /** * Generated class for the ProfilePage page. * * See https://ionicframework.com/docs/components/#navigation for more info on * Ionic pages and navigation. */ @Component({ selector: 'page-profile', templateUrl: 'profile.html', }) export class ProfilePage { private user: any; private posts: Array = []; private bookmarks: Array = []; private before_id_post: string; private before_id_stars: string; private myUsername: string; public activeTab: string = 'posts'; constructor(public navCtrl: NavController, public navParams: NavParams, public popoverCtrl: PopoverController, public toastCtrl: ToastController) { this.user = this.navParams.data.user; this.myUsername = this.navParams.data.me; } ionViewDidLoad() { console.log('ionViewDidLoad ProfilePage'); let params = { include_deleted: 0, include_raw: 1, count: 40 }; pnut.postsFrom(this.user.id, params).then(res => { this.posts = this.parseData(res.data); this.before_id_post = res.meta.min_id; }); pnut.bookmarks(this.user.id, params).then(res => { this.bookmarks = this.parseData(res.data); this.before_id_stars = res.meta.min_id; }); } fetchOlderPosts(infiniteScroll, stream) { let before_id = this.before_id_post; let fetcher = pnut.postsFrom; if (stream === 'bookmarks') { before_id = this.before_id_stars; fetcher = pnut.bookmarks; } let params = { include_deleted: 0, include_raw: 1, before_id: before_id, count: 40 }; fetcher(this.user.id, params).then(res => { if (res.data.length > 0) { if (stream === 'posts') { this.posts.push.apply(this.posts, this.parseData(res.data)); this.before_id_post = res.meta.min_id; } else if (stream === 'bookmarks') { this.bookmarks.push.apply(this.bookmarks, this.parseData(res.data)); this.before_id_stars = res.meta.min_id; } } infiniteScroll.complete(); }).catch(err => { console.log(err); }); } parseData(data) { var pdata = []; for (var i = 0; i < data.length; i++) { if (!data[i].is_deleted) { if (data[i]['repost_of']) { data[i] = data[i]['repost_of'] var reposted_by_string = ""; let rplen = 0; if (typeof data[i]['reposted_by'] !== "undefined") { rplen = data[i]['reposted_by'].length; } for (var j = 0; j < rplen; j++) { reposted_by_string = reposted_by_string + data[i]['reposted_by'][j]['username'] + ", "; } } if (data[i].content) { for (var k = 0; k < data[i]['content']['entities']['mentions'].length; k++) { var men = data[i]['content']['entities']['mentions'][k]['text']; if (this.myUsername === men) { data[i]['you_are_mentioned'] = true; } } } pdata.push(data[i]); } } return pdata; } showUserList(list) { this.navCtrl.push(UserListPage, { userid: this.user.id, username: this.myUsername, list: list}); } followUser() { if (this.user.you_follow) { pnut.unfollow(this.user.id).then(res => { this.user = res.data; this.presentToast('User unfollowed'); }); } else { pnut.follow(this.user.id).then(res => { this.user = res.data; this.presentToast('User followed'); }); } } presentProfileMenu(myEvent) { let popover = this.popoverCtrl.create(ProfileMenu, { userid: this.user.id, username: this.user.username}); popover.present({ev: myEvent}); } presentToast(text) { let toast = this.toastCtrl.create({ position: 'top', message: text, duration: 2000 }); toast.present(); } }