profile.ts 2.9 KB

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