app.component.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.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. });
  37. this.pages = [
  38. { title: "Home", icon: "home", component: HomePage },
  39. { title: "Search", icon: "search", component: SearchPage },
  40. { title: "Settings", icon: "settings", component: SettingsPage }
  41. ];
  42. }
  43. async initApp() {
  44. const isLoggedIn = await this.authProvider.isLoggedIn();
  45. if (isLoggedIn) {
  46. this.rootPage = HomePage;
  47. const userId = await this.storage.get("userId");
  48. this.user = await this.twitter.fetchUser(userId);
  49. } else {
  50. this.rootPage = LoginPage;
  51. }
  52. }
  53. showProfile(userId) {
  54. this.nav.push(ProfilePage, { userId });
  55. }
  56. openPage(page) {
  57. if (page.component === HomePage) {
  58. this.nav.setRoot(HomePage);
  59. } else {
  60. this.nav.push(page.component);
  61. }
  62. }
  63. logout() {
  64. this.authProvider.logout();
  65. this.nav.setRoot(LoginPage);
  66. }
  67. get banner() {
  68. if (this.user.profile_banner_url) {
  69. return this.user.profile_banner_url;
  70. } else {
  71. return this.user.profile_background_image_url_https;
  72. }
  73. }
  74. }