PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/providers/remote-db.service.ts

https://gitlab.com/hearoy/M1-projetNomade
TypeScript | 172 lines | 109 code | 18 blank | 45 comment | 12 complexity | f98cc14eb400e9dde2c0abc02a0b8e31 MD5 | raw file
  1. import { Album, ImageForMap, MyAlbum, MyAlbums$, MyPhotos$ } from './detailed-photos.provider';
  2. import { AuthService } from './auth.service';
  3. import { Injectable, Inject } from '@angular/core';
  4. import { Subject } from 'rxjs';
  5. import { AngularFire, FirebaseApp, FirebaseListObservable } from 'angularfire2';
  6. import * as firebase from 'firebase';
  7. export declare interface ImageForMap4DB extends ImageForMap {
  8. latLng: { lat: number, lng: number }
  9. }
  10. declare interface AlbumMeta {
  11. size: number,
  12. title: string,
  13. $key: string,
  14. }
  15. @Injectable()
  16. export class RemoteDBService {
  17. private _metas: FirebaseListObservable<AlbumMeta[]>;
  18. private _albums: FirebaseListObservable<ImageForMap4DB[][]>;
  19. private init: boolean = false;
  20. private myAlbums: MyAlbums$;
  21. constructor(public af: AngularFire, public auth: AuthService) {//, @Inject(FirebaseApp) private firebApp : firebase.app.App) {
  22. }
  23. get albums() {
  24. return this._albums;
  25. }
  26. // storage() {
  27. // this.firebApp.storage();
  28. // }
  29. private _initialize() {
  30. if (!this.init) {
  31. this._metas = this.af.database.list(`/${this.auth.uid}/albums/meta`, {
  32. query: { orderByChild: 'title' }
  33. });
  34. this._albums = this.af.database.list(`/${this.auth.uid}/albums/photos`, {
  35. query: { orderByKey: true }
  36. });
  37. this.init = true;
  38. return false;
  39. } else
  40. return this.init;
  41. }
  42. private createAlbumPath(key: string): FirebaseListObservable<ImageForMap4DB[]> {
  43. return this.af.database.list(`/${this.auth.uid}/albums/photos/${key}`);
  44. }
  45. private updateAlbum(key: number, meta: any, photos: ImageForMap[]) {
  46. // debugger;
  47. const afSetPromise = function(ref : firebase.database.Reference, value) { return new Promise((resolve, reject) => ref.set(value, a=> resolve(a)) );};
  48. const val = {};
  49. val[key.toString()] = photos;
  50. afSetPromise(this._albums.$ref.ref, val).then(_ => console.log('photos updated')).catch(_ => console.log('photos error'));
  51. //this._albums.update(key.toString(), photos).then(_ => console.log('photos updated')).catch(_ => console.log('photos error'));
  52. this._metas.update(key.toString(), meta).then(_ => console.log('meta updated')).catch(_ => console.log('meta error'));
  53. }
  54. saveAlbum(album: Album, idx: number) {
  55. this._initialize();
  56. let meta: any = {
  57. size: album.photos.length,
  58. title: album.title
  59. };
  60. let photos: ImageForMap4DB[] = album.photos.map(val => {
  61. const res = <ImageForMap4DB>Object.assign({} as ImageForMap4DB, val);
  62. res.latLng = this.latLng2Litteral(val.latLng); //{ lat: (<google.maps.LatLng>val.latLng).lat(), lng: (<google.maps.LatLng>val.latLng).lng() };
  63. return res;
  64. });
  65. this.updateAlbum(album.id, meta, photos);
  66. // let dbStruct = {
  67. // albums: {
  68. // meta: {
  69. // 0: { // albumId
  70. // size: 3, // album.photos.length
  71. // title: 'aze'
  72. // }
  73. // },
  74. // photos: {
  75. // 0: { // albumId
  76. // 0: {'...' : '...'}, // album.photos[0]
  77. // 1: {'...' : '...'}
  78. // }
  79. // }
  80. // }
  81. // };
  82. }
  83. download() {
  84. if (this._initialize())
  85. return this.myAlbums;
  86. let myAlbums = new MyAlbums$(new Subject<Album>());
  87. this.myAlbums = myAlbums;
  88. // debugger;
  89. const convert2ImgForMap = (val: ImageForMap4DB) => {
  90. // debugger;
  91. const res = <ImageForMap>val;
  92. res.latLng = this.latLngLitteral2LatLng(val.latLng);
  93. return res;
  94. };
  95. // get the keys of the albums, from the metadata'
  96. const keys$ = this._metas.take(1).pluck<string>('0', '$key');
  97. // get the albums, from each key ~ [albums[photos... ]... ]
  98. const albums$$ = keys$.map(key => this.createAlbumPath(key));
  99. const myPhotos$$= albums$$.map(album$ => {
  100. // transform the albums of Image4MapDb into an observable of MyPhotos$
  101. const photos$ = album$.map(arr => arr.map(convert2ImgForMap)).map(album => {
  102. const myPhotos = new MyPhotos$();
  103. myPhotos.push(...album);
  104. return myPhotos;
  105. });
  106. return photos$;
  107. });
  108. myPhotos$$.subscribe(myPhotos$ => {
  109. myPhotos$.subscribe(myPhotos => {
  110. const id = myPhotos[0].albumId;
  111. const album = myAlbums[id];
  112. if (album === undefined)
  113. myAlbums.push(MyAlbum.fromPhotos(myPhotos));
  114. else {
  115. album.photos.push(...myPhotos);
  116. }
  117. });
  118. });
  119. // const photos = keys$.mergeMap<ImageForMap4DB[]>(key => { debugger;return this.createAlbumPath(key).take(1) }); // pb: when a new key arrives, albums get mixed up
  120. // photos.map(albumPhotos => { debugger;return albumPhotos.map(convert2ImgForMap)})
  121. // .map((photos: ImageForMap[]) => {
  122. // const myPhotos = new MyPhotos$();
  123. // myPhotos.push(...photos);
  124. // return myPhotos;
  125. // })
  126. // .subscribe(function next(myPhotos) {
  127. // myAlbums.push(MyAlbum.fromPhotos(myPhotos));
  128. // });
  129. // this._albums.map((photos: ImageForMap4DB[][]) => photos[0].map(convert2ImgForMap))
  130. // .map((photos: ImageForMap[]) => {
  131. // const myPhotos = new MyPhotos$();
  132. // myPhotos.push(...photos);
  133. // return myPhotos;
  134. // })
  135. // .subscribe(function next(myPhotos) {
  136. // myAlbums.push(MyAlbum.fromPhotos(myPhotos));
  137. // });
  138. return myAlbums;
  139. }
  140. private latLng2Litteral(latLng: google.maps.LatLng | google.maps.LatLngLiteral) {
  141. if (latLng instanceof google.maps.LatLng) {
  142. return { lat: latLng.lat(), lng: latLng.lng() };
  143. } else {
  144. return latLng;
  145. }
  146. }
  147. private latLngLitteral2LatLng(latLng: google.maps.LatLng | google.maps.LatLngLiteral) {
  148. if (latLng instanceof google.maps.LatLng) {
  149. return latLng;
  150. } else {
  151. return new google.maps.LatLng(latLng.lat, latLng.lng);
  152. }
  153. }
  154. }