mock-provider.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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, replyid) {
  56. console.log('inside writetweet',retwt,replyid);
  57. // console.log('inside writetweet obj is:',this.mockTweets[0].full_text);
  58. let newTweet = Object.assign({}, this.mockTweets[0]);
  59. newTweet.full_text = twt;
  60. newTweet.id = Math.floor(100000 + Math.random() * 900000);
  61. newTweet.id_str = newTweet.id ;
  62. newTweet.full_text = twt;
  63. newTweet.retweet = retwt;
  64. newTweet.replyToStatusId = replyid;
  65. if(isPrivate){
  66. newTweet.private_tweet = true;
  67. }
  68. console.log('new tweet ', newTweet);
  69. this.mockTweets.push(newTweet);
  70. this.writefile(this.mockTweets);
  71. }
  72. public async writefile(mocktweet) {
  73. await this.platform.ready()
  74. .then(_ => console.log('Device ready.'));
  75. //check if file exists
  76. this.result = this.file.writeFile(this.file.dataDirectory, 'twets.json', JSON.stringify(mocktweet), { replace: true, append: false });
  77. console.log("writing the file:", this.result);
  78. console.log('Reading the file:', this.readFile());
  79. }
  80. public async readFile() {
  81. var readTweets: [];
  82. console.log('in readfile', this.mockTweets);
  83. this.file.checkFile(this.file.dataDirectory, 'twets.json')
  84. .then(() => {
  85. console.log('file exists');
  86. this.file.readAsText(this.file.dataDirectory, 'twets.json')
  87. .then(data => {
  88. readTweets = JSON.parse(data);
  89. if (!this.fileRead) {
  90. // readTweets.forEach(el=>this.mockTweets.push(el));
  91. this.mockTweets = [...readTweets];
  92. this.fileRead = true;
  93. }
  94. console.log('file is: ', this.mockTweets);
  95. })
  96. .catch(err => console.error('error in reading file', err));
  97. })
  98. .catch(err => {
  99. console.error('File doesnt exist', err);
  100. this.file.writeFile(this.file.dataDirectory, 'twets.json', JSON.stringify(this.mockTweets), { replace: true });
  101. });
  102. }
  103. public likeTweet(id){
  104. console.log('fetch tweet id:',id , this.mockTweets);
  105. const result = this.mockTweets.find(x=>x.id_str == id);
  106. // this.mockTweets.forEach(el=>{
  107. // console.log("mocktweet el is:",el);
  108. // })
  109. result.favorited = true;
  110. result.favorite_count++;
  111. console.log('tweet found and modified:',result);
  112. return result;
  113. }
  114. public unlikeTweet(id){
  115. console.log('fetch tweet id:',id , this.mockTweets);
  116. const result = this.mockTweets.find(x=>x.id_str == id);
  117. // this.mockTweets.forEach(el=>{
  118. // console.log("mocktweet el is:",el);
  119. // })
  120. result.favorited = false;
  121. result.favorite_count--;
  122. console.log('tweet found and modified:',result);
  123. return result;
  124. }
  125. private async fetchPrivateTweets(privateTweetsData: object[]) {
  126. }
  127. }