Updated fragment system, again

This commit is contained in:
X1nto 2020-04-14 01:07:19 +04:00
parent 9b7f704863
commit 246af47421
11 changed files with 50 additions and 88 deletions

View File

@ -3,7 +3,7 @@
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vanced.manager"> package="com.vanced.manager">
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" /> <uses-permission android:name="android.permission.DELETE_PACKAGES" />
<application <application
android:allowBackup="true" android:allowBackup="true"

View File

@ -5,7 +5,6 @@ import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import com.google.android.material.bottomnavigation.BottomNavigationView import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.fragment.app.FragmentTransaction import androidx.fragment.app.FragmentTransaction
import com.google.android.material.appbar.MaterialToolbar import com.google.android.material.appbar.MaterialToolbar
@ -19,17 +18,10 @@ class MainActivity : ThemeActivity() {
private val homeFragment: HomeFragment = HomeFragment() private val homeFragment: HomeFragment = HomeFragment()
private val settingsFragment: SettingsFragment = SettingsFragment() private val settingsFragment: SettingsFragment = SettingsFragment()
private var currentFragment: Fragment? = null
private var activeFragment: Int = R.id.navigation_home
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
savedInstanceState?.let {
activeFragment = it.getInt(CURRENT_FRAGMENT, R.id.navigation_home)
}
val toolbar: MaterialToolbar = findViewById(R.id.home_toolbar) val toolbar: MaterialToolbar = findViewById(R.id.home_toolbar)
setSupportActionBar(toolbar) setSupportActionBar(toolbar)
@ -39,24 +31,45 @@ class MainActivity : ThemeActivity() {
showSecurityDialog() showSecurityDialog()
} }
when (activeFragment) { supportFragmentManager
R.id.navigation_home -> currentFragment = homeFragment .beginTransaction()
R.id.navigation_settings -> currentFragment = settingsFragment .add(R.id.frame_layout, settingsFragment).hide(settingsFragment)
} .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.add(R.id.frame_layout, homeFragment)
if (savedInstanceState == null) { .commit()
supportFragmentManager supportActionBar?.title = getString(R.string.title_home)
.beginTransaction()
.add(R.id.frame_layout, settingsFragment).hide(settingsFragment)
.add(R.id.frame_layout, homeFragment).hide(homeFragment)
.show(currentFragment!!)
.commit()
}
val navView: BottomNavigationView = findViewById(R.id.bottom_nav) val navView: BottomNavigationView = findViewById(R.id.bottom_nav)
navView.setOnNavigationItemSelectedListener{ navView.setOnNavigationItemSelectedListener{ item ->
setFragments(it.itemId) when (item.itemId) {
R.id.navigation_home -> {
supportFragmentManager
.beginTransaction()
.hide(settingsFragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.add(homeFragment)
.commit()
supportActionBar?.title = getString(R.string.title_home)
return@setOnNavigationItemSelectedListener true
}
R.id.navigation_settings -> {
supportFragmentManager
.beginTransaction()
.hide(homeFragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.add(settingsFragment)
.commit()
supportActionBar?.title = getString(R.string.title_settings)
return@setOnNavigationItemSelectedListener true
}
}
false
}
navView.setOnNavigationItemReselectedListener {
} }
} }
@ -90,41 +103,4 @@ class MainActivity : ThemeActivity() {
editor.apply() editor.apply()
} }
private fun setFragments(itemId: Int): Boolean {
activeFragment = itemId
when (itemId) {
R.id.navigation_home -> {
if (currentFragment is HomeFragment) {
return false
}
supportFragmentManager
.beginTransaction()
.hide(currentFragment!!)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.show(homeFragment)
.commit()
currentFragment = homeFragment
}
R.id.navigation_settings -> {
if (currentFragment is SettingsFragment) {
return false
}
supportFragmentManager
.beginTransaction()
.hide(currentFragment!!)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.show(settingsFragment)
.commit()
currentFragment = settingsFragment
}
}
return true
}
companion object{
const val CURRENT_FRAGMENT = "current_fragment"
}
} }

View File

@ -24,9 +24,8 @@ open class ThemeActivity : AppCompatActivity() {
setFinalTheme(currentTheme) setFinalTheme(currentTheme)
if (android.os.Build.VERSION.SDK_INT < 28) { if (android.os.Build.VERSION.SDK_INT < 28) {
setTaskBG() setTaskBG()
super.onCreate(savedInstanceState)
} }
super.onCreate(savedInstanceState)
} }
override fun onResume() { override fun onResume() {

View File

@ -42,7 +42,6 @@ class HomeFragment : Fragment() {
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
(activity as MainActivity).supportActionBar?.title = getString(R.string.title_home)
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)

View File

@ -2,6 +2,7 @@ package com.vanced.manager.ui.fragments
import android.content.res.Configuration import android.content.res.Configuration
import android.os.Bundle import android.os.Bundle
import android.view.View
import androidx.preference.ListPreference import androidx.preference.ListPreference
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
@ -13,8 +14,6 @@ class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences, rootKey) setPreferencesFromResource(R.xml.preferences, rootKey)
(activity as MainActivity).supportActionBar?.title = getString(R.string.title_settings)
val updateCheck: Preference? = findPreference("update_check") val updateCheck: Preference? = findPreference("update_check")
val themeSwitch: ListPreference? = findPreference("theme_mode") val themeSwitch: ListPreference? = findPreference("theme_mode")

View File

@ -1,6 +1,6 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" <vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp" android:width="34dp"
android:height="24dp" android:height="34dp"
android:viewportWidth="24.0" android:viewportWidth="24.0"
android:viewportHeight="24.0"> android:viewportHeight="24.0">
<path <path

View File

@ -1,6 +1,6 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" <vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp" android:width="34dp"
android:height="24dp" android:height="34dp"
android:viewportWidth="24" android:viewportWidth="24"
android:viewportHeight="24"> android:viewportHeight="24">
<path <path

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="?colorSurface" /> <item android:drawable="?colorSurface" />

View File

@ -7,21 +7,6 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<!--
If my own implementation of bottom navigation viewer
will suck a HUGE dick, I'll use navigation component
<fragment
android:id="@+id/nav_host_fragment"
android:layout_width="0dp"
android:layout_height="0dp"
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="true"
app:navGraph="@navigation/mobile_navigation"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"/> -->
<com.google.android.material.appbar.AppBarLayout <com.google.android.material.appbar.AppBarLayout
android:id="@+id/home_appbar" android:id="@+id/home_appbar"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -48,6 +33,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="?colorSurface" android:background="?colorSurface"
android:layout_alignParentBottom="true" android:layout_alignParentBottom="true"
app:labelVisibilityMode="unlabeled"
app:menu="@menu/bottom_nav_menu" /> app:menu="@menu/bottom_nav_menu" />
<FrameLayout <FrameLayout

View File

@ -1,6 +1,6 @@
<resources> <resources>
<style name="SplashTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar"> <style name="SplashTheme" parent="Theme.MaterialComponents.NoActionBar">
<item name="android:windowBackground">@drawable/splash_logo</item> <item name="android:windowBackground">@drawable/splash_logo</item>
<item name="android:windowTranslucentStatus">true</item> <item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item> <item name="android:windowTranslucentNavigation">true</item>

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" <PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app="http://schemas.android.com/apk/res-auto">
<Preference <Preference
@ -24,6 +25,7 @@
android:icon="@drawable/ic_palette_black_24dp" android:icon="@drawable/ic_palette_black_24dp"
android:key="accent_color" android:key="accent_color"
android:title="Accent Color" android:title="Accent Color"
app:isPreferenceVisible="false"
android:entries="@array/accent_color" android:entries="@array/accent_color"
android:entryValues="@array/accent_color"/> android:entryValues="@array/accent_color"/>