tweet-actions.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. this.privateFavoriteCount = likeEntry.likes;
  42. this.ref.detectChanges();
  43. }
  44. addPrivateLike(id: string) {
  45. this.vibration.vibrate([100, 50, 100]);
  46. console.log(id);
  47. this.gun.addLike(id).then(() => {
  48. this.privateFavoriteCount++;
  49. this.ref.detectChanges();
  50. });
  51. }
  52. toggleLike(id: string) {
  53. this.vibration.vibrate([100, 50, 100]);
  54. if (this.data["favorited"]) {
  55. this.removeLike(id);
  56. } else {
  57. this.like(id);
  58. }
  59. }
  60. private like(id: string): void {
  61. this.twitter.likeTweet(id).then(() => {
  62. this.data["favorited"] = true;
  63. this.data["favorite_count"]++;
  64. this.ref.detectChanges();
  65. });
  66. }
  67. private removeLike(id: string): void {
  68. this.twitter.unlikeTweet(id).then(() => {
  69. this.data["favorited"] = false;
  70. this.data["favorite_count"]--;
  71. this.ref.detectChanges();
  72. });
  73. }
  74. retweetStatus(id: string): void {
  75. this.navCtrl.push(WriteTweetPage, { tweetId: id });
  76. }
  77. replyToStatus(id: string): void {
  78. this.navCtrl.push(WriteTweetPage, { replyToStatus: id });
  79. }
  80. }