Skip to content
This repository has been archived by the owner on Mar 15, 2022. It is now read-only.

Commit

Permalink
Switched to TabLayout for variant selection
Browse files Browse the repository at this point in the history
  • Loading branch information
X1nto committed Aug 26, 2020
1 parent 3805145 commit 3a3624e
Show file tree
Hide file tree
Showing 13 changed files with 305 additions and 81 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.vanced.manager.adapter

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.vanced.manager.ui.fragments.MainFragment

class SectionVariantAdapter(fragment: Fragment): FragmentStateAdapter(fragment) {

override fun getItemCount(): Int = 3

override fun createFragment(position: Int): Fragment = MainFragment()

}
54 changes: 54 additions & 0 deletions app/src/main/java/com/vanced/manager/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import androidx.navigation.ui.setupWithNavController
import androidx.preference.PreferenceManager
import com.crowdin.platform.Crowdin
import com.crowdin.platform.LoadingStateListener
import com.google.android.material.tabs.TabLayoutMediator
import com.google.android.material.tabs.TabLayout
import com.google.firebase.messaging.FirebaseMessaging
import com.vanced.manager.R
import com.vanced.manager.databinding.ActivityMainBinding
Expand All @@ -22,6 +24,7 @@ import com.vanced.manager.utils.AppUtils.installing
import com.vanced.manager.utils.InternetTools
import com.vanced.manager.utils.PackageHelper
import com.vanced.manager.utils.ThemeHelper.setFinalTheme
import com.vanced.manager.adapter.SectionVariantAdapter
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -53,10 +56,61 @@ class MainActivity : AppCompatActivity() {
setSupportActionBar(homeToolbar)
val appBarConfiguration = AppBarConfiguration(navHost.graph)
homeToolbar.setupWithNavController(navHost, appBarConfiguration)
mainViewpager.adapter = SectionVariantAdapter()
TabLayoutMediator(mainTablayout, mainViewpager) { tab, position ->
when (position) {
0 -> tab.text = "nonroot"
1 -> tab.text = "Music"
2 -> tab.text = "root"
}
}.attach()

when (variant) {
"nonroot" -> mainTablayout.getTabAt(0).select()
"music" -> mainTablayout.getTabAt(1).select()
"nonroot" -> mainTablayout.getTabAt(2).select()
}

mainTablayout.setOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {

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")
}
}
}

}

}

navHost.addOnDestinationChangedListener { _, currFrag: NavDestination, _ ->
setDisplayHomeAsUpEnabled(currFrag.id != R.id.home_fragment)
val tabHide = AnimationUtils.loadAnimation(this, R.anim.tablayout_exit)
val tabShow = AnimationUtils.loadAnimation(this, R.anim.tablayout_enter)
if (currFrag.id != R.id.home_fragment) {
binding.variantTabContainer.startAnimation(tabHide)
binding.variantTabContainer.visibility = View.GONE
} else {
binding.variantTabContainer.visibility = View.VISIBLE
binding.variantTabContainer.startAnimation(tabShow)
}
}

initDialogs()
Expand Down
18 changes: 15 additions & 3 deletions app/src/main/java/com/vanced/manager/ui/fragments/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,19 @@ class HomeFragment : Fragment(), View.OnClickListener {
super.onViewCreated(view, savedInstanceState)
binding.viewModel = viewModel

val variantPref = getDefaultSharedPreferences(requireActivity()).getString("vanced_variant", "nonroot")
val arg = "variant"
val variant = getDefaultSharedPreferences(requireActivity()).getString("vanced_variant")

with(binding) {
when (variant) {
"nonroot" -> includeMusicLayout.musicCard.visibility = View.GONE
"music" -> includeVancedLayout.vancedCard.visibility = View.GONE
"root" -> {
includeMusicLayout.musicCard.visibility = View.GONE
includeMicrogLayout.microgCard.visibility = View.GONE
}
}
}

with(binding) {
rootSwitch.setOnClickListener(this@HomeFragment)
Expand All @@ -72,9 +84,9 @@ class HomeFragment : Fragment(), View.OnClickListener {
}

with(binding.includeChangelogsLayout) {
viewpager.adapter = if (variantPref == "root") SectionPageRootAdapter(this@HomeFragment) else SectionPageAdapter(this@HomeFragment)
viewpager.adapter = if (variant == "root") SectionPageRootAdapter(this@HomeFragment) else SectionPageAdapter(this@HomeFragment)
TabLayoutMediator(tablayout, viewpager) { tab, position ->
if (variantPref == "root")
if (variant == "root")
when (position) {
0 -> tab.text = "Vanced"
1 -> tab.text = "Manager"
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/com/vanced/manager/ui/fragments/MainFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import androidx.fragment.app.Fragment

class HomeFragment : Fragment() {

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
requireActivity().title = getString(R.string.title_home)
setHasOptionsMenu(true)
return inflater.inflate(inflater, R.layout.fragment_main, container, false)
}

}
3 changes: 1 addition & 2 deletions app/src/main/java/com/vanced/manager/utils/AppUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ object AppUtils {
status.contains("IFile_Missing") -> context.getString(R.string.IFile_Missing)
status.contains("ModApk_Missing") -> context.getString(R.string.ModApk_Missing)
status.contains("Files_Missing_VA") -> context.getString(R.string.Files_Missing_VA)
status.contains("Corrupt_Data") -> context.getString(R.string.Corrupt_Data)
else ->
if (MiuiHelper.isMiui())
context.getString(R.string.installation_miui)
Expand All @@ -70,4 +69,4 @@ object AppUtils {
}


}
}
9 changes: 9 additions & 0 deletions app/src/main/res/anim/tablayout_enter.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

<translate
android:duration="500"
android:fromYDelta="-100%p"
android:toYDelta="0"

</set>
10 changes: 10 additions & 0 deletions app/src/main/res/anim/tablayout_exit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

<translate
android:duration="500"
android:fromYDelta="0"
android:toYDelta="-100%p"

</set>

34 changes: 24 additions & 10 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
android:id="@+id/bottom_nav_host"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/home_appbar"
app:defaultNavHost="true"
app:navGraph="@navigation/mobile_navigation"/>

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/home_appbar"
android:layout_width="match_parent"
Expand All @@ -33,7 +24,30 @@
android:background="@android:color/transparent"/>

</com.google.android.material.appbar.AppBarLayout>

<LinearLayout
android:id="@+id/variant_tab_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<com.google.android.material.tabs.TabLayout
android:id="@+id/main_tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?colorSurfaceVariant"
app:tabIndicatorColor="?colorPrimary"
app:tabSelectedTextColor="?colorPrimary"
app:tabTextColor="?colorPrimary" />

<androidx.viewpager2.widget.ViewPager2
android:id="@+id/main_viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />

</LinearLayout>

</RelativeLayout>

</layout>
</layout>
64 changes: 1 addition & 63 deletions app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,68 +29,7 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="4dp"
android:animateLayoutChanges="true"
android:orientation="vertical">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginEnd="25dp"
android:orientation="horizontal">

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true">

<com.google.android.material.button.MaterialButton
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/nonroot"
android:textAllCaps="false"
android:visibility="@{viewModel.nonrootModeSelected ? View.VISIBLE : View.GONE}"
bind:strokeColor="?colorPrimary"
bind:strokeWidth="2dp" />

<com.google.android.material.button.MaterialButton
android:id="@+id/nonroot_switch"
style="@style/ButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/nonroot"
android:visibility="@{viewModel.nonrootModeSelected ? View.GONE : View.VISIBLE}" />

</FrameLayout>

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true">

<com.google.android.material.button.MaterialButton
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/root"
android:textAllCaps="false"
android:visibility="@{viewModel.nonrootModeSelected ? View.GONE : View.VISIBLE}"
bind:strokeColor="?colorPrimary"
bind:strokeWidth="2dp" />

<com.google.android.material.button.MaterialButton
android:id="@+id/root_switch"
style="@style/ButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/root"
android:visibility="@{viewModel.nonrootModeSelected ? View.VISIBLE : View.GONE}" />

</FrameLayout>

</RelativeLayout>
android:orientation="horizontal">

<include
android:id="@+id/include_vanced_layout"
Expand All @@ -110,7 +49,6 @@
android:layout_marginStart="@dimen/stdp"
android:layout_marginTop="@dimen/stdp"
android:layout_marginEnd="@dimen/stdp"
android:visibility="@{viewModel.nonrootModeSelected ? View.VISIBLE : View.GONE}"
bind:viewModel="@{viewModel}" />

<include
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/layout/fragment_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/bottom_nav_host"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/home_appbar"
app:defaultNavHost="true"
app:navGraph="@navigation/mobile_navigation"/>
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_vanced_changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
android:layout_height="match_parent"
android:textSize="16sp" />

</FrameLayout>
</FrameLayout>
3 changes: 1 addition & 2 deletions app/src/main/res/layout/include_changelogs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
android:layout_height="wrap_content"
android:background="?colorSurfaceVariant"
android:visibility="gone"
app:layout_constraintTop_toBottomOf="parent"
app:tabIndicatorColor="?colorPrimary"
app:tabSelectedTextColor="?colorPrimary"
app:tabTextColor="?colorPrimary"
Expand All @@ -70,4 +69,4 @@

</com.google.android.material.card.MaterialCardView>

</layout>
</layout>
Loading

0 comments on commit 3a3624e

Please sign in to comment.