package com.berider.app.common.utils import android.app.Activity import android.content.Intent import android.os.Bundle import android.os.Parcelable import android.view.inputmethod.InputMethodManager import androidx.activity.OnBackPressedCallback import androidx.appcompat.app.ActionBar import androidx.appcompat.app.AppCompatActivity import androidx.core.os.bundleOf import com.afollestad.materialdialogs.LayoutMode import com.afollestad.materialdialogs.customview.customView import com.berider.app.common.R import com.berider.app.common.navigation.Navigation import com.berider.app.common.sharedpref.Generic import com.berider.app.common.sharedpref.credential.CredentialsManager import kotlinx.android.synthetic.main.unauthorized_bottom_sheet.* import org.jetbrains.anko.contentView /** * Created by pavel.petkevich@skodaautodigilab.com on 12.March.2020 */ /** * AppCompatActivity extension function, make from primary colored action bar transparent. * @param isTransparent - Boolean, true-is transparent, otherwise primary colored. * @param showTile - Boolean, true for show title, otherwise do not show title. * @return The Activity's ActionBar, or null if it does not have one. */ fun AppCompatActivity.actionBarTransparent( isTransparent: Boolean = true, showTile: Boolean = false ) = supportActionBar?.apply { setBackgroundDrawable(getDrawable(if (isTransparent) R.color.transparent else R.color.colorOnPrimary)) setDisplayShowTitleEnabled(showTile) } /** * Actionbar extension, for hiding/showing action bar. * @param show - Boolean, true for show, otherwise false for hiding. */ fun ActionBar.showActionBar(show: Boolean = true) { if (show) show() else hide() } /** * AppCompatActivity extension function on registering onBack pressed callback. * @param onBackPressed - lambda function for overriding callback in calling place. * @return OnBackPressedCallback - on back pressed callback. */ fun AppCompatActivity.registerOnBackPressedListener(onBackPressed: () -> Unit): OnBackPressedCallback = object : OnBackPressedCallback(true) { override fun handleOnBackPressed() { onBackPressed.invoke() } }.also { onBackPressedDispatcher.addCallback(this, it) } /** * Activity extension function for safely of hiding the soft key board.s * @return Boolean - in case of hided true, and false if the keyboard wasn't open. */ fun Activity.hideKeyboard() = contentView?.windowToken?.let { wToken -> (getSystemService(Activity.INPUT_METHOD_SERVICE) as? InputMethodManager)?.hideSoftInputFromWindow(wToken, 0) } /** * Activity extension function for getting * Generic parcelable object from bundle. * @param bundleName - name of bundle. * @param argName - argument name. * @return T - generic parcelable object, nullable. */ fun Activity.getBundleArg(bundleName: String, argName: String) = intent?.getBundleExtra(bundleName)?.getParcelable(argName) /** * Activity extension function for getting argument with type Any from bundle. * @param bundleName - name of bundle. * @param argName - argument name. * @return Any - [Any] type, nullable. */ fun Activity.getBundleArg(bundleName: String, argName: String) = intent?.getBundleExtra(bundleName)?.get(argName) /** * Activity extension function for getting bundle name from name of ::class.java. * @return bundle name. */ fun Activity.getBundleName() = "${this::class.java.name}-bundle" /** * Class extension function for getting bundle name from name of Class. * @return bundle name. */ fun Class.getBundleName() = "$name-bundle" /** * Activity extension function, for getting bundle with [bundleName] and [argKeys]. * @return bundle - Returns a new [Bundle] with the given key/value pairs as elements. */ fun Activity.getBundleArgs(bundleName: String, vararg argKeys: String) = bundleOf().apply { argKeys.forEach { argKey -> intent?.getBundleExtra(bundleName)?.get(argKey)?.let { arg -> putAny(argKey, arg) } } } /** * Bundle extension function for putting Any [arg] in correct data type. * @param argKey - key for bundle element. * @param arg - bundle element. * @return Boolean - false in case of value is not supported otherwise true. */ fun Bundle.putAny(argKey: String, arg: Any): Boolean { when { Generic().checkType(arg) -> (arg as? String)?.let { putString(argKey, it) } Generic().checkType(arg) -> (arg as? Boolean)?.let { putBoolean(argKey, it) } Generic().checkType(arg) -> (arg as? Float)?.let { putFloat(argKey, it) } Generic().checkType(arg) -> (arg as? Int)?.let { putInt(argKey, it) } Generic().checkType(arg) -> (arg as? Long)?.let { putLong(argKey, it) } else -> return false } return true } /** * Activity extension, finish activity with result. * @param result - Integer by default [Activity.RESULT_OK], which you can use also you will find here [Activity]. * @param intent - in case you want to return some data use the intent, by default is null. */ fun Activity.finishWithResult(result: Int = Activity.RESULT_OK, intent: Intent? = null) { setResult(result, intent) finish() } /** * Activity extension function, for rendering unauthorized state or continue [block] if the user authorized. * @param credentials - [CredentialsManager]. * @param navigation - [Navigation] - for navigating. */ fun Activity.renderUnauthorizedState(credentials: CredentialsManager, navigation: Navigation, block: () -> Unit) { if (credentials.hasValidCredentials()) block() else unauthorizedBottomSheet(navigation) } /** * Activity extension function, for showing unauthorized bottom sheet. * @param navigation - for navigating into specific sections of Auth Flow [Navigation]. */ fun Activity.unauthorizedBottomSheet(navigation: Navigation) { showBottomSheet(layoutMode = LayoutMode.WRAP_CONTENT) { md -> md.customView(R.layout.unauthorized_bottom_sheet).apply { btnUnauthorizedLogin?.setOnClickListener { navigation.navigateToLogin(this@unauthorizedBottomSheet) md.cancel() } btnUnauthorizedSingUp?.setOnClickListener { navigation.navigateToRegistration(this@unauthorizedBottomSheet) md.cancel() } } } }