Added "input" settings item, that opens custom input dialog

Updated order of some items in settings
This commit is contained in:
Viktor De Pasquale
2019-11-29 20:07:33 +01:00
parent 02e323133d
commit dec1094a59
9 changed files with 352 additions and 12 deletions

View File

@@ -1,14 +1,22 @@
package com.topjohnwu.magisk.redesign.settings
import android.content.Context
import android.os.Environment
import android.view.LayoutInflater
import androidx.databinding.Bindable
import com.topjohnwu.magisk.BR
import com.topjohnwu.magisk.BuildConfig
import com.topjohnwu.magisk.Config
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.databinding.DialogSettingsDownloadPathBinding
import com.topjohnwu.magisk.databinding.DialogSettingsUpdateChannelBinding
import com.topjohnwu.magisk.extensions.get
import com.topjohnwu.magisk.extensions.subscribeK
import com.topjohnwu.magisk.utils.Utils
import com.topjohnwu.magisk.utils.asTransitive
import com.topjohnwu.magisk.utils.availableLocales
import com.topjohnwu.magisk.utils.currentLocale
import java.io.File
// --- Customization
@@ -69,16 +77,61 @@ object Restore : SettingsItem.Blank() {
fun HideOrRestore() =
if (get<Context>().packageName == BuildConfig.APPLICATION_ID) Hide else Restore
//todo new dialog
object DownloadPath
object DownloadPath : SettingsItem.Input() {
override var value: String by dataObservable(Config.downloadPath) { Config.downloadPath = it }
override val title = R.string.settings_download_path_title.asTransitive()
override val intermediate: String?
get() = if (Utils.ensureDownloadPath(result) != null) result else null
var result = value
@Bindable get
set(value) {
field = value
notifyChange(BR.result)
notifyChange(BR.path)
}
val path
@Bindable get() = File(
Environment.getExternalStorageDirectory(),
result
).absolutePath.orEmpty()
//fixme this
object UpdateChannel : SettingsItem.Selector() {
override var value by dataObservable(Config.updateChannel) { Config.updateChannel = it }
override fun getView(context: Context) = DialogSettingsDownloadPathBinding
.inflate(LayoutInflater.from(context)).also { it.data = this }.root
}
//fixme new dialog
object UpdateChannelUrl
object UpdateChannel : SettingsItem.Selector() {
override var value by dataObservable(Config.updateChannel) { Config.updateChannel = it }
override val title = R.string.settings_update_channel_title.asTransitive()
init {
val entries = resources.getStringArray(R.array.update_channel).let {
if (!Utils.isCanary && Config.updateChannel < Config.Value.CANARY_CHANNEL)
it.take(it.size - 2).toTypedArray() else it
}
setValues(
entries,
resources.getStringArray(R.array.value_array)
)
}
}
object UpdateChannelUrl : SettingsItem.Input() {
override val title = R.string.settings_update_custom.asTransitive()
override var value: String by dataObservable(Config.customChannelUrl) {
Config.customChannelUrl = it
}
override val intermediate: String? get() = result
var result = value
@Bindable get
set(value) {
field = value
notifyChange(BR.result)
}
override fun getView(context: Context) = DialogSettingsUpdateChannelBinding
.inflate(LayoutInflater.from(context)).also { it.data = this }.root
}
object UpdateChecker : SettingsItem.Toggle() {
override val title = R.string.settings_check_update_title.asTransitive()

View File

@@ -1,5 +1,6 @@
package com.topjohnwu.magisk.redesign.settings
import android.content.Context
import android.content.res.Resources
import android.view.MotionEvent
import android.view.View
@@ -8,6 +9,7 @@ import androidx.databinding.Bindable
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.topjohnwu.magisk.BR
import com.topjohnwu.magisk.Config
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.model.entity.recycler.ObservableItem
import com.topjohnwu.magisk.model.events.DieEvent
@@ -29,10 +31,11 @@ class SettingsViewModel : CompatViewModel(), SettingsItem.Callback {
val itemBinding = itemBindingOf<SettingsItem> { it.bindExtra(BR.callback, this) }
val items = diffListOf(
Customization,
Theme, Language, Redesign,
Theme, Language, Redesign, DownloadPath,
Manager,
ClearRepoCache, HideOrRestore(), UpdateChecker, SystemlessHosts, Biometrics,
UpdateChannel, UpdateChannelUrl, ClearRepoCache, HideOrRestore(), UpdateChecker,
SystemlessHosts, Biometrics,
Magisk,
SafeMode, MagiskHide,
@@ -44,11 +47,23 @@ class SettingsViewModel : CompatViewModel(), SettingsItem.Callback {
override fun onItemPressed(view: View, item: SettingsItem) = when (item) {
// use only instances you want, don't declare everything
Theme -> Navigation.theme().publish()
Redesign -> DieEvent().publish()
is Theme -> Navigation.theme().publish()
is Redesign -> DieEvent().publish()
is UpdateChannel -> item.openUrlIfNecessary(view)
else -> Unit
}
private fun UpdateChannel.openUrlIfNecessary(view: View) {
if (value == Config.Value.CUSTOM_CHANNEL) {
if (UpdateChannelUrl.value.isBlank()) {
UpdateChannelUrl.onPressed(view, this@SettingsViewModel)
}
UpdateChannelUrl.isEnabled = true
} else {
UpdateChannelUrl.isEnabled = false
}
}
}
sealed class SettingsItem : ObservableItem<SettingsItem>() {
@@ -60,6 +75,13 @@ sealed class SettingsItem : ObservableItem<SettingsItem>() {
@Bindable
open val description: TransitiveText = TransitiveText.empty
var isEnabled = true
@Bindable get
set(value) {
field = value
notifyChange(BR.enabled)
}
protected open val isFullSpan: Boolean = false
@CallSuper
@@ -127,6 +149,40 @@ sealed class SettingsItem : ObservableItem<SettingsItem>() {
}
abstract class Input : Value<String>(), KoinComponent {
override val layoutRes = R.layout.item_settings_input
protected val resources get() = get<Resources>()
protected abstract val intermediate: String?
override fun onPressed(view: View, callback: Callback) {
MagiskDialog(view.context)
.applyTitle(title.getText(resources))
.applyView(getView(view.context))
.applyButton(MagiskDialog.ButtonType.POSITIVE) {
titleRes = android.R.string.ok
onClick {
intermediate?.let { result ->
preventDismiss = false
value = result
it.dismiss()
super.onPressed(view, callback)
return@onClick
}
preventDismiss = true
}
}
.applyButton(MagiskDialog.ButtonType.NEGATIVE) {
titleRes = android.R.string.cancel
}
.reveal()
}
abstract fun getView(context: Context): View
}
abstract class Selector : Value<Int>(), KoinComponent {
override val layoutRes = R.layout.item_settings_selector