Jelajahi Sumber

Refactor initialization process

Carsten Porth 5 tahun lalu
induk
melakukan
0fa7b087f5
1 mengubah file dengan 24 tambahan dan 20 penghapusan
  1. 24 20
      app/src/app/app.component.ts

+ 24 - 20
app/src/app/app.component.ts

@@ -32,26 +32,14 @@ export class MyApp {
     private twitter: TwitterApiProvider,
     private storage: Storage
   ) {
-    platform
-      .ready()
-      .then(() => {
-        // Okay, so the platform is ready and our plugins are available.
-        // Here you can do any higher level native things you might need.
-        //this.setHomepage();
+    platform.ready().then(() => {
+      // Okay, so the platform is ready and our plugins are available.
+      // Here you can do any higher level native things you might need.
 
-        statusBar.styleDefault();
-        splashScreen.hide();
-
-        return this.authProvider.isLoggedIn();
-      })
-      .then(isLoggedIn => {
-        this.rootPage = isLoggedIn ? HomePage : LoginPage;
-        return this.storage.get("userId");
-      })
-      .then(userId => this.twitter.fetchUser(userId))
-      .then(res => {
-        this.user = res;
-      });
+      statusBar.styleDefault();
+      splashScreen.hide();
+      this.initApp();
+    });
 
     this.pages = [
       { title: "Home", icon: "home", component: HomePage },
@@ -60,12 +48,28 @@ export class MyApp {
     ];
   }
 
+  async initApp() {
+    const isLoggedIn = await this.authProvider.isLoggedIn();
+
+    if (isLoggedIn) {
+      this.rootPage = HomePage;
+      const userId = await this.storage.get("userId");
+      this.user = await this.twitter.fetchUser(userId);
+    } else {
+      this.rootPage = LoginPage;
+    }
+  }
+
   showProfile(userId) {
     this.nav.push(ProfilePage, { userId });
   }
 
   openPage(page) {
-    this.nav.push(page.component);
+    if (page.component === HomePage) {
+      this.nav.setRoot(HomePage);
+    } else {
+      this.nav.push(page.component);
+    }
   }
 
   logout() {