Skip to content

Commit

Permalink
PegUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
goku143u committed Jul 18, 2024
1 parent c337be7 commit 3a404ac
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
12 changes: 5 additions & 7 deletions app/src/main/java/dev/pegasus/utils/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@ package dev.pegasus.utils.sample

import android.content.Intent
import android.os.Bundle
import android.util.Log
import dev.pegasus.utils.base.ParentActivity
import androidx.appcompat.app.AppCompatActivity
import dev.pegasus.utils.sample.activity.BitmapActivity
import dev.pegasus.utils.sample.activity.DateActivity
import dev.pegasus.utils.sample.activity.FileActivity
import dev.pegasus.utils.sample.databinding.ActivityMainBinding

class MainActivity : ParentActivity<ActivityMainBinding>(ActivityMainBinding::inflate) {
class MainActivity : AppCompatActivity() {

private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }

override fun onCreate(savedInstanceState: Bundle?) {
Log.d("ASD", "MainActivity: onCreate: called")
super.onCreate(savedInstanceState)
Log.d("ASD", "MainActivity: onCreate: removed")
}
setContentView(binding.root)

override fun onCreated() {
binding.mbBitmapMain.setOnClickListener { startActivity(Intent(this, BitmapActivity::class.java)) }
binding.mbDateMain.setOnClickListener { startActivity(Intent(this, DateActivity::class.java)) }
binding.mbFileMain.setOnClickListener { startActivity(Intent(this, FileActivity::class.java)) }
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
tools:context=".MainActivity">


<Button
android:id="@+id/mb_bitmap_Main"
android:layout_width="0dp"
Expand Down Expand Up @@ -39,4 +37,5 @@
app:layout_constraintEnd_toEndOf="@+id/mb_date_Main"
app:layout_constraintStart_toStartOf="@+id/mb_date_Main"
app:layout_constraintTop_toBottomOf="@+id/mb_date_Main" />

</androidx.constraintlayout.widget.ConstraintLayout>
40 changes: 37 additions & 3 deletions pegutils/src/main/java/dev/pegasus/utils/base/ParentActivity.kt
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()
}

0 comments on commit 3a404ac

Please sign in to comment.