InstalledAppsFragment: Move event handling to fragment
* Just fetch the list again, its not that expensive Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
@@ -20,10 +20,12 @@
|
|||||||
package com.aurora.store.view.ui.all
|
package com.aurora.store.view.ui.all
|
||||||
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.util.Log
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import androidx.fragment.app.viewModels
|
import androidx.fragment.app.viewModels
|
||||||
import com.aurora.gplayapi.data.models.App
|
import com.aurora.gplayapi.data.models.App
|
||||||
import com.aurora.store.R
|
import com.aurora.store.R
|
||||||
|
import com.aurora.store.data.event.BusEvent
|
||||||
import com.aurora.store.databinding.FragmentAppsBinding
|
import com.aurora.store.databinding.FragmentAppsBinding
|
||||||
import com.aurora.store.view.epoxy.views.HeaderViewModel_
|
import com.aurora.store.view.epoxy.views.HeaderViewModel_
|
||||||
import com.aurora.store.view.epoxy.views.app.AppListViewModel_
|
import com.aurora.store.view.epoxy.views.app.AppListViewModel_
|
||||||
@@ -31,10 +33,15 @@ 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.aurora.store.viewmodel.all.InstalledViewModel
|
import com.aurora.store.viewmodel.all.InstalledViewModel
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
|
import org.greenrobot.eventbus.EventBus
|
||||||
|
import org.greenrobot.eventbus.Subscribe
|
||||||
|
import org.greenrobot.eventbus.ThreadMode
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class InstalledAppsFragment : BaseFragment(R.layout.fragment_apps) {
|
class InstalledAppsFragment : BaseFragment(R.layout.fragment_apps) {
|
||||||
|
|
||||||
|
private val TAG = InstalledAppsFragment::class.java.simpleName
|
||||||
|
|
||||||
private var _binding: FragmentAppsBinding? = null
|
private var _binding: FragmentAppsBinding? = null
|
||||||
private val binding: FragmentAppsBinding
|
private val binding: FragmentAppsBinding
|
||||||
get() = _binding!!
|
get() = _binding!!
|
||||||
@@ -48,6 +55,31 @@ class InstalledAppsFragment : BaseFragment(R.layout.fragment_apps) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onStart() {
|
||||||
|
super.onStart()
|
||||||
|
EventBus.getDefault().register(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onStop() {
|
||||||
|
EventBus.getDefault().unregister(this)
|
||||||
|
super.onStop()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||||
|
fun onEvent(event: BusEvent) {
|
||||||
|
when (event) {
|
||||||
|
is BusEvent.InstallEvent,
|
||||||
|
is BusEvent.UninstallEvent,
|
||||||
|
is BusEvent.Blacklisted -> {
|
||||||
|
viewModel.getInstalledApps(requireContext())
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {
|
||||||
|
Log.i(TAG, "Got an unhandled event: $event")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
super.onViewCreated(view, savedInstanceState)
|
super.onViewCreated(view, savedInstanceState)
|
||||||
_binding = FragmentAppsBinding.bind(view)
|
_binding = FragmentAppsBinding.bind(view)
|
||||||
@@ -57,6 +89,8 @@ class InstalledAppsFragment : BaseFragment(R.layout.fragment_apps) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
updateController(null)
|
updateController(null)
|
||||||
|
|
||||||
|
viewModel.getInstalledApps(view.context)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroyView() {
|
override fun onDestroyView() {
|
||||||
|
|||||||
@@ -19,43 +19,25 @@
|
|||||||
|
|
||||||
package com.aurora.store.viewmodel.all
|
package com.aurora.store.viewmodel.all
|
||||||
|
|
||||||
import android.annotation.SuppressLint
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import com.aurora.extensions.flushAndAdd
|
|
||||||
import com.aurora.gplayapi.data.models.App
|
import com.aurora.gplayapi.data.models.App
|
||||||
import com.aurora.store.data.event.BusEvent
|
|
||||||
import com.aurora.store.util.AppUtil
|
import com.aurora.store.util.AppUtil
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
|
||||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import org.greenrobot.eventbus.EventBus
|
|
||||||
import org.greenrobot.eventbus.Subscribe
|
|
||||||
import org.greenrobot.eventbus.ThreadMode
|
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
import javax.inject.Inject
|
|
||||||
|
|
||||||
@HiltViewModel
|
class InstalledViewModel : ViewModel() {
|
||||||
@SuppressLint("StaticFieldLeak") // false positive, see https://github.com/google/dagger/issues/3253
|
|
||||||
class InstalledViewModel @Inject constructor(
|
|
||||||
@ApplicationContext private val context: Context
|
|
||||||
) : ViewModel() {
|
|
||||||
|
|
||||||
private val TAG = InstalledViewModel::class.java.simpleName
|
private val TAG = InstalledViewModel::class.java.simpleName
|
||||||
|
|
||||||
var appList: MutableList<App> = mutableListOf()
|
private var appList: MutableList<App> = mutableListOf()
|
||||||
val liveData: MutableLiveData<List<App>> = MutableLiveData()
|
val liveData: MutableLiveData<List<App>> = MutableLiveData()
|
||||||
|
|
||||||
init {
|
fun getInstalledApps(context: Context) {
|
||||||
EventBus.getDefault().register(this)
|
|
||||||
observe()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun observe() {
|
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
viewModelScope.launch(Dispatchers.IO) {
|
||||||
try {
|
try {
|
||||||
appList = AppUtil.getFilteredInstalledApps(context).toMutableList()
|
appList = AppUtil.getFilteredInstalledApps(context).toMutableList()
|
||||||
@@ -65,42 +47,4 @@ class InstalledViewModel @Inject constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Subscribe(threadMode = ThreadMode.BACKGROUND)
|
|
||||||
fun onEvent(event: BusEvent) {
|
|
||||||
when (event) {
|
|
||||||
is BusEvent.InstallEvent -> {
|
|
||||||
updateListAndPost(event.packageName)
|
|
||||||
}
|
|
||||||
|
|
||||||
is BusEvent.UninstallEvent -> {
|
|
||||||
updateListAndPost(event.packageName)
|
|
||||||
}
|
|
||||||
|
|
||||||
is BusEvent.Blacklisted -> {
|
|
||||||
observe()
|
|
||||||
}
|
|
||||||
|
|
||||||
else -> {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun updateListAndPost(packageName: String) {
|
|
||||||
//Remove from current list
|
|
||||||
val updatedList = appList.filter {
|
|
||||||
it.packageName != packageName
|
|
||||||
}.toList()
|
|
||||||
|
|
||||||
appList.flushAndAdd(updatedList)
|
|
||||||
|
|
||||||
//Post new update list
|
|
||||||
liveData.postValue(appList.sortedBy { it.displayName.lowercase(Locale.getDefault()) })
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onCleared() {
|
|
||||||
EventBus.getDefault().unregister(this)
|
|
||||||
super.onCleared()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user