Przeglądaj źródła

Added toast service

master
kj1352 5 lat temu
rodzic
commit
351648ef5f
4 zmienionych plików z 44 dodań i 19 usunięć
  1. +2
    -0
      src/app/app.module.ts
  2. +6
    -19
      src/app/malls/malls.page.ts
  3. +12
    -0
      src/app/services/toast.service.spec.ts
  4. +24
    -0
      src/app/services/toast.service.ts

+ 2
- 0
src/app/app.module.ts Wyświetl plik

@@ -10,6 +10,7 @@ import { HttpClientModule } from '@angular/common/http';

// Services import
import { MallService } from './services/mall.service';
import { ToastService } from './services/toast.service';

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
@@ -34,6 +35,7 @@ import { Geolocation } from '@ionic-native/geolocation/ngx';
SplashScreen,
MallService,
Geolocation,
ToastService,
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
],
bootstrap: [AppComponent]


+ 6
- 19
src/app/malls/malls.page.ts Wyświetl plik

@@ -1,7 +1,6 @@
import { Component, OnInit, ViewChild,ElementRef } from '@angular/core';
import { MallService } from '../services/mall.service';
import { Router } from '@angular/router';
import { Geolocation } from '@ionic-native/geolocation/ngx';

@Component({
selector: 'app-malls',
@@ -13,7 +12,6 @@ export class MallsPage implements OnInit {
selected_tab: string = 'you';
tempMalls: any = [];
allMalls: any = [];
nearByMalls: any = [];
show_sort_popup: boolean = false;
showSearchBar: boolean = false;
selected_sort: string = null;
@@ -21,34 +19,22 @@ export class MallsPage implements OnInit {

constructor(
private mallService: MallService,
private router: Router,
private geolocation: Geolocation
private router: Router
) { }

ngOnInit() {
this.fetchMallsByLocation();
this.storeAllMalls();
this.storeAllMalls();
}

fetchMallsByLocation() {
this.geolocation.getCurrentPosition().then((position) => {
this.mallService.mallsByLocation(position.coords.latitude, position.coords.longitude)
.then((response) => {
this.nearByMalls = response;
this.tempMalls = response;
})
});
}

toggleSearchBar() {
if (this.searchTerm.length > 0) {
this.searchTerm = '';
this.tempMalls = JSON.parse(JSON.stringify(this.allMalls));
} else {
this.showSearchBar = !this.showSearchBar;
this.fetchMallsByLocation();
setTimeout(()=>{ // this will make the execution after the above boolean has changed
this.searchElement.nativeElement.focus();
}, 1000);
}, 1);
}
}

@@ -58,7 +44,7 @@ export class MallsPage implements OnInit {
return mallData.mall.mall_name.toLowerCase().includes(this.searchTerm.toLowerCase());
});
} else {
this.tempMalls = JSON.parse(JSON.stringify(this.nearByMalls));
this.tempMalls = JSON.parse(JSON.stringify(this.allMalls));
}
}

@@ -66,6 +52,7 @@ export class MallsPage implements OnInit {
this.mallService.allMalls().then((response) => {
localStorage.allMalls = JSON.stringify(response);
this.allMalls = JSON.parse(localStorage.allMalls);
this.tempMalls = JSON.parse(localStorage.allMalls);
}, (error) => {
console.log(error)
});


+ 12
- 0
src/app/services/toast.service.spec.ts Wyświetl plik

@@ -0,0 +1,12 @@
import { TestBed } from '@angular/core/testing';

import { ToastService } from './toast.service';

describe('ToastService', () => {
beforeEach(() => TestBed.configureTestingModule({}));

it('should be created', () => {
const service: ToastService = TestBed.get(ToastService);
expect(service).toBeTruthy();
});
});

+ 24
- 0
src/app/services/toast.service.ts Wyświetl plik

@@ -0,0 +1,24 @@
import { Injectable } from '@angular/core';
import { ToastController } from '@ionic/angular';

@Injectable({
providedIn: 'root'
})
export class ToastService {

constructor(public toastController: ToastController) { }

async presentToast(message: string, color?: string) {
const toast = await this.toastController.create({
message: message,
position: 'bottom',
duration: 4000,
color: color? color: 'danger',
buttons: [{
text: 'OK',
role: 'cancel',
}]
});
toast.present();
}
}

Ładowanie…
Anuluj
Zapisz