Merge pull request #1238 from Andries-Smit/feat/home-back-button

Home back button improvements
This commit is contained in:
Milos Kozak 2022-01-29 19:03:14 +01:00 committed by GitHub
commit ad8e95440d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -21,6 +21,7 @@ import android.widget.TextView
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.Toolbar
import androidx.core.view.GravityCompat
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayoutMediator
import com.google.firebase.crashlytics.FirebaseCrashlytics
@ -87,6 +88,7 @@ class MainActivity : NoSplashAppCompatActivity() {
private lateinit var actionBarDrawerToggle: ActionBarDrawerToggle
private var pluginPreferencesMenuItem: MenuItem? = null
private var menu: Menu? = null
private var menuOpen = false
private lateinit var binding: ActivityMainBinding
@ -258,11 +260,20 @@ class MainActivity : NoSplashAppCompatActivity() {
}
override fun onMenuOpened(featureId: Int, menu: Menu): Boolean {
menuOpen = true
if (binding.mainDrawerLayout.isDrawerOpen(GravityCompat.START)) {
binding.mainDrawerLayout.closeDrawers()
}
val result = super.onMenuOpened(featureId, menu)
menu.findItem(R.id.nav_treatments)?.isEnabled = profileFunction.getProfile() != null
return result
}
override fun onPanelClosed(featureId: Int, menu: Menu) {
menuOpen = false;
super.onPanelClosed(featureId, menu)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
this.menu = menu
menuInflater.inflate(R.menu.menu_main, menu)
@ -359,6 +370,22 @@ class MainActivity : NoSplashAppCompatActivity() {
return actionBarDrawerToggle.onOptionsItemSelected(item)
}
override fun onBackPressed() {
if (binding.mainDrawerLayout.isDrawerOpen(GravityCompat.START)) {
binding.mainDrawerLayout.closeDrawers()
return
}
if (menuOpen) {
this.menu?.close()
return
}
if (binding.mainPager.currentItem != 0) {
binding.mainPager.currentItem = 0
return
}
super.onBackPressed()
}
// Correct place for calling setUserStats() would be probably MainApp
// but we need to have it called at least once a day. Thus this location