package com.biopassid.signaturesdkreactnative import android.content.Context import android.graphics.Bitmap import android.graphics.Color import android.util.Base64 import android.util.Log import android.util.Size import br.com.biopassid.signaturesdk.config.SignatureConfig import br.com.biopassid.signaturesdk.config.SignatureScreenOrientation import com.facebook.react.bridge.ReadableMap import java.io.ByteArrayOutputStream const val TAG = "SignatureSdkReactNativeModule/Utils" fun mapToSignatureConfig(context: Context?, signatureConfig: ReadableMap): SignatureConfig { val mapConfig = signatureConfig.toHashMap() val licenseKey = mapConfig["licenseKey"] as String val screenOrientation: SignatureScreenOrientation = when (mapConfig["screenOrientation"] as String) { "LANDSCAPE" -> SignatureScreenOrientation.LANDSCAPE else -> SignatureScreenOrientation.PORTRAIT } val pencilColor = mapConfig["pencilColor"] as String val pencilWidth = mapConfig["pencilWidth"] as Double val backgroundColor = mapConfig["backgroundColor"] as String val overlayColor = mapConfig["overlayColor"] as String val fontFamily = mapConfig["fontFamily"] as String // Gets title text properties val mapTitleText = mapConfig["titleText"] as Map<*, *> val titleTextEnabled = mapTitleText["enabled"] as Boolean val titleTextContent = mapTitleText["content"] as String val titleTextColor = mapTitleText["textColor"] as String val titleTextSize = mapTitleText["textSize"] as Double // Gets back button properties val mapBackButton = mapConfig["backButton"] as Map<*, *> val backButtonEnabled = mapBackButton["enabled"] as Boolean val backButtonBackgroundColor = mapBackButton["backgroundColor"] as String val backButtonPadding = mapBackButton["buttonPadding"] as Double val mapBackButtonSize = mapBackButton["buttonSize"] as Map<*, *> val backButtonSize = Size( (mapBackButtonSize["width"] as Double).toInt(), (mapBackButtonSize["height"] as Double).toInt() ) val mapBackButtonIcon = mapBackButton["iconOptions"] as Map<*, *> val backButtonIconEnabled = mapBackButtonIcon["enabled"] as Boolean val backButtonIconFile = mapBackButtonIcon["iconFile"] as String val backButtonIconColor = mapBackButtonIcon["iconColor"] as String val mapBackButtonIconSize = mapBackButtonIcon["iconSize"] as Map<*, *> val backButtonIconSize = Size( (mapBackButtonIconSize["width"] as Double).toInt(), (mapBackButtonIconSize["height"] as Double).toInt() ) val mapBackButtonLabel = mapBackButton["labelOptions"] as Map<*, *> val backButtonLabelEnabled = mapBackButtonLabel["enabled"] as Boolean val backButtonLabelContent = mapBackButtonLabel["content"] as String val backButtonLabelTextColor = mapBackButtonLabel["textColor"] as String val backButtonLabelTextSize = mapBackButtonLabel["textSize"] as Double // Gets save button properties val mapSaveButton = mapConfig["saveButton"] as Map<*, *> val saveButtonEnabled = mapSaveButton["enabled"] as Boolean val saveButtonBackgroundColor = mapSaveButton["backgroundColor"] as String val saveButtonPadding = mapSaveButton["buttonPadding"] as Double val mapSaveButtonSize = mapSaveButton["buttonSize"] as Map<*, *> val saveButtonSize = Size( (mapSaveButtonSize["width"] as Double).toInt(), (mapSaveButtonSize["height"] as Double).toInt() ) val mapSaveButtonIcon = mapSaveButton["iconOptions"] as Map<*, *> val saveButtonIconEnabled = mapSaveButtonIcon["enabled"] as Boolean val saveButtonIconFile = mapSaveButtonIcon["iconFile"] as String val saveButtonIconColor = mapSaveButtonIcon["iconColor"] as String val mapSaveButtonIconSize = mapSaveButtonIcon["iconSize"] as Map<*, *> val saveButtonIconSize = Size( (mapSaveButtonIconSize["width"] as Double).toInt(), (mapSaveButtonIconSize["height"] as Double).toInt() ) val mapSaveButtonLabel = mapSaveButton["labelOptions"] as Map<*, *> val saveButtonLabelEnabled = mapSaveButtonLabel["enabled"] as Boolean val saveButtonLabelContent = mapSaveButtonLabel["content"] as String val saveButtonLabelTextColor = mapSaveButtonLabel["textColor"] as String val saveButtonLabelTextSize = mapSaveButtonLabel["textSize"] as Double // Gets delete button properties val mapDeleteButton = mapConfig["deleteButton"] as Map<*, *> val deleteButtonEnabled = mapDeleteButton["enabled"] as Boolean val deleteButtonBackgroundColor = mapDeleteButton["backgroundColor"] as String val deleteButtonPadding = mapDeleteButton["buttonPadding"] as Double val mapDeleteButtonSize = mapDeleteButton["buttonSize"] as Map<*, *> val deleteButtonSize = Size( (mapDeleteButtonSize["width"] as Double).toInt(), (mapDeleteButtonSize["height"] as Double).toInt() ) val mapDeleteButtonIcon = mapDeleteButton["iconOptions"] as Map<*, *> val deleteButtonIconEnabled = mapDeleteButtonIcon["enabled"] as Boolean val deleteButtonIconFile = mapDeleteButtonIcon["iconFile"] as String val deleteButtonIconColor = mapDeleteButtonIcon["iconColor"] as String val mapDeleteButtonIconSize = mapDeleteButtonIcon["iconSize"] as Map<*, *> val deleteButtonIconSize = Size( (mapDeleteButtonIconSize["width"] as Double).toInt(), (mapDeleteButtonIconSize["height"] as Double).toInt() ) val mapDeleteButtonLabel = mapDeleteButton["labelOptions"] as Map<*, *> val deleteButtonLabelEnabled = mapDeleteButtonLabel["enabled"] as Boolean val deleteButtonLabelContent = mapDeleteButtonLabel["content"] as String val deleteButtonLabelTextColor = mapDeleteButtonLabel["textColor"] as String val deleteButtonLabelTextSize = mapDeleteButtonLabel["textSize"] as Double // Gets undo button properties val mapUndoButton = mapConfig["undoButton"] as Map<*, *> val undoButtonEnabled = mapUndoButton["enabled"] as Boolean val undoButtonBackgroundColor = mapUndoButton["backgroundColor"] as String val undoButtonPadding = mapUndoButton["buttonPadding"] as Double val mapUndoButtonSize = mapUndoButton["buttonSize"] as Map<*, *> val undoButtonSize = Size( (mapUndoButtonSize["width"] as Double).toInt(), (mapUndoButtonSize["height"] as Double).toInt() ) val mapUndoButtonIcon = mapUndoButton["iconOptions"] as Map<*, *> val undoButtonIconEnabled = mapUndoButtonIcon["enabled"] as Boolean val undoButtonIconFile = mapUndoButtonIcon["iconFile"] as String val undoButtonIconColor = mapUndoButtonIcon["iconColor"] as String val mapUndoButtonIconSize = mapUndoButtonIcon["iconSize"] as Map<*, *> val undoButtonIconSize = Size( (mapUndoButtonIconSize["width"] as Double).toInt(), (mapUndoButtonIconSize["height"] as Double).toInt() ) val mapUndoButtonLabel = mapUndoButton["labelOptions"] as Map<*, *> val undoButtonLabelEnabled = mapUndoButtonLabel["enabled"] as Boolean val undoButtonLabelContent = mapUndoButtonLabel["content"] as String val undoButtonLabelTextColor = mapUndoButtonLabel["textColor"] as String val undoButtonLabelTextSize = mapUndoButtonLabel["textSize"] as Double // Gets font family resource val fontFamilyRes = getResourceId(context, "font", fontFamily) // Gets back button image resource val backButtonIconRes = getResourceId(context, "drawable", backButtonIconFile) // Gets save button image resource val saveButtonIconRes = getResourceId(context, "drawable", saveButtonIconFile) // Gets delete button image resource val deleteButtonIconRes = getResourceId(context, "drawable", deleteButtonIconFile) // Gets undo button image resource val undoButtonIconRes = getResourceId(context, "drawable", undoButtonIconFile) val config = SignatureConfig() config.licenseKey = licenseKey config.screenOrientation = screenOrientation config.pencilColor = hexStringToColor(pencilColor) ?: config.pencilColor config.pencilWidth = pencilWidth.toInt() config.backgroundColor = hexStringToColor(backgroundColor) ?: config.backgroundColor config.overlayColor = hexStringToColor(overlayColor) ?: config.overlayColor config.fontFamily = fontFamilyRes ?: br.com.biopassid.signaturesdk.R.font.signaturesdk_opensans_bold // Sets title text properties config.titleText.enabled = titleTextEnabled config.titleText.content = titleTextContent config.titleText.textColor = hexStringToColor(titleTextColor) ?: config.titleText.textColor config.titleText.textSize = titleTextSize.toInt() // Sets back button properties config.backButton.enabled = backButtonEnabled config.backButton.backgroundColor = hexStringToColor(backButtonBackgroundColor) ?: config.backButton.backgroundColor config.backButton.buttonPadding = backButtonPadding.toInt() config.backButton.buttonSize = backButtonSize config.backButton.iconOptions.enabled = backButtonIconEnabled config.backButton.iconOptions.iconFile = backButtonIconRes ?: br.com.biopassid.signaturesdk.R.drawable.signaturesdk_ic_close config.backButton.iconOptions.iconColor = hexStringToColor(backButtonIconColor) ?: config.backButton.iconOptions.iconColor config.backButton.iconOptions.iconSize = backButtonIconSize config.backButton.labelOptions.enabled = backButtonLabelEnabled config.backButton.labelOptions.content = backButtonLabelContent config.backButton.labelOptions.textColor = hexStringToColor(backButtonLabelTextColor) ?: config.backButton.labelOptions.textColor config.backButton.labelOptions.textSize = backButtonLabelTextSize.toInt() // Sets save button properties config.saveButton.enabled = saveButtonEnabled config.saveButton.backgroundColor = hexStringToColor(saveButtonBackgroundColor) ?: config.saveButton.backgroundColor config.saveButton.buttonPadding = saveButtonPadding.toInt() config.saveButton.buttonSize = saveButtonSize config.saveButton.iconOptions.enabled = saveButtonIconEnabled config.saveButton.iconOptions.iconFile = saveButtonIconRes ?: br.com.biopassid.signaturesdk.R.drawable.signaturesdk_ic_save config.saveButton.iconOptions.iconColor = hexStringToColor(saveButtonIconColor) ?: config.saveButton.iconOptions.iconColor config.saveButton.iconOptions.iconSize = saveButtonIconSize config.saveButton.labelOptions.enabled = saveButtonLabelEnabled config.saveButton.labelOptions.content = saveButtonLabelContent config.saveButton.labelOptions.textColor = hexStringToColor(saveButtonLabelTextColor) ?: config.saveButton.labelOptions.textColor config.saveButton.labelOptions.textSize = saveButtonLabelTextSize.toInt() // Sets delete button properties config.deleteButton.enabled = deleteButtonEnabled config.deleteButton.backgroundColor = hexStringToColor(deleteButtonBackgroundColor) ?: config.deleteButton.backgroundColor config.deleteButton.buttonPadding = deleteButtonPadding.toInt() config.deleteButton.buttonSize = deleteButtonSize config.deleteButton.iconOptions.enabled = deleteButtonIconEnabled config.deleteButton.iconOptions.iconFile = deleteButtonIconRes ?: br.com.biopassid.signaturesdk.R.drawable.signaturesdk_ic_delete config.deleteButton.iconOptions.iconColor = hexStringToColor(deleteButtonIconColor) ?: config.deleteButton.iconOptions.iconColor config.deleteButton.iconOptions.iconSize = deleteButtonIconSize config.deleteButton.labelOptions.enabled = deleteButtonLabelEnabled config.deleteButton.labelOptions.content = deleteButtonLabelContent config.deleteButton.labelOptions.textColor = hexStringToColor(deleteButtonLabelTextColor) ?: config.deleteButton.labelOptions.textColor config.deleteButton.labelOptions.textSize = deleteButtonLabelTextSize.toInt() // Sets undo button properties config.undoButton.enabled = undoButtonEnabled config.undoButton.backgroundColor = hexStringToColor(undoButtonBackgroundColor) ?: config.undoButton.backgroundColor config.undoButton.buttonPadding = undoButtonPadding.toInt() config.undoButton.buttonSize = undoButtonSize config.undoButton.iconOptions.enabled = undoButtonIconEnabled config.undoButton.iconOptions.iconFile = undoButtonIconRes ?: br.com.biopassid.signaturesdk.R.drawable.signaturesdk_ic_undo config.undoButton.iconOptions.iconColor = hexStringToColor(undoButtonIconColor) ?: config.undoButton.iconOptions.iconColor config.undoButton.iconOptions.iconSize = undoButtonIconSize config.undoButton.labelOptions.enabled = undoButtonLabelEnabled config.undoButton.labelOptions.content = undoButtonLabelContent config.undoButton.labelOptions.textColor = hexStringToColor(undoButtonLabelTextColor) ?: config.undoButton.labelOptions.textColor config.undoButton.labelOptions.textSize = undoButtonLabelTextSize.toInt() return config } fun bitmapToBase64(bitmap: Bitmap): String { val stream = ByteArrayOutputStream() bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream) val byteArray = stream.toByteArray() bitmap.recycle() stream.close() return Base64.encodeToString(byteArray, Base64.NO_WRAP) } private fun hexStringToColor(hexString: String): Int? { return try { Color.parseColor(hexString) } catch (e: Exception) { Log.e(TAG, "Error when trying parse color.") null } } private fun getResourceId( context: Context?, resourceName: String, variableName: String ): Int? { try { val resId = context?.resources?.getIdentifier( variableName, resourceName, context.packageName ) if (resId != null && resId != 0) { return resId } Log.e(TAG, "Could not get resource id.") return null } catch (e: Exception) { Log.e(TAG, "Could not get resource id.") return null } }