mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-12-31 05:37:34 +00:00
Added primitive implementation of modules screen
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package com.topjohnwu.magisk.model.entity.recycler
|
||||
|
||||
import android.content.res.Resources
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.annotation.StringRes
|
||||
import com.topjohnwu.magisk.R
|
||||
import com.topjohnwu.magisk.databinding.ComparableRvItem
|
||||
@@ -9,7 +11,10 @@ import com.topjohnwu.magisk.extensions.get
|
||||
import com.topjohnwu.magisk.extensions.toggle
|
||||
import com.topjohnwu.magisk.model.entity.module.Module
|
||||
import com.topjohnwu.magisk.model.entity.module.Repo
|
||||
import com.topjohnwu.magisk.redesign.module.ModuleViewModel
|
||||
import com.topjohnwu.magisk.utils.KObservableField
|
||||
import com.topjohnwu.magisk.utils.rotationTo
|
||||
import com.topjohnwu.magisk.utils.setRevealed
|
||||
|
||||
class ModuleRvItem(val item: Module) : ComparableRvItem<ModuleRvItem>() {
|
||||
|
||||
@@ -72,4 +77,35 @@ class RepoRvItem(val item: Repo) : ComparableRvItem<RepoRvItem>() {
|
||||
override fun contentSameAs(other: RepoRvItem): Boolean = item == other.item
|
||||
|
||||
override fun itemSameAs(other: RepoRvItem): Boolean = item.id == other.item.id
|
||||
}
|
||||
|
||||
class ModuleItem(val item: Module) : ComparableRvItem<ModuleItem>() {
|
||||
|
||||
override val layoutRes = R.layout.item_module_md2
|
||||
|
||||
val isExpanded = KObservableField(false)
|
||||
val isEnabled = KObservableField(item.enable)
|
||||
|
||||
val isModified get() = item.enable != isEnabled.value
|
||||
|
||||
fun toggle(viewModel: ModuleViewModel) {
|
||||
isEnabled.toggle()
|
||||
viewModel.moveToState(this)
|
||||
}
|
||||
|
||||
fun toggle(view: View) {
|
||||
isExpanded.toggle()
|
||||
view.rotationTo(if (isExpanded.value) 225 else 180)
|
||||
(view.parent as ViewGroup)
|
||||
.findViewById<View>(R.id.module_expand_container)
|
||||
.setRevealed(isExpanded.value)
|
||||
}
|
||||
|
||||
override fun contentSameAs(other: ModuleItem): Boolean = item.version == other.item.version
|
||||
&& item.versionCode == other.item.versionCode
|
||||
&& item.description == other.item.description
|
||||
&& item.name == other.item.name
|
||||
|
||||
override fun itemSameAs(other: ModuleItem): Boolean = item.id == other.item.id
|
||||
|
||||
}
|
||||
@@ -1,16 +1,49 @@
|
||||
package com.topjohnwu.magisk.redesign.module
|
||||
|
||||
import com.topjohnwu.magisk.BR
|
||||
import com.topjohnwu.magisk.model.entity.recycler.ModuleRvItem
|
||||
import com.topjohnwu.magisk.extensions.subscribeK
|
||||
import com.topjohnwu.magisk.model.entity.module.Module
|
||||
import com.topjohnwu.magisk.model.entity.recycler.ModuleItem
|
||||
import com.topjohnwu.magisk.redesign.compat.CompatViewModel
|
||||
import com.topjohnwu.magisk.redesign.home.itemBindingOf
|
||||
import com.topjohnwu.magisk.redesign.superuser.diffListOf
|
||||
import io.reactivex.Single
|
||||
|
||||
class ModuleViewModel : CompatViewModel() {
|
||||
|
||||
val items = diffListOf<ModuleRvItem>()
|
||||
val itemBinding = itemBindingOf<ModuleRvItem> {
|
||||
val items = diffListOf<ModuleItem>()
|
||||
val itemsPending = diffListOf<ModuleItem>()
|
||||
val itemBinding = itemBindingOf<ModuleItem> {
|
||||
it.bindExtra(BR.viewModel, this)
|
||||
}
|
||||
|
||||
override fun refresh() = Single.fromCallable { Module.loadModules() }
|
||||
.map { it.map { ModuleItem(it) } }
|
||||
.map { it to items.calculateDiff(it) }
|
||||
.subscribeK {
|
||||
items.update(it.first, it.second)
|
||||
items.forEach { moveToState(it) }
|
||||
}
|
||||
|
||||
fun moveToState(item: ModuleItem) {
|
||||
val isActive = items.indexOfFirst { it.itemSameAs(item) } != -1
|
||||
val isPending = itemsPending.indexOfFirst { it.itemSameAs(item) } != -1
|
||||
|
||||
when {
|
||||
isActive && isPending -> if (item.isModified) {
|
||||
items.remove(item)
|
||||
} else {
|
||||
itemsPending.remove(item)
|
||||
}
|
||||
isActive && item.isModified -> {
|
||||
items.remove(item)
|
||||
itemsPending.add(item)
|
||||
}
|
||||
isPending && !item.isModified -> {
|
||||
itemsPending.remove(item)
|
||||
items.add(item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -443,4 +443,11 @@ fun ProgressBar.setProgressAnimated(newProgress: Int) {
|
||||
addUpdateListener { progress = it.animatedValue as Int }
|
||||
tag = this
|
||||
}.start()
|
||||
}
|
||||
|
||||
@BindingAdapter("android:rotation")
|
||||
fun View.setRotationNotAnimated(rotation: Int) {
|
||||
if (animation != null) {
|
||||
this.rotation = rotation.toFloat()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user