Move setContentView out of onCreate

This commit is contained in:
topjohnwu 2020-09-11 02:31:41 -07:00
parent e66b0bf3b2
commit d7f7508fa2
5 changed files with 28 additions and 32 deletions

View File

@ -48,6 +48,9 @@ abstract class BaseUIActivity<VM : BaseViewModel, Binding : ViewDataBinding> :
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
setTheme(themeRes) setTheme(themeRes)
super.onCreate(savedInstanceState)
startObserveEvents()
// We need to set the window background explicitly since for whatever reason it's not // We need to set the window background explicitly since for whatever reason it's not
// propagated upstream // propagated upstream
@ -55,16 +58,6 @@ abstract class BaseUIActivity<VM : BaseViewModel, Binding : ViewDataBinding> :
.use { it.getDrawable(0) } .use { it.getDrawable(0) }
.also { window.setBackgroundDrawable(it) } .also { window.setBackgroundDrawable(it) }
super.onCreate(savedInstanceState)
startObserveEvents()
binding = DataBindingUtil.setContentView<Binding>(this, layoutRes).also {
it.setVariable(BR.viewModel, viewModel)
it.lifecycleOwner = this
}
ensureInsets()
directionsDispatcher.observe(this) { directionsDispatcher.observe(this) {
it?.navigate() it?.navigate()
// we don't want the directions to be re-dispatched, so we preemptively set them to null // we don't want the directions to be re-dispatched, so we preemptively set them to null
@ -74,6 +67,15 @@ abstract class BaseUIActivity<VM : BaseViewModel, Binding : ViewDataBinding> :
} }
} }
fun setContentView() {
binding = DataBindingUtil.setContentView<Binding>(this, layoutRes).also {
it.setVariable(BR.viewModel, viewModel)
it.lifecycleOwner = this
}
ensureInsets()
}
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
viewModel.requestRefresh() viewModel.requestRefresh()

View File

@ -51,6 +51,12 @@ class DieEvent : ViewEvent(), ActivityExecutor {
} }
} }
class ShowUIEvent : ViewEvent(), ActivityExecutor {
override fun invoke(activity: BaseUIActivity<*, *>) {
activity.setContentView()
}
}
class RecreateEvent : ViewEvent(), ActivityExecutor { class RecreateEvent : ViewEvent(), ActivityExecutor {
override fun invoke(activity: BaseUIActivity<*, *>) { override fun invoke(activity: BaseUIActivity<*, *>) {
activity.recreate() activity.recreate()

View File

@ -14,7 +14,6 @@ import androidx.core.view.setPadding
import androidx.core.view.updateLayoutParams import androidx.core.view.updateLayoutParams
import androidx.navigation.NavDirections import androidx.navigation.NavDirections
import com.google.android.material.card.MaterialCardView import com.google.android.material.card.MaterialCardView
import com.topjohnwu.magisk.BuildConfig
import com.topjohnwu.magisk.MainDirections import com.topjohnwu.magisk.MainDirections
import com.topjohnwu.magisk.R import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.arch.BaseUIActivity import com.topjohnwu.magisk.arch.BaseUIActivity
@ -60,6 +59,7 @@ open class MainActivity : BaseUIActivity<MainViewModel, ActivityMainMd2Binding>(
return return
} }
setContentView()
showUnsupportedMessage() showUnsupportedMessage()
askForHomeShortcut() askForHomeShortcut()

View File

@ -18,9 +18,9 @@ import com.topjohnwu.magisk.core.model.su.SuPolicy.Companion.DENY
import com.topjohnwu.magisk.core.su.SuRequestHandler import com.topjohnwu.magisk.core.su.SuRequestHandler
import com.topjohnwu.magisk.core.utils.BiometricHelper import com.topjohnwu.magisk.core.utils.BiometricHelper
import com.topjohnwu.magisk.events.DieEvent import com.topjohnwu.magisk.events.DieEvent
import com.topjohnwu.magisk.events.ShowUIEvent
import com.topjohnwu.magisk.ui.superuser.SpinnerRvItem import com.topjohnwu.magisk.ui.superuser.SpinnerRvItem
import com.topjohnwu.magisk.utils.set import com.topjohnwu.magisk.utils.set
import com.topjohnwu.superuser.internal.UiThreadHandler
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import me.tatarka.bindingcollectionadapter2.BindingListViewAdapter import me.tatarka.bindingcollectionadapter2.BindingListViewAdapter
import me.tatarka.bindingcollectionadapter2.ItemBinding import me.tatarka.bindingcollectionadapter2.ItemBinding
@ -33,26 +33,14 @@ class SuRequestViewModel(
private val res: Resources private val res: Resources
) : BaseViewModel() { ) : BaseViewModel() {
@get:Bindable lateinit var icon: Drawable
var icon: Drawable? = null lateinit var title: String
set(value) = set(value, field, { field = it }, BR.icon) lateinit var packageName: String
@get:Bindable
var title = ""
set(value) = set(value, field, { field = it }, BR.title)
@get:Bindable
var packageName = ""
set(value) = set(value, field, { field = it }, BR.packageName)
@get:Bindable @get:Bindable
var denyText = res.getString(R.string.deny) var denyText = res.getString(R.string.deny)
set(value) = set(value, field, { field = it }, BR.denyText) set(value) = set(value, field, { field = it }, BR.denyText)
@get:Bindable
var warningText = res.getString(R.string.su_warning)
set(value) = set(value, field, { field = it }, BR.warningText)
@get:Bindable @get:Bindable
var selectedItemPosition = 0 var selectedItemPosition = 0
set(value) = set(value, field, { field = it }, BR.selectedItemPosition) set(value) = set(value, field, { field = it }, BR.selectedItemPosition)
@ -124,14 +112,14 @@ class SuRequestViewModel(
icon = policy.applicationInfo.loadIcon(pm) icon = policy.applicationInfo.loadIcon(pm)
title = policy.appName title = policy.appName
packageName = policy.packageName packageName = policy.packageName
UiThreadHandler.handler.post {
// Delay is required to properly do selection
selectedItemPosition = timeoutPrefs.getInt(policy.packageName, 0) selectedItemPosition = timeoutPrefs.getInt(policy.packageName, 0)
}
// Set timer // Set timer
val millis = SECONDS.toMillis(Config.suDefaultTimeout.toLong()) val millis = SECONDS.toMillis(Config.suDefaultTimeout.toLong())
timer = SuTimer(millis, 1000).apply { start() } timer = SuTimer(millis, 1000).apply { start() }
// Actually show the UI
ShowUIEvent().publish()
} }
private inner class SuTimer( private inner class SuTimer(

View File

@ -109,7 +109,7 @@
android:layout_gravity="center_horizontal" android:layout_gravity="center_horizontal"
android:layout_margin="@dimen/l1" android:layout_margin="@dimen/l1"
android:gravity="center" android:gravity="center"
android:text="@{viewModel.warningText}" android:text="@string/su_warning"
android:textAppearance="@style/AppearanceFoundation.Body" android:textAppearance="@style/AppearanceFoundation.Body"
android:textColor="?colorError" android:textColor="?colorError"
android:textStyle="bold" android:textStyle="bold"