fetching newer and older posts

This commit is contained in:
Morgan McMillian 2017-06-10 22:43:38 -07:00
parent 9cc1d4b981
commit 204b8e490d
4 changed files with 68 additions and 23 deletions

View file

@ -1,7 +1,7 @@
import { Component } from '@angular/core'; import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular'; import { NavController, NavParams } from 'ionic-angular';
import { Storage } from '@ionic/storage'; import { Storage } from '@ionic/storage';
import { HomePage } from '../home/home'; import { StreamPage } from '../stream/stream';
import { OauthCordova } from 'ng2-cordova-oauth/platform/cordova'; import { OauthCordova } from 'ng2-cordova-oauth/platform/cordova';
// import { OauthBrowser } from 'ng2-cordova-oauth/platform/browser'; // import { OauthBrowser } from 'ng2-cordova-oauth/platform/browser';
@ -35,7 +35,7 @@ export class LoginPage {
console.log('RESULT: ' + JSON.stringify(success)); console.log('RESULT: ' + JSON.stringify(success));
this.storage.set('token', success['access_token']); this.storage.set('token', success['access_token']);
pnut.token = success['access_token']; pnut.token = success['access_token'];
this.navCtrl.setRoot(HomePage); this.navCtrl.setRoot(StreamPage, {stream: 'global'});
}, error=> { }, error=> {
console.log('ERROR: ' + error); console.log('ERROR: ' + error);
}); });

View file

@ -1,5 +1,5 @@
import { Component } from '@angular/core'; import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular'; import { NavController, NavParams } from 'ionic-angular';
/** /**
* Generated class for the PostDetailsPage page. * Generated class for the PostDetailsPage page.

View file

@ -18,6 +18,10 @@
<ion-content> <ion-content>
<ion-refresher (ionRefresh)="fetchNewerPosts($event)">
<ion-refresher-content></ion-refresher-content>
</ion-refresher>
<ion-list> <ion-list>
<ion-card *ngFor="let post of posts"> <ion-card *ngFor="let post of posts">
<ion-item> <ion-item>
@ -48,7 +52,7 @@
</button> </button>
</ion-col> </ion-col>
<ion-col> <ion-col>
<button ion-button icon-left clear small block> <button ion-button icon-left clear small block (click)="showQuotedPost(post)">
<ion-icon name="quote"></ion-icon> <ion-icon name="quote"></ion-icon>
</button> </button>
</ion-col> </ion-col>
@ -70,15 +74,19 @@
<div *ngIf="post.counts.replies > 0">{{ post.counts.replies }}</div> <div *ngIf="post.counts.replies > 0">{{ post.counts.replies }}</div>
</button> </button>
</ion-col> </ion-col>
<ion-col> <!-- <ion-col>
<button ion-button icon-left clear small block> <button ion-button icon-left clear small block>
<ion-icon name="more"></ion-icon> <ion-icon name="more"></ion-icon>
</button> </button>
</ion-col> </ion-col> -->
</ion-row> </ion-row>
</ion-card> </ion-card>
</ion-list> </ion-list>
<ion-infinite-scroll (ionInfinite)="fetchOlderPosts($event)">
<ion-infinite-scroll-content></ion-infinite-scroll-content>
</ion-infinite-scroll>
<ion-fab right bottom> <ion-fab right bottom>
<button ion-fab (click)="showNewPost()"> <button ion-fab (click)="showNewPost()">
<ion-icon name="add"></ion-icon> <ion-icon name="add"></ion-icon>

View file

@ -1,5 +1,5 @@
import { Component } from '@angular/core'; import { Component } from '@angular/core';
import { ViewController, IonicPage, NavController, NavParams, ModalController } from 'ionic-angular'; import { ViewController, NavController, NavParams, ModalController } from 'ionic-angular';
import { PostDetailsPage } from '../post-details/post-details'; import { PostDetailsPage } from '../post-details/post-details';
import * as pnut from 'pnut-butter'; import * as pnut from 'pnut-butter';
@ -17,37 +17,63 @@ import * as pnut from 'pnut-butter';
export class StreamPage { export class StreamPage {
title: string; title: string;
posts: Array<Object> = [] posts: Array<Object> = [];
since_id: string;
before_id: string;
fetcher: any;
constructor(public navCtrl: NavController, public navParams: NavParams, public modalCtrl: ModalController) { constructor(public navCtrl: NavController, public navParams: NavParams, public modalCtrl: ModalController) {
// console.log(JSON.stringify(navParams)); // console.log(JSON.stringify(navParams));
switch (navParams.data.stream) { switch (navParams.data.stream) {
case 'global': case 'global':
this.title = 'Global'; this.title = 'Global';
this.fetchGlobal(); this.fetcher = pnut.global;
this.fetchPosts();
break; break;
case 'personal': case 'personal':
this.title = 'Home'; this.title = 'Home';
this.fetchPersonal(); this.fetcher = pnut.personal;
this.fetchPosts();
break; break;
} }
} }
fetchGlobal() { fetchOlderPosts(infiniteScroll) {
console.log('-- fetching global stream --'); this.fetcher({include_raw: 1, before_id: this.before_id, count: 40}).then(res => {
pnut.global({include_raw: 1, count: 50}).then(res => { if (res.data.length > 0) {
// console.log(res); this.posts.push.apply(this.posts, res.data);
this.posts = res.data; this.before_id = res.meta.min_id;
// console.log(this.posts); }
console.log('since_id: ' + this.since_id);
console.log('before_id: ' + this.before_id);
infiniteScroll.complete();
}).catch(err => { }).catch(err => {
console.log(err); console.log(err);
}); });
} }
fetchPersonal() { fetchNewerPosts(refresher) {
console.log('-- fetching personal stream --'); this.fetcher({include_raw: 1, since_id: this.since_id, count: 40}).then(res => {
pnut.personal().then(res => { if (res.data.length > 0) {
Array.prototype.unshift.apply(this.posts, res.data);
this.since_id = res.meta.max_id;
}
console.log('since_id: ' + this.since_id);
console.log('before_id: ' + this.before_id);
refresher.complete();
}).catch(err => {
console.log(err);
});
}
fetchPosts() {
console.log('-- fetching global stream --');
this.fetcher({include_raw: 1, count: 40}).then(res => {
this.posts = res.data; this.posts = res.data;
this.since_id = res.meta.max_id;
this.before_id = res.meta.min_id;
console.log('since_id: ' + this.since_id);
console.log('before_id: ' + this.before_id);
}).catch(err => { }).catch(err => {
console.log(err); console.log(err);
}); });
@ -112,11 +138,16 @@ export class StreamPage {
} }
showReplyPost(postData) { showReplyPost(postData) {
console.log(postData);
let newPostModal = this.modalCtrl.create(NewPostModal, {type: 'reply', post: postData}); let newPostModal = this.modalCtrl.create(NewPostModal, {type: 'reply', post: postData});
newPostModal.present(); newPostModal.present();
} }
showQuotedPost(postData) {
console.log(postData);
let newPostModal = this.modalCtrl.create(NewPostModal, {type: 'quote', post: postData});
newPostModal.present();
}
} }
@Component({ @Component({
@ -131,7 +162,7 @@ export class StreamPage {
</button> </button>
</ion-buttons> </ion-buttons>
<ion-title>New Post</ion-title> <ion-title>{{ title }}</ion-title>
</ion-toolbar> </ion-toolbar>
</ion-header> </ion-header>
@ -153,6 +184,7 @@ export class StreamPage {
` `
}) })
export class NewPostModal { export class NewPostModal {
title: string;
replyid: string; replyid: string;
ptext: string; ptext: string;
options: Object; options: Object;
@ -168,8 +200,13 @@ export class NewPostModal {
this.ptext = this.ptext + this.parseMentions(navParams.data.post.content.entities.mentions); this.ptext = this.ptext + this.parseMentions(navParams.data.post.content.entities.mentions);
} }
} }
this.title = "Reply to " + navParams.data.post.user.username
} else if (navParams.data.type === 'quote') {
this.ptext = " >> @" + navParams.data.post.user.username + ": " + navParams.data.post.content.text;
this.title = "New Post";
} else {
this.title = "New Post";
} }
console.log('replying to ' + this.replyid);
} }
dismiss() { dismiss() {