All Downloads are FREE. Search and download functionalities are using the official Maven repository.

androidMain.tech.skot.core.components.SKScreenView.kt Maven / Gradle / Ivy

There is a newer version: 1.2.9
Show newest version
package tech.skot.core.components

import android.view.View
import android.view.WindowManager
import androidx.annotation.CallSuper
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.viewbinding.ViewBinding
import tech.skot.core.SKLog
import tech.skot.core.toColor
import tech.skot.core.view.Color
import tech.skot.view.extensions.systemBars
import tech.skot.view.extensions.updatePadding


abstract class SKScreenView(
    override val proxy: SKScreenViewProxy,
    activity: SKActivity,
    fragment: Fragment?,
    binding: B
) : SKComponentView(proxy, activity, fragment, binding) {
    val view: View = binding.root


    private var onBackPressed: (() -> Unit)? = null
    fun setOnBackPressed(onBackPressed: (() -> Unit)?) {
        this.onBackPressed = onBackPressed
    }


    protected val originalPaddingTop = view.paddingTop


    @CallSuper
    open fun onResume() {
        if (fragment !is DialogFragment) {
            if (secured) {
                activity.window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)
            }
            else {
                activity.window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
            }

            if (fragment?.arguments?.getBoolean(ScreensManager.SK_ARGUMENT_CAN_SET_FULL_SCREEN) != false) {
                activity.setFullScreen(
                    fullScreen,
                    lightStatusBar,
                    onWindowInset ?: (if (withWindowsInsetsPaddingTop) {
                        {
                            view.updatePadding(top = originalPaddingTop + it.systemBars().top)
                        }
                    } else null)
                )
            }
        }
        onStatusBarColor(proxy.statusBarColor)
        proxy.onResume()

    }

    @CallSuper
    open fun onPause() {
        proxy.onPause()
    }


    open val fullScreen: Boolean = false
    open val lightStatusBar: Boolean? = null
    open val secured:Boolean = false

    fun onStatusBarColor(color: Color?) {
        if (fragment !is DialogFragment) {
            activity.requestedStatusBarColor = color?.toColor(context)
            activity.window.statusBarColor =
                color?.toColor(context) ?: activity.statusBarColor
        }
    }

    protected open val withWindowsInsetsPaddingTop: Boolean = false

    open val onWindowInset: ((windowInsets: WindowInsetsCompat) -> Unit)? = null


    init {
        ScreensManager.backPressed.observe(lifecycleOwner = lifecycleOwner) {
            if (lifecycleOwner.lifecycle.currentState.isAtLeast(Lifecycle.State.RESUMED)) {
                onBackPressed?.invoke()
            }
        }

    }

}