Sfoglia il codice sorgente

Fixed refreshing buttons after user logged in (don't keep activities enabled) and other minor fixes

Vadik Sirekanyan 7 anni fa
parent
commit
81d17487a8

+ 13 - 22
app/src/main/java/com/sirekanyan/knigopis/feature/MainPresenter.kt

@@ -57,30 +57,29 @@ class MainPresenterImpl(
     private var userLoggedIn = false
 
     override val state
-        get() = currentTab?.let { MainPresenterState(it.itemId) }
+        get() = currentTab?.let { MainPresenterState(it) }
 
     override fun init(state: MainPresenterState?) {
-        val currentTab = state?.currentTab?.let { CurrentTab.getByItemId(it) }
-        val defaultTab = if (auth.isAuthorized()) HOME_TAB else NOTES_TAB
-        refresh(currentTab ?: defaultTab)
-        refreshNavigation()
         view.setDarkThemeOptionChecked(config.isDarkTheme)
+        val defaultTab = if (auth.isAuthorized()) HOME_TAB else NOTES_TAB
+        this.currentTab = state?.currentTab ?: defaultTab
     }
 
     override fun start() {
-        refreshOptionsMenu()
+        refreshButtons()
+        refresh(currentTab)
+    }
+
+    override fun resume() {
         auth.loadAccessToken().bind({
-            refreshOptionsMenu()
+            refreshButtons()
             if (userLoggedIn) {
                 userLoggedIn = false
-                refresh()
+                refresh(HOME_TAB)
             }
         }, {
             logError("cannot check credentials", it)
         })
-    }
-
-    override fun resume() {
         if (booksChanged) {
             booksChanged = false
             refresh(isForce = true)
@@ -107,11 +106,11 @@ class MainPresenterImpl(
         }
     }
 
-    private fun refreshOptionsMenu() {
-        refreshNavigation()
+    private fun refreshButtons() {
         auth.isAuthorized().let { authorized ->
             view.showLoginOption(!authorized)
             view.showProfileOption(authorized)
+            view.showNavigation(authorized)
         }
     }
 
@@ -230,14 +229,6 @@ class MainPresenterImpl(
         router.openUserScreen(note.userId, note.userName, note.userImage)
     }
 
-    private fun refreshNavigation() {
-        if (auth.isAuthorized()) {
-            view.showNavigation()
-        } else {
-            view.hideNavigation()
-        }
-    }
-
     private fun refreshHomeTab(tab: CurrentTab) {
         bookRepository.observeBooks()
             .io2main()
@@ -297,7 +288,7 @@ class MainPresenterImpl(
                     } else {
                         router.openLoginScreen()
                     }
-                    refreshOptionsMenu()
+                    refreshButtons()
                 }
                 it.shouldShowRequestPermissionRationale -> {
                     view.showPermissionsRetryDialog()

+ 11 - 12
app/src/main/java/com/sirekanyan/knigopis/feature/MainView.kt

@@ -40,8 +40,7 @@ interface MainView : LoginView, BooksView, UsersView, NotesView {
     fun showProgress()
     fun hideProgress()
     fun hideSwipeRefresh()
-    fun showNavigation()
-    fun hideNavigation()
+    fun showNavigation(isVisible: Boolean)
     fun setNavigation(itemId: Int)
     fun showLoginOption(isVisible: Boolean)
     fun showProfileOption(isVisible: Boolean)
@@ -215,19 +214,19 @@ class MainViewImpl(
         swipeRefresh.isRefreshing = false
     }
 
-    override fun showNavigation() {
-        bottomNavigation.show()
-        bottomNavigation.setOnNavigationItemSelectedListener { item ->
-            callbacks.onNavigationClicked(item.itemId)
-            true
+    override fun showNavigation(isVisible: Boolean) {
+        if (isVisible) {
+            bottomNavigation.show()
+            bottomNavigation.setOnNavigationItemSelectedListener { item ->
+                callbacks.onNavigationClicked(item.itemId)
+                true
+            }
+        } else {
+            bottomNavigation.hide()
+            bottomNavigation.setOnNavigationItemSelectedListener(null)
         }
     }
 
-    override fun hideNavigation() {
-        bottomNavigation.hide()
-        bottomNavigation.setOnNavigationItemSelectedListener(null)
-    }
-
     override fun setNavigation(itemId: Int) {
         bottomNavigation.selectedItemId = itemId
     }

+ 4 - 3
app/src/main/java/com/sirekanyan/knigopis/feature/users/MainPresenterState.kt

@@ -1,14 +1,15 @@
 package com.sirekanyan.knigopis.feature.users
 
 import android.os.Bundle
+import com.sirekanyan.knigopis.model.CurrentTab
 
 private const val CURRENT_TAB_KEY = "current_tab"
 
 fun Bundle.getMainState(): MainPresenterState =
-    MainPresenterState(getInt(CURRENT_TAB_KEY))
+    MainPresenterState(CurrentTab.getByItemId(getInt(CURRENT_TAB_KEY)))
 
 fun Bundle.saveMainState(state: MainPresenterState) {
-    putInt(CURRENT_TAB_KEY, state.currentTab)
+    putInt(CURRENT_TAB_KEY, state.currentTab.itemId)
 }
 
-class MainPresenterState(val currentTab: Int)
+class MainPresenterState(val currentTab: CurrentTab)

+ 1 - 0
app/src/main/res/layout/activity_main.xml

@@ -63,6 +63,7 @@
         android:layout_height="wrap_content"
         android:layout_gravity="bottom"
         android:background="?android:attr/windowBackground"
+        android:visibility="gone"
         app:menu="@menu/navigation" />
 
 </LinearLayout>