tweet-actions.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { Component, Input, ChangeDetectorRef } from "@angular/core";
  2. import { TwitterApiProvider } from "../../providers/twitter-api/twitter-api";
  3. import { NavController } from "ionic-angular";
  4. import { WriteTweetPage } from "../../pages/write-tweet/write-tweet";
  5. import { P2pDatabaseGunProvider } from "../../providers/p2p-database-gun/p2p-database-gun";
  6. import { Vibration } from "@ionic-native/vibration";
  7. @Component({
  8. selector: "tweet-actions",
  9. templateUrl: "tweet-actions.html"
  10. })
  11. export class TweetActionsComponent {
  12. @Input()
  13. data: any[];
  14. privateFavoriteCount: number = 0;
  15. constructor(
  16. private twitter: TwitterApiProvider,
  17. private ref: ChangeDetectorRef,
  18. private navCtrl: NavController,
  19. private gun: P2pDatabaseGunProvider,
  20. private vibration: Vibration
  21. ) {}
  22. ngOnInit() {
  23. this.getPrivateLikes(this.id);
  24. }
  25. get favoriteCount() {
  26. if (this.data["retweeted_status"]) {
  27. return this.data["retweeted_status"]["favorite_count"];
  28. } else {
  29. return this.data["favorite_count"];
  30. }
  31. }
  32. get id() {
  33. if (this.data["retweeted_status"]) {
  34. return this.data["retweeted_status"]["id_str"];
  35. } else {
  36. return this.data["id_str"];
  37. }
  38. }
  39. private async getPrivateLikes(id: string) {
  40. const likeEntry = await this.gun.getLikes(this.id);
  41. if (likeEntry.likes > 0) {
  42. this.privateFavoriteCount = likeEntry.likes;
  43. this.ref.detectChanges();
  44. }
  45. }
  46. addPrivateLike(id: string) {
  47. this.vibration.vibrate([100, 50, 100]);
  48. this.gun.addLike(id).then(() => {
  49. this.privateFavoriteCount++;
  50. this.ref.detectChanges();
  51. });
  52. }
  53. toggleLike(id: string) {
  54. this.vibration.vibrate([100, 50, 100]);
  55. if (this.data["favorited"]) {
  56. this.removeLike(id);
  57. } else {
  58. this.like(id);
  59. }
  60. }
  61. private like(id: string): void {
  62. this.twitter.likeTweet(id).then(() => {
  63. this.data["favorited"] = true;
  64. this.data["favorite_count"]++;
  65. this.ref.detectChanges();
  66. });
  67. }
  68. private removeLike(id: string): void {
  69. this.twitter.unlikeTweet(id).then(() => {
  70. this.data["favorited"] = false;
  71. this.data["favorite_count"]--;
  72. this.ref.detectChanges();
  73. });
  74. }
  75. retweetStatus(id: string): void {
  76. this.navCtrl.push(WriteTweetPage, { tweetId: id });
  77. }
  78. replyToStatus(id: string): void {
  79. this.navCtrl.push(WriteTweetPage, { replyToStatus: id });
  80. }
  81. }