home.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { Component } from "@angular/core";
  2. import {
  3. IonicPage,
  4. NavController,
  5. MenuController,
  6. InfiniteScroll,
  7. Refresher,
  8. LoadingController
  9. } from "ionic-angular";
  10. import { WriteTweetPage } from "../write-tweet/write-tweet";
  11. import { FeedProvider } from "../../providers/feed/feed";
  12. @IonicPage()
  13. @Component({
  14. selector: "page-home",
  15. templateUrl: "home.html"
  16. })
  17. export class HomePage {
  18. menuController: MenuController;
  19. tweets;
  20. constructor(
  21. public navCtrl: NavController,
  22. private menuCtrl: MenuController,
  23. private feed: FeedProvider,
  24. private loadingCtrl: LoadingController
  25. ) {}
  26. ionViewDidLoad() {
  27. this.menuCtrl.enable(true, "sideNav");
  28. }
  29. ionViewDidEnter() {
  30. const loading = this.loadingCtrl.create();
  31. loading.present();
  32. this.feed
  33. .loadHomeTimeline()
  34. .then(tweets => (this.tweets = tweets))
  35. .catch(err => console.error(err))
  36. .then(() => loading.dismiss());
  37. }
  38. doRefresh(refresher: Refresher) {
  39. this.feed.loadHomeTimeline().then(tweets => {
  40. this.tweets = tweets;
  41. refresher.complete();
  42. });
  43. }
  44. loadMore(infiniteScroll: InfiniteScroll) {
  45. this.feed
  46. .loadHomeTimeline(this.oldestPublicTweet, this.oldestPrivateTweet)
  47. .then(tweets => {
  48. this.tweets = this.tweets.concat(tweets);
  49. infiniteScroll.complete();
  50. });
  51. }
  52. writeTweet() {
  53. this.navCtrl.push(WriteTweetPage);
  54. }
  55. get publicTweets() {
  56. return this.tweets.filter(tweet => !tweet.private_tweet);
  57. }
  58. get privateTweets() {
  59. return this.tweets.filter(tweet => tweet.private_tweet);
  60. }
  61. get oldestPublicTweet() {
  62. if (this.publicTweets.length > 0) {
  63. return this.publicTweets.reduce((acc, cur) =>
  64. acc.id < cur.id ? acc : cur
  65. );
  66. } else {
  67. return undefined;
  68. }
  69. }
  70. get oldestPrivateTweet() {
  71. if (this.privateTweets.length > 0) {
  72. return this.privateTweets.reduce((acc, cur) =>
  73. new Date(acc.created_at) < new Date(cur.created_at) ? acc : cur
  74. );
  75. } else {
  76. return undefined;
  77. }
  78. }
  79. }