-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
43 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 37 additions & 3 deletions
40
pegutils/src/main/java/dev/pegasus/utils/base/ParentActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,73 @@ | ||
package dev.pegasus.utils.base | ||
|
||
import android.os.Bundle | ||
import android.util.Log | ||
import android.view.LayoutInflater | ||
import androidx.activity.enableEdgeToEdge | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen | ||
import androidx.core.view.ViewCompat | ||
import androidx.core.view.WindowInsetsCompat | ||
import androidx.core.view.WindowInsetsControllerCompat | ||
import androidx.core.view.updatePadding | ||
import androidx.viewbinding.ViewBinding | ||
import dev.pegasus.utils.utils.PegasusHelperUtils.TAG | ||
|
||
abstract class ParentActivity<T : ViewBinding>(val bindingFactory: (LayoutInflater) -> T, private val installSplash: Boolean = false) : AppCompatActivity() { | ||
abstract class ParentActivity<T : ViewBinding>(private val bindingFactory: (LayoutInflater) -> T, private val installSplash: Boolean = false) : AppCompatActivity() { | ||
|
||
protected val binding by lazy { bindingFactory(layoutInflater) } | ||
protected var includeTopPadding = true | ||
protected var includeBottomPadding = true | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
if (installSplash) { | ||
installSplashScreen() | ||
} | ||
|
||
enableEdgeToEdge() | ||
enableDynamicTheme() | ||
|
||
setContentView(binding.root) | ||
setPadding() | ||
hideStatusBar(-1) | ||
onCreated() | ||
} | ||
|
||
private fun setPadding() { | ||
ViewCompat.setOnApplyWindowInsetsListener(binding.root) { v, insets -> | ||
val bars = insets.getInsets(WindowInsetsCompat.Type.systemBars() or WindowInsetsCompat.Type.displayCutout()) | ||
v.updatePadding(left = bars.left, top = bars.top, right = bars.right, bottom = bars.bottom) | ||
val topPadding = if (includeTopPadding) bars.top else 0 | ||
val bottomPadding = if (includeBottomPadding) bars.bottom else 0 | ||
v.updatePadding(left = bars.left, top = topPadding, right = bars.right, bottom = bottomPadding) | ||
WindowInsetsCompat.CONSUMED | ||
} | ||
} | ||
|
||
protected open fun enableDynamicTheme() {} | ||
|
||
/** | ||
* @param type | ||
* 0: Show SystemBars | ||
* 1: Hide StatusBars | ||
* 2: Hide NavigationBars | ||
* 3: Hide SystemBars | ||
*/ | ||
|
||
protected open fun hideStatusBar(type: Int) { | ||
WindowInsetsControllerCompat(window, window.decorView).apply { | ||
systemBarsBehavior = when (type) { | ||
0 -> WindowInsetsControllerCompat.BEHAVIOR_DEFAULT | ||
else -> WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE | ||
} | ||
when (type) { | ||
0 -> show(WindowInsetsCompat.Type.systemBars()) | ||
1 -> hide(WindowInsetsCompat.Type.statusBars()) | ||
2 -> hide(WindowInsetsCompat.Type.navigationBars()) | ||
else -> hide(WindowInsetsCompat.Type.systemBars()) | ||
} | ||
} | ||
} | ||
|
||
abstract fun onCreated() | ||
} |