import { Component, Input, ChangeDetectorRef } from "@angular/core"; import { TwitterApiProvider } from "../../providers/twitter-api/twitter-api"; import { NavController } from "ionic-angular"; import { WriteTweetPage } from "../../pages/write-tweet/write-tweet"; import { P2pDatabaseGunProvider } from "../../providers/p2p-database-gun/p2p-database-gun"; import { Vibration } from "@ionic-native/vibration"; import { Storage } from "@ionic/storage"; import { MockProvider } from "../../providers/mock-provider/mock-provider"; @Component({ selector: "tweet-actions", templateUrl: "tweet-actions.html" }) export class TweetActionsComponent { @Input() data: any[]; privateFavoriteCount: number = 0; constructor( private twitter: TwitterApiProvider, private storage: Storage, private ref: ChangeDetectorRef, private navCtrl: NavController, private gun: P2pDatabaseGunProvider, private vibration: Vibration, private mockProv: MockProvider ) {} ngOnInit() { console.log('ngOnInit calling getPrivateLikes '); this.getPrivateLikes(this.id); } get favoriteCount() { console.log('favoriteCount'); if (this.data["retweeted_status"]) { return this.data["retweeted_status"]["favorite_count"]; } else { return this.data["favorite_count"]; } } get id() { if (this.data["retweeted_status"]) { return this.data["retweeted_status"]["id_str"]; } else { console.log('id',this.data["id_str"]); return this.data["id_str"]; } } private async getPrivateLikes(id: string) { console.log('getPrivateLikes',id); const likeEntry = await this.gun.getLikes(this.id); if (likeEntry.likes > 0) { this.privateFavoriteCount = likeEntry.likes; this.ref.detectChanges(); } } addPrivateLike(id: string) { console.log('addPrivateLike',id); this.vibration.vibrate([100, 50, 100]); this.gun.addLike(id).then(() => { this.privateFavoriteCount++; this.ref.detectChanges(); }); } toggleLike(id: string) { console.log('toggleLike: ',id); this.vibration.vibrate([100, 50, 100]); if (this.data["favorited"]) { this.removeLike(id); } else { this.like(id); } } private like(id: string): void { console.log('like : ', id); if(this.storage.get("mockup")){ console.log('like tweet in mock'); this.mockProv.likeTweet(id); } else{ this.twitter.likeTweet(id).then(() => { this.data["favorited"] = true; this.data["favorite_count"]++; this.ref.detectChanges(); }); } } private removeLike(id: string): void { console.log('removeLike:',id); if(this.storage.get("mockup")){ console.log('unlike tweet in mock'); this.mockProv.unlikeTweet(id); } else{ this.twitter.unlikeTweet(id).then(() => { this.data["favorited"] = false; this.data["favorite_count"]--; this.ref.detectChanges(); }); } } retweetStatus(id: string): void { if(this.storage.get("mockup")){ console.log('Retweeting status'); this.navCtrl.push(WriteTweetPage, { tweetId: id }); } else{ console.log('retweetStatus'); this.navCtrl.push(WriteTweetPage, { tweetId: id }); } } replyToStatus(id: string): void { console.log('replyToStatus'); this.navCtrl.push(WriteTweetPage, { replyToStatus: id }); } }