From baba5949006d6a229472e8e508cf5a82dddce9ae Mon Sep 17 00:00:00 2001 From: Xinto Date: Sat, 19 Sep 2020 17:56:24 +0400 Subject: [PATCH] Optimised changelog viewpager --- .gitignore | 1 + .../manager/adapter/ChangelogAdapter.kt | 38 +++++++++++++++++++ .../manager/adapter/SectionPageAdapter.kt | 23 ----------- .../manager/adapter/SectionPageRootAdapter.kt | 20 ---------- ...ionVariantAdapter.kt => VariantAdapter.kt} | 2 +- .../com/vanced/manager/model/DataModel.kt | 5 ++- .../com/vanced/manager/ui/MainActivity.kt | 4 +- .../manager/ui/fragments/HomeFragment.kt | 34 ++++++----------- .../ui/fragments/ManagerChangelogFragment.kt | 28 -------------- .../ui/fragments/MicrogChangelogFragment.kt | 28 -------------- .../ui/fragments/MusicChangelogFragment.kt | 32 ---------------- .../ui/fragments/VancedChangelogFragment.kt | 28 -------------- .../manager/ui/viewmodels/HomeViewModel.kt | 12 +++--- .../ui/viewmodels/HomeViewModelFactory.kt | 14 +++++++ .../res/layout/fragment_manager_changelog.xml | 26 ------------- .../res/layout/fragment_microg_changelog.xml | 27 ------------- .../res/layout/fragment_music_changelog.xml | 27 ------------- .../res/layout/fragment_vanced_changelog.xml | 28 -------------- app/src/main/res/layout/view_changelog.xml | 12 ++++++ 19 files changed, 88 insertions(+), 301 deletions(-) create mode 100644 app/src/main/java/com/vanced/manager/adapter/ChangelogAdapter.kt delete mode 100644 app/src/main/java/com/vanced/manager/adapter/SectionPageAdapter.kt delete mode 100644 app/src/main/java/com/vanced/manager/adapter/SectionPageRootAdapter.kt rename app/src/main/java/com/vanced/manager/adapter/{SectionVariantAdapter.kt => VariantAdapter.kt} (76%) delete mode 100644 app/src/main/java/com/vanced/manager/ui/fragments/ManagerChangelogFragment.kt delete mode 100644 app/src/main/java/com/vanced/manager/ui/fragments/MicrogChangelogFragment.kt delete mode 100644 app/src/main/java/com/vanced/manager/ui/fragments/MusicChangelogFragment.kt delete mode 100644 app/src/main/java/com/vanced/manager/ui/fragments/VancedChangelogFragment.kt create mode 100644 app/src/main/java/com/vanced/manager/ui/viewmodels/HomeViewModelFactory.kt delete mode 100644 app/src/main/res/layout/fragment_manager_changelog.xml delete mode 100644 app/src/main/res/layout/fragment_microg_changelog.xml delete mode 100644 app/src/main/res/layout/fragment_music_changelog.xml delete mode 100644 app/src/main/res/layout/fragment_vanced_changelog.xml create mode 100644 app/src/main/res/layout/view_changelog.xml diff --git a/.gitignore b/.gitignore index 683a3248..6b242099 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ build/ out/ app/src/main/java/com/vanced/manager/core/base/DummyJava.java app/build/ +app/release local.properties /.github/ *.iml diff --git a/app/src/main/java/com/vanced/manager/adapter/ChangelogAdapter.kt b/app/src/main/java/com/vanced/manager/adapter/ChangelogAdapter.kt new file mode 100644 index 00000000..fe9ba7ab --- /dev/null +++ b/app/src/main/java/com/vanced/manager/adapter/ChangelogAdapter.kt @@ -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() { + + 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) + } + +} diff --git a/app/src/main/java/com/vanced/manager/adapter/SectionPageAdapter.kt b/app/src/main/java/com/vanced/manager/adapter/SectionPageAdapter.kt deleted file mode 100644 index b0068675..00000000 --- a/app/src/main/java/com/vanced/manager/adapter/SectionPageAdapter.kt +++ /dev/null @@ -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() - } - } - -} diff --git a/app/src/main/java/com/vanced/manager/adapter/SectionPageRootAdapter.kt b/app/src/main/java/com/vanced/manager/adapter/SectionPageRootAdapter.kt deleted file mode 100644 index d3fd89df..00000000 --- a/app/src/main/java/com/vanced/manager/adapter/SectionPageRootAdapter.kt +++ /dev/null @@ -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() - } - } - -} \ No newline at end of file diff --git a/app/src/main/java/com/vanced/manager/adapter/SectionVariantAdapter.kt b/app/src/main/java/com/vanced/manager/adapter/VariantAdapter.kt similarity index 76% rename from app/src/main/java/com/vanced/manager/adapter/SectionVariantAdapter.kt rename to app/src/main/java/com/vanced/manager/adapter/VariantAdapter.kt index ae6df23e..5432397f 100644 --- a/app/src/main/java/com/vanced/manager/adapter/SectionVariantAdapter.kt +++ b/app/src/main/java/com/vanced/manager/adapter/VariantAdapter.kt @@ -5,7 +5,7 @@ import androidx.fragment.app.FragmentActivity import androidx.viewpager2.adapter.FragmentStateAdapter import com.vanced.manager.ui.fragments.MainFragment -class SectionVariantAdapter(fragmentActivity: FragmentActivity) : FragmentStateAdapter(fragmentActivity) { +class VariantAdapter(fragmentActivity: FragmentActivity) : FragmentStateAdapter(fragmentActivity) { override fun getItemCount(): Int = 2 diff --git a/app/src/main/java/com/vanced/manager/model/DataModel.kt b/app/src/main/java/com/vanced/manager/model/DataModel.kt index 2db9f0d1..8baae623 100644 --- a/app/src/main/java/com/vanced/manager/model/DataModel.kt +++ b/app/src/main/java/com/vanced/manager/model/DataModel.kt @@ -3,6 +3,7 @@ package com.vanced.manager.model import android.content.Context import android.graphics.drawable.Drawable import android.os.Build +import android.util.Log import androidx.core.content.ContextCompat import androidx.databinding.ObservableBoolean import androidx.databinding.ObservableField @@ -37,9 +38,9 @@ open class DataModel( fun fetch() { 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)) - versionCode.set(jsonObject?.int("versionCode")?: 0) + versionCode.set(jsonObject?.int("versionCode") ?: 0) installedVersionCode.set(getPkgVersionCode(isAppInstalled.get(), appPkg)) buttonTxt.set(compareInt(installedVersionCode.get(), versionCode.get())) buttonIcon.set(compareIntDrawable(installedVersionCode.get(), versionCode.get())) diff --git a/app/src/main/java/com/vanced/manager/ui/MainActivity.kt b/app/src/main/java/com/vanced/manager/ui/MainActivity.kt index d488a4d1..8332d77d 100644 --- a/app/src/main/java/com/vanced/manager/ui/MainActivity.kt +++ b/app/src/main/java/com/vanced/manager/ui/MainActivity.kt @@ -15,7 +15,7 @@ import com.google.android.material.tabs.TabLayoutMediator import com.google.firebase.messaging.FirebaseMessaging import com.topjohnwu.superuser.Shell 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.ui.dialogs.DialogContainer import com.vanced.manager.ui.fragments.UpdateCheckFragment @@ -78,7 +78,7 @@ class MainActivity : AppCompatActivity() { with(binding) { lifecycleOwner = this@MainActivity setSupportActionBar(homeToolbar) - mainViewpager.adapter = SectionVariantAdapter(this@MainActivity) + mainViewpager.adapter = VariantAdapter(this@MainActivity) mainViewpager.isUserInputEnabled = false TabLayoutMediator(mainTablayout, mainViewpager) { tab, position -> tab.text = if (position == 1) "root" else "nonroot" diff --git a/app/src/main/java/com/vanced/manager/ui/fragments/HomeFragment.kt b/app/src/main/java/com/vanced/manager/ui/fragments/HomeFragment.kt index dcc6aa68..be99d355 100644 --- a/app/src/main/java/com/vanced/manager/ui/fragments/HomeFragment.kt +++ b/app/src/main/java/com/vanced/manager/ui/fragments/HomeFragment.kt @@ -14,12 +14,12 @@ import androidx.navigation.findNavController import com.google.android.material.tabs.TabLayout import com.google.android.material.tabs.TabLayoutMediator import com.vanced.manager.R -import com.vanced.manager.adapter.SectionPageAdapter -import com.vanced.manager.adapter.SectionPageRootAdapter +import com.vanced.manager.adapter.ChangelogAdapter import com.vanced.manager.databinding.FragmentHomeBinding import com.vanced.manager.ui.dialogs.DialogContainer.installAlertBuilder import com.vanced.manager.ui.events.Event import com.vanced.manager.ui.viewmodels.HomeViewModel +import com.vanced.manager.ui.viewmodels.HomeViewModelFactory import com.vanced.manager.utils.AppUtils.installing open class HomeFragment : Fragment(), View.OnClickListener { @@ -27,7 +27,9 @@ open class HomeFragment : Fragment(), View.OnClickListener { private lateinit var binding: FragmentHomeBinding private lateinit var variant: String 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()) } override fun onCreateView( @@ -39,13 +41,11 @@ open class HomeFragment : Fragment(), View.OnClickListener { variant = if (requireActivity().findViewById(R.id.main_tablayout).selectedTabPosition == 1) "root" else "nonroot" binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false) binding.viewModel = viewModel - viewModel.variant = variant return binding.root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - viewModel.navigateDestination.observe(viewLifecycleOwner, Observer> { val content = it.getContentIfNotHandled() if(content != null){ @@ -73,27 +73,17 @@ open class HomeFragment : Fragment(), View.OnClickListener { } with(binding.includeChangelogsLayout) { - viewpager.adapter = - if (variant == "root") - SectionPageRootAdapter(this@HomeFragment) - else - SectionPageAdapter(this@HomeFragment) + viewpager.adapter = ChangelogAdapter(variant, this@HomeFragment.viewModel) + val nonrootTitles = arrayOf("Vanced", "Music", "microG", "Manager") + val rootTitles = arrayOf("Vanced", "Manager") TabLayoutMediator(tablayout, viewpager) { tab, position -> tab.text = - if (variant == "root") { - when (position) { - 0 -> "Vanced" - else -> "Manager" + if (variant == "root") { + rootTitles[position] + } else { + nonrootTitles[position] } - } else { - when (position) { - 0 -> "Vanced" - 1 -> "Music" - 2 -> "MicroG" - else -> "Manager" - } - } }.attach() } diff --git a/app/src/main/java/com/vanced/manager/ui/fragments/ManagerChangelogFragment.kt b/app/src/main/java/com/vanced/manager/ui/fragments/ManagerChangelogFragment.kt deleted file mode 100644 index 61124a9c..00000000 --- a/app/src/main/java/com/vanced/manager/ui/fragments/ManagerChangelogFragment.kt +++ /dev/null @@ -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 - } - -} diff --git a/app/src/main/java/com/vanced/manager/ui/fragments/MicrogChangelogFragment.kt b/app/src/main/java/com/vanced/manager/ui/fragments/MicrogChangelogFragment.kt deleted file mode 100644 index 9391a37a..00000000 --- a/app/src/main/java/com/vanced/manager/ui/fragments/MicrogChangelogFragment.kt +++ /dev/null @@ -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 - } - -} diff --git a/app/src/main/java/com/vanced/manager/ui/fragments/MusicChangelogFragment.kt b/app/src/main/java/com/vanced/manager/ui/fragments/MusicChangelogFragment.kt deleted file mode 100644 index ee8f88bd..00000000 --- a/app/src/main/java/com/vanced/manager/ui/fragments/MusicChangelogFragment.kt +++ /dev/null @@ -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 - } - -} diff --git a/app/src/main/java/com/vanced/manager/ui/fragments/VancedChangelogFragment.kt b/app/src/main/java/com/vanced/manager/ui/fragments/VancedChangelogFragment.kt deleted file mode 100644 index ad7ee9e6..00000000 --- a/app/src/main/java/com/vanced/manager/ui/fragments/VancedChangelogFragment.kt +++ /dev/null @@ -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 - } - -} diff --git a/app/src/main/java/com/vanced/manager/ui/viewmodels/HomeViewModel.kt b/app/src/main/java/com/vanced/manager/ui/viewmodels/HomeViewModel.kt index 0926b343..7ac96da3 100644 --- a/app/src/main/java/com/vanced/manager/ui/viewmodels/HomeViewModel.kt +++ b/app/src/main/java/com/vanced/manager/ui/viewmodels/HomeViewModel.kt @@ -5,6 +5,7 @@ import android.content.ActivityNotFoundException import android.content.ComponentName import android.content.Context import android.content.Intent +import android.util.Log import android.widget.Toast import androidx.core.content.ContextCompat.startActivity import androidx.databinding.ObservableBoolean @@ -28,13 +29,12 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch -open class HomeViewModel(application: Application): AndroidViewModel(application) { +open class HomeViewModel(application: Application, val variant: String): AndroidViewModel(application) { val app = application private val managerApp = application as App //val variant = getDefaultSharedPreferences(application).getString("vanced_variant", "nonroot") - var variant = "nonroot" val vanced = ObservableField() val microg = ObservableField() @@ -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 vancedPkgName = - if (variant == "root") + if (variant == "root") "com.google.android.youtube" else "com.vanced.android.youtube" @@ -144,16 +144,14 @@ open class HomeViewModel(application: Application): AndroidViewModel(application init { fetching.set(true) - //while (managerApp.manager == null) - //this.wait() - 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)) + microg.set(DataModel(managerApp.microg, app = "microg", context = app)) manager.set(DataModel(managerApp.manager, app = "manager", context = app)) vancedProgress.set(ProgressModel()) musicProgress.set(ProgressModel()) microgProgress.set(ProgressModel()) + Log.d("Test", variant) fetching.set(false) } diff --git a/app/src/main/java/com/vanced/manager/ui/viewmodels/HomeViewModelFactory.kt b/app/src/main/java/com/vanced/manager/ui/viewmodels/HomeViewModelFactory.kt new file mode 100644 index 00000000..c35c7b92 --- /dev/null +++ b/app/src/main/java/com/vanced/manager/ui/viewmodels/HomeViewModelFactory.kt @@ -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 create(modelClass: Class): T { + return HomeViewModel(application, variant) as T + } + +} \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_manager_changelog.xml b/app/src/main/res/layout/fragment_manager_changelog.xml deleted file mode 100644 index 7255e5d0..00000000 --- a/app/src/main/res/layout/fragment_manager_changelog.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - - - - - - - - - - diff --git a/app/src/main/res/layout/fragment_microg_changelog.xml b/app/src/main/res/layout/fragment_microg_changelog.xml deleted file mode 100644 index 1b049f16..00000000 --- a/app/src/main/res/layout/fragment_microg_changelog.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - - - - - - - - - - diff --git a/app/src/main/res/layout/fragment_music_changelog.xml b/app/src/main/res/layout/fragment_music_changelog.xml deleted file mode 100644 index 00c6642a..00000000 --- a/app/src/main/res/layout/fragment_music_changelog.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - - - - - - - - - - diff --git a/app/src/main/res/layout/fragment_vanced_changelog.xml b/app/src/main/res/layout/fragment_vanced_changelog.xml deleted file mode 100644 index 73dfa639..00000000 --- a/app/src/main/res/layout/fragment_vanced_changelog.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - - - - - - - - - - - - diff --git a/app/src/main/res/layout/view_changelog.xml b/app/src/main/res/layout/view_changelog.xml new file mode 100644 index 00000000..3cfa4780 --- /dev/null +++ b/app/src/main/res/layout/view_changelog.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file