Added new search functionality to module screen

This commit is contained in:
Viktor De Pasquale
2019-11-14 18:56:03 +01:00
parent c69dcf3e20
commit 9d1d1710eb
14 changed files with 395 additions and 56 deletions

View File

@@ -8,7 +8,12 @@ import com.topjohnwu.magisk.model.entity.module.Repo
interface RepoBase {
fun getRepos(offset: Int, limit: Int = 10): List<Repo>
fun getRepos(offset: Int, limit: Int = LIMIT): List<Repo>
fun searchRepos(query: String, offset: Int, limit: Int = LIMIT): List<Repo>
companion object {
const val LIMIT = 10
}
}
@@ -18,6 +23,19 @@ interface RepoByUpdatedDao : RepoBase {
@Query("SELECT * FROM repos ORDER BY last_update DESC LIMIT :limit OFFSET :offset")
override fun getRepos(offset: Int, limit: Int): List<Repo>
@Query(
"""SELECT *
FROM repos
WHERE
(author LIKE '%' || :query || '%') ||
(name LIKE '%' || :query || '%') ||
(description LIKE '%' || :query || '%')
ORDER BY last_update DESC
LIMIT :limit
OFFSET :offset"""
)
override fun searchRepos(query: String, offset: Int, limit: Int): List<Repo>
}
@Dao
@@ -26,4 +44,18 @@ interface RepoByNameDao : RepoBase {
@Query("SELECT * FROM repos ORDER BY name COLLATE NOCASE LIMIT :limit OFFSET :offset")
override fun getRepos(offset: Int, limit: Int): List<Repo>
@Query(
"""SELECT *
FROM repos
WHERE
(author LIKE '%' || :query || '%') ||
(name LIKE '%' || :query || '%') ||
(description LIKE '%' || :query || '%')
ORDER BY name COLLATE NOCASE
LIMIT :limit
OFFSET :offset"""
)
override fun searchRepos(query: String, offset: Int, limit: Int): List<Repo>
}

View File

@@ -116,19 +116,7 @@ open class MainActivity : CompatActivity<MainViewModel, ActivityMainMd2Binding>(
transactionType: FragNavController.TransactionType
) {
setDisplayHomeAsUpEnabled(!navigation.isRoot)
val lapam = binding.mainBottomBar.layoutParams as ViewGroup.MarginLayoutParams
val height = binding.mainBottomBar.measuredHeight
val verticalMargin = lapam.let { it.topMargin + it.bottomMargin }
val maxTranslation = height + verticalMargin
val translation = if (navigation.isRoot) 0 else maxTranslation
binding.mainBottomBar.animate()
.translationY(translation.toFloat())
.setInterpolator(FastOutSlowInInterpolator())
.withStartAction { if (navigation.isRoot) binding.mainBottomBar.isVisible = true }
.withEndAction { if (!navigation.isRoot) binding.mainBottomBar.isVisible = false }
.start()
requestNavigationHidden(!navigation.isRoot)
}
override fun peekSystemWindowInsets(insets: Insets) {
@@ -143,4 +131,19 @@ open class MainActivity : CompatActivity<MainViewModel, ActivityMainMd2Binding>(
}
}
internal fun requestNavigationHidden(hide: Boolean = true) {
val lapam = binding.mainBottomBar.layoutParams as ViewGroup.MarginLayoutParams
val height = binding.mainBottomBar.measuredHeight
val verticalMargin = lapam.let { it.topMargin + it.bottomMargin }
val maxTranslation = height + verticalMargin
val translation = if (!hide) 0 else maxTranslation
binding.mainBottomBar.animate()
.translationY(translation.toFloat())
.setInterpolator(FastOutSlowInInterpolator())
.withStartAction { if (!hide) binding.mainBottomBar.isVisible = true }
.withEndAction { if (hide) binding.mainBottomBar.isVisible = false }
.start()
}
}

View File

@@ -18,7 +18,7 @@ abstract class CompatFragment<ViewModel : CompatViewModel, Binding : ViewDataBin
private val delegate by lazy { CompatDelegate(this) }
private val compatActivity get() = requireActivity() as CompatActivity<*, *>
protected val compatActivity get() = requireActivity() as CompatActivity<*, *>
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

View File

@@ -0,0 +1,26 @@
package com.topjohnwu.magisk.redesign.compat
import android.os.Handler
import android.os.Looper
interface Queryable {
val queryDelay: Long
val queryHandler: Handler
val queryRunnable: Runnable
fun submitQuery()
companion object {
fun impl(delay: Long = 1000L) = object : Queryable {
override val queryDelay = delay
override val queryHandler = Handler(Looper.getMainLooper())
override val queryRunnable = Runnable { TODO() }
override fun submitQuery() {
queryHandler.removeCallbacks(queryRunnable)
queryHandler.postDelayed(queryRunnable, queryDelay)
}
}
}
}

View File

@@ -1,8 +1,6 @@
package com.topjohnwu.magisk.redesign.hide
import android.content.pm.ApplicationInfo
import android.os.Handler
import android.os.Looper
import androidx.databinding.Bindable
import com.topjohnwu.magisk.BR
import com.topjohnwu.magisk.data.repository.MagiskRepository
@@ -16,20 +14,18 @@ import com.topjohnwu.magisk.model.entity.StatefulProcess
import com.topjohnwu.magisk.model.entity.recycler.HideItem
import com.topjohnwu.magisk.model.entity.recycler.HideProcessItem
import com.topjohnwu.magisk.redesign.compat.CompatViewModel
import com.topjohnwu.magisk.redesign.compat.Queryable
import com.topjohnwu.magisk.redesign.home.itemBindingOf
import com.topjohnwu.magisk.utils.DiffObservableList
import com.topjohnwu.magisk.utils.FilterableDiffObservableList
import com.topjohnwu.magisk.utils.KObservableField
import com.topjohnwu.magisk.utils.currentLocale
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
class HideViewModel(
private val magiskRepo: MagiskRepository
) : CompatViewModel() {
) : CompatViewModel(), Queryable by Queryable.impl(1000) {
private val queryHandler = Handler(Looper.getMainLooper())
private val queryRunnable = Runnable { query() }
override val queryRunnable = Runnable { query() }
var isShowSystem = false
@Bindable get
@@ -67,7 +63,7 @@ class HideViewModel(
.applyViewModel(this)
.subscribeK {
items.update(it.first, it.second)
query()
submitQuery()
}
// ---
@@ -87,9 +83,9 @@ class HideViewModel(
// ---
private fun submitQuery() {
override fun submitQuery() {
queryHandler.removeCallbacks(queryRunnable)
queryHandler.postDelayed(queryRunnable, 1000)
queryHandler.postDelayed(queryRunnable, queryDelay)
}
private fun query(
@@ -130,7 +126,7 @@ class HideViewModel(
}
inline fun <T : ComparableRvItem<T>> filterableListOf(
inline fun <T : ComparableRvItem<*>> filterableListOf(
vararg newItems: T
) = FilterableDiffObservableList(object : DiffObservableList.Callback<T> {
override fun areItemsTheSame(oldItem: T, newItem: T) = oldItem.genericItemSameAs(newItem)

View File

@@ -5,7 +5,10 @@ import android.os.Bundle
import android.view.View
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.databinding.FragmentModuleMd2Binding
import com.topjohnwu.magisk.redesign.MainActivity
import com.topjohnwu.magisk.redesign.compat.CompatFragment
import com.topjohnwu.magisk.redesign.compat.hideKeyboard
import com.topjohnwu.magisk.redesign.hide.MotionRevealHelper
import com.topjohnwu.magisk.utils.EndlessRecyclerScrollListener
import org.koin.androidx.viewmodel.ext.android.viewModel
@@ -14,7 +17,7 @@ class ModuleFragment : CompatFragment<ModuleViewModel, FragmentModuleMd2Binding>
override val layoutRes = R.layout.fragment_module_md2
override val viewModel by viewModel<ModuleViewModel>()
private lateinit var listener: EndlessRecyclerScrollListener
private val listeners = hashSetOf<EndlessRecyclerScrollListener>()
override fun consumeSystemWindowInsets(insets: Insets) = insets
@@ -26,21 +29,45 @@ class ModuleFragment : CompatFragment<ModuleViewModel, FragmentModuleMd2Binding>
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setEndlessScroller()
setEndlessSearch()
binding.moduleFilterToggle.setOnClickListener {
(activity as? MainActivity)?.requestNavigationHidden()
MotionRevealHelper.withViews(binding.moduleFilter, binding.moduleFilterToggle, true)
}
binding.moduleFilterInclude.moduleFilterDone.setOnClickListener {
(activity as? MainActivity)?.requestNavigationHidden(false)
hideKeyboard()
MotionRevealHelper.withViews(binding.moduleFilter, binding.moduleFilterToggle, false)
}
}
override fun onDestroyView() {
if (this::listener.isInitialized) {
binding.moduleRemote.removeOnScrollListener(listener)
listeners.forEach {
binding.moduleRemote.removeOnScrollListener(it)
binding.moduleFilterInclude.moduleFilterList.removeOnScrollListener(it)
}
super.onDestroyView()
}
override fun onPreBind(binding: FragmentModuleMd2Binding) = Unit
private fun setEndlessScroller() {
val lama = binding.moduleRemote.layoutManager ?: return
lama.isAutoMeasureEnabled = false
listener = EndlessRecyclerScrollListener(lama, viewModel::loadRemote)
val listener = EndlessRecyclerScrollListener(lama, viewModel::loadRemote)
binding.moduleRemote.addOnScrollListener(listener)
listeners.add(listener)
}
private fun setEndlessSearch() {
val lama = binding.moduleFilterInclude.moduleFilterList.layoutManager ?: return
lama.isAutoMeasureEnabled = false
val listener = EndlessRecyclerScrollListener(lama, viewModel::loadMoreQuery)
binding.moduleFilterInclude.moduleFilterList.addOnScrollListener(listener)
listeners.add(listener)
}
}

View File

@@ -1,6 +1,7 @@
package com.topjohnwu.magisk.redesign.module
import androidx.annotation.WorkerThread
import androidx.databinding.Bindable
import androidx.databinding.ViewDataBinding
import com.topjohnwu.magisk.BR
import com.topjohnwu.magisk.Config
@@ -19,11 +20,14 @@ import com.topjohnwu.magisk.model.entity.recycler.RepoItem
import com.topjohnwu.magisk.model.entity.recycler.SectionTitle
import com.topjohnwu.magisk.model.events.dialog.ModuleInstallDialog
import com.topjohnwu.magisk.redesign.compat.CompatViewModel
import com.topjohnwu.magisk.redesign.compat.Queryable
import com.topjohnwu.magisk.redesign.home.itemBindingOf
import com.topjohnwu.magisk.redesign.superuser.diffListOf
import com.topjohnwu.magisk.tasks.RepoUpdater
import com.topjohnwu.magisk.utils.KObservableField
import com.topjohnwu.magisk.utils.currentLocale
import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import me.tatarka.bindingcollectionadapter2.BindingRecyclerViewAdapter
import timber.log.Timber
@@ -33,7 +37,27 @@ class ModuleViewModel(
private val repoName: RepoByNameDao,
private val repoUpdated: RepoByUpdatedDao,
private val repoUpdater: RepoUpdater
) : CompatViewModel() {
) : CompatViewModel(), Queryable by Queryable.impl(1000) {
override val queryRunnable = Runnable { query() }
var query = ""
@Bindable get
set(value) {
if (field == value) return
field = value
notifyPropertyChanged(BR.query)
submitQuery()
// Yes we do lie about the search being loaded
searchLoading.value = true
}
private var queryJob: Disposable? = null
val searchLoading = KObservableField(false)
val itemsSearch = diffListOf<RepoItem>()
val itemSearchBinding = itemBindingOf<RepoItem> {
it.bindExtra(BR.viewModel, this)
}
val adapter = adapterOf<ComparableRvItem<*>>()
val items = diffListOf<ComparableRvItem<*>>()
@@ -102,7 +126,7 @@ class ModuleViewModel(
return
}
remoteJob = Single.fromCallable { itemsRemote.size }
.flatMap { loadRepos(offset = it) }
.flatMap { loadRemoteInternal(offset = it) }
.map { it.map { RepoItem(it) } }
.subscribeK(onError = {
Timber.e(it)
@@ -114,14 +138,49 @@ class ModuleViewModel(
}
}
private fun loadRepos(
// ---
override fun submitQuery() {
queryHandler.removeCallbacks(queryRunnable)
queryHandler.postDelayed(queryRunnable, queryDelay)
}
private fun queryInternal(query: String, offset: Int): Single<List<RepoItem>> {
if (query.isBlank()) {
return Single.just(listOf<RepoItem>())
.doOnSubscribe { itemsSearch.clear() }
.subscribeOn(AndroidSchedulers.mainThread())
}
return Single.fromCallable { dao.searchRepos(query, offset) }
.map { it.map { RepoItem(it) } }
}
private fun query(query: String = this.query, offset: Int = 0) {
queryJob?.dispose()
queryJob = queryInternal(query, offset)
.map { it to itemsSearch.calculateDiff(it) }
.observeOn(AndroidSchedulers.mainThread())
.doOnSuccess { searchLoading.value = false }
.subscribeK { itemsSearch.update(it.first, it.second) }
}
@Synchronized
fun loadMoreQuery() {
if (queryJob?.isDisposed == false) return
queryJob = queryInternal(query, itemsSearch.size)
.subscribeK { itemsSearch.addAll(it) }
}
// ---
private fun loadRemoteInternal(
offset: Int = 0,
downloadRepos: Boolean = offset == 0
): Single<List<Repo>> = Single.fromCallable { dao.getRepos(offset) }.flatMap {
when {
// in case we find result empty and offset is initial we need to refresh the repos.
downloadRepos && it.isEmpty() && offset == 0 -> downloadRepos()
.andThen(loadRepos(downloadRepos = false))
.andThen(loadRemoteInternal(downloadRepos = false))
else -> Single.just(it)
}
}
@@ -137,10 +196,11 @@ class ModuleViewModel(
.sortedBy { it.item.name.toLowerCase(currentLocale) }
.toList()
private fun update(repo: Repo, progress: Int) = Single.fromCallable { itemsRemote }
.map { it.first { it.item.id == repo.id } }
.subscribeK { it.progress.value = progress }
.add()
private fun update(repo: Repo, progress: Int) =
Single.fromCallable { itemsRemote + itemsSearch }
.map { it.first { it.item.id == repo.id } }
.subscribeK { it.progress.value = progress }
.add()
// ---