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 { AuthProvider } from "../providers/auth/auth";
  7. import { HomePage } from "../pages/home/home";
  8. import { SearchPage } from "../pages/search/search";
  9. import { SettingsPage } from "../pages/settings/settings";
  10. import { LoginPage } from "../pages/login/login";
  11. import { ProfilePage } from "../pages/profile/profile";
  12. import { TwitterApiProvider } from "../providers/twitter-api/twitter-api";
  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 twitter: TwitterApiProvider,
  28. private storage: Storage,
  29. private events: Events
  30. ) {
  31. platform.ready().then(() => {
  32. // Okay, so the platform is ready and our plugins are available.
  33. // Here you can do any higher level native things you might need.
  34. statusBar.styleDefault();
  35. splashScreen.hide();
  36. this.initApp();
  37. console.log('SUVSCRIBED EVENT RECVD');
  38. this.events.subscribe("user:login", () => this.setUser());
  39. });
  40. this.pages = [
  41. { title: "Home", icon: "home", component: HomePage },
  42. { title: "Search", icon: "search", component: SearchPage },
  43. { title: "Settings", icon: "settings", component: SettingsPage }
  44. ];
  45. }
  46. async initApp() {
  47. const isLoggedIn = await this.authProvider.isLoggedIn();
  48. if (isLoggedIn) {
  49. this.rootPage = HomePage;
  50. await this.setUser();
  51. } else {
  52. this.rootPage = LoginPage;
  53. }
  54. }
  55. async setUser() {
  56. const userId = await this.storage.get("userId");
  57. console.log('settings the user as:',userId);
  58. this.user = await this.twitter.fetchUser(userId);
  59. }
  60. showProfile(userId) {
  61. this.nav.push(ProfilePage, { userId });
  62. }
  63. openPage(page) {
  64. if (page.component === HomePage) {
  65. this.nav.setRoot(HomePage);
  66. } else {
  67. this.nav.push(page.component);
  68. }
  69. }
  70. logout() {
  71. this.authProvider.logout();
  72. this.nav.setRoot(LoginPage);
  73. }
  74. get banner() {
  75. if (this.user.profile_banner_url) {
  76. return this.user.profile_banner_url;
  77. } else {
  78. return this.user.profile_background_image_url_https;
  79. }
  80. }
  81. }