mock-provider.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { Injectable } from "@angular/core";
  2. import { Storage } from "@ionic/storage";
  3. import { Platform } from 'ionic-angular';
  4. import { File } from '@ionic-native/file';
  5. import { HttpClient } from "@angular/common/http";
  6. import * as tweets from './tweet.json';
  7. declare var cordova: any;
  8. @Injectable()
  9. export class MockProvider {
  10. friends;
  11. userId: string;
  12. mockTweets;
  13. result;
  14. fileRead: Boolean;
  15. constructor(
  16. private storage: Storage,
  17. private file: File,
  18. public http: HttpClient,
  19. private platform: Platform
  20. ) {
  21. console.log('tweets are:', tweets);
  22. this.fileRead = false;
  23. this.mockTweets = [...tweets];
  24. console.log(' this.mockTweets are:', this.mockTweets);
  25. this.storage.get("userId").then(userId => (this.userId = userId));
  26. }
  27. /**
  28. * Retrives the public and private tweets for a user
  29. * Since it is loaded in batches of 20 public tweets, public and private tweet are used as reference to load next 20 tweets
  30. * @param userId user id
  31. * @param oldestPublicTweet oldest public tweet
  32. * @param oldestPrivateTweet oldest private tweet
  33. */
  34. public async loadUserTimeline(
  35. userId,
  36. oldestPublicTweet ? ,
  37. oldestPrivateTweet ?
  38. ) {
  39. }
  40. /**
  41. * Retrieves the home feed for the logged in user
  42. * Since it is loaded in batches of 20 public tweets, public and private tweet are used as reference to load next 20 tweets
  43. * @param oldestPublicTweet oldest public tweet
  44. * @param oldestPrivateTweet oldest private tweet
  45. */
  46. public async loadHomeTimeline(oldestPublicTweet ? , oldestPrivateTweet ? ) {
  47. console.log('hometime line called in mockprovider');
  48. this.readFile().then(tweets => {
  49. console.log('new mocktweets after replace', this.mockTweets);
  50. })
  51. .catch(err => console.error('error in readfile', err));
  52. return this.mockTweets;
  53. }
  54. public async replyTweet() {}
  55. public writeTweet(twt: string, isPrivate: boolean, retwt, reply) {
  56. console.log('inside writetweet', retwt, reply);
  57. let newTweet = Object.assign({}, this.mockTweets[0]);
  58. newTweet.full_text = twt;
  59. newTweet.id = Math.floor(100000 + Math.random() * 900000);
  60. newTweet.id_str = newTweet.id;
  61. newTweet.full_text = twt;
  62. if(retwt){
  63. newTweet.is_quote_status = true;
  64. newTweet.quoted_status = retwt;
  65. newTweet.quoted_status_id = retwt.id;
  66. newTweet.quoted_status_id_str = retwt.id_str;
  67. }
  68. if(reply){
  69. newTweet.in_reply_to_screen_name=reply.user.name;
  70. newTweet.in_reply_to_status_id = reply.id;
  71. newTweet.in_reply_to_status_id_str = reply.id_str;
  72. newTweet.in_reply_to_user_id = reply.user.id;
  73. newTweet.in_reply_to_user_id_str = reply.user.id_str;
  74. }
  75. if (isPrivate) {
  76. newTweet.private_tweet = true;
  77. }
  78. console.log('new tweet ', newTweet);
  79. this.mockTweets.push(newTweet);
  80. this.writefile(this.mockTweets);
  81. }
  82. public async writefile(mocktweet) {
  83. await this.platform.ready()
  84. .then(_ => console.log('Device ready.'));
  85. //check if file exists
  86. this.result = this.file.writeFile(this.file.dataDirectory, 'twets.json', JSON.stringify(mocktweet), { replace: true, append: false });
  87. console.log("writing the file:", this.result);
  88. console.log('Reading the file:', this.readFile());
  89. }
  90. public async readFile() {
  91. var readTweets: [];
  92. console.log('in readfile', this.mockTweets);
  93. this.file.checkFile(this.file.dataDirectory, 'twets.json')
  94. .then(() => {
  95. console.log('file exists');
  96. this.file.readAsText(this.file.dataDirectory, 'twets.json')
  97. .then(data => {
  98. readTweets = JSON.parse(data);
  99. if (!this.fileRead) {
  100. // readTweets.forEach(el=>this.mockTweets.push(el));
  101. this.mockTweets = [...readTweets];
  102. this.fileRead = true;
  103. }
  104. console.log('file is: ', this.mockTweets);
  105. })
  106. .catch(err => console.error('error in reading file', err));
  107. })
  108. .catch(err => {
  109. console.error('File doesnt exist', err);
  110. this.file.writeFile(this.file.dataDirectory, 'twets.json', JSON.stringify(this.mockTweets), { replace: true });
  111. });
  112. }
  113. public likeTweet(id) {
  114. console.log('fetch tweet id:', id, this.mockTweets);
  115. const result = this.mockTweets.find(x => x.id_str == id);
  116. // this.mockTweets.forEach(el=>{
  117. // console.log("mocktweet el is:",el);
  118. // })
  119. result.favorited = true;
  120. result.favorite_count++;
  121. console.log('tweet found and modified:', result);
  122. return result;
  123. }
  124. public unlikeTweet(id) {
  125. console.log('fetch tweet id:', id, this.mockTweets);
  126. const result = this.mockTweets.find(x => x.id_str == id);
  127. // this.mockTweets.forEach(el=>{
  128. // console.log("mocktweet el is:",el);
  129. // })
  130. result.favorited = false;
  131. result.favorite_count--;
  132. console.log('tweet found and modified:', result);
  133. return result;
  134. }
  135. public fetchTweet(tweetID) {
  136. return this.mockTweets.find(x => x.id_str == tweetID);
  137. }
  138. private async fetchPrivateTweets(privateTweetsData: object[]) {
  139. }
  140. }