Skip to content

Commit

Permalink
Some more small changes, fixes upstream TacoTheDank#48
Browse files Browse the repository at this point in the history
  • Loading branch information
unbiaseduser-github committed Oct 24, 2022
1 parent b85e1b9 commit 38d6911
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 35 deletions.
10 changes: 10 additions & 0 deletions app/src/main/java/taco/scoop/core/MainViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import java.util.ArrayList;
import java.util.List;

import taco.scoop.core.data.app.App;
import taco.scoop.core.data.crash.Crash;

public class MainViewModel extends ViewModel {
private final MutableLiveData<List<Crash>> crashes = new MutableLiveData<>(new ArrayList<>());
private final MutableLiveData<String> searchTerm = new MutableLiveData<>("");
private Crash combinedCrash;
private final MutableLiveData<List<App>> apps = new MutableLiveData<>(new ArrayList<>());

public LiveData<List<Crash>> getCrashes() {
return crashes;
Expand All @@ -37,4 +39,12 @@ public void setCombinedCrash(Crash combinedCrash) {
public Crash getCombinedCrash() {
return combinedCrash;
}

public LiveData<List<App>> getApps() {
return apps;
}

public void setApps(List<App> apps) {
this.apps.setValue(apps);
}
}
58 changes: 28 additions & 30 deletions app/src/main/java/taco/scoop/ui/activity/BlacklistAppsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,25 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import rikka.lifecycle.sharedViewModels
import taco.scoop.R
import taco.scoop.core.MainViewModel
import taco.scoop.core.data.app.App
import taco.scoop.core.data.app.AppLoader
import taco.scoop.databinding.ActivityBlacklistAppsBinding
import taco.scoop.ui.adapter.AppAdapter
import taco.scoop.ui.helper.ToolbarElevationHelper
import taco.scoop.util.PreferenceHelper

class BlacklistAppsActivity : AppCompatActivity(), SearchView.OnQueryTextListener,
SearchView.OnCloseListener {
class BlacklistAppsActivity : AppCompatActivity(), SearchView.OnQueryTextListener {

private var mAdapter: AppAdapter? = null
private var mIsLoading = false
private lateinit var mAdapter: AppAdapter
private lateinit var binding: ActivityBlacklistAppsBinding
private val mainViewModel: MainViewModel by sharedViewModels {
ViewModelProvider(this)[MainViewModel::class.java]
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -33,44 +37,40 @@ class BlacklistAppsActivity : AppCompatActivity(), SearchView.OnQueryTextListene

binding.blacklistView.layoutManager = LinearLayoutManager(this)

savedInstanceState?.let {
mIsLoading = it.getBoolean("mIsLoading")
}

mAdapter = AppAdapter()
binding.blacklistView.adapter = mAdapter

ToolbarElevationHelper(binding.blacklistView, binding.blacklistToolbar.toolbar)
}
mainViewModel.searchTerm.observe(this) {
mAdapter.search(it)
}

override fun onResume() {
super.onResume()
if (!mIsLoading) {
AppLoader().loadData(this)
updateViewStates(true)
mainViewModel.apps.observe(this) {
if (it.isEmpty()) {
AppLoader().loadData(this)
updateViewStates(true)
} else {
mAdapter.setApps(it, PreferenceHelper.blacklistList)
updateViewStates(false)
}
}
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putBoolean("mIsLoading", mIsLoading)
ToolbarElevationHelper(binding.blacklistView, binding.blacklistToolbar.toolbar)
}

override fun onPause() {
PreferenceHelper.editBlacklistPackages(mAdapter!!.selectedPackages)
PreferenceHelper.editBlacklistPackages(mAdapter.selectedPackages)
super.onPause()
}

private fun updateViewStates(loading: Boolean) {
mIsLoading = loading
val empty = mAdapter!!.isEmpty
val empty = mAdapter.isEmpty
// When one is visible, the other isn't (and vice versa)
binding.blacklistProgressbar.isVisible = loading
binding.blacklistView.isGone = loading || empty
}

fun onDataLoaded(apps: ArrayList<App?>?) {
mAdapter!!.setApps(apps, PreferenceHelper.blacklistList)
mainViewModel.setApps(apps)
updateViewStates(false)
}

Expand All @@ -79,17 +79,15 @@ class BlacklistAppsActivity : AppCompatActivity(), SearchView.OnQueryTextListene
val searchItem = menu.findItem(R.id.menu_blacklist_search)
val searchView = searchItem.actionView as SearchView
searchView.setOnQueryTextListener(this)
searchView.setOnCloseListener(this)
if ((mainViewModel.searchTerm.value as String).isNotEmpty()) {
searchItem.expandActionView()
searchView.setQuery(mainViewModel.searchTerm.value, false)
}
return true
}

override fun onClose(): Boolean {
mAdapter!!.search(null)
return false
}

override fun onQueryTextChange(newText: String): Boolean {
mAdapter!!.search(newText)
mainViewModel.setSearchTerm(newText)
return true
}

Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/taco/scoop/ui/adapter/AppAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

public class AppAdapter extends RecyclerView.Adapter<AppAdapter.AppViewHolder> {

private final ArrayList<App> mSearchedItems = new ArrayList<>();
private ArrayList<App> mItems = new ArrayList<>();
private final List<App> mSearchedItems = new ArrayList<>();
private List<App> mItems = new ArrayList<>();
private boolean mSearchActive = false;

public void setApps(ArrayList<App> items, List<String> blacklisted) {
public void setApps(List<App> items, List<String> blacklisted) {
mItems = items;
for (App a : mItems)
for (String pkg : blacklisted)
Expand All @@ -39,7 +39,7 @@ public boolean isEmpty() {
return mItems.isEmpty();
}

public ArrayList<String> getSelectedPackages() {
public List<String> getSelectedPackages() {
ArrayList<String> items = new ArrayList<>();
for (App a : mItems)
if (a.selected)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/taco/scoop/util/PreferenceHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ object PreferenceHelper {
val blacklistList: List<String>
get() = listOf(*blacklistedPackages?.split(",".toRegex())!!.toTypedArray())

fun editBlacklistPackages(packages: ArrayList<String>) {
fun editBlacklistPackages(packages: List<String>) {
sharedPreferences.edit {
putString(
getKey(R.string.key_blacklisted_packages),
Expand Down

0 comments on commit 38d6911

Please sign in to comment.