package com.unitreactnativecomponents.nativeComponents.visapresentation import android.content.Context import android.content.res.Configuration import android.util.AttributeSet import android.view.LayoutInflater import android.widget.RelativeLayout import com.unitreactnativecomponents.R /** * This theme enum outlines the background color of your activity */ enum class UNGooglePayButtonTheme { Dark, Colorful, Light, } /** * This style enum outlines the desired style for your button(Outlined can be applied only with a 'Dark' or 'Colorful' theme) */ enum class UNGooglePayButtonStyle { Regular, Outlined, } /** * A class that was created to make Google's branding review implementation easier for Unit customers. */ class UNGooglePayView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, ) : RelativeLayout(context, attrs, defStyleAttr) { private var whiteAddToGooglePayButtonNoShadow: RelativeLayout private var blackAddToGooglePayButton: RelativeLayout private var whiteAddToGooglePayButton: RelativeLayout private var viewLayout: RelativeLayout private var onAddToWalletClicked: (() -> Unit)? = null private var shouldSetThemeAccordingToSystemTheme = false private var isDarkMode = false /** * This variable holds the style of the 'Add To GPay button'. * when it is set it will redisplay the button if it is necessary. */ private var currentStyle: UNGooglePayButtonStyle = UNGooglePayButtonStyle.Regular set(value) { val hasChanged = field != value field = value if (hasChanged) { display() } } /** * This variable holds the theme of the app's background color. * when it is set it will redisplay the button if it is necessary. */ private var currentTheme: UNGooglePayButtonTheme = UNGooglePayButtonTheme.Light set(value) { val hasChanged = field != value field = value if (hasChanged) { display() } } init { val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater inflater.inflate(R.layout.un_view_google_push_provisioning, this) whiteAddToGooglePayButtonNoShadow = findViewById(R.id.white_add_to_googlepay_button_no_shadow) blackAddToGooglePayButton = findViewById(R.id.black_add_to_googlepay_button) whiteAddToGooglePayButton = findViewById(R.id.white_add_to_googlepay_button) viewLayout = findViewById(R.id.google_view_push_provisioning_layout) blackAddToGooglePayButton.setOnClickListener { onAddToWalletClicked?.invoke() } whiteAddToGooglePayButton.setOnClickListener { onAddToWalletClicked?.invoke() } whiteAddToGooglePayButtonNoShadow.setOnClickListener { onAddToWalletClicked?.invoke() } showAddToGooglePlayButton(blackAddToGooglePayButton) } fun setStyle(style: UNGooglePayButtonStyle) { this.currentStyle = style } fun setTheme(theme: UNGooglePayButtonTheme) { setThemeAccordingToSystemTheme(false) this.currentTheme = theme } fun onAddToWalletClicked(callback: () -> Unit) { onAddToWalletClicked = callback } /** * This method will remove all the views related to this class off the screen. */ fun clearGooglePayViews() { viewLayout.visibility = INVISIBLE whiteAddToGooglePayButtonNoShadow.visibility = INVISIBLE blackAddToGooglePayButton.visibility = INVISIBLE whiteAddToGooglePayButton.visibility = INVISIBLE } /** * This private method will display the view according to night mode or it's theme and style. */ private fun display() { clearGooglePayViews() viewLayout.visibility = VISIBLE if (shouldSetThemeAccordingToSystemTheme) { showAddToGooglePlayButtonByUserUiMode() } else { updateViewsAccordingToThemeAndStyle() } } private fun updateViewsAccordingToThemeAndStyle() { when (currentTheme) { UNGooglePayButtonTheme.Light -> { when (currentStyle) { UNGooglePayButtonStyle.Regular -> { showAddToGooglePlayButton(blackAddToGooglePayButton) } UNGooglePayButtonStyle.Outlined -> { showAddToGooglePlayButton(whiteAddToGooglePayButtonNoShadow) } } } UNGooglePayButtonTheme.Dark, UNGooglePayButtonTheme.Colorful -> { when (currentStyle) { UNGooglePayButtonStyle.Regular -> { showAddToGooglePlayButton(whiteAddToGooglePayButton) } else -> { } } } } } /** * This method will determine the 'Add to GPay' button theme according to the mobile phone's Night mode if it is set to 'true'. */ fun setThemeAccordingToSystemTheme(shouldSetThemeAccordingToSystemTheme: Boolean) { this.shouldSetThemeAccordingToSystemTheme = shouldSetThemeAccordingToSystemTheme updateViewAccordingToSystemIfNeeded() } fun updateThemeAccordingToSystem() { updateViewAccordingToSystemIfNeeded() } private fun updateViewAccordingToSystemIfNeeded() { if (shouldSetThemeAccordingToSystemTheme) { changeThemeAccordingToSystem() } } private fun changeThemeAccordingToSystem() { when (resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) { Configuration.UI_MODE_NIGHT_YES -> { isDarkMode = true currentTheme = UNGooglePayButtonTheme.Dark } Configuration.UI_MODE_NIGHT_NO -> { isDarkMode = false currentTheme = UNGooglePayButtonTheme.Light } } } private fun showAddToGooglePlayButtonByUserUiMode() { val buttonType = if (isDarkMode) whiteAddToGooglePayButton else blackAddToGooglePayButton showAddToGooglePlayButton(buttonType) } private fun showAddToGooglePlayButton(button: RelativeLayout) { button.visibility = VISIBLE } }