app.component.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { Component, ViewChild } from "@angular/core";
  2. import { Nav, Platform, Events } from "ionic-angular";
  3. import { StatusBar } from "@ionic-native/status-bar";
  4. import { SplashScreen } from "@ionic-native/splash-screen";
  5. import { Storage } from "@ionic/storage";
  6. import { LoginPage } from "../pages/login/login";
  7. import { AuthProvider } from "../providers/auth/auth";
  8. import { HomePage } from "../pages/home/home";
  9. import { SearchPage } from "../pages/search/search";
  10. import { SettingsPage } from "../pages/settings/settings";
  11. import { ProfilePage } from "../pages/profile/profile";
  12. import * as user from './user.json';
  13. @Component({
  14. templateUrl: "app.html"
  15. })
  16. export class MyApp {
  17. @ViewChild(Nav)
  18. nav: Nav;
  19. rootPage: any;
  20. pages: Array<{ title: string; icon: string; component: any }>;
  21. user: any;
  22. constructor(
  23. platform: Platform,
  24. statusBar: StatusBar,
  25. splashScreen: SplashScreen,
  26. private authProvider: AuthProvider,
  27. private storage: Storage,
  28. private events: Events
  29. ) {
  30. platform.ready().then(() => {
  31. // Okay, so the platform is ready and our plugins are available.
  32. // Here you can do any higher level native things you might need.
  33. statusBar.styleDefault();
  34. splashScreen.hide();
  35. this.initApp();
  36. console.log('SUVSCRIBED EVENT RECVD');
  37. this.events.subscribe("user:login", () => this.setUser());
  38. });
  39. this.pages = [
  40. { title: "Home", icon: "home", component: HomePage },
  41. { title: "Search", icon: "search", component: SearchPage },
  42. { title: "Settings", icon: "settings", component: SettingsPage }
  43. ];
  44. }
  45. async initApp() {
  46. const isLoggedIn = await this.authProvider.isLoggedIn();
  47. if (isLoggedIn) {
  48. this.rootPage = HomePage;
  49. await this.setUser();
  50. } else {
  51. this.rootPage = LoginPage;;
  52. }
  53. }
  54. async setUser() {
  55. const userId = await this.storage.get("userId");
  56. console.log('settings the user as:',userId);
  57. this.user = user;
  58. console.log("set user is:",this.user);
  59. // this.user = await this.twitter.fetchUser(userId);
  60. }
  61. showProfile(userId) {
  62. this.nav.push(ProfilePage, { userId });
  63. }
  64. openPage(page) {
  65. if (page.component === HomePage) {
  66. this.nav.setRoot(HomePage);
  67. } else {
  68. this.nav.push(page.component);
  69. }
  70. }
  71. logout() {
  72. this.authProvider.logout();
  73. this.nav.setRoot(LoginPage);
  74. }
  75. get banner() {
  76. if (this.user.profile_banner_url) {
  77. return this.user.profile_banner_url;
  78. } else {
  79. return this.user.profile_background_image_url_https;
  80. }
  81. }
  82. }