Browse Source

Hide navigation if user is not authorized

Vadik Sirekanyan 7 years ago
parent
commit
e3b4656a57

+ 14 - 7
app/src/main/java/me/vadik/knigopis/MainActivity.kt

@@ -81,7 +81,9 @@ class MainActivity : AppCompatActivity(), Router {
         initRecyclerView(notesRecyclerView)
         val currentTabId = savedInstanceState?.getInt(CURRENT_TAB_KEY)
         val currentTab = currentTabId?.let { CurrentTab.getByItemId(it) }
-        initNavigationView(currentTab)
+        val defaultTab = if (auth.isAuthorized()) HOME_TAB else NOTES_TAB
+        refresh(currentTab ?: defaultTab)
+        initNavigationView()
         initToolbar(toolbar)
         addBookButton.setOnClickListener {
             startActivityForResult(createNewBookIntent(), BOOK_REQUEST_CODE)
@@ -180,12 +182,16 @@ class MainActivity : AppCompatActivity(), Router {
         )
     }
 
-    private fun initNavigationView(currentTab: CurrentTab?) {
-        val defaultTab = if (auth.isAuthorized()) HOME_TAB else NOTES_TAB
-        refresh(currentTab ?: defaultTab)
-        bottomNavigation.setOnNavigationItemSelectedListener { item ->
-            setCurrentTab(CurrentTab.getByItemId(item.itemId))
-            true
+    private fun initNavigationView() {
+        if (auth.isAuthorized()) {
+            bottomNavigation.show()
+            bottomNavigation.setOnNavigationItemSelectedListener { item ->
+                setCurrentTab(CurrentTab.getByItemId(item.itemId))
+                true
+            }
+        } else {
+            bottomNavigation.hide()
+            bottomNavigation.setOnNavigationItemSelectedListener(null)
         }
     }
 
@@ -309,6 +315,7 @@ class MainActivity : AppCompatActivity(), Router {
     }
 
     private fun refreshOptionsMenu() {
+        initNavigationView()
         loginOption.isVisible = true
         profileOption.isVisible = auth.isAuthorized()
         if (auth.isAuthorized()) {

+ 1 - 4
app/src/main/java/me/vadik/knigopis/auth/KAuth.kt

@@ -34,10 +34,7 @@ class KAuthImpl(
 
     override fun getAccessToken(): String = preferences.getString(ACCESS_TOKEN_KEY, "")
 
-    override fun getTokenRequest(): Intent {
-        return Intent(context, UloginAuthActivity::class.java)
-            .putExtra(UloginAuthActivity.FIELDS, arrayOf(TOKEN_KEY))
-    }
+    override fun getTokenRequest() = Intent(context, UloginAuthActivity::class.java)
 
     override fun getUserProfile(): String? {
         return preferences.getString(USER_PROFILE, null)