Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
sdex committed Aug 12, 2021
1 parent 7b3a2c5 commit 95ddccb
Show file tree
Hide file tree
Showing 27 changed files with 162 additions and 445 deletions.
2 changes: 0 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@

<activity android:name=".AboutActivity" />

<activity android:name=".app.legacy.OreoPackageManagerBugActivity" />

<activity
android:name=".preferences.SettingsActivity"
android:label="@string/settings_activity" />
Expand Down
71 changes: 13 additions & 58 deletions app/src/main/java/com/sdex/activityrunner/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
package com.sdex.activityrunner

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import android.os.Bundle
import android.text.TextUtils
import android.view.Menu
import android.view.MenuItem
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatDelegate
import androidx.appcompat.widget.SearchView
import androidx.appcompat.widget.SearchView.OnQueryTextListener
import androidx.lifecycle.ViewModelProvider
import com.sdex.activityrunner.app.ApplicationsListAdapter
import com.sdex.activityrunner.app.ApplicationsListViewModel
import com.sdex.activityrunner.app.legacy.OreoPackageManagerBugActivity
import com.sdex.activityrunner.app.MainViewModel
import com.sdex.activityrunner.extensions.addDivider
import com.sdex.activityrunner.intent.IntentBuilderActivity
import com.sdex.activityrunner.preferences.AppPreferences
Expand All @@ -26,29 +22,20 @@ import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : BaseActivity() {

private val viewModel by viewModels<ApplicationsListViewModel>()
private val viewModel by viewModels<MainViewModel>()

private val appPreferences by lazy { AppPreferences(this) }
private val adapter by lazy { ApplicationsListAdapter(this) }

private var isShowSystemAppIndicator: Boolean = false
private var searchText: String? = null

override fun getLayout(): Int {
return R.layout.activity_main
}
override fun getLayout() = R.layout.activity_main

public override fun onCreate(savedInstanceState: Bundle?) {
AppCompatDelegate.setDefaultNightMode(appPreferences.theme)
super.onCreate(savedInstanceState)

ApplicationsListJob.enqueueWork(this, Intent())

isShowSystemAppIndicator = appPreferences.isShowSystemAppIndicator

searchText = savedInstanceState?.getString(STATE_SEARCH_TEXT)

viewModel.getItems(searchText).observe(this) {
viewModel.items.observe(this) {
adapter.submitList(it)
progress.hide()
}
Expand All @@ -57,44 +44,14 @@ class MainActivity : BaseActivity() {

list.addDivider(this)
list.adapter = adapter

checkOreoBug()
}

@SuppressLint("NotifyDataSetChanged")
override fun onStart() {
super.onStart()
if (appPreferences.isShowSystemAppIndicator != isShowSystemAppIndicator) {
isShowSystemAppIndicator = appPreferences.isShowSystemAppIndicator
viewModel.getItems(searchText).observe(this) {
adapter.submitList(it)
}
}
}

public override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putString(STATE_SEARCH_TEXT, searchText)
}

private fun filter(text: String) {
this.searchText = text
viewModel.getItems(text).observe(this) {
adapter.submitList(it)
}
}

// https://issuetracker.google.com/issues/73289329
private fun checkOreoBug() {
if (VERSION.SDK_INT == VERSION_CODES.O) {
if (!appPreferences.isOreoBugWarningShown) {
val viewModel = ViewModelProvider(this).get(MainViewModel::class.java)
viewModel.packages.observe(this) {
if (it!!.isEmpty()) {
overridePendingTransition(0, 0)
startActivity(Intent(this, OreoPackageManagerBugActivity::class.java))
}
}
}
if (appPreferences.isShowSystemAppIndicator != adapter.isShowSystemAppIndicator) {
adapter.isShowSystemAppIndicator = appPreferences.isShowSystemAppIndicator
adapter.notifyDataSetChanged()
}
}

Expand All @@ -105,8 +62,9 @@ class MainActivity : BaseActivity() {
val hint = getString(R.string.action_search_hint)
searchView.queryHint = hint

if (!TextUtils.isEmpty(searchText)) {
searchView.post { searchView.setQuery(searchText, false) }
val searchQuery = viewModel.searchQuery.value
if (!searchQuery.isNullOrEmpty()) {
searchView.post { searchView.setQuery(searchQuery, false) }
searchItem.expandActionView()
UIUtils.setMenuItemsVisibility(menu, searchItem, false)
}
Expand All @@ -117,7 +75,7 @@ class MainActivity : BaseActivity() {
}

override fun onQueryTextChange(newText: String): Boolean {
filter(newText)
viewModel.searchQuery.value = newText
return false
}
})
Expand All @@ -128,7 +86,6 @@ class MainActivity : BaseActivity() {
}

override fun onMenuItemActionCollapse(item: MenuItem): Boolean {
searchText = null
UIUtils.setMenuItemsVisibility(menu, true)
invalidateOptionsMenu()
return true
Expand Down Expand Up @@ -157,8 +114,6 @@ class MainActivity : BaseActivity() {

companion object {

private const val STATE_SEARCH_TEXT = "state_search_text"

init {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
}
Expand Down
28 changes: 0 additions & 28 deletions app/src/main/java/com/sdex/activityrunner/MainViewModel.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,40 +1,35 @@
package com.sdex.activityrunner.app

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.View.GONE
import android.view.View.VISIBLE
import androidx.activity.viewModels
import androidx.appcompat.widget.SearchView
import androidx.lifecycle.Observer
import com.sdex.activityrunner.R
import com.sdex.activityrunner.db.cache.ApplicationModel
import com.sdex.activityrunner.extensions.addDivider
import com.sdex.activityrunner.extensions.enableBackButton
import com.sdex.activityrunner.preferences.AppPreferences
import com.sdex.activityrunner.ui.SnackbarContainerActivity
import com.sdex.commons.BaseActivity
import com.sdex.commons.util.UIUtils
import kotlinx.android.synthetic.main.activity_activities_list.*

class ActivitiesListActivity : BaseActivity(), SnackbarContainerActivity {
class ActivitiesListActivity : BaseActivity() {

private val appPreferences: AppPreferences by lazy { AppPreferences(this) }
private val viewModel: ActivitiesListViewModel by viewModels()
private val viewModel by viewModels<ActivitiesListViewModel>()
private val appPreferences by lazy { AppPreferences(this) }

private lateinit var appPackageName: String

private var isShowNotExported: Boolean = false
private var appPackageName: String? = null
private var searchText: String? = null

override fun getLayout(): Int {
return R.layout.activity_activities_list
}
override fun getLayout() = R.layout.activity_activities_list

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -52,22 +47,22 @@ class ActivitiesListActivity : BaseActivity(), SnackbarContainerActivity {

searchText = savedInstanceState?.getString(STATE_SEARCH_TEXT)

viewModel.getItems(appPackageName!!).observe(this, Observer {
viewModel.getItems(appPackageName).observe(this) {
adapter.submitList(it)
val size = it!!.size
val size = it.size
setSubtitle(resources.getQuantityString(R.plurals.activities_count, size, size))
if (size == 0 && searchText == null) {
empty.visibility = VISIBLE
} else {
empty.visibility = GONE
}
})
}

isShowNotExported = appPreferences.showNotExported

turnOnAdvanced.setOnClickListener {
appPreferences.showNotExported = true
viewModel.reloadItems(appPackageName!!)
viewModel.reloadItems(appPackageName)
}

if (!appPreferences.showNotExported && !appPreferences.isNotExportedDialogShown) {
Expand All @@ -80,7 +75,7 @@ class ActivitiesListActivity : BaseActivity(), SnackbarContainerActivity {
override fun onStart() {
super.onStart()
if (appPreferences.showNotExported != isShowNotExported) {
viewModel.reloadItems(appPackageName!!)
viewModel.reloadItems(appPackageName)
}
}

Expand Down Expand Up @@ -128,17 +123,9 @@ class ActivitiesListActivity : BaseActivity(), SnackbarContainerActivity {
return super.onCreateOptionsMenu(menu)
}

override fun getView(): View {
return container
}

override fun getActivity(): Activity {
return this
}

private fun filter(text: String) {
this.searchText = text
viewModel.filterItems(appPackageName!!, searchText)
viewModel.filterItems(appPackageName, searchText)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,17 @@ import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.sdex.activityrunner.R
import com.sdex.activityrunner.app.dialog.ActivityMenuDialog
import com.sdex.activityrunner.app.dialog.ActivityOptionsDialog
import com.sdex.activityrunner.glide.GlideApp
import com.sdex.activityrunner.ui.SnackbarContainerActivity
import com.sdex.commons.BaseActivity
import kotlinx.android.synthetic.main.item_activity.view.*

class ActivitiesListAdapter(snackbarContainerActivity: SnackbarContainerActivity) :
ListAdapter<ActivityModel, ActivitiesListAdapter.ViewHolder>(DIFF_CALLBACK) {
class ActivitiesListAdapter(
activity: BaseActivity
) : ListAdapter<ActivityModel, ActivitiesListAdapter.ViewHolder>(DIFF_CALLBACK) {

private val glide: RequestManager
private val launcher: ActivityLauncher

init {
val activity = snackbarContainerActivity.getActivity()
this.glide = GlideApp.with(activity)
this.launcher = ActivityLauncher(snackbarContainerActivity)
}
private val glide = GlideApp.with(activity)
private val launcher = ActivityLauncher(activity)

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(parent.context)
Expand All @@ -37,12 +32,12 @@ class ActivitiesListAdapter(snackbarContainerActivity: SnackbarContainerActivity
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bindTo(getItem(position), glide, launcher)
holder.bind(getItem(position), glide, launcher)
}

class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

fun bindTo(
fun bind(
item: ActivityModel,
glide: RequestManager,
launcher: ActivityLauncher
Expand Down Expand Up @@ -79,29 +74,28 @@ class ActivitiesListAdapter(snackbarContainerActivity: SnackbarContainerActivity

private fun showActivityMenu(context: Context, model: ActivityModel) {
val activity = context as FragmentActivity
val dialog = ActivityMenuDialog.newInstance(model)
dialog.show(activity.supportFragmentManager, ActivityMenuDialog.TAG)
val dialog = ActivityOptionsDialog.newInstance(model)
dialog.show(activity.supportFragmentManager, ActivityOptionsDialog.TAG)
}
}

companion object {

val DIFF_CALLBACK: DiffUtil.ItemCallback<ActivityModel> =
object : DiffUtil.ItemCallback<ActivityModel>() {

override fun areItemsTheSame(
oldItem: ActivityModel,
newItem: ActivityModel
): Boolean {
return oldItem == newItem
}

override fun areContentsTheSame(
oldItem: ActivityModel,
newItem: ActivityModel
): Boolean {
return oldItem == newItem
}
val DIFF_CALLBACK = object : DiffUtil.ItemCallback<ActivityModel>() {

override fun areItemsTheSame(
oldItem: ActivityModel,
newItem: ActivityModel
): Boolean {
return oldItem == newItem
}

override fun areContentsTheSame(
oldItem: ActivityModel,
newItem: ActivityModel
): Boolean {
return oldItem == newItem
}
}
}
}
Loading

0 comments on commit 95ddccb

Please sign in to comment.