Added binding between navigation view and navigation components

Removed bunch of code focusing on the hamburger not being stationary
This commit is contained in:
Viktor De Pasquale 2019-04-11 21:17:54 +02:00
parent 94ba7cb0c5
commit 0efaddff23
8 changed files with 70 additions and 128 deletions

View File

@ -31,4 +31,8 @@ object Navigation {
navDirections { destination = R.id.logFragment }
}
object Main {
const val OPEN_NAV = 1
}
}

View File

@ -1,44 +1,26 @@
package com.topjohnwu.magisk.ui
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.fragment.app.Fragment
import com.google.android.material.navigation.NavigationView
import androidx.core.view.GravityCompat
import androidx.navigation.ui.setupWithNavController
import com.topjohnwu.magisk.ClassMap
import com.topjohnwu.magisk.Config
import com.topjohnwu.magisk.Const
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.databinding.ActivityMainBinding
import com.topjohnwu.magisk.model.navigation.Navigation
import com.topjohnwu.magisk.ui.base.MagiskActivity
import com.topjohnwu.magisk.ui.hide.MagiskHideFragment
import com.topjohnwu.magisk.ui.home.MagiskFragment
import com.topjohnwu.magisk.ui.log.LogFragment
import com.topjohnwu.magisk.ui.module.ModulesFragment
import com.topjohnwu.magisk.ui.module.ReposFragment
import com.topjohnwu.magisk.ui.settings.SettingsFragment
import com.topjohnwu.magisk.ui.superuser.SuperuserFragment
import com.topjohnwu.magisk.utils.Utils
import com.topjohnwu.net.Networking
import com.topjohnwu.superuser.Shell
import kotlinx.android.synthetic.main.toolbar.*
import org.koin.androidx.viewmodel.ext.android.viewModel
open class MainActivity : MagiskActivity<MainViewModel, ActivityMainBinding>(),
NavigationView.OnNavigationItemSelectedListener {
open class MainActivity : MagiskActivity<MainViewModel, ActivityMainBinding>() {
override val layoutRes: Int = R.layout.activity_main
override val viewModel: MainViewModel by viewModel()
override val navHostId: Int = R.id.main_nav_host
private val mDrawerHandler = Handler()
private var mDrawerItem: Int = 0
private var toolbarElevation: Float = 0.toFloat()
/*override fun getDarkTheme(): Int {
return R.style.AppTheme_Dark
}*/
@ -51,118 +33,39 @@ open class MainActivity : MagiskActivity<MainViewModel, ActivityMainBinding>(),
super.onCreate(savedInstanceState)
checkHideSection()
setSupportActionBar(toolbar)
setSupportActionBar(binding.mainInclude.mainToolbar)
val toggle = object :
ActionBarDrawerToggle(
this,
binding.drawerLayout,
toolbar,
R.string.magisk,
R.string.magisk
) {
override fun onDrawerOpened(drawerView: View) {
super.onDrawerOpened(drawerView)
super.onDrawerSlide(drawerView, 0f) // this disables the arrow @ completed tate
}
override fun onDrawerSlide(drawerView: View, slideOffset: Float) {
super.onDrawerSlide(drawerView, 0f) // this disables the animation
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
toolbarElevation = toolbar!!.elevation
}
binding.drawerLayout.addDrawerListener(toggle)
toggle.syncState()
if (savedInstanceState == null) {
val section = intent.getStringExtra(Const.Key.OPEN_SECTION)
fromShortcut = section != null
navigate(section)
}
binding.navView.setNavigationItemSelectedListener(this)
binding.navView.setupWithNavController(navController)
}
override fun onBackPressed() {
if (binding.drawerLayout.isDrawerOpen(binding.navView)) {
binding.drawerLayout.closeDrawer(binding.navView)
} else if (mDrawerItem != R.id.magisk && !fromShortcut) {
navigate(R.id.magisk)
} else {
finish()
super.onBackPressed()
}
}
override fun onNavigationItemSelected(menuItem: MenuItem): Boolean {
mDrawerHandler.removeCallbacksAndMessages(null)
mDrawerHandler.postDelayed({ navigate(menuItem.itemId) }, 250)
binding.drawerLayout.closeDrawer(binding.navView)
return true
override fun onSimpleEventDispatched(event: Int) {
super.onSimpleEventDispatched(event)
when (event) {
Navigation.Main.OPEN_NAV -> openNav()
}
}
private fun openNav() = binding.drawerLayout.openDrawer(GravityCompat.START)
fun checkHideSection() {
val menu = binding.navView.menu
menu.findItem(R.id.magiskhide).isVisible =
menu.findItem(R.id.magiskHideFragment).isVisible =
Shell.rootAccess() && Config.get<Any>(Config.Key.MAGISKHIDE) as Boolean
menu.findItem(R.id.modules).isVisible = Shell.rootAccess() && Config.magiskVersionCode >= 0
menu.findItem(R.id.downloads).isVisible = (Networking.checkNetworkStatus(this)
&& Shell.rootAccess() && Config.magiskVersionCode >= 0)
menu.findItem(R.id.log).isVisible = Shell.rootAccess()
menu.findItem(R.id.superuser).isVisible = Utils.showSuperUser()
}
@Deprecated("Will be replaced by navigation components")
fun navigate(item: String?) {
var itemId = R.id.magisk
if (item != null) {
when (item) {
"superuser" -> itemId = R.id.superuser
"modules" -> itemId = R.id.modules
"downloads" -> itemId = R.id.downloads
"magiskhide" -> itemId = R.id.magiskhide
"log" -> itemId = R.id.log
"settings" -> itemId = R.id.settings
}
}
navigate(itemId)
}
@Deprecated("Will be replaced by navigation components")
fun navigate(itemId: Int) {
mDrawerItem = itemId
binding.navView.setCheckedItem(itemId)
when (itemId) {
R.id.magisk -> {
fromShortcut = false
displayFragment(MagiskFragment(), true)
}
R.id.superuser -> displayFragment(SuperuserFragment(), true)
R.id.modules -> displayFragment(ModulesFragment(), true)
R.id.downloads -> displayFragment(ReposFragment(), true)
R.id.magiskhide -> displayFragment(MagiskHideFragment(), true)
R.id.log -> displayFragment(LogFragment(), false)
R.id.settings -> displayFragment(SettingsFragment(), true)
}
}
@Deprecated("")
private fun displayFragment(navFragment: Fragment, setElevation: Boolean) {
/*supportInvalidateOptionsMenu();
getSupportFragmentManager()
.beginTransaction()
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.replace(R.id.content_frame, navFragment)
.commitNow();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
toolbar.setElevation(setElevation ? toolbarElevation : 0);
}*/
}
companion object {
private var fromShortcut = false
menu.findItem(R.id.modulesFragment).isVisible =
Shell.rootAccess() && Config.magiskVersionCode >= 0
menu.findItem(R.id.reposFragment).isVisible =
(Networking.checkNetworkStatus(this) && Shell.rootAccess() && Config.magiskVersionCode >= 0)
menu.findItem(R.id.logFragment).isVisible =
Shell.rootAccess()
menu.findItem(R.id.superuserFragment).isVisible =
Utils.showSuperUser()
}
}

View File

@ -1,6 +1,11 @@
package com.topjohnwu.magisk.ui
import com.topjohnwu.magisk.model.navigation.Navigation
import com.topjohnwu.magisk.ui.base.MagiskViewModel
class MainViewModel : MagiskViewModel()
class MainViewModel : MagiskViewModel() {
fun navPressed() = Navigation.Main.OPEN_NAV.publish()
}

View File

@ -0,0 +1,11 @@
package com.topjohnwu.magisk.utils
import android.view.View
import androidx.appcompat.widget.Toolbar
import androidx.databinding.BindingAdapter
@BindingAdapter("onNavigationClick")
fun setOnNavigationClickedListener(view: Toolbar, listener: View.OnClickListener) {
view.setNavigationOnClickListener(listener)
}

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#fff"
android:pathData="M3,6H21V8H3V6M3,11H21V13H3V11M3,16H21V18H3V16Z" />
</vector>

View File

@ -19,6 +19,7 @@
tools:openDrawer="start">
<include
android:id="@+id/main_include"
layout="@layout/activity_main_content"
viewModel="@{viewModel}" />

View File

@ -19,7 +19,15 @@
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="@layout/toolbar" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/main_toolbar"
onNavigationClick="@{() -> viewModel.navPressed()}"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/primary"
app:navigationIcon="@drawable/ic_menu"
app:title="@string/magisk"
app:titleTextColor="@android:color/white" />
</com.google.android.material.appbar.AppBarLayout>

View File

@ -6,17 +6,17 @@
android:id="@+id/main_group">
<item
android:id="@+id/magisk"
android:id="@+id/magiskFragment"
android:icon="@drawable/ic_magisk_outline"
android:title="@string/magisk"/>
<item
android:id="@+id/superuser"
android:id="@+id/superuserFragment"
android:icon="@drawable/ic_superuser"
android:title="@string/superuser"
android:visible="false"/>
<item
android:id="@+id/magiskhide"
android:id="@+id/magiskHideFragment"
android:icon="@drawable/ic_magiskhide"
android:title="@string/magiskhide"
android:visible="false"/>
@ -28,13 +28,13 @@
android:id="@+id/second_group">
<item
android:id="@+id/modules"
android:id="@+id/modulesFragment"
android:icon="@drawable/ic_extension"
android:title="@string/modules"
android:visible="false"/>
<item
android:id="@+id/downloads"
android:id="@+id/reposFragment"
android:icon="@drawable/ic_cloud_download"
android:title="@string/downloads"/>
@ -45,7 +45,7 @@
android:id="@+id/third_group">
<item
android:id="@+id/log"
android:id="@+id/logFragment"
android:icon="@drawable/ic_bug_report"
android:title="@string/log" />