0
0
Fork 0
mirror of https://github.com/YTVanced/VancedManager synced 2024-11-22 03:05:11 +00:00

Optimised changelog viewpager

This commit is contained in:
Xinto 2020-09-19 17:56:24 +04:00
parent 9b18450ad6
commit baba594900
19 changed files with 88 additions and 301 deletions

1
.gitignore vendored
View file

@ -4,6 +4,7 @@ build/
out/ out/
app/src/main/java/com/vanced/manager/core/base/DummyJava.java app/src/main/java/com/vanced/manager/core/base/DummyJava.java
app/build/ app/build/
app/release
local.properties local.properties
/.github/ /.github/
*.iml *.iml

View file

@ -0,0 +1,38 @@
package com.vanced.manager.adapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.vanced.manager.R
import com.vanced.manager.ui.viewmodels.HomeViewModel
class ChangelogAdapter(private val variant: String, viewModel: HomeViewModel?): RecyclerView.Adapter<ChangelogAdapter.ChangelogViewHolder>() {
private val nonrootChangelogs = arrayOf(viewModel?.vanced?.get()?.changelog?.get(), viewModel?.music?.get()?.changelog?.get(), viewModel?.microg?.get()?.changelog?.get(), viewModel?.manager?.get()?.changelog?.get())
private val rootChangelogs = arrayOf(viewModel?.vanced?.get()?.changelog?.get(), viewModel?.manager?.get()?.changelog?.get())
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChangelogViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val view = layoutInflater.inflate(R.layout.view_changelog, parent, false)
return ChangelogViewHolder(view)
}
override fun getItemCount(): Int = if (variant == "root") 2 else 4
override fun onBindViewHolder(holder: ChangelogViewHolder, position: Int) {
holder.changelog.text =
if (variant == "root")
rootChangelogs[position]
else
nonrootChangelogs[position]
}
open class ChangelogViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val changelog: TextView = itemView.findViewById(R.id.changelog_text)
}
}

View file

@ -1,23 +0,0 @@
package com.vanced.manager.adapter
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.vanced.manager.ui.fragments.ManagerChangelogFragment
import com.vanced.manager.ui.fragments.MicrogChangelogFragment
import com.vanced.manager.ui.fragments.MusicChangelogFragment
import com.vanced.manager.ui.fragments.VancedChangelogFragment
class SectionPageAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {
override fun getItemCount(): Int = 3
override fun createFragment(position: Int): Fragment {
return when (position) {
0 -> VancedChangelogFragment()
1 -> MusicChangelogFragment()
2 -> MicrogChangelogFragment()
else -> ManagerChangelogFragment()
}
}
}

View file

@ -1,20 +0,0 @@
package com.vanced.manager.adapter
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.vanced.manager.ui.fragments.ManagerChangelogFragment
import com.vanced.manager.ui.fragments.VancedChangelogFragment
class SectionPageRootAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {
override fun getItemCount(): Int = 2
override fun createFragment(position: Int): Fragment {
return when (position) {
0 -> VancedChangelogFragment()
1 -> ManagerChangelogFragment()
else -> throw NotImplementedError()
}
}
}

View file

@ -5,7 +5,7 @@ import androidx.fragment.app.FragmentActivity
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(fragmentActivity: FragmentActivity) : FragmentStateAdapter(fragmentActivity) { class VariantAdapter(fragmentActivity: FragmentActivity) : FragmentStateAdapter(fragmentActivity) {
override fun getItemCount(): Int = 2 override fun getItemCount(): Int = 2

View file

@ -3,6 +3,7 @@ package com.vanced.manager.model
import android.content.Context import android.content.Context
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.os.Build import android.os.Build
import android.util.Log
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.databinding.ObservableBoolean import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableField import androidx.databinding.ObservableField
@ -37,9 +38,9 @@ open class DataModel(
fun fetch() { fun fetch() {
isAppInstalled.set(isPackageInstalled(appPkg, context.packageManager)) isAppInstalled.set(isPackageInstalled(appPkg, context.packageManager))
versionName.set(jsonObject?.string("version")?.removeSuffix("-vanced") ?: context.getString(R.string.unavailable)) versionName.set(jsonObject?.string("version")?.removeSuffix("-vanced") ?: context.getString(R.string.unavailable))
installedVersionName.set(getPkgVersionName(isAppInstalled.get(), appPkg)) installedVersionName.set(getPkgVersionName(isAppInstalled.get(), appPkg))
versionCode.set(jsonObject?.int("versionCode")?: 0) versionCode.set(jsonObject?.int("versionCode") ?: 0)
installedVersionCode.set(getPkgVersionCode(isAppInstalled.get(), appPkg)) installedVersionCode.set(getPkgVersionCode(isAppInstalled.get(), appPkg))
buttonTxt.set(compareInt(installedVersionCode.get(), versionCode.get())) buttonTxt.set(compareInt(installedVersionCode.get(), versionCode.get()))
buttonIcon.set(compareIntDrawable(installedVersionCode.get(), versionCode.get())) buttonIcon.set(compareIntDrawable(installedVersionCode.get(), versionCode.get()))

View file

@ -15,7 +15,7 @@ import com.google.android.material.tabs.TabLayoutMediator
import com.google.firebase.messaging.FirebaseMessaging import com.google.firebase.messaging.FirebaseMessaging
import com.topjohnwu.superuser.Shell import com.topjohnwu.superuser.Shell
import com.vanced.manager.R import com.vanced.manager.R
import com.vanced.manager.adapter.SectionVariantAdapter import com.vanced.manager.adapter.VariantAdapter
import com.vanced.manager.databinding.ActivityMainBinding import com.vanced.manager.databinding.ActivityMainBinding
import com.vanced.manager.ui.dialogs.DialogContainer import com.vanced.manager.ui.dialogs.DialogContainer
import com.vanced.manager.ui.fragments.UpdateCheckFragment import com.vanced.manager.ui.fragments.UpdateCheckFragment
@ -78,7 +78,7 @@ class MainActivity : AppCompatActivity() {
with(binding) { with(binding) {
lifecycleOwner = this@MainActivity lifecycleOwner = this@MainActivity
setSupportActionBar(homeToolbar) setSupportActionBar(homeToolbar)
mainViewpager.adapter = SectionVariantAdapter(this@MainActivity) mainViewpager.adapter = VariantAdapter(this@MainActivity)
mainViewpager.isUserInputEnabled = false mainViewpager.isUserInputEnabled = false
TabLayoutMediator(mainTablayout, mainViewpager) { tab, position -> TabLayoutMediator(mainTablayout, mainViewpager) { tab, position ->
tab.text = if (position == 1) "root" else "nonroot" tab.text = if (position == 1) "root" else "nonroot"

View file

@ -14,12 +14,12 @@ import androidx.navigation.findNavController
import com.google.android.material.tabs.TabLayout import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator import com.google.android.material.tabs.TabLayoutMediator
import com.vanced.manager.R import com.vanced.manager.R
import com.vanced.manager.adapter.SectionPageAdapter import com.vanced.manager.adapter.ChangelogAdapter
import com.vanced.manager.adapter.SectionPageRootAdapter
import com.vanced.manager.databinding.FragmentHomeBinding import com.vanced.manager.databinding.FragmentHomeBinding
import com.vanced.manager.ui.dialogs.DialogContainer.installAlertBuilder import com.vanced.manager.ui.dialogs.DialogContainer.installAlertBuilder
import com.vanced.manager.ui.events.Event import com.vanced.manager.ui.events.Event
import com.vanced.manager.ui.viewmodels.HomeViewModel import com.vanced.manager.ui.viewmodels.HomeViewModel
import com.vanced.manager.ui.viewmodels.HomeViewModelFactory
import com.vanced.manager.utils.AppUtils.installing import com.vanced.manager.utils.AppUtils.installing
open class HomeFragment : Fragment(), View.OnClickListener { open class HomeFragment : Fragment(), View.OnClickListener {
@ -27,7 +27,9 @@ open class HomeFragment : Fragment(), View.OnClickListener {
private lateinit var binding: FragmentHomeBinding private lateinit var binding: FragmentHomeBinding
private lateinit var variant: String private lateinit var variant: String
private var isExpanded: Boolean = false private var isExpanded: Boolean = false
private val viewModel: HomeViewModel by viewModels() private val viewModel: HomeViewModel by viewModels {
HomeViewModelFactory(requireActivity().application, variant)
}
private val localBroadcastManager by lazy { LocalBroadcastManager.getInstance(requireActivity()) } private val localBroadcastManager by lazy { LocalBroadcastManager.getInstance(requireActivity()) }
override fun onCreateView( override fun onCreateView(
@ -39,13 +41,11 @@ open class HomeFragment : Fragment(), View.OnClickListener {
variant = if (requireActivity().findViewById<TabLayout>(R.id.main_tablayout).selectedTabPosition == 1) "root" else "nonroot" variant = if (requireActivity().findViewById<TabLayout>(R.id.main_tablayout).selectedTabPosition == 1) "root" else "nonroot"
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false) binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false)
binding.viewModel = viewModel binding.viewModel = viewModel
viewModel.variant = variant
return binding.root return binding.root
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
viewModel.navigateDestination.observe(viewLifecycleOwner, Observer<Event<Int>> { viewModel.navigateDestination.observe(viewLifecycleOwner, Observer<Event<Int>> {
val content = it.getContentIfNotHandled() val content = it.getContentIfNotHandled()
if(content != null){ if(content != null){
@ -73,27 +73,17 @@ open class HomeFragment : Fragment(), View.OnClickListener {
} }
with(binding.includeChangelogsLayout) { with(binding.includeChangelogsLayout) {
viewpager.adapter = viewpager.adapter = ChangelogAdapter(variant, this@HomeFragment.viewModel)
if (variant == "root") val nonrootTitles = arrayOf("Vanced", "Music", "microG", "Manager")
SectionPageRootAdapter(this@HomeFragment) val rootTitles = arrayOf("Vanced", "Manager")
else
SectionPageAdapter(this@HomeFragment)
TabLayoutMediator(tablayout, viewpager) { tab, position -> TabLayoutMediator(tablayout, viewpager) { tab, position ->
tab.text = tab.text =
if (variant == "root") { if (variant == "root") {
when (position) { rootTitles[position]
0 -> "Vanced" } else {
else -> "Manager" nonrootTitles[position]
} }
} else {
when (position) {
0 -> "Vanced"
1 -> "Music"
2 -> "MicroG"
else -> "Manager"
}
}
}.attach() }.attach()
} }

View file

@ -1,28 +0,0 @@
package com.vanced.manager.ui.fragments
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.vanced.manager.R
import com.vanced.manager.databinding.FragmentManagerChangelogBinding
import com.vanced.manager.ui.viewmodels.HomeViewModel
class ManagerChangelogFragment : Fragment() {
private lateinit var binding: FragmentManagerChangelogBinding
private val viewModel: HomeViewModel by viewModels()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_manager_changelog, container, false)
binding.viewModel = viewModel
return binding.root
}
}

View file

@ -1,28 +0,0 @@
package com.vanced.manager.ui.fragments
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.vanced.manager.R
import com.vanced.manager.databinding.FragmentMicrogChangelogBinding
import com.vanced.manager.ui.viewmodels.HomeViewModel
class MicrogChangelogFragment : Fragment() {
private lateinit var binding: FragmentMicrogChangelogBinding
private val viewModel: HomeViewModel by viewModels()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_microg_changelog, container, false)
binding.viewModel = viewModel
return binding.root
}
}

View file

@ -1,32 +0,0 @@
package com.vanced.manager.ui.fragments
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.vanced.manager.R
import com.vanced.manager.databinding.FragmentMusicChangelogBinding
import com.vanced.manager.ui.viewmodels.HomeViewModel
class MusicChangelogFragment : Fragment() {
private lateinit var binding: FragmentMusicChangelogBinding
private val viewModel: HomeViewModel by viewModels()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_music_changelog, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.viewModel = viewModel
}
}

View file

@ -1,28 +0,0 @@
package com.vanced.manager.ui.fragments
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.vanced.manager.R
import com.vanced.manager.databinding.FragmentVancedChangelogBinding
import com.vanced.manager.ui.viewmodels.HomeViewModel
class VancedChangelogFragment : Fragment() {
private lateinit var binding: FragmentVancedChangelogBinding
private val viewModel: HomeViewModel by viewModels()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_vanced_changelog, container, false)
binding.viewModel = viewModel
return binding.root
}
}

View file

@ -5,6 +5,7 @@ import android.content.ActivityNotFoundException
import android.content.ComponentName import android.content.ComponentName
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.util.Log
import android.widget.Toast import android.widget.Toast
import androidx.core.content.ContextCompat.startActivity import androidx.core.content.ContextCompat.startActivity
import androidx.databinding.ObservableBoolean import androidx.databinding.ObservableBoolean
@ -28,13 +29,12 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
open class HomeViewModel(application: Application): AndroidViewModel(application) { open class HomeViewModel(application: Application, val variant: String): AndroidViewModel(application) {
val app = application val app = application
private val managerApp = application as App private val managerApp = application as App
//val variant = getDefaultSharedPreferences(application).getString("vanced_variant", "nonroot") //val variant = getDefaultSharedPreferences(application).getString("vanced_variant", "nonroot")
var variant = "nonroot"
val vanced = ObservableField<DataModel>() val vanced = ObservableField<DataModel>()
val microg = ObservableField<DataModel>() val microg = ObservableField<DataModel>()
@ -61,7 +61,7 @@ open class HomeViewModel(application: Application): AndroidViewModel(application
private val microgToast = Toast.makeText(app, R.string.no_microg, Toast.LENGTH_LONG) private val microgToast = Toast.makeText(app, R.string.no_microg, Toast.LENGTH_LONG)
private val vancedPkgName = private val vancedPkgName =
if (variant == "root") if (variant == "root")
"com.google.android.youtube" "com.google.android.youtube"
else else
"com.vanced.android.youtube" "com.vanced.android.youtube"
@ -144,16 +144,14 @@ open class HomeViewModel(application: Application): AndroidViewModel(application
init { init {
fetching.set(true) fetching.set(true)
//while (managerApp.manager == null)
//this.wait()
vanced.set(DataModel(managerApp.vanced, variant, "vanced", app)) vanced.set(DataModel(managerApp.vanced, variant, "vanced", app))
microg.set(DataModel(managerApp.microg, app = "microg", context = app))
music.set(DataModel(managerApp.music, app = "music", context = app)) music.set(DataModel(managerApp.music, app = "music", context = app))
microg.set(DataModel(managerApp.microg, app = "microg", context = app))
manager.set(DataModel(managerApp.manager, app = "manager", context = app)) manager.set(DataModel(managerApp.manager, app = "manager", context = app))
vancedProgress.set(ProgressModel()) vancedProgress.set(ProgressModel())
musicProgress.set(ProgressModel()) musicProgress.set(ProgressModel())
microgProgress.set(ProgressModel()) microgProgress.set(ProgressModel())
Log.d("Test", variant)
fetching.set(false) fetching.set(false)
} }

View file

@ -0,0 +1,14 @@
package com.vanced.manager.ui.viewmodels
import android.app.Application
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
class HomeViewModelFactory(private val application: Application, private val variant: String = "nonroot") :
ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return HomeViewModel(application, variant) as T
}
}

View file

@ -1,26 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="com.vanced.manager.ui.viewmodels.HomeViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="16sp"
android:text="@{viewModel.manager.changelog}"/>
</LinearLayout>
</layout>

View file

@ -1,27 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="com.vanced.manager.ui.viewmodels.HomeViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.fragments.MicrogChangelogFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="16sp"
android:text="@{viewModel.microg.changelog}"/>
</LinearLayout>
</layout>

View file

@ -1,27 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="com.vanced.manager.ui.viewmodels.HomeViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.fragments.MusicChangelogFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="16sp"
android:text="@{viewModel.music.changelog}"/>
</LinearLayout>
</layout>

View file

@ -1,28 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="com.vanced.manager.ui.viewmodels.HomeViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.fragments.VancedChangelogFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="16sp"
android:text="@{viewModel.vanced.changelog}"/>
</LinearLayout>
</layout>

View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/changelog_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="16sp"/>
</FrameLayout>