Created
April 3, 2020 13:28
-
-
Save fredroo/04f10d974500effa7b64b05d06a071c4 to your computer and use it in GitHub Desktop.
IONIC 5 COMPONENT
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 { Component, ViewChild, CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA } from '@angular/core'; | |
import { Router } from '@angular/router'; | |
import { Platform, MenuController, AlertController} from '@ionic/angular'; | |
import { SplashScreen } from '@ionic-native/splash-screen/ngx'; | |
import { StatusBar } from '@ionic-native/status-bar/ngx'; | |
import md5 from 'crypto-js/md5'; | |
import { NetworkCheckProvider } from './services/network-check/network-check'; | |
import { AppConfigProvider } from './services/app-config/app-config'; | |
import { AuthProvider } from './services/auth/auth'; | |
import { RedirectorProvider } from './services/redirector/redirector'; | |
import { TranslateService } from '@ngx-translate/core'; | |
import { Storage } from '@ionic/storage'; | |
@Component({ | |
selector: 'app-root', | |
templateUrl: 'app.component.html', | |
styleUrls: ['app.component.scss'] | |
}) | |
export class AppComponent { | |
rootPage: any; | |
pages: Array<{ title: string, url: any, icon: string }>; | |
user: any; | |
gravatarUrl = 'https://www.gravatar.com/avatar/nouser.jpg'; | |
constructor( | |
private platform: Platform, | |
private splashScreen: SplashScreen, | |
private statusBar: StatusBar, | |
public menu: MenuController, | |
public alertCtrl: AlertController, | |
private translate: TranslateService, | |
private appConfig: AppConfigProvider, | |
public auth: AuthProvider, | |
public redirector: RedirectorProvider, | |
public networkCheck: NetworkCheckProvider, | |
private storage: Storage, | |
private navCtrl: Router | |
) { | |
this.sideMenu(); | |
this.initializeApp(); | |
} | |
sideMenu() { | |
this.pages = [ | |
{ title: 'Início', url: '/pages/homepage', icon: 'home'}, | |
{ title: 'Produtos', url: '/pages/productslistpage', icon: 'cart'}, | |
{ title: 'Minhas Vendas', url: '/pages/mysalespage', icon: 'cart'}, | |
{ title: 'Minhas Compras', url: '/pages/myshoppingpage', icon: 'cart'}, | |
{ title: 'Config. Saque', url: '/pages/bankdraftconfigpage', icon: 'settings'}, | |
{ title: 'Publicações', url: '/pages/postspage', icon: 'document'}, | |
{ title: 'Indicar', url: '/pages/invitefriendspage', icon: 'person-add'}, | |
{ title: 'Conta', url: '/pages/accountpage', icon: 'cash'}, | |
{ title: 'Perfil', url: '/pages/profilepage', icon: 'person'}, | |
{ title: 'Redes Sociais', url: '/pages/sociaisnetworkspage', icon: 'planet'}, | |
{ title: 'Termos de Uso', url: '/pages/termspage', icon: 'ribbon'}, | |
{ title: 'Privacidade', url: '/pages/privacypage', icon: 'flag'}, | |
{ title: 'Contato', url: '/pages/contactpage', icon: 'paper-plane'}, | |
{ title: 'Idioma', url: '/pages/languagepage', icon: 'globe'}, | |
{ title: 'Configurações', url: '/pages/settingspage', icon: 'settings'}, | |
{ title: 'Problema', url: '/pages/bugpage', icon: 'bug'}, | |
{ title: 'Sobre', url: '/pages/aboutpage', icon: 'information-circle'}, | |
{ title: 'Ajuda', icon: 'help-buoy', url: '/pages/helppage' } | |
]; | |
} | |
ionViewDidEnter() { | |
this.splashScreen.hide(); | |
} | |
initializeApp() { | |
this.platform.ready().then(() => { | |
this.getFirstPage(); | |
this.statusBar.styleDefault(); | |
}); | |
this.auth.userSubject().subscribe(user => { | |
this.user = user; | |
this.gravatar(); | |
}); | |
// this.translate.addLangs(["br","en","es"]); | |
this.translate.setDefaultLang(this.appConfig.getLanguage()); | |
this.translate.use(this.appConfig.getLanguage()); | |
// this.geolocation.getCurrentPosition().then((resp) => { | |
// console.log(resp); | |
// }).catch((error) => { | |
// console.log('Error getting location', error); | |
// }); | |
// | |
// let watch = this.geolocation.watchPosition(); | |
// watch.subscribe((data) => { | |
// console.log(data) | |
// }); | |
} | |
private gravatar() { | |
if (this.user) { | |
this.gravatarUrl = `https://www.gravatar.com/avatar/${md5(this.user.email, 'hex')}`; | |
} | |
} | |
public openPage(page) { | |
// close the menu when clicking a link from the menu | |
this.menu.close(); | |
// navigate to the new page if it is not the current page | |
this.navCtrl.navigateByUrl(page.url); | |
} | |
async logout() { | |
const alert = await this.alertCtrl.create({ | |
header: 'Confirmação', | |
subHeader: 'Você tem certeza que deseja sair?', | |
buttons: [{ | |
text: 'Sim', | |
handler: () => { | |
this.auth.logout().then(() => { | |
this.navCtrl.navigateByUrl('/LoginPage'); | |
}).catch(() => { | |
this.navCtrl.navigateByUrl('/LoginPage'); | |
}); | |
} | |
}, { | |
text: 'Cancelar', | |
role: 'cancel' | |
}] | |
}); | |
await alert.present(); | |
} | |
private getFirstPage() { | |
this.storage.get('welcome').then(done => { | |
if (!done) { | |
//this.rootPage = 'WelcomePage'; | |
this.navCtrl.navigateByUrl('/WelcomePage'); | |
} else { | |
this.auth.check().then(isLogged => { | |
if (!isLogged) { | |
//this.rootPage = 'LoginPage'; | |
this.navCtrl.navigateByUrl('/LoginPage'); | |
} else { | |
//this.rootPage = 'AccountPage'; | |
this.navCtrl.navigateByUrl('/AccountPage'); | |
} | |
}); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment