app.component.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { Component, ViewChild } from "@angular/core";
  2. import { Nav, Platform } 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. ) {
  30. platform
  31. .ready()
  32. .then(() => {
  33. // Okay, so the platform is ready and our plugins are available.
  34. // Here you can do any higher level native things you might need.
  35. //this.setHomepage();
  36. statusBar.styleDefault();
  37. splashScreen.hide();
  38. return this.authProvider.isLoggedIn();
  39. })
  40. .then(isLoggedIn => {
  41. this.rootPage = isLoggedIn ? HomePage : LoginPage;
  42. return this.storage.get("userId");
  43. })
  44. .then(userId => this.twitter.fetchUser(userId))
  45. .then(res => {
  46. this.user = res;
  47. });
  48. this.pages = [
  49. { title: "Home", icon: "home", component: HomePage },
  50. { title: "Search", icon: "search", component: SearchPage },
  51. { title: "Settings", icon: "settings", component: SettingsPage }
  52. ];
  53. }
  54. showProfile(userId) {
  55. this.nav.push(ProfilePage, { userId });
  56. }
  57. openPage(page) {
  58. this.nav.setRoot(page.component);
  59. }
  60. logout() {
  61. this.authProvider.logout();
  62. this.nav.setRoot(LoginPage);
  63. }
  64. }