Skip to content

Commit

Permalink
Merge pull request #427 from Infomaniak/fix-bottomnavigation-not-chec…
Browse files Browse the repository at this point in the history
…ked-item

fix bottomsheet item not reselected with custom
  • Loading branch information
KevinBoulongne authored Jan 7, 2022
2 parents 8a8fef9 + 83252ad commit 48d86a8
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/com/infomaniak/drive/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import androidx.lifecycle.lifecycleScope
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import coil.ImageLoader
import coil.request.ImageRequest
import coil.transform.CircleCropTransformation
Expand All @@ -60,6 +59,7 @@ import com.infomaniak.drive.data.services.DownloadReceiver
import com.infomaniak.drive.data.services.UploadWorker
import com.infomaniak.drive.launchInAppReview
import com.infomaniak.drive.utils.*
import com.infomaniak.drive.utils.NavigationUiUtils.setupWithNavControllerCustom
import com.infomaniak.drive.utils.SyncUtils.launchAllUpload
import com.infomaniak.drive.utils.SyncUtils.startContentObserverService
import com.infomaniak.drive.utils.Utils.getRootName
Expand Down Expand Up @@ -100,7 +100,7 @@ class MainActivity : BaseActivity() {
}
}

bottomNavigation.setupWithNavController(navController)
bottomNavigation.setupWithNavControllerCustom(navController)
bottomNavigation.itemIconTintList = ContextCompat.getColorStateList(this, R.color.item_icon_tint_bottom)
bottomNavigation.selectedItemId = UISettings(this).bottomNavigationSelectedItem
bottomNavigation.setOnItemReselectedListener { item ->
Expand Down
90 changes: 90 additions & 0 deletions app/src/main/java/com/infomaniak/drive/utils/NavigationUiUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Infomaniak kDrive - Android
* Copyright (C) 2022 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.drive.utils

import android.os.Bundle
import androidx.annotation.IdRes
import androidx.core.view.forEach
import androidx.navigation.NavController
import androidx.navigation.NavDestination
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.ui.NavigationUI.onNavDestinationSelected
import androidx.navigation.ui.onNavDestinationSelected
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.android.material.navigation.NavigationBarView
import com.google.android.material.navigationrail.NavigationRailView
import java.lang.ref.WeakReference

/**
* Custom version of [androidx.navigation.ui.NavigationUI].
*
* Currently, when we go to "Shared with me" for example, and return to the file list from
* the bottom navigation, we cannot reselect the menu item from the bottom navigation.
*
* @see [issue](https://issuetracker.google.com/issues/206147604)
*/
object NavigationUiUtils {

/**
* Sets up a [NavigationBarView] to use with a [NavController]. This will call
* [android.view.MenuItem.onNavDestinationSelected] when a menu item is selected.
*
* The selected item in the NavigationView will automatically be updated when the destination changes.
*/
fun NavigationBarView.setupWithNavControllerCustom(navController: NavController) {
setupWithNavController(this, navController)
}

/**
* Sets up a [NavigationBarView] to use with a [NavController]. This
* will call [onNavDestinationSelected] when a menu item is selected.
*
* The selected item in the NavigationBarView will automatically be updated when the destination changes.
*
* @param navigationBarView The NavigationBarView ([BottomNavigationView] or
* [NavigationRailView]) that should be kept in sync with changes to the NavController.
* @param navController The NavController that supplies the primary menu. Navigation actions
* on this NavController will be reflected in the selected item in the NavigationBarView.
*/
private fun setupWithNavController(navigationBarView: NavigationBarView, navController: NavController) {
navigationBarView.setOnItemSelectedListener { item ->
item.isChecked = true
onNavDestinationSelected(item, navController)
}

val weakReference = WeakReference(navigationBarView)
navController.addOnDestinationChangedListener(object : NavController.OnDestinationChangedListener {

override fun onDestinationChanged(controller: NavController, destination: NavDestination, arguments: Bundle?) {
val view = weakReference.get()
if (view == null) {
navController.removeOnDestinationChangedListener(this)
return
}
view.menu.forEach { item ->
if (destination.matchDestination(item.itemId) && !item.isChecked) {
item.isChecked = true
}
}
}

})
}

private fun NavDestination.matchDestination(@IdRes destId: Int) = hierarchy.any { it.id == destId }
}

0 comments on commit 48d86a8

Please sign in to comment.