profile.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { Component, ViewChild } from "@angular/core";
  2. import {
  3. IonicPage,
  4. NavController,
  5. NavParams,
  6. InfiniteScroll,
  7. Content,
  8. LoadingController
  9. } from "ionic-angular";
  10. import { TwitterApiProvider } from "../../providers/twitter-api/twitter-api";
  11. import { FeedProvider } from "../../providers/feed/feed";
  12. @IonicPage()
  13. @Component({
  14. selector: "page-profile",
  15. templateUrl: "profile.html"
  16. })
  17. export class ProfilePage {
  18. user: any = [];
  19. tweets: any[];
  20. oldestLoadedTweetId;
  21. enableRefresh: boolean = true;
  22. enableInfiniteScroll: boolean = true;
  23. @ViewChild(Content)
  24. content: Content;
  25. constructor(
  26. public navCtrl: NavController,
  27. private loadingCtrl: LoadingController,
  28. private navParams: NavParams,
  29. private twitter: TwitterApiProvider,
  30. private feed: FeedProvider
  31. ) {}
  32. ionViewDidLoad() {
  33. // Show loading indicator
  34. const loading = this.loadingCtrl.create();
  35. loading.present();
  36. // Read user id
  37. const userId = this.navParams.get("userId");
  38. // Fetch user details from Twitter
  39. this.twitter.fetchUser(userId).then(res => (this.user = res));
  40. // Load user's timeline from Twitter and P2P
  41. this.feed.loadUserTimeline(userId).then(res => {
  42. if (res.length > 0) {
  43. // Store tweets
  44. this.tweets = res;
  45. } else {
  46. this.enableInfiniteScroll = false;
  47. }
  48. // Hide loading indicator
  49. loading.dismiss();
  50. });
  51. }
  52. doRefresh(refresher) {
  53. this.feed.loadUserTimeline(this.user.id_str).then(res => {
  54. if (res.length > 0) {
  55. // Replace tweets
  56. this.tweets = res;
  57. }
  58. // Hide loading icon
  59. refresher.complete();
  60. });
  61. }
  62. loadMore(infiniteScroll: InfiniteScroll) {
  63. if (this.enableInfiniteScroll) {
  64. this.feed
  65. .loadUserTimeline(
  66. this.user.id_str,
  67. this.oldestPublicTweet,
  68. this.oldestPrivateTweet
  69. )
  70. .then(res => {
  71. if (res.length > 0) {
  72. // Append loaded tweets
  73. this.tweets = this.tweets.concat(res);
  74. } else {
  75. this.enableInfiniteScroll = false;
  76. }
  77. // Hide loading icon
  78. infiniteScroll.complete();
  79. });
  80. } else {
  81. // Hide loading icon
  82. infiniteScroll.complete();
  83. }
  84. }
  85. get publicTweets() {
  86. return this.tweets.filter(tweet => !tweet.private_tweet);
  87. }
  88. get privateTweets() {
  89. return this.tweets.filter(tweet => tweet.private_tweet);
  90. }
  91. get oldestPublicTweet() {
  92. if (this.publicTweets.length > 0) {
  93. return this.publicTweets.reduce(
  94. (acc, cur) => (acc.id < cur.id ? acc : cur)
  95. );
  96. } else {
  97. return undefined;
  98. }
  99. }
  100. get oldestPrivateTweet() {
  101. if (this.privateTweets.length > 0) {
  102. return this.privateTweets.reduce(
  103. (acc, cur) =>
  104. new Date(acc.created_at) < new Date(cur.created_at) ? acc : cur
  105. );
  106. } else {
  107. return undefined;
  108. }
  109. }
  110. onScroll(event) {
  111. this.enableRefresh = event.scrollTop === 0;
  112. }
  113. }