Last active
June 9, 2021 20:27
-
-
Save ozzpy/945def08f7f2f4c1ab9bb76335723032 to your computer and use it in GitHub Desktop.
tracker.service.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Injectable } from '@angular/core'; | |
| import {AngularFirestore} from "@angular/fire/firestore"; | |
| import * as firebase from "firebase"; | |
| import {AngularFireAuth} from "@angular/fire/auth"; | |
| import {HttpClient} from "@angular/common/http"; | |
| import {Geolocation} from "@ionic-native/geolocation/ngx"; | |
| import {ApiService} from "./api.service"; | |
| import {BehaviorSubject, Subject} from "rxjs"; | |
| import {UtilService} from "./util.service"; | |
| export interface PositionModel{ | |
| lat: number, | |
| lng: number, | |
| updateDate: string | |
| } | |
| @Injectable({ | |
| providedIn: 'root' | |
| }) | |
| export class OrderTrackerService { | |
| private uid: string = null; | |
| private tracker = new BehaviorSubject(true); | |
| private inProgress = new Subject(); | |
| private db = firebase.firestore(); | |
| private lat: number; | |
| private lng: number; | |
| private watch:any; | |
| constructor( private http: HttpClient, | |
| private geolocation: Geolocation, | |
| private api: ApiService, | |
| private utils: UtilService, | |
| private fireAuth: AngularFireAuth, | |
| private adb: AngularFirestore) { | |
| this.requestAuth().then( (user:any) => { | |
| console.log("OrderTrackerService::",user); | |
| this.uid = user.uid; | |
| this.getOnlineTracker(this.uid); | |
| }); | |
| } | |
| sendTrack$(status:boolean) { | |
| this.tracker.next(status); | |
| if(status===true){ | |
| this.startLocation(); | |
| } else{ | |
| this.stopLocation(); | |
| } | |
| } | |
| async getTrack$() : Promise<any> { | |
| return await new Promise((resolve,reject)=>{ | |
| this.tracker.subscribe( res => { | |
| resolve(res); | |
| }); | |
| }); | |
| } | |
| watchTrack$() { | |
| return this.tracker; | |
| } | |
| public getOnlineTracker(uid): Promise<any> { | |
| return new Promise<any>((resolve, reject) => { | |
| this.adb.collection('tracker').doc(uid).get().subscribe((position: any) => { | |
| console.log('getOnlineTracker::',position); | |
| console.log('getOnlineTracker::data::',position.data()); | |
| //resolve(position.data()); | |
| resolve(position); | |
| }, error => { | |
| reject(error); | |
| }); | |
| }); | |
| } | |
| startLocation() { | |
| this.geolocation.getCurrentPosition().then((resp) => { | |
| // console.log(resp); | |
| // console.log('current lat', resp); | |
| this.updateLocation(resp.coords.latitude, resp.coords.longitude); | |
| }).catch((error) => { | |
| console.log('Error getting location', error); | |
| }); | |
| //let watchs: Geolocation = this.geolocation.watchPosition(); | |
| this.watch = this.geolocation.watchPosition({maximumAge:1000,enableHighAccuracy:true,timeout: 10000}) | |
| this.watch.subscribe((data) => { | |
| console.log('live update', data); | |
| this.updateLocation(data.coords.latitude, data.coords.longitude); | |
| }); | |
| } | |
| updateLocation(lat, lng) { | |
| localStorage.setItem('lat', lat); | |
| localStorage.setItem('lng', lng); | |
| if ( localStorage.getItem('uid') ) { | |
| // console.log('can update'); | |
| const param: PositionModel = { | |
| lat: lat, | |
| lng: lng, | |
| updateDate: new Date().getMilliseconds().toString() | |
| } | |
| this.requestUpdateTracker(localStorage.getItem('uid'),param).then( ok=>{ | |
| console.log("updateLocation::requestUpdateTracker::updated!"); | |
| }).catch(fail=>{ | |
| this.utils.showToast(fail, 'danger', 'bottom').then(); | |
| }); | |
| } else { | |
| this.requestAuth().then( (user: any)=>{ | |
| console.log('TODO::updateLocation::by::uid',user); | |
| }); | |
| } | |
| } | |
| stopLocation() { | |
| this.watch = null; | |
| } | |
| public requestAuth() { | |
| return new Promise((resolve) => { | |
| this.fireAuth.auth.onAuthStateChanged(user => { | |
| console.log('Tracker::AuthState::',user); | |
| resolve(user); | |
| }); | |
| }); | |
| } | |
| public requestUpdateTracker(uid, param:PositionModel): Promise<any> { | |
| return new Promise<any>((resolve, reject) => { | |
| this.db.collection('tracker').doc(uid).update(param).then((data) => { | |
| resolve(data); | |
| }).catch(error => { | |
| reject(error); | |
| }) | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment