mock-provider.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. /**
  42. * Retrieves the home feed for the logged in user
  43. * Since it is loaded in batches of 20 public tweets, public and private tweet are used as reference to load next 20 tweets
  44. * @param oldestPublicTweet oldest public tweet
  45. * @param oldestPrivateTweet oldest private tweet
  46. */
  47. public async loadHomeTimeline(oldestPublicTweet?, oldestPrivateTweet?) {
  48. console.log('hometime line called in mockprovider');
  49. this.readFile().then(tweets => {
  50. console.log('new mocktweets after replace',this.mockTweets);
  51. })
  52. .catch(err => console.error('error in readfile',err));
  53. return this.mockTweets;
  54. }
  55. public async replyTweet(){}
  56. public writeTweet(twt:string){
  57. // console.log('inside writetweet',this.mockTweets[0]);
  58. // console.log('inside writetweet obj is:',this.mockTweets[0].full_text);
  59. let newTweet = Object.assign({}, this.mockTweets[0]);
  60. newTweet.full_text = twt;
  61. console.log('new tweet ',newTweet);
  62. this.mockTweets.push(newTweet);
  63. this.writefile(this.mockTweets);
  64. }
  65. public async writefile(mocktweet){
  66. await this.platform.ready()
  67. .then(_ => console.log('Device ready.'));
  68. //check if file exists
  69. this.result = this.file.writeFile(this.file.dataDirectory, 'twets.json', JSON.stringify(mocktweet), {replace: true, append:false});
  70. console.log("writing the file:",this.result);
  71. console.log('Reading the file:',this.readFile());
  72. }
  73. public async readFile() {
  74. var readTweets:[];
  75. console.log('in readfile',this.mockTweets);
  76. this.file.checkFile(this.file.dataDirectory, 'twets.json')
  77. .then(() => {
  78. console.log('file exists');
  79. this.file.readAsText(this.file.dataDirectory, 'twets.json')
  80. .then(data=>{
  81. readTweets = JSON.parse(data);
  82. if(!this.fileRead){
  83. // readTweets.forEach(el=>this.mockTweets.push(el));
  84. this.mockTweets = [...readTweets];
  85. this.fileRead = true;
  86. }
  87. console.log('file is: ',this.mockTweets);
  88. })
  89. .catch(err => console.error('error in reading file',err));
  90. })
  91. .catch(err => {
  92. console.error('File doesnt exist',err);
  93. this.file.writeFile(this.file.dataDirectory, 'twets.json', JSON.stringify(this.mockTweets), {replace: true});
  94. });
  95. }
  96. private async fetchPrivateTweets(privateTweetsData: object[]) {
  97. }
  98. }