tweet-actions.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. import { Storage } from "@ionic/storage";
  8. import { MockProvider } from "../../providers/mock-provider/mock-provider";
  9. @Component({
  10. selector: "tweet-actions",
  11. templateUrl: "tweet-actions.html"
  12. })
  13. export class TweetActionsComponent {
  14. @Input()
  15. data: any[];
  16. privateFavoriteCount: number = 0;
  17. constructor(
  18. private twitter: TwitterApiProvider,
  19. private storage: Storage,
  20. private ref: ChangeDetectorRef,
  21. private navCtrl: NavController,
  22. private gun: P2pDatabaseGunProvider,
  23. private vibration: Vibration,
  24. private mockProv: MockProvider
  25. ) {}
  26. ngOnInit() {
  27. console.log('ngOnInit calling getPrivateLikes ');
  28. this.getPrivateLikes(this.id);
  29. }
  30. get favoriteCount() {
  31. console.log('favoriteCount');
  32. if (this.data["retweeted_status"]) {
  33. return this.data["retweeted_status"]["favorite_count"];
  34. } else {
  35. return this.data["favorite_count"];
  36. }
  37. }
  38. get id() {
  39. if (this.data["retweeted_status"]) {
  40. return this.data["retweeted_status"]["id_str"];
  41. } else {
  42. console.log('id',this.data["id_str"]);
  43. return this.data["id_str"];
  44. }
  45. }
  46. private async getPrivateLikes(id: string) {
  47. console.log('getPrivateLikes',id);
  48. const likeEntry = await this.gun.getLikes(this.id);
  49. if (likeEntry.likes > 0) {
  50. this.privateFavoriteCount = likeEntry.likes;
  51. this.ref.detectChanges();
  52. }
  53. }
  54. addPrivateLike(id: string) {
  55. console.log('addPrivateLike',id);
  56. this.vibration.vibrate([100, 50, 100]);
  57. this.gun.addLike(id).then(() => {
  58. this.privateFavoriteCount++;
  59. this.ref.detectChanges();
  60. });
  61. }
  62. toggleLike(id: string) {
  63. console.log('toggleLike: ',id);
  64. this.vibration.vibrate([100, 50, 100]);
  65. if (this.data["favorited"]) {
  66. this.removeLike(id);
  67. } else {
  68. this.like(id);
  69. }
  70. }
  71. private like(id: string): void {
  72. console.log('like : ', id);
  73. if(this.storage.get("mockup")){
  74. console.log('like tweet in mock');
  75. this.mockProv.likeTweet(id);
  76. }
  77. else{
  78. this.twitter.likeTweet(id).then(() => {
  79. this.data["favorited"] = true;
  80. this.data["favorite_count"]++;
  81. this.ref.detectChanges();
  82. });
  83. }
  84. }
  85. private removeLike(id: string): void {
  86. console.log('removeLike:',id);
  87. if(this.storage.get("mockup")){
  88. console.log('unlike tweet in mock');
  89. this.mockProv.unlikeTweet(id);
  90. }
  91. else{
  92. this.twitter.unlikeTweet(id).then(() => {
  93. this.data["favorited"] = false;
  94. this.data["favorite_count"]--;
  95. this.ref.detectChanges();
  96. });
  97. }
  98. }
  99. retweetStatus(id: string): void {
  100. if(this.storage.get("mockup")){
  101. console.log('Retweeting status');
  102. this.navCtrl.push(WriteTweetPage, { tweetId: id });
  103. }
  104. else{
  105. console.log('retweetStatus');
  106. this.navCtrl.push(WriteTweetPage, { tweetId: id });
  107. }
  108. }
  109. replyToStatus(id: string): void {
  110. console.log('replyToStatus');
  111. this.navCtrl.push(WriteTweetPage, { replyToStatus: id });
  112. }
  113. }