0
0
Fork 0
mirror of https://github.com/YTVanced/VancedManager synced 2024-11-17 16:55:10 +00:00

fixed tablayout and adapter

This commit is contained in:
X1nto 2020-08-26 16:29:05 +04:00
parent b1e0db8598
commit e0b0ce9130
3 changed files with 50 additions and 38 deletions

View file

@ -1,11 +1,12 @@
package com.vanced.manager.adapter package com.vanced.manager.adapter
import android.os.Bundle
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import androidx.viewpager2.adapter.FragmentStateAdapter import androidx.viewpager2.adapter.FragmentStateAdapter
import com.vanced.manager.ui.fragments.MainFragment import com.vanced.manager.ui.fragments.MainFragment
class SectionVariantAdapter(fragment: Fragment): FragmentStateAdapter(fragment) { class SectionVariantAdapter(fragmentActivity: FragmentActivity): FragmentStateAdapter(fragmentActivity) {
override fun getItemCount(): Int = 3 override fun getItemCount(): Int = 3

View file

@ -4,13 +4,15 @@ import android.content.Context
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import android.view.MenuItem import android.view.MenuItem
import android.view.View
import android.view.animation.AnimationUtils
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil import androidx.databinding.DataBindingUtil
import androidx.navigation.NavDestination import androidx.navigation.NavDestination
import androidx.navigation.findNavController import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupWithNavController import androidx.navigation.ui.setupWithNavController
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager.getDefaultSharedPreferences
import com.crowdin.platform.Crowdin import com.crowdin.platform.Crowdin
import com.crowdin.platform.LoadingStateListener import com.crowdin.platform.LoadingStateListener
import com.google.android.material.tabs.TabLayoutMediator import com.google.android.material.tabs.TabLayoutMediator
@ -46,6 +48,35 @@ class MainActivity : AppCompatActivity() {
} }
private val tabListener = object : TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab) {
getDefaultSharedPreferences(this@MainActivity).edit().apply {
when (tab.position) {
0 -> putString("vanced_variant", "nonroot").apply()
1 -> putString("vanced_variant", "music").apply()
2 -> putString("vanced_variant", "root").apply()
}
}
}
override fun onTabUnselected(tab: TabLayout.Tab) {
TODO("There's nothing to do here actually")
}
override fun onTabReselected(tab: TabLayout.Tab) {
getDefaultSharedPreferences(this@MainActivity).edit().apply {
when (tab.position) {
0 -> putString("vanced_variant", "nonroot").apply()
1 -> putString("vanced_variant", "music").apply()
2 -> putString("vanced_variant", "root").apply()
}
}
}
}
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
setFinalTheme(this) setFinalTheme(this)
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -56,7 +87,7 @@ class MainActivity : AppCompatActivity() {
setSupportActionBar(homeToolbar) setSupportActionBar(homeToolbar)
val appBarConfiguration = AppBarConfiguration(navHost.graph) val appBarConfiguration = AppBarConfiguration(navHost.graph)
homeToolbar.setupWithNavController(navHost, appBarConfiguration) homeToolbar.setupWithNavController(navHost, appBarConfiguration)
mainViewpager.adapter = SectionVariantAdapter() mainViewpager.adapter = SectionVariantAdapter(this@MainActivity)
TabLayoutMediator(mainTablayout, mainViewpager) { tab, position -> TabLayoutMediator(mainTablayout, mainViewpager) { tab, position ->
when (position) { when (position) {
0 -> tab.text = "nonroot" 0 -> tab.text = "nonroot"
@ -65,38 +96,13 @@ class MainActivity : AppCompatActivity() {
} }
}.attach() }.attach()
when (variant) { when (getDefaultSharedPreferences(this@MainActivity).getString("vanced_variant", "nonroot")) {
"nonroot" -> mainTablayout.getTabAt(0).select() "nonroot" -> mainTablayout.getTabAt(0)?.select()
"music" -> mainTablayout.getTabAt(1).select() "music" -> mainTablayout.getTabAt(1)?.select()
"nonroot" -> mainTablayout.getTabAt(2).select() "root" -> mainTablayout.getTabAt(2)?.select()
} }
mainTablayout.setOnTabSelectedListener(object : TabLayout.OnTabSelectedListener { mainTablayout.addOnTabSelectedListener(tabListener)
override fun onTabSelected(tab: TabLayout.Tab) {
getDefaultSharedPreferences(requireActivity()).edit().apply {
when (tab.position) {
0 -> putString("vanced_variant", "nonroot")
0 -> putString("vanced_variant", "music")
0 -> putString("vanced_variant", "root")
}
}
}
override fun onTabUnselected(tab: TabLayout.Tab) {}
override fun onTabReselected(tab: TabLayout.Tab) {
getDefaultSharedPreferences(requireActivity()).edit().apply {
when (tab.position) {
0 -> putString("vanced_variant", "nonroot")
0 -> putString("vanced_variant", "music")
0 -> putString("vanced_variant", "root")
}
}
}
}
} }
@ -119,6 +125,7 @@ class MainActivity : AppCompatActivity() {
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
binding.mainTablayout.removeOnTabSelectedListener(tabListener)
Crowdin.unregisterDataLoadingObserver(loadingObserver) Crowdin.unregisterDataLoadingObserver(loadingObserver)
} }
@ -164,7 +171,7 @@ class MainActivity : AppCompatActivity() {
} }
private fun initDialogs() { private fun initDialogs() {
val prefs = PreferenceManager.getDefaultSharedPreferences(this) val prefs = getDefaultSharedPreferences(this)
val variant = prefs.getString("vanced_variant", "nonroot") val variant = prefs.getString("vanced_variant", "nonroot")
val showRootDialog = prefs.getBoolean("show_root_dialog", true) val showRootDialog = prefs.getBoolean("show_root_dialog", true)
@ -178,7 +185,6 @@ class MainActivity : AppCompatActivity() {
} }
!prefs.getBoolean("statement", true) -> DialogContainer.statementFalse(this) !prefs.getBoolean("statement", true) -> DialogContainer.statementFalse(this)
variant == "root" -> { variant == "root" -> {
if (PackageHelper.getPackageVersionName( if (PackageHelper.getPackageVersionName(
"com.google.android.youtube", "com.google.android.youtube",
packageManager packageManager

View file

@ -1,8 +1,13 @@
package com.vanced.manager.ui.fragments package com.vanced.manager.ui.fragments
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import com.vanced.manager.R
class HomeFragment : Fragment() { class MainFragment : Fragment() {
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater, container: ViewGroup?,
@ -10,7 +15,7 @@ class HomeFragment : Fragment() {
): View? { ): View? {
requireActivity().title = getString(R.string.title_home) requireActivity().title = getString(R.string.title_home)
setHasOptionsMenu(true) setHasOptionsMenu(true)
return inflater.inflate(inflater, R.layout.fragment_main, container, false) return inflater.inflate(R.layout.fragment_main, container, false)
} }
} }