AppsGamesFragment: Merge InstalledAppsFragment logic

There is just one fragment now that we have dropped logic for purchase history

Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
Aayush Gupta
2024-11-20 10:24:05 +07:00
parent 47adb803bd
commit c95e1f09c4
3 changed files with 94 additions and 196 deletions

View File

@@ -19,72 +19,115 @@
package com.aurora.store.view.ui.all package com.aurora.store.view.ui.all
import android.content.pm.PackageInfo
import android.os.Bundle import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View import android.view.View
import androidx.core.content.ContextCompat import androidx.fragment.app.viewModels
import androidx.fragment.app.Fragment import androidx.lifecycle.lifecycleScope
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
import androidx.viewpager2.adapter.FragmentStateAdapter import com.aurora.store.AuroraApp
import com.aurora.store.R import com.aurora.store.data.event.InstallerEvent
import com.aurora.store.data.providers.AuthProvider import com.aurora.store.data.model.MinimalApp
import com.aurora.store.databinding.FragmentGenericWithPagerBinding import com.aurora.store.databinding.FragmentGenericWithSearchBinding
import com.aurora.store.view.epoxy.views.HeaderViewModel_
import com.aurora.store.view.epoxy.views.PackageInfoViewModel_
import com.aurora.store.view.epoxy.views.shimmer.AppListViewShimmerModel_
import com.aurora.store.view.ui.commons.BaseFragment import com.aurora.store.view.ui.commons.BaseFragment
import com.google.android.material.tabs.TabLayout import com.aurora.store.viewmodel.all.InstalledViewModel
import com.google.android.material.tabs.TabLayoutMediator
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject import kotlinx.coroutines.launch
@AndroidEntryPoint @AndroidEntryPoint
class AppsGamesFragment : BaseFragment<FragmentGenericWithPagerBinding>() { class AppsGamesFragment : BaseFragment<FragmentGenericWithSearchBinding>() {
@Inject
lateinit var authProvider: AuthProvider private val viewModel: InstalledViewModel by viewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
viewLifecycleOwner.lifecycleScope.launch {
viewModel.packages.collect {
updateController(it)
}
}
viewLifecycleOwner.lifecycleScope.launch {
AuroraApp.events.installerEvent.collect {
when (it) {
is InstallerEvent.Installed,
is InstallerEvent.Uninstalled -> {
viewModel.fetchApps()
}
else -> {}
}
}
}
// Toolbar // Toolbar
binding.layoutActionToolbar.toolbar.apply { binding.layoutToolbarNative.apply {
elevation = 0f imgActionPrimary.visibility = View.VISIBLE
title = getString(R.string.title_apps_games) imgActionSecondary.visibility = View.GONE
navigationIcon = ContextCompat.getDrawable(view.context, R.drawable.ic_arrow_back)
setNavigationOnClickListener { findNavController().navigateUp() }
}
// ViewPager imgActionPrimary.setOnClickListener { findNavController().navigateUp() }
binding.pager.apply {
isUserInputEnabled = false
adapter = ViewPagerAdapter(childFragmentManager, lifecycle, authProvider.isAnonymous)
}
TabLayoutMediator( inputSearch.addTextChangedListener(object : TextWatcher {
binding.tabLayout, override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
binding.pager, if (s.isNullOrEmpty()) {
true updateController(viewModel.packages.value)
) { tab: TabLayout.Tab, position: Int -> } else {
when (position) { val filteredPackages = viewModel.packages.value?.filter {
0 -> tab.text = getString(R.string.title_installed) it.applicationInfo!!.loadLabel(requireContext().packageManager)
else -> {} .contains(s, true)
} }
}.attach() updateController(filteredPackages)
} }
}
internal class ViewPagerAdapter( override fun afterTextChanged(s: Editable?) {}
fragment: FragmentManager, override fun beforeTextChanged(
lifecycle: Lifecycle, s: CharSequence?,
private val isAnonymous: Boolean start: Int,
) : count: Int,
FragmentStateAdapter(fragment, lifecycle) { after: Int
override fun createFragment(position: Int): Fragment { ) {
return when (position) { }
0 -> InstalledAppsFragment.newInstance() })
else -> Fragment()
}
}
override fun getItemCount(): Int {
return 1
} }
} }
private fun updateController(packages: List<PackageInfo>?) {
binding.recycler.withModels {
setFilterDuplicates(true)
if (packages == null) {
for (i in 1..10) {
add(
AppListViewShimmerModel_()
.id(i)
)
}
} else {
add(
HeaderViewModel_()
.id("header")
.title("${packages.size} apps installed")
)
packages.forEach { app ->
add(
PackageInfoViewModel_()
.id(app.packageName.hashCode())
.packageInfo(app)
.click { _ -> openDetailsFragment(app.packageName) }
.longClick { _ ->
openAppMenuSheet(MinimalApp.fromPackageInfo(requireContext(), app))
false
}
)
}
}
}
}
} }

View File

@@ -1,109 +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.all
import android.content.pm.PackageInfo
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.aurora.store.AuroraApp
import com.aurora.store.data.event.InstallerEvent
import com.aurora.store.data.model.MinimalApp
import com.aurora.store.databinding.FragmentAppsBinding
import com.aurora.store.view.epoxy.views.HeaderViewModel_
import com.aurora.store.view.epoxy.views.PackageInfoViewModel_
import com.aurora.store.view.epoxy.views.shimmer.AppListViewShimmerModel_
import com.aurora.store.view.ui.commons.BaseFragment
import com.aurora.store.viewmodel.all.InstalledViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
@AndroidEntryPoint
class InstalledAppsFragment : BaseFragment<FragmentAppsBinding>() {
private val TAG = InstalledAppsFragment::class.java.simpleName
private val viewModel: InstalledViewModel by viewModels()
companion object {
@JvmStatic
fun newInstance(): InstalledAppsFragment {
return InstalledAppsFragment()
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewLifecycleOwner.lifecycleScope.launch {
viewModel.packages.collect {
updateController(it)
}
}
viewLifecycleOwner.lifecycleScope.launch {
AuroraApp.events.installerEvent.collect {
when (it) {
is InstallerEvent.Installed,
is InstallerEvent.Uninstalled -> {
viewModel.fetchApps()
}
else -> {
Log.i(TAG, "Got an unhandled event: $it")
}
}
}
}
}
private fun updateController(packages: List<PackageInfo>?) {
binding.recycler.withModels {
setFilterDuplicates(true)
if (packages == null) {
for (i in 1..10) {
add(
AppListViewShimmerModel_()
.id(i)
)
}
} else {
add(
HeaderViewModel_()
.id("header")
.title("${packages.size} apps installed")
)
packages.forEach { app ->
add(
PackageInfoViewModel_()
.id(app.packageName.hashCode())
.packageInfo(app)
.click { _ -> openDetailsFragment(app.packageName) }
.longClick { _ ->
openAppMenuSheet(MinimalApp.fromPackageInfo(requireContext(), app))
false
}
)
}
}
}
}
}

View File

@@ -1,36 +0,0 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ 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/>.
~
-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.airbnb.epoxy.EpoxyRecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="@dimen/height_bottom_adj"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:stackFromEnd="false"
tools:listitem="@layout/view_app_list" />
</RelativeLayout>