[LeakCanary] *ContainerFragment: Refactor to use viewBinding and viewModels
Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
@@ -20,14 +20,11 @@
|
||||
package com.aurora.store.view.ui.apps
|
||||
|
||||
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.FragmentManager
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.viewpager2.adapter.FragmentStateAdapter
|
||||
import com.aurora.gplayapi.data.models.AuthData
|
||||
import com.aurora.store.R
|
||||
import com.aurora.store.data.providers.AuthProvider
|
||||
import com.aurora.store.databinding.FragmentAppsGamesBinding
|
||||
@@ -40,48 +37,32 @@ import com.google.android.material.tabs.TabLayout
|
||||
import com.google.android.material.tabs.TabLayoutMediator
|
||||
|
||||
|
||||
class AppsContainerFragment : Fragment() {
|
||||
class AppsContainerFragment : Fragment(R.layout.fragment_apps_games) {
|
||||
|
||||
private lateinit var B: FragmentAppsGamesBinding
|
||||
private lateinit var authData: AuthData
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View {
|
||||
B = FragmentAppsGamesBinding.bind(
|
||||
inflater.inflate(
|
||||
R.layout.fragment_apps_games,
|
||||
container,
|
||||
false
|
||||
)
|
||||
)
|
||||
return B.root
|
||||
}
|
||||
private var _binding: FragmentAppsGamesBinding? = null
|
||||
private val binding get() = _binding!!
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
authData = AuthProvider.with(requireContext()).getAuthData()
|
||||
setupViewPager()
|
||||
}
|
||||
_binding = FragmentAppsGamesBinding.bind(view)
|
||||
|
||||
private fun setupViewPager() {
|
||||
// ViewPager
|
||||
val isForYouEnabled = Preferences.getBoolean(
|
||||
requireContext(),
|
||||
Preferences.PREFERENCE_FOR_YOU
|
||||
)
|
||||
|
||||
val isGoogleAccount = !authData.isAnonymous
|
||||
val isGoogleAccount = !AuthProvider.with(requireContext()).getAuthData().isAnonymous
|
||||
|
||||
B.pager.adapter = ViewPagerAdapter(
|
||||
binding.pager.adapter = ViewPagerAdapter(
|
||||
childFragmentManager,
|
||||
lifecycle,
|
||||
isGoogleAccount,
|
||||
isForYouEnabled
|
||||
)
|
||||
|
||||
B.pager.isUserInputEnabled = false //Disable viewpager scroll to avoid scroll conflicts
|
||||
binding.pager.isUserInputEnabled =
|
||||
false //Disable viewpager scroll to avoid scroll conflicts
|
||||
|
||||
val tabTitles: MutableList<String> = mutableListOf<String>().apply {
|
||||
if (isForYouEnabled) {
|
||||
@@ -96,11 +77,20 @@ class AppsContainerFragment : Fragment() {
|
||||
}
|
||||
}
|
||||
|
||||
TabLayoutMediator(B.tabLayout, B.pager, true) { tab: TabLayout.Tab, position: Int ->
|
||||
TabLayoutMediator(
|
||||
binding.tabLayout,
|
||||
binding.pager,
|
||||
true
|
||||
) { tab: TabLayout.Tab, position: Int ->
|
||||
tab.text = tabTitles[position]
|
||||
}.attach()
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
_binding = null
|
||||
}
|
||||
|
||||
internal class ViewPagerAdapter(
|
||||
fragment: FragmentManager,
|
||||
lifecycle: Lifecycle,
|
||||
|
||||
@@ -20,29 +20,21 @@
|
||||
package com.aurora.store.view.ui.commons
|
||||
|
||||
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.FragmentManager
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.viewpager2.adapter.FragmentStateAdapter
|
||||
import androidx.viewpager2.widget.ViewPager2
|
||||
import com.aurora.Constants
|
||||
import com.aurora.gplayapi.data.models.AuthData
|
||||
import com.aurora.store.R
|
||||
import com.aurora.store.data.providers.AuthProvider
|
||||
import com.aurora.store.databinding.FragmentTopChartBinding
|
||||
import com.aurora.store.view.ui.games.TopChartFragment
|
||||
|
||||
|
||||
class TopChartContainerFragment : Fragment() {
|
||||
class TopChartContainerFragment : Fragment(R.layout.fragment_top_chart) {
|
||||
|
||||
private lateinit var B: FragmentTopChartBinding
|
||||
|
||||
private lateinit var authData: AuthData
|
||||
|
||||
private var chartType = 0
|
||||
private var _binding: FragmentTopChartBinding? = null
|
||||
private val binding get() = _binding!!
|
||||
|
||||
companion object {
|
||||
@JvmStatic
|
||||
@@ -55,57 +47,45 @@ class TopChartContainerFragment : Fragment() {
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View {
|
||||
B = FragmentTopChartBinding.bind(
|
||||
inflater.inflate(
|
||||
R.layout.fragment_top_chart,
|
||||
container,
|
||||
false
|
||||
)
|
||||
)
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
_binding = FragmentTopChartBinding.bind(view)
|
||||
|
||||
var chartType = 0
|
||||
val bundle = arguments
|
||||
if (bundle != null) {
|
||||
chartType = bundle.getInt(Constants.TOP_CHART_TYPE, 0)
|
||||
}
|
||||
|
||||
return B.root
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
authData = AuthProvider.with(requireContext()).getAuthData()
|
||||
setupViewPager()
|
||||
}
|
||||
|
||||
private fun setupViewPager() {
|
||||
B.pager.adapter = ViewPagerAdapter(childFragmentManager, lifecycle, chartType)
|
||||
B.topTabGroup.setOnCheckedStateChangeListener { _, checkedIds ->
|
||||
// ViewPager
|
||||
binding.pager.adapter = ViewPagerAdapter(childFragmentManager, lifecycle, chartType)
|
||||
binding.topTabGroup.setOnCheckedStateChangeListener { _, checkedIds ->
|
||||
when (checkedIds[0]) {
|
||||
R.id.tab_top_free -> B.pager.setCurrentItem(0, true)
|
||||
R.id.tab_top_grossing -> B.pager.setCurrentItem(1, true)
|
||||
R.id.tab_trending -> B.pager.setCurrentItem(2, true)
|
||||
R.id.tab_top_paid -> B.pager.setCurrentItem(3, true)
|
||||
R.id.tab_top_free -> binding.pager.setCurrentItem(0, true)
|
||||
R.id.tab_top_grossing -> binding.pager.setCurrentItem(1, true)
|
||||
R.id.tab_trending -> binding.pager.setCurrentItem(2, true)
|
||||
R.id.tab_top_paid -> binding.pager.setCurrentItem(3, true)
|
||||
}
|
||||
}
|
||||
|
||||
B.pager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
|
||||
binding.pager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
|
||||
override fun onPageSelected(position: Int) {
|
||||
super.onPageSelected(position)
|
||||
when (position) {
|
||||
0 -> B.topTabGroup.check(R.id.tab_top_free)
|
||||
1 -> B.topTabGroup.check(R.id.tab_top_grossing)
|
||||
2 -> B.topTabGroup.check(R.id.tab_trending)
|
||||
3 -> B.topTabGroup.check(R.id.tab_top_paid)
|
||||
0 -> binding.topTabGroup.check(R.id.tab_top_free)
|
||||
1 -> binding.topTabGroup.check(R.id.tab_top_grossing)
|
||||
2 -> binding.topTabGroup.check(R.id.tab_trending)
|
||||
3 -> binding.topTabGroup.check(R.id.tab_top_paid)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
_binding = null
|
||||
}
|
||||
|
||||
internal class ViewPagerAdapter(
|
||||
fragment: FragmentManager,
|
||||
lifecycle: Lifecycle,
|
||||
|
||||
@@ -17,34 +17,29 @@
|
||||
*
|
||||
*/
|
||||
|
||||
package com.aurora.store.view.ui.apps
|
||||
package com.aurora.store.view.ui.commons
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.fragment.app.viewModels
|
||||
import com.aurora.Constants
|
||||
import com.aurora.gplayapi.data.models.StreamCluster
|
||||
import com.aurora.gplayapi.helpers.TopChartsHelper.Chart
|
||||
import com.aurora.gplayapi.helpers.TopChartsHelper.Type
|
||||
import com.aurora.store.R
|
||||
import com.aurora.store.databinding.FragmentTopContainerBinding
|
||||
import com.aurora.store.view.custom.recycler.EndlessRecyclerOnScrollListener
|
||||
import com.aurora.store.view.epoxy.views.AppProgressViewModel_
|
||||
import com.aurora.store.view.epoxy.views.app.AppListViewModel_
|
||||
import com.aurora.store.view.epoxy.views.shimmer.AppListViewShimmerModel_
|
||||
import com.aurora.store.view.ui.commons.BaseFragment
|
||||
import com.aurora.store.viewmodel.topchart.BaseChartViewModel
|
||||
import com.aurora.store.viewmodel.topchart.TopFreeAppChartViewModel
|
||||
import com.aurora.store.viewmodel.topchart.TopGrossingAppChartViewModel
|
||||
import com.aurora.store.viewmodel.topchart.TopPaidAppChartViewModel
|
||||
import com.aurora.store.viewmodel.topchart.TrendingAppChartViewModel
|
||||
import com.aurora.store.viewmodel.topchart.TopChartViewModel
|
||||
|
||||
class TopChartFragment : BaseFragment() {
|
||||
class TopChartFragment : BaseFragment(R.layout.fragment_top_container) {
|
||||
|
||||
private lateinit var VM: BaseChartViewModel
|
||||
private lateinit var B: FragmentTopContainerBinding
|
||||
private var _binding: FragmentTopContainerBinding? = null
|
||||
private val binding get() = _binding!!
|
||||
|
||||
private lateinit var streamCluster: StreamCluster
|
||||
private val viewModel: TopChartViewModel by viewModels()
|
||||
|
||||
private var chartType = 0
|
||||
private var chartCategory = 0
|
||||
@@ -61,18 +56,9 @@ class TopChartFragment : BaseFragment() {
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View {
|
||||
B = FragmentTopContainerBinding.bind(
|
||||
inflater.inflate(
|
||||
R.layout.fragment_top_container,
|
||||
container,
|
||||
false
|
||||
)
|
||||
)
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
_binding = FragmentTopContainerBinding.bind(view)
|
||||
|
||||
val bundle = arguments
|
||||
if (bundle != null) {
|
||||
@@ -80,41 +66,42 @@ class TopChartFragment : BaseFragment() {
|
||||
chartCategory = bundle.getInt(Constants.TOP_CHART_CATEGORY, 0)
|
||||
}
|
||||
|
||||
when (chartCategory) {
|
||||
0 -> VM = ViewModelProvider(this)[TopFreeAppChartViewModel::class.java]
|
||||
1 -> VM = ViewModelProvider(this)[TopGrossingAppChartViewModel::class.java]
|
||||
2 -> VM = ViewModelProvider(this)[TrendingAppChartViewModel::class.java]
|
||||
3 -> VM = ViewModelProvider(this)[TopPaidAppChartViewModel::class.java]
|
||||
when (chartType) {
|
||||
0 -> when (chartCategory) {
|
||||
0 -> viewModel.getStreamCluster(Type.APPLICATION, Chart.TOP_SELLING_FREE)
|
||||
1 -> viewModel.getStreamCluster(Type.APPLICATION, Chart.TOP_GROSSING)
|
||||
2 -> viewModel.getStreamCluster(Type.APPLICATION, Chart.MOVERS_SHAKERS)
|
||||
3 -> viewModel.getStreamCluster(Type.APPLICATION, Chart.TOP_SELLING_PAID)
|
||||
}
|
||||
|
||||
1 -> when (chartCategory) {
|
||||
0 -> viewModel.getStreamCluster(Type.GAME, Chart.TOP_SELLING_FREE)
|
||||
1 -> viewModel.getStreamCluster(Type.GAME, Chart.TOP_GROSSING)
|
||||
2 -> viewModel.getStreamCluster(Type.GAME, Chart.MOVERS_SHAKERS)
|
||||
3 -> viewModel.getStreamCluster(Type.GAME, Chart.TOP_SELLING_PAID)
|
||||
}
|
||||
}
|
||||
|
||||
return B.root
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
VM.liveData.observe(viewLifecycleOwner) {
|
||||
if (!::streamCluster.isInitialized)
|
||||
attachRecycler()
|
||||
|
||||
streamCluster = it
|
||||
|
||||
updateController(streamCluster)
|
||||
viewModel.liveData.observe(viewLifecycleOwner) {
|
||||
updateController(it)
|
||||
}
|
||||
|
||||
binding.recycler.addOnScrollListener(object : EndlessRecyclerOnScrollListener() {
|
||||
override fun onLoadMore(currentPage: Int) {
|
||||
viewModel.nextCluster()
|
||||
}
|
||||
})
|
||||
|
||||
updateController(null)
|
||||
}
|
||||
|
||||
private fun attachRecycler() {
|
||||
B.recycler.addOnScrollListener(object : EndlessRecyclerOnScrollListener() {
|
||||
override fun onLoadMore(currentPage: Int) {
|
||||
VM.nextCluster()
|
||||
}
|
||||
})
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
_binding = null
|
||||
}
|
||||
|
||||
private fun updateController(streamCluster: StreamCluster?) {
|
||||
B.recycler.withModels {
|
||||
binding.recycler.withModels {
|
||||
setFilterDuplicates(true)
|
||||
if (streamCluster == null) {
|
||||
for (i in 1..6) {
|
||||
@@ -20,14 +20,11 @@
|
||||
package com.aurora.store.view.ui.games
|
||||
|
||||
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.FragmentManager
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.viewpager2.adapter.FragmentStateAdapter
|
||||
import com.aurora.gplayapi.data.models.AuthData
|
||||
import com.aurora.store.R
|
||||
import com.aurora.store.data.providers.AuthProvider
|
||||
import com.aurora.store.databinding.FragmentAppsGamesBinding
|
||||
@@ -40,48 +37,31 @@ import com.google.android.material.tabs.TabLayout
|
||||
import com.google.android.material.tabs.TabLayoutMediator
|
||||
|
||||
|
||||
class GamesContainerFragment : Fragment() {
|
||||
class GamesContainerFragment : Fragment(R.layout.fragment_apps_games) {
|
||||
|
||||
private lateinit var B: FragmentAppsGamesBinding
|
||||
private lateinit var authData: AuthData
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View {
|
||||
B = FragmentAppsGamesBinding.bind(
|
||||
inflater.inflate(
|
||||
R.layout.fragment_apps_games,
|
||||
container,
|
||||
false
|
||||
)
|
||||
)
|
||||
return B.root
|
||||
}
|
||||
private var _binding: FragmentAppsGamesBinding? = null
|
||||
private val binding get() = _binding!!
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
authData = AuthProvider.with(requireContext()).getAuthData()
|
||||
setupViewPager()
|
||||
}
|
||||
_binding = FragmentAppsGamesBinding.bind(view)
|
||||
|
||||
private fun setupViewPager() {
|
||||
// ViewPager
|
||||
val isForYouEnabled = Preferences.getBoolean(
|
||||
requireContext(),
|
||||
Preferences.PREFERENCE_FOR_YOU
|
||||
)
|
||||
|
||||
val isGoogleAccount = !authData.isAnonymous
|
||||
val isGoogleAccount = !AuthProvider.with(requireContext()).getAuthData().isAnonymous
|
||||
|
||||
B.pager.adapter = ViewPagerAdapter(
|
||||
binding.pager.adapter = ViewPagerAdapter(
|
||||
childFragmentManager,
|
||||
lifecycle,
|
||||
isGoogleAccount,
|
||||
isForYouEnabled
|
||||
)
|
||||
|
||||
B.pager.isUserInputEnabled = false
|
||||
binding.pager.isUserInputEnabled = false
|
||||
|
||||
val tabTitles: MutableList<String> = mutableListOf<String>().apply {
|
||||
if (isForYouEnabled) {
|
||||
@@ -96,11 +76,20 @@ class GamesContainerFragment : Fragment() {
|
||||
}
|
||||
}
|
||||
|
||||
TabLayoutMediator(B.tabLayout, B.pager, true) { tab: TabLayout.Tab, position: Int ->
|
||||
TabLayoutMediator(
|
||||
binding.tabLayout,
|
||||
binding.pager,
|
||||
true
|
||||
) { tab: TabLayout.Tab, position: Int ->
|
||||
tab.text = tabTitles[position]
|
||||
}.attach()
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
super.onDestroyView()
|
||||
_binding = null
|
||||
}
|
||||
|
||||
internal class ViewPagerAdapter(
|
||||
fragment: FragmentManager,
|
||||
lifecycle: Lifecycle,
|
||||
|
||||
@@ -1,159 +0,0 @@
|
||||
/*
|
||||
* Aurora Store
|
||||
* Copyright (C) 2021, Rahul Kumar Patel <whyorean@gmail.com>
|
||||
*
|
||||
* Aurora Store is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Aurora Store is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Aurora Store. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
package com.aurora.store.view.ui.games
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import com.aurora.Constants
|
||||
import com.aurora.gplayapi.data.models.StreamCluster
|
||||
import com.aurora.store.R
|
||||
import com.aurora.store.databinding.FragmentTopContainerBinding
|
||||
import com.aurora.store.view.custom.recycler.EndlessRecyclerOnScrollListener
|
||||
import com.aurora.store.view.epoxy.views.AppProgressViewModel_
|
||||
import com.aurora.store.view.epoxy.views.app.AppListViewModel_
|
||||
import com.aurora.store.view.epoxy.views.shimmer.AppListViewShimmerModel_
|
||||
import com.aurora.store.view.ui.commons.BaseFragment
|
||||
import com.aurora.store.viewmodel.topchart.BaseChartViewModel
|
||||
import com.aurora.store.viewmodel.topchart.TopFreeAppChartViewModel
|
||||
import com.aurora.store.viewmodel.topchart.TopFreeGameChartViewModel
|
||||
import com.aurora.store.viewmodel.topchart.TopGrossingAppChartViewModel
|
||||
import com.aurora.store.viewmodel.topchart.TopGrossingGameChartViewModel
|
||||
import com.aurora.store.viewmodel.topchart.TopPaidAppChartViewModel
|
||||
import com.aurora.store.viewmodel.topchart.TopPaidGameChartViewModel
|
||||
import com.aurora.store.viewmodel.topchart.TrendingAppChartViewModel
|
||||
import com.aurora.store.viewmodel.topchart.TrendingGameChartViewModel
|
||||
|
||||
class TopChartFragment : BaseFragment() {
|
||||
|
||||
private lateinit var VM: BaseChartViewModel
|
||||
private lateinit var B: FragmentTopContainerBinding
|
||||
|
||||
private var chartType = 0
|
||||
private var chartCategory = 0
|
||||
|
||||
companion object {
|
||||
@JvmStatic
|
||||
fun newInstance(chartType: Int, chartCategory: Int): TopChartFragment {
|
||||
return TopChartFragment().apply {
|
||||
arguments = Bundle().apply {
|
||||
putInt(Constants.TOP_CHART_TYPE, chartType)
|
||||
putInt(Constants.TOP_CHART_CATEGORY, chartCategory)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
): View? {
|
||||
B = FragmentTopContainerBinding.bind(
|
||||
inflater.inflate(
|
||||
R.layout.fragment_top_container,
|
||||
container,
|
||||
false
|
||||
)
|
||||
)
|
||||
|
||||
val bundle = arguments
|
||||
if (bundle != null) {
|
||||
chartType = bundle.getInt(Constants.TOP_CHART_TYPE, 0)
|
||||
chartCategory = bundle.getInt(Constants.TOP_CHART_CATEGORY, 0)
|
||||
}
|
||||
|
||||
when (chartType) {
|
||||
0 -> when (chartCategory) {
|
||||
0 -> VM =
|
||||
ViewModelProvider(requireActivity()).get(TopFreeAppChartViewModel::class.java)
|
||||
1 -> VM =
|
||||
ViewModelProvider(requireActivity()).get(TopGrossingAppChartViewModel::class.java)
|
||||
2 -> VM =
|
||||
ViewModelProvider(requireActivity()).get(TrendingAppChartViewModel::class.java)
|
||||
3 -> VM =
|
||||
ViewModelProvider(requireActivity()).get(TopPaidAppChartViewModel::class.java)
|
||||
}
|
||||
|
||||
1 -> when (chartCategory) {
|
||||
0 -> VM =
|
||||
ViewModelProvider(requireActivity()).get(TopFreeGameChartViewModel::class.java)
|
||||
1 -> VM =
|
||||
ViewModelProvider(requireActivity()).get(TopGrossingGameChartViewModel::class.java)
|
||||
2 -> VM =
|
||||
ViewModelProvider(requireActivity()).get(TrendingGameChartViewModel::class.java)
|
||||
3 -> VM =
|
||||
ViewModelProvider(requireActivity()).get(TopPaidGameChartViewModel::class.java)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return B.root
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
VM.liveData.observe(viewLifecycleOwner) {
|
||||
updateController(it)
|
||||
}
|
||||
|
||||
B.recycler.addOnScrollListener(object : EndlessRecyclerOnScrollListener() {
|
||||
override fun onLoadMore(currentPage: Int) {
|
||||
VM.nextCluster()
|
||||
}
|
||||
})
|
||||
|
||||
updateController(null)
|
||||
}
|
||||
|
||||
private fun updateController(streamCluster: StreamCluster?) {
|
||||
B.recycler.withModels {
|
||||
setFilterDuplicates(true)
|
||||
if (streamCluster == null) {
|
||||
for (i in 1..6) {
|
||||
add(
|
||||
AppListViewShimmerModel_()
|
||||
.id(i)
|
||||
)
|
||||
}
|
||||
} else {
|
||||
streamCluster.clusterAppList.forEach { app ->
|
||||
add(
|
||||
AppListViewModel_()
|
||||
.id(app.id)
|
||||
.app(app)
|
||||
.click { _ -> openDetailsFragment(app.packageName, app) }
|
||||
)
|
||||
}
|
||||
|
||||
if (streamCluster.hasNext()) {
|
||||
add(
|
||||
AppProgressViewModel_()
|
||||
.id("progress")
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
/*
|
||||
* Aurora Store
|
||||
* Copyright (C) 2021, Rahul Kumar Patel <whyorean@gmail.com>
|
||||
*
|
||||
* Aurora Store is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Aurora Store is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Aurora Store. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
package com.aurora.store.viewmodel.topchart
|
||||
|
||||
import android.app.Application
|
||||
import com.aurora.gplayapi.helpers.TopChartsHelper
|
||||
|
||||
class TopFreeAppChartViewModel(application: Application) : BaseChartViewModel(application) {
|
||||
|
||||
init {
|
||||
type = TopChartsHelper.Type.APPLICATION
|
||||
chart = TopChartsHelper.Chart.TOP_SELLING_FREE
|
||||
observe()
|
||||
}
|
||||
}
|
||||
|
||||
class TopGrossingAppChartViewModel(application: Application) : BaseChartViewModel(application) {
|
||||
|
||||
init {
|
||||
type = TopChartsHelper.Type.APPLICATION
|
||||
chart = TopChartsHelper.Chart.TOP_GROSSING
|
||||
observe()
|
||||
}
|
||||
}
|
||||
|
||||
class TrendingAppChartViewModel(application: Application) : BaseChartViewModel(application) {
|
||||
|
||||
init {
|
||||
type = TopChartsHelper.Type.APPLICATION
|
||||
chart = TopChartsHelper.Chart.MOVERS_SHAKERS
|
||||
observe()
|
||||
}
|
||||
}
|
||||
|
||||
class TopPaidAppChartViewModel(application: Application) : BaseChartViewModel(application) {
|
||||
|
||||
init {
|
||||
type = TopChartsHelper.Type.APPLICATION
|
||||
chart = TopChartsHelper.Chart.TOP_SELLING_PAID
|
||||
observe()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
/*
|
||||
* Aurora Store
|
||||
* Copyright (C) 2021, Rahul Kumar Patel <whyorean@gmail.com>
|
||||
*
|
||||
* Aurora Store is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Aurora Store is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Aurora Store. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
package com.aurora.store.viewmodel.topchart
|
||||
|
||||
import android.app.Application
|
||||
import com.aurora.gplayapi.helpers.TopChartsHelper
|
||||
|
||||
class TopFreeGameChartViewModel(application: Application) : BaseChartViewModel(application) {
|
||||
|
||||
init {
|
||||
type = TopChartsHelper.Type.GAME
|
||||
chart = TopChartsHelper.Chart.TOP_SELLING_FREE
|
||||
observe()
|
||||
}
|
||||
}
|
||||
|
||||
class TopGrossingGameChartViewModel(application: Application) : BaseChartViewModel(application) {
|
||||
|
||||
init {
|
||||
type = TopChartsHelper.Type.GAME
|
||||
chart = TopChartsHelper.Chart.TOP_GROSSING
|
||||
observe()
|
||||
}
|
||||
}
|
||||
|
||||
class TrendingGameChartViewModel(application: Application) : BaseChartViewModel(application) {
|
||||
|
||||
init {
|
||||
type = TopChartsHelper.Type.GAME
|
||||
chart = TopChartsHelper.Chart.MOVERS_SHAKERS
|
||||
observe()
|
||||
}
|
||||
}
|
||||
|
||||
class TopPaidGameChartViewModel(application: Application) : BaseChartViewModel(application) {
|
||||
|
||||
init {
|
||||
type = TopChartsHelper.Type.GAME
|
||||
chart = TopChartsHelper.Chart.TOP_SELLING_PAID
|
||||
observe()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,29 +33,19 @@ import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.supervisorScope
|
||||
|
||||
abstract class BaseChartViewModel(application: Application) : BaseAndroidViewModel(application) {
|
||||
class TopChartViewModel(application: Application) : BaseAndroidViewModel(application) {
|
||||
|
||||
private val authData: AuthData = AuthProvider.with(application).getAuthData()
|
||||
private val topChartsHelper: TopChartsHelper =
|
||||
TopChartsHelper(authData).using(HttpClient.getPreferredClient(application))
|
||||
|
||||
lateinit var type: TopChartsHelper.Type
|
||||
lateinit var chart: TopChartsHelper.Chart
|
||||
|
||||
val liveData: MutableLiveData<StreamCluster> = MutableLiveData()
|
||||
var streamCluster: StreamCluster = StreamCluster()
|
||||
|
||||
fun getStreamCluster(
|
||||
type: TopChartsHelper.Type,
|
||||
chart: TopChartsHelper.Chart
|
||||
): StreamCluster {
|
||||
return topChartsHelper.getCluster(type, chart)
|
||||
}
|
||||
|
||||
override fun observe() {
|
||||
fun getStreamCluster(type: TopChartsHelper.Type, chart: TopChartsHelper.Chart) {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
try {
|
||||
streamCluster = getStreamCluster(type, chart)
|
||||
streamCluster = topChartsHelper.getCluster(type, chart)
|
||||
liveData.postValue(streamCluster)
|
||||
} catch (e: Exception) {
|
||||
requestState = RequestState.Pending
|
||||
@@ -63,6 +53,8 @@ abstract class BaseChartViewModel(application: Application) : BaseAndroidViewMod
|
||||
}
|
||||
}
|
||||
|
||||
override fun observe() {}
|
||||
|
||||
fun nextCluster() {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
supervisorScope {
|
||||
Reference in New Issue
Block a user