app.component.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. 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. this.user = await this.twitter.fetchUser(userId);
  57. }
  58. showProfile(userId) {
  59. this.nav.push(ProfilePage, { userId });
  60. }
  61. openPage(page) {
  62. if (page.component === HomePage) {
  63. this.nav.setRoot(HomePage);
  64. } else {
  65. this.nav.push(page.component);
  66. }
  67. }
  68. logout() {
  69. this.authProvider.logout();
  70. this.nav.setRoot(LoginPage);
  71. }
  72. get banner() {
  73. if (this.user.profile_banner_url) {
  74. return this.user.profile_banner_url;
  75. } else {
  76. return this.user.profile_background_image_url_https;
  77. }
  78. }
  79. }