Skip to content

Commit

Permalink
Save user location to firestore
Browse files Browse the repository at this point in the history
Closes #22
  • Loading branch information
salah3x committed Jul 28, 2019
1 parent 1f53df7 commit 758c6de
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { Component } from '@angular/core';

import { Platform } from '@ionic/angular';
import { Plugins, Capacitor } from '@capacitor/core';
import { StoreService } from './shared/store.service';

@Component({
selector: 'app-root',
templateUrl: 'app.component.html'
})
export class AppComponent {
constructor(private platform: Platform) {
constructor(private platform: Platform, private store: StoreService) {
this.initializeApp();
}

Expand All @@ -18,5 +19,6 @@ export class AppComponent {
Plugins.SplashScreen.hide();
}
});
this.store.shareMyLocation();
}
}
6 changes: 6 additions & 0 deletions src/app/shared/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ export interface Request {
to: string;
timestamp: firebase.firestore.Timestamp;
}

export interface Location {
userId: string;
location: firebase.firestore.GeoPoint;
date: firebase.firestore.Timestamp;
}
64 changes: 60 additions & 4 deletions src/app/shared/store.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,32 @@ import { HttpClient } from '@angular/common/http';
import { AngularFireAuth } from '@angular/fire/auth';
import { AngularFirestore } from '@angular/fire/firestore';
import { firestore } from 'firebase/app';
import { combineLatest, of, Observable, throwError } from 'rxjs';
import { AlertController } from '@ionic/angular';
import { Plugins } from '@capacitor/core';
import {
combineLatest,
of,
Observable,
throwError,
Subscription,
timer
} from 'rxjs';
import { switchMap, map, take, catchError, tap } from 'rxjs/operators';

import { User, Friendship, Request } from './models';
import { User, Friendship, Request, Location } from './models';
import { environment } from '../../environments/environment';

@Injectable({
providedIn: 'root'
})
export class StoreService {
private subs: Subscription;

constructor(
private authService: AngularFireAuth,
private db: AngularFirestore,
private http: HttpClient
private http: HttpClient,
private alertCtrl: AlertController
) {}

getUser(): Observable<User> {
Expand Down Expand Up @@ -266,8 +278,52 @@ export class StoreService {
take(1),
switchMap(user =>
this.db.doc<User>(`users/${user.uid}`).update({ isSharing: sharing })
)
),
tap(() => {
this.subs.unsubscribe();
if (sharing) {
this.shareMyLocation();
}
})
)
.toPromise();
}

shareMyLocation() {
this.subs = this.getUser()
.pipe(
switchMap(user =>
user && user.isSharing
? timer(0, 5000).pipe(
switchMap(_ => Plugins.Geolocation.getCurrentPosition()),
map(
location =>
({
userId: user.id,
location: new firestore.GeoPoint(
location.coords.latitude,
location.coords.longitude
),
date: firestore.Timestamp.now()
} as Location)
),
switchMap(location =>
this.db
.doc<Location>(`locations/${location.userId}`)
.set(location)
),
catchError(async err => {
await (await this.alertCtrl.create({
header: 'Error',
message: err.message || 'Unknown',
buttons: [{ text: 'Close', role: 'cancel' }]
})).present();
return of(null);
})
)
: of(null)
)
)
.subscribe();
}
}

0 comments on commit 758c6de

Please sign in to comment.