Skip to content
This repository has been archived by the owner on Jan 29, 2023. It is now read-only.

Commit

Permalink
Fix saving selected tab on activity recreating (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
vase4kin committed Apr 18, 2020
1 parent be3907f commit f98d1ed
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Andrey Tolpeev
* Copyright 2020 Andrey Tolpeev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,14 +16,18 @@

package com.github.vase4kin.teamcityapp.app_navigation

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.github.vase4kin.teamcityapp.R
import com.ncapdevi.fragnav.FragNavController
import com.ncapdevi.fragnav.FragNavTransactionOptions

const val ARG_SELECTED_TAB = "arg_selected_tab"

interface AppNavigationInteractor {
fun initNavigation()
fun initNavigation(savedInstanceState: Bundle?)
fun onSaveInstanceState(outState: Bundle?)
fun switchTab(index: Int)
}

Expand All @@ -42,16 +46,21 @@ class AppNavigationInteractorImpl constructor(

private lateinit var fragNavController: FragNavController

override fun initNavigation() {
override fun initNavigation(savedInstanceState: Bundle?) {
fragNavController = FragNavController(fragmentManager, R.id.container).apply {
fragmentHideStrategy = FragNavController.DETACH_ON_NAVIGATE_HIDE_ON_SWITCH
rootFragmentListener = this@AppNavigationInteractorImpl
defaultTransactionOptions = defaultTransition
}
fragNavController.initialize(FragNavController.TAB1)
fragNavController.initialize(savedInstanceState = savedInstanceState)
}

override fun switchTab(index: Int) {
fragNavController.switchTab(index)
}

override fun onSaveInstanceState(outState: Bundle?) {
fragNavController.onSaveInstanceState(outState)
outState?.putInt(ARG_SELECTED_TAB, fragNavController.currentStackIndex)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.github.vase4kin.teamcityapp.app_navigation

import android.os.Bundle
import android.os.Handler
import android.widget.TextView
import androidx.annotation.StringRes
Expand All @@ -35,7 +36,7 @@ interface BottomNavigationView {
/**
* Init bottom navigation
*/
fun initViews(listener: ViewListener)
fun initViews(listener: ViewListener, savedInstanceState: Bundle?)

fun showFavoritesFab()
fun showFilterFab()
Expand All @@ -62,12 +63,15 @@ class BottomNavigationViewImpl(

private lateinit var listener: BottomNavigationView.ViewListener

override fun initViews(listener: BottomNavigationView.ViewListener) {
override fun initViews(
listener: BottomNavigationView.ViewListener,
savedInstanceState: Bundle?
) {
this.listener = listener
initViews()
initBottomNavView()
initFab()
interactor.initNavigation()
interactor.initNavigation(savedInstanceState)
}

private fun initViews() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class HomeModule {
@Provides
fun providesFragmentFactory(): FragmentFactory = FragmentFactoryImpl()

@HomeActivityScope
@Provides
fun providesAppNavigationInteractor(
activity: HomeActivity,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Andrey Tolpeev
* Copyright 2020 Andrey Tolpeev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,14 +16,15 @@

package com.github.vase4kin.teamcityapp.home.presenter

import android.os.Bundle
import com.github.vase4kin.teamcityapp.app_navigation.AppNavigationItem

interface HomePresenter {

/**
* On activity create callback
*/
fun onCreate()
fun onCreate(savedInstanceState: Bundle? = null)

/**
* On activity destroy callback
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Andrey Tolpeev
* Copyright 2020 Andrey Tolpeev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,8 +16,10 @@

package com.github.vase4kin.teamcityapp.home.presenter

import android.os.Bundle
import com.github.vase4kin.teamcityapp.R
import com.github.vase4kin.teamcityapp.account.create.data.OnLoadingListener
import com.github.vase4kin.teamcityapp.app_navigation.ARG_SELECTED_TAB
import com.github.vase4kin.teamcityapp.app_navigation.AppNavigationItem
import com.github.vase4kin.teamcityapp.app_navigation.BottomNavigationView
import com.github.vase4kin.teamcityapp.buildlog.data.BuildLogInteractor
Expand Down Expand Up @@ -48,11 +50,18 @@ class HomePresenterImpl @Inject constructor(
/**
* {@inheritDoc}
*/
override fun onCreate() {
start()
override fun onCreate(savedInstanceState: Bundle?) {
start(savedInstanceState)
view.initViews(this)
// Set title for Projects tab
bottomNavigationView.setTitle(R.string.projects_drawer_item)
// Set title for Projects tab if nothing is saved
if (savedInstanceState == null) {
bottomNavigationView.setTitle(R.string.projects_drawer_item)
} else {
val selectedTab = savedInstanceState.getInt(ARG_SELECTED_TAB)
AppNavigationItem.values().getOrNull(selectedTab)?.let {
bottomNavigationView.setTitle(it.title)
}
}
// Load notifications
loadNotificationsCount()
}
Expand Down Expand Up @@ -118,9 +127,9 @@ class HomePresenterImpl @Inject constructor(
/**
* Open root projects if active user is available
*/
fun start() {
fun start(savedInstanceState: Bundle?) {
baseUrl = dataManager.activeUser.teamcityUrl
bottomNavigationView.initViews(this)
bottomNavigationView.initViews(this, savedInstanceState)
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 Andrey Tolpeev
* Copyright 2020 Andrey Tolpeev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@ import android.content.Intent
import android.os.Bundle
import com.github.vase4kin.teamcityapp.R
import com.github.vase4kin.teamcityapp.TeamCityApplication
import com.github.vase4kin.teamcityapp.app_navigation.AppNavigationInteractor
import com.github.vase4kin.teamcityapp.app_navigation.AppNavigationItem
import com.github.vase4kin.teamcityapp.base.extractor.BundleExtractorValues
import com.github.vase4kin.teamcityapp.drawer.view.DrawerTimeOut
Expand All @@ -39,10 +40,13 @@ class HomeActivity : DaggerAppCompatActivity() {
@Inject
lateinit var sharedUserStorage: SharedUserStorage

@Inject
lateinit var appNavigationInteractor: AppNavigationInteractor

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
presenter.onCreate()
presenter.onCreate(savedInstanceState)
}

override fun onResume() {
Expand Down Expand Up @@ -76,6 +80,11 @@ class HomeActivity : DaggerAppCompatActivity() {
}
}

override fun onSaveInstanceState(outState: Bundle) {
appNavigationInteractor.onSaveInstanceState(outState)
super.onSaveInstanceState(outState)
}

private fun reinitDeps() {
(this.applicationContext as TeamCityApplication).buildRestApiInjectorWithBaseUrl(
sharedUserStorage.activeUser.teamcityUrl
Expand Down

0 comments on commit f98d1ed

Please sign in to comment.