Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { Storage } from '@ionic/storage';
  3.  
  4. declare var _: any;
  5.  
  6. @Injectable()
  7. export class LocalStorageHelper {
  8. constructor(
  9. public storage: Storage
  10. ){}
  11.  
  12. db: any = {
  13. cases: [],
  14.  
  15. caseNotes: [],
  16. caseSessions: [],
  17. caseInvoices: [],
  18. caseTimeline: [],
  19.  
  20. therapists: [],
  21. status: [],
  22. specialties: [],
  23. profile: {},
  24. };
  25.  
  26. loadDb(cb){
  27. var i = 1;
  28.  
  29. this.storage.get("db").then(
  30. (db) => {
  31. if(db){
  32. this.db = db;
  33. }
  34.  
  35. cb()
  36. }
  37. )
  38. }
  39.  
  40. saveDb(){
  41. this.storage.set("db", this.db)
  42. }
  43.  
  44. newId() {
  45. function s4() {
  46. return Math.floor((1 + Math.random()) * 0x10000)
  47. .toString(16)
  48. .substring(1);
  49. }
  50. return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
  51. s4() + '-' + s4() + s4() + s4();
  52. }
  53.  
  54. addItemToList(listName, item, noId){
  55. if(noId!=true){
  56. item.Id = this.newId();
  57. }
  58.  
  59. if(!this.db[listName]){
  60. this.db[listName] = [];
  61. }
  62.  
  63. this.db[listName].push(item);
  64.  
  65. this.saveDb();
  66. }
  67.  
  68. updateItemToList(listName, item){
  69. for (var i = this.db[listName].length - 1; i >= 0; i--) {
  70. if(this.db[listName][i].Id==item.Id){
  71. Object.assign(this.db[listName][i], item);
  72. break;
  73. }
  74. }
  75.  
  76. this.saveDb();
  77. }
  78.  
  79. saveItem(listName, item){
  80. this.db[listName] = item
  81.  
  82. this.saveDb();
  83. }
  84.  
  85. getItemFromList(listName, id){
  86. return _.find(this.db[listName], {Id: id});
  87. }
  88.  
  89. getListByParams(listName, params){
  90. return _.find(this.db[listName], params);
  91. }
  92.  
  93. getList(listName){
  94. return this.db[listName];
  95. }
  96.  
  97. getListItemsByParams(listName, params){
  98. return _.filter(this.db[listName], (i)=> {
  99. return i[params.field] == params.value;
  100. });
  101. }
  102.  
  103. deleteItemFromList(listName, id){
  104. for (var i = this.db[listName].length - 1; i >= 0; i--) {
  105. if(this.db[listName][i].Id==id){
  106. this.db[listName].splice(i, 1);
  107.  
  108. break;
  109. }
  110. }
  111.  
  112. this.saveDb()
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement