mirror of
https://github.com/topjohnwu/Magisk.git
synced 2024-11-24 02:25:28 +00:00
Added compat layer for activities and fragments
This change is made so logic is not placed within the "old" base substrate. Changes made in the redesign could potentially affect the already working part which we obviously do not want.
This commit is contained in:
parent
14e49f3c80
commit
8c224da5d5
@ -1,13 +1,18 @@
|
||||
package com.topjohnwu.magisk.redesign
|
||||
|
||||
import android.graphics.Insets
|
||||
import com.topjohnwu.magisk.R
|
||||
import com.topjohnwu.magisk.databinding.ActivityMainMd2Binding
|
||||
import com.topjohnwu.magisk.ui.base.MagiskActivity
|
||||
import com.topjohnwu.magisk.redesign.compat.CompatActivity
|
||||
import org.koin.androidx.viewmodel.ext.android.viewModel
|
||||
|
||||
open class MainActivity : MagiskActivity<MainViewModel, ActivityMainMd2Binding>() {
|
||||
open class MainActivity : CompatActivity<MainViewModel, ActivityMainMd2Binding>() {
|
||||
|
||||
override val layoutRes = R.layout.activity_main_md2
|
||||
override val viewModel by viewModel<MainViewModel>()
|
||||
|
||||
override fun peekSystemWindowInsets(insets: Insets) {
|
||||
viewModel.insets.value = insets
|
||||
}
|
||||
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
package com.topjohnwu.magisk.redesign
|
||||
|
||||
import com.topjohnwu.magisk.ui.base.MagiskViewModel
|
||||
import com.topjohnwu.magisk.redesign.compat.CompatViewModel
|
||||
|
||||
class MainViewModel : MagiskViewModel()
|
||||
class MainViewModel : CompatViewModel()
|
@ -0,0 +1,21 @@
|
||||
package com.topjohnwu.magisk.redesign.compat
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import androidx.databinding.ViewDataBinding
|
||||
import com.topjohnwu.magisk.ui.base.MagiskActivity
|
||||
|
||||
abstract class CompatActivity<ViewModel : CompatViewModel, Binding : ViewDataBinding> :
|
||||
MagiskActivity<ViewModel, Binding>(), CompatView<ViewModel> {
|
||||
|
||||
override val viewRoot: View get() = binding.root
|
||||
|
||||
private val delegate by lazy { CompatDelegate(this) }
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
delegate.ensureInsets()
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package com.topjohnwu.magisk.redesign.compat
|
||||
|
||||
import android.graphics.Insets
|
||||
import androidx.core.view.ViewCompat
|
||||
import androidx.core.view.WindowInsetsCompat
|
||||
|
||||
class CompatDelegate internal constructor(
|
||||
private val view: CompatView<*>
|
||||
) {
|
||||
|
||||
fun ensureInsets() {
|
||||
ViewCompat.setOnApplyWindowInsetsListener(view.viewRoot) { _, insets ->
|
||||
insets.asInsets()
|
||||
.also { view.peekSystemWindowInsets(it) }
|
||||
.let { view.consumeSystemWindowInsets(it) }
|
||||
.also { if (it != Insets.NONE) view.viewModel.insets.value = it }
|
||||
.subtractBy(insets)
|
||||
}
|
||||
}
|
||||
|
||||
private fun WindowInsetsCompat.asInsets() = Insets.of(
|
||||
systemWindowInsetLeft,
|
||||
systemWindowInsetTop,
|
||||
systemWindowInsetRight,
|
||||
systemWindowInsetBottom
|
||||
)
|
||||
|
||||
private fun Insets.subtractBy(insets: WindowInsetsCompat) = insets.replaceSystemWindowInsets(
|
||||
insets.systemWindowInsetLeft - left,
|
||||
insets.systemWindowInsetTop - top,
|
||||
insets.systemWindowInsetRight - right,
|
||||
insets.systemWindowInsetBottom - bottom
|
||||
)
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.topjohnwu.magisk.redesign.compat
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import androidx.databinding.ViewDataBinding
|
||||
import com.topjohnwu.magisk.ui.base.MagiskFragment
|
||||
|
||||
abstract class CompatFragment<ViewModel : CompatViewModel, Binding : ViewDataBinding>
|
||||
: MagiskFragment<ViewModel, Binding>(), CompatView<ViewModel> {
|
||||
|
||||
override val viewRoot: View get() = binding.root
|
||||
|
||||
private val delegate by lazy { CompatDelegate(this) }
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
delegate.ensureInsets()
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.topjohnwu.magisk.redesign.compat
|
||||
|
||||
import android.graphics.Insets
|
||||
import android.view.View
|
||||
|
||||
internal interface CompatView<ViewModel : CompatViewModel> {
|
||||
|
||||
val viewRoot: View
|
||||
val viewModel: ViewModel
|
||||
|
||||
fun peekSystemWindowInsets(insets: Insets) = Unit
|
||||
fun consumeSystemWindowInsets(insets: Insets) = Insets.NONE
|
||||
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.topjohnwu.magisk.redesign.compat
|
||||
|
||||
import android.graphics.Insets
|
||||
import com.skoumal.teanity.util.KObservableField
|
||||
import com.topjohnwu.magisk.ui.base.MagiskViewModel
|
||||
|
||||
abstract class CompatViewModel : MagiskViewModel() {
|
||||
|
||||
val insets = KObservableField(Insets.NONE)
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user