diff --git a/app/src/main/java/com/topjohnwu/magisk/redesign/MainActivity.kt b/app/src/main/java/com/topjohnwu/magisk/redesign/MainActivity.kt index cf46defd5..6b4a7ef37 100644 --- a/app/src/main/java/com/topjohnwu/magisk/redesign/MainActivity.kt +++ b/app/src/main/java/com/topjohnwu/magisk/redesign/MainActivity.kt @@ -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() { +open class MainActivity : CompatActivity() { override val layoutRes = R.layout.activity_main_md2 override val viewModel by viewModel() + override fun peekSystemWindowInsets(insets: Insets) { + viewModel.insets.value = insets + } + } \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/redesign/MainViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/redesign/MainViewModel.kt index 9da0545fd..41edb6da4 100644 --- a/app/src/main/java/com/topjohnwu/magisk/redesign/MainViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/redesign/MainViewModel.kt @@ -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() \ No newline at end of file +class MainViewModel : CompatViewModel() \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/redesign/compat/CompatActivity.kt b/app/src/main/java/com/topjohnwu/magisk/redesign/compat/CompatActivity.kt new file mode 100644 index 000000000..f56bcb118 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/redesign/compat/CompatActivity.kt @@ -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 : + MagiskActivity(), CompatView { + + override val viewRoot: View get() = binding.root + + private val delegate by lazy { CompatDelegate(this) } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + delegate.ensureInsets() + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/redesign/compat/CompatDelegate.kt b/app/src/main/java/com/topjohnwu/magisk/redesign/compat/CompatDelegate.kt new file mode 100644 index 000000000..6d7a222ee --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/redesign/compat/CompatDelegate.kt @@ -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 + ) + +} \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/redesign/compat/CompatFragment.kt b/app/src/main/java/com/topjohnwu/magisk/redesign/compat/CompatFragment.kt new file mode 100644 index 000000000..196f37ae2 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/redesign/compat/CompatFragment.kt @@ -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 + : MagiskFragment(), CompatView { + + 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() + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/redesign/compat/CompatView.kt b/app/src/main/java/com/topjohnwu/magisk/redesign/compat/CompatView.kt new file mode 100644 index 000000000..4eb84f0c8 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/redesign/compat/CompatView.kt @@ -0,0 +1,14 @@ +package com.topjohnwu.magisk.redesign.compat + +import android.graphics.Insets +import android.view.View + +internal interface CompatView { + + val viewRoot: View + val viewModel: ViewModel + + fun peekSystemWindowInsets(insets: Insets) = Unit + fun consumeSystemWindowInsets(insets: Insets) = Insets.NONE + +} \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/redesign/compat/CompatViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/redesign/compat/CompatViewModel.kt new file mode 100644 index 000000000..11c1d388b --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/redesign/compat/CompatViewModel.kt @@ -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) + +} \ No newline at end of file