search-results-tweets-popular.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { Component } from "@angular/core";
  2. import {
  3. IonicPage,
  4. NavController,
  5. NavParams,
  6. Refresher,
  7. InfiniteScroll,
  8. Events
  9. } from "ionic-angular";
  10. import { TwitterApiProvider } from "../../providers/twitter-api/twitter-api";
  11. @IonicPage()
  12. @Component({
  13. selector: "page-search-results-tweets-popular",
  14. templateUrl: "search-results-tweets-popular.html"
  15. })
  16. export class SearchResultsTweetsPopularPage {
  17. query: string;
  18. tweets = [];
  19. constructor(
  20. public navCtrl: NavController,
  21. public navParams: NavParams,
  22. private twitter: TwitterApiProvider,
  23. private events: Events
  24. ) {
  25. this.query = this.navParams.data;
  26. this.events.subscribe("query:changed", query => {
  27. if (query.length) {
  28. this.twitter
  29. .searchPopularTweets(query)
  30. .then(res => (this.tweets = res));
  31. this.query = query;
  32. }
  33. });
  34. }
  35. async ionViewDidLoad() {
  36. if (this.query.length) {
  37. this.tweets = await this.twitter.searchPopularTweets(this.query);
  38. }
  39. }
  40. doRefresh(refresher: Refresher) {
  41. this.twitter.searchPopularTweets(this.query).then(tweets => {
  42. this.tweets = tweets;
  43. refresher.complete();
  44. });
  45. }
  46. loadMore(infiniteScroll: InfiniteScroll) {
  47. this.twitter
  48. .searchPopularTweets(this.query, this.oldestTweet)
  49. .then(tweets => {
  50. this.tweets["statuses"] = this.tweets["statuses"].concat(
  51. tweets["statuses"]
  52. );
  53. infiniteScroll.complete();
  54. });
  55. }
  56. get oldestTweet() {
  57. if (this.tweets.length > 0) {
  58. return this.tweets.reduce((acc, cur) => (acc.id < cur.id ? acc : cur))[
  59. "id_str"
  60. ];
  61. } else {
  62. return undefined;
  63. }
  64. }
  65. get enableRefresh() {
  66. return this.query.length > 0;
  67. }
  68. get enableInfiniteScroll() {
  69. return this.query.length > 0;
  70. }
  71. }