172 lines
4.9 KiB
Kotlin
172 lines
4.9 KiB
Kotlin
package ru.omni_devel.cards
|
|
|
|
import android.content.Context
|
|
import android.graphics.Bitmap
|
|
import android.graphics.BitmapFactory
|
|
import android.os.LocaleList
|
|
import androidx.compose.ui.graphics.ImageBitmap
|
|
import androidx.compose.ui.graphics.asAndroidBitmap
|
|
import com.google.android.gms.wearable.Wearable
|
|
import com.google.zxing.BarcodeFormat
|
|
import com.google.zxing.EncodeHintType
|
|
import com.google.zxing.MultiFormatWriter
|
|
import com.journeyapps.barcodescanner.BarcodeEncoder
|
|
import org.json.JSONObject
|
|
import java.io.ByteArrayOutputStream
|
|
import android.util.Base64
|
|
import android.widget.Toast
|
|
import androidx.compose.ui.graphics.asImageBitmap
|
|
import kotlinx.serialization.json.Json
|
|
|
|
fun generateCode(value: String, format: BarcodeFormat): Bitmap {
|
|
val writer = MultiFormatWriter()
|
|
|
|
val (width, height) = when (format) {
|
|
BarcodeFormat.QR_CODE, BarcodeFormat.DATA_MATRIX, BarcodeFormat.AZTEC -> 800 to 800
|
|
else -> 800 to 400
|
|
}
|
|
|
|
val hints = mapOf(
|
|
EncodeHintType.CHARACTER_SET to "UTF-8"
|
|
)
|
|
|
|
val matrix = writer.encode(value, format, width, height, hints)
|
|
|
|
return BarcodeEncoder().createBitmap(matrix)
|
|
}
|
|
|
|
fun sendToWatch(context: Context, path: String, message: String): Boolean {
|
|
val nodeClient = Wearable.getNodeClient(context)
|
|
|
|
var isSuccess = false
|
|
|
|
nodeClient.connectedNodes.addOnSuccessListener { nodes ->
|
|
if (nodes.isEmpty()) {
|
|
isSuccess = false
|
|
|
|
return@addOnSuccessListener
|
|
}
|
|
|
|
var completedCount = 0
|
|
var isAnySuccess = false
|
|
|
|
nodes.forEach { node ->
|
|
Wearable.getMessageClient(context).sendMessage(
|
|
node.id,
|
|
path,
|
|
message.toByteArray(Charsets.UTF_8)
|
|
).addOnCompleteListener(context.mainExecutor) { task ->
|
|
completedCount++
|
|
|
|
if (task.isSuccessful) {
|
|
isAnySuccess = true
|
|
}
|
|
|
|
if (completedCount == nodes.size) {
|
|
isSuccess = isAnySuccess
|
|
}
|
|
}.addOnFailureListener(context.mainExecutor) {
|
|
isSuccess = false
|
|
}
|
|
}
|
|
}
|
|
|
|
return isSuccess
|
|
}
|
|
|
|
fun syncCardsWithWatch(context: Context, cards: List<CardInfo>): Boolean {
|
|
return sendToWatch(context, "/updateCards", Json.encodeToString(cards))
|
|
}
|
|
|
|
fun checkCardData(name: String, value: String): Int? {
|
|
if (name.isEmpty()) {
|
|
return R.string.card_name_should_not_be_empty
|
|
} else if (value.isEmpty()) {
|
|
return R.string.card_value_should_not_be_empty
|
|
} else if (name.length > 32) {
|
|
return R.string.name_must_be_shorter_than_32_symbols
|
|
}
|
|
|
|
return null
|
|
}
|
|
|
|
fun getLanguage(context: Context): String {
|
|
val locales: LocaleList = context.resources.configuration.locales
|
|
val primaryLocale = locales[0]
|
|
val languageCode = primaryLocale.language
|
|
|
|
return languageCode
|
|
}
|
|
|
|
fun selectItemNameViaLanguage(names: LinkedHashMap<String, String>, language: String): String {
|
|
return names[language] ?: names.values.firstOrNull() ?: "???"
|
|
}
|
|
|
|
fun jsonObjectToMap(obj: JSONObject): LinkedHashMap<String, String> {
|
|
val map = LinkedHashMap<String, String>()
|
|
val keys = obj.keys()
|
|
|
|
while (keys.hasNext()) {
|
|
val key = keys.next()
|
|
map[key] = obj.getString(key)
|
|
}
|
|
|
|
return map
|
|
}
|
|
|
|
fun imageBitmapToBase64(imageBitmap: ImageBitmap): String {
|
|
val bitmap = imageBitmap.asAndroidBitmap()
|
|
val outputStream = ByteArrayOutputStream()
|
|
|
|
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
|
|
|
|
val byteArray = outputStream.toByteArray()
|
|
|
|
return Base64.encodeToString(byteArray, Base64.DEFAULT)
|
|
}
|
|
|
|
fun base64ToImageBitmap(base64String: String): ImageBitmap {
|
|
val decodedBytes = Base64.decode(base64String, Base64.DEFAULT)
|
|
val bitmap = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.size)
|
|
|
|
return bitmap.asImageBitmap()
|
|
}
|
|
|
|
fun processQrScript(input: String, fills: Map<String, String>): String {
|
|
val outerRegex = Regex("""\$(\w+)((?:\([^)]*\))*)\$""")
|
|
val innerRegex = Regex("""\(([^)]*)\)""")
|
|
|
|
var result = input
|
|
|
|
outerRegex.findAll(input).forEach { match ->
|
|
val fullBlock = match.value
|
|
val name = match.groupValues[1]
|
|
val contents = innerRegex.findAll(match.groupValues[2]).map { it.groupValues[1] }.toList()
|
|
|
|
var blockResult = fills[name] ?: return@forEach
|
|
|
|
contents.forEach { content ->
|
|
val args = content.split(" ")
|
|
val action = args[0]
|
|
|
|
when (action) {
|
|
"first" -> {
|
|
val count = args[1].toInt()
|
|
|
|
blockResult = blockResult.take(count)
|
|
}
|
|
|
|
"last" -> {
|
|
val count = args[1].toInt()
|
|
|
|
blockResult = blockResult.takeLast(count)
|
|
}
|
|
}
|
|
}
|
|
|
|
result = result.replace(fullBlock, blockResult)
|
|
}
|
|
|
|
return result
|
|
}
|