Compare commits
38
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2a538fdf1c | ||
|
|
ff214eb3d2 | ||
|
|
bdde3d8fbf | ||
|
|
5da00a1e88 | ||
|
|
7e3a991548 | ||
|
|
e8b572bf9a | ||
|
|
d702928c28 | ||
|
|
28205df5e2 | ||
|
|
26069c19bd | ||
|
|
17ac8d18a3 | ||
|
|
c80958f339 | ||
|
|
8fe59df6f9 | ||
|
|
d3c194ce9c | ||
|
|
13d6ba0b52 | ||
|
|
3cf6b876f9 | ||
|
|
bb57ab007c | ||
|
|
d3ef728007 | ||
|
|
1fa90ff0b7 | ||
|
|
25c38418a3 | ||
|
|
0a383573e2 | ||
|
|
e6ebd574fd | ||
|
|
d7ff167e32 | ||
|
|
9d9b90deb1 | ||
|
|
24927ba7ef | ||
|
|
aea6607c29 | ||
|
|
12b64766ef | ||
|
|
576642a920 | ||
|
|
2cc7a3a2c3 | ||
|
|
464094e6cb | ||
|
|
df95702247 | ||
|
|
1272e87ccd | ||
|
|
123af530ed | ||
|
|
564245aa08 | ||
|
|
2277ae340c | ||
|
|
edcaf6e02c | ||
|
|
da33a6813b | ||
|
|
ca355f4135 | ||
|
|
4a5355e633 |
@@ -23,14 +23,17 @@ android {
|
||||
minSdk = 30
|
||||
targetSdk = 36
|
||||
versionCode = generateVersionCode()
|
||||
versionName = "2.3.1"
|
||||
versionName = "4.3.0"
|
||||
|
||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
debug {
|
||||
applicationIdSuffix = ".debug"
|
||||
}
|
||||
release {
|
||||
isMinifyEnabled = false
|
||||
isMinifyEnabled = true
|
||||
proguardFiles(
|
||||
getDefaultProguardFile("proguard-android-optimize.txt"),
|
||||
"proguard-rules.pro"
|
||||
@@ -44,6 +47,7 @@ android {
|
||||
buildFeatures {
|
||||
compose = true
|
||||
viewBinding = true
|
||||
buildConfig = true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,4 +78,7 @@ dependencies {
|
||||
implementation("com.google.android.gms:play-services-wearable:18.1.0")
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.9.0")
|
||||
implementation(libs.compose.colorpicker)
|
||||
implementation("io.ktor:ktor-client-core:3.5.0")
|
||||
implementation("io.ktor:ktor-client-cio:3.5.0")
|
||||
implementation("androidx.compose.material:material-icons-extended")
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
@@ -1,6 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
@@ -8,6 +10,23 @@
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/Theme.OmniCards">
|
||||
<activity
|
||||
android:name=".SelectIconActivity"
|
||||
android:exported="false"
|
||||
android:label="@string/title_activity_select_icon"
|
||||
android:theme="@style/Theme.OmniCards" />
|
||||
<activity
|
||||
android:name=".FillTemplateActivity"
|
||||
android:exported="false"
|
||||
android:theme="@style/Theme.OmniCards" />
|
||||
<activity
|
||||
android:name=".SelectTemplateActivity"
|
||||
android:exported="false"
|
||||
android:theme="@style/Theme.OmniCards" />
|
||||
<activity
|
||||
android:name=".ChooseColorActivity"
|
||||
android:exported="false"
|
||||
android:theme="@style/Theme.OmniCards" />
|
||||
<activity
|
||||
android:name=".BackupActivity"
|
||||
android:exported="false"
|
||||
|
||||
@@ -11,6 +11,10 @@ import androidx.activity.enableEdgeToEdge
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.Save
|
||||
import androidx.compose.material.icons.outlined.Restore
|
||||
import androidx.compose.material3.AlertDialog
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
@@ -131,15 +135,19 @@ fun BackupPage(innerPadding: PaddingValues, getDbFun: () -> DbHelper) {
|
||||
)
|
||||
}
|
||||
|
||||
Page("Backup", innerPadding) {
|
||||
FullWidthButton(
|
||||
Page(stringResource(R.string.backup), innerPadding) {
|
||||
AdaptiveButton(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
icon = Icons.Outlined.Save,
|
||||
onClick = {
|
||||
createBackupLauncher.launch("omnicards-backup.json")
|
||||
}
|
||||
) {
|
||||
Text(stringResource(R.string.do_backup))
|
||||
}
|
||||
FullWidthButton(
|
||||
AdaptiveButton(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
icon = Icons.Outlined.Restore,
|
||||
onClick = {
|
||||
restoreFromBackupLauncher.launch("application/json")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package ru.omni_devel.cards
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import org.json.JSONArray
|
||||
|
||||
@Serializable
|
||||
class CardIcon(
|
||||
val names: LinkedHashMap<String, String>,
|
||||
val iconId: String? = null
|
||||
) {
|
||||
companion object {
|
||||
fun parseIconsDataFromJson(json: String): List<CardIcon> {
|
||||
val array = JSONArray(json)
|
||||
val icons = mutableListOf<CardIcon>()
|
||||
|
||||
for (i in 0 until array.length()) {
|
||||
val obj = array.getJSONObject(i)
|
||||
|
||||
icons.add(CardIcon(
|
||||
names = jsonObjectToMap(obj.getJSONObject("names")),
|
||||
iconId = if (obj.has("iconId")) obj.getString("iconId") else null
|
||||
))
|
||||
}
|
||||
|
||||
return icons
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,4 +15,5 @@ class CardInfo (
|
||||
val codeType: BarcodeFormat,
|
||||
val codeValue: String,
|
||||
val color: String?,
|
||||
val icon: String?
|
||||
) {}
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
package ru.omni_devel.cards
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.content.res.Configuration
|
||||
import android.os.Bundle
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.LocalActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.platform.LocalConfiguration
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.core.graphics.toColorInt
|
||||
import com.github.skydoves.colorpicker.compose.ColorEnvelope
|
||||
import com.github.skydoves.colorpicker.compose.HsvColorPicker
|
||||
import com.github.skydoves.colorpicker.compose.rememberColorPickerController
|
||||
import ru.omni_devel.cards.ui.theme.OmniCardsTheme
|
||||
|
||||
class ChooseColorActivity : ComponentActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
enableEdgeToEdge()
|
||||
|
||||
val currentColor = intent.getStringExtra("currentColor")
|
||||
|
||||
setContent {
|
||||
OmniCardsTheme {
|
||||
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
|
||||
EditColorPage(innerPadding, currentColor)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun EditColorPage(innerPadding: PaddingValues, currentColor: String?) {
|
||||
val activityContext = LocalActivity.current
|
||||
val configuration = LocalConfiguration.current
|
||||
|
||||
val screenHeight = configuration.screenHeightDp.dp
|
||||
val isPortrait = configuration.orientation == Configuration.ORIENTATION_PORTRAIT
|
||||
|
||||
var color by rememberSaveable { mutableStateOf(currentColor) }
|
||||
|
||||
val colorPickerController = rememberColorPickerController()
|
||||
|
||||
Page(
|
||||
stringResource(R.string.choosing_color),
|
||||
innerPadding
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier.verticalScroll(rememberScrollState()),
|
||||
verticalArrangement = Arrangement.spacedBy(8.dp),
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
var colorPickerModifier = Modifier
|
||||
.padding(10.dp)
|
||||
|
||||
if (isPortrait) {
|
||||
colorPickerModifier = colorPickerModifier
|
||||
.fillMaxWidth()
|
||||
.height(450.dp)
|
||||
} else {
|
||||
val size = screenHeight * 0.5f
|
||||
|
||||
colorPickerModifier = colorPickerModifier
|
||||
.height(size)
|
||||
.width(size)
|
||||
}
|
||||
|
||||
HsvColorPicker(
|
||||
modifier = colorPickerModifier,
|
||||
initialColor = color?.let { Color(it.toColorInt()) },
|
||||
controller = colorPickerController,
|
||||
onColorChanged = { colorEnvelope: ColorEnvelope ->
|
||||
color = "#${colorEnvelope.hexCode.substring(2)}"
|
||||
}
|
||||
)
|
||||
|
||||
AdaptiveButton(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
onClick = {
|
||||
val resultIntent = Intent()
|
||||
|
||||
resultIntent.putExtra("color", color)
|
||||
|
||||
activityContext!!.setResult(Activity.RESULT_OK, resultIntent)
|
||||
activityContext.finish()
|
||||
}
|
||||
) {
|
||||
Text(stringResource(R.string.do_save))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package ru.omni_devel.cards
|
||||
|
||||
const val REPO_BASE_URL = "https://gitea.omni-devel.ru/omni/OmniCardsRepo/raw/branch/main"
|
||||
|
||||
val TEMPLATES_REPO_FILE = if (BuildConfig.DEBUG)
|
||||
"templates.debug.json"
|
||||
else
|
||||
"templates.json"
|
||||
|
||||
const val ICONS_REPO_FILE = "icons.json"
|
||||
const val ICONS_REPO_FOLDER = "icons"
|
||||
|
||||
val SEARCH_REPLACE_SYMBOLS = mapOf(
|
||||
" " to "",
|
||||
"-" to "",
|
||||
"_" to "",
|
||||
"ё" to "е"
|
||||
)
|
||||
@@ -4,11 +4,14 @@ import android.content.ContentValues
|
||||
import android.content.Context
|
||||
import android.database.sqlite.SQLiteDatabase
|
||||
import android.database.sqlite.SQLiteOpenHelper
|
||||
import androidx.core.database.getStringOrNull
|
||||
import com.google.zxing.BarcodeFormat
|
||||
|
||||
class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?) : SQLiteOpenHelper(context, "omni_cards", factory, 2) {
|
||||
class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?) : SQLiteOpenHelper(context, "omni_cards", factory, 6) {
|
||||
override fun onCreate(db: SQLiteDatabase?) {
|
||||
db!!.execSQL("CREATE TABLE IF NOT EXISTS cards (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, codeType TEXT, codeValue TEXT, color TEXT)")
|
||||
db!!.execSQL(
|
||||
"CREATE TABLE IF NOT EXISTS cards (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, codeType TEXT, codeValue TEXT, color TEXT, icon TEXT)"
|
||||
)
|
||||
}
|
||||
|
||||
override fun onUpgrade(
|
||||
@@ -19,30 +22,52 @@ class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?)
|
||||
if (oldVersion < 2) {
|
||||
db!!.execSQL("ALTER TABLE cards ADD COLUMN color TEXT")
|
||||
}
|
||||
|
||||
if (oldVersion < 3) {
|
||||
db!!.execSQL("ALTER TABLE cards ADD COLUMN position INTEGER DEFAULT 0")
|
||||
db.execSQL("ALTER TABLE cards ADD COLUMN iconPath TEXT")
|
||||
db.execSQL("UPDATE cards SET position = id")
|
||||
}
|
||||
|
||||
fun addCard(name: String, codeValue: String, codeType: BarcodeFormat, color: String?) {
|
||||
if (oldVersion < 4) {
|
||||
db!!.execSQL("ALTER TABLE cards ADD COLUMN monochrome INTEGER DEFAULT 0")
|
||||
}
|
||||
|
||||
if (oldVersion < 5) {
|
||||
db!!.execSQL("ALTER TABLE cards DROP COLUMN iconPath")
|
||||
db.execSQL("ALTER TABLE cards DROP COLUMN monochrome")
|
||||
db.execSQL("ALTER TABLE cards DROP COLUMN position")
|
||||
}
|
||||
|
||||
if (oldVersion < 6) {
|
||||
db!!.execSQL("ALTER TABLE cards ADD COLUMN icon TEXT")
|
||||
}
|
||||
}
|
||||
|
||||
fun addCard(name: String, codeValue: String, codeType: BarcodeFormat, color: String?, icon: String?) {
|
||||
val db = this.writableDatabase
|
||||
|
||||
val values = ContentValues()
|
||||
|
||||
values.put("name", name)
|
||||
values.put("codeValue", codeValue)
|
||||
values.put("codeType", codeType.name)
|
||||
values.put("color", color)
|
||||
|
||||
val db = this.writableDatabase
|
||||
values.put("icon", icon)
|
||||
|
||||
db.insert("cards", null, values)
|
||||
|
||||
db.close()
|
||||
}
|
||||
|
||||
fun editCard(id: Int, name: String, codeValue: String, codeType: BarcodeFormat, color: String?) {
|
||||
fun editCard(id: Int, name: String, codeValue: String, codeType: BarcodeFormat, color: String?, icon: String?) {
|
||||
val values = ContentValues()
|
||||
|
||||
values.put("name", name)
|
||||
values.put("codeValue", codeValue)
|
||||
values.put("codeType", codeType.name)
|
||||
values.put("color", color)
|
||||
values.put("icon", icon)
|
||||
|
||||
val db = this.writableDatabase
|
||||
|
||||
@@ -54,7 +79,7 @@ class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?)
|
||||
fun getCards(): List<CardInfo> {
|
||||
val db = this.readableDatabase
|
||||
|
||||
val cursor = db.rawQuery("SELECT * FROM cards", null)
|
||||
val cursor = db.rawQuery("SELECT * FROM cards ORDER BY id ASC", null)
|
||||
val cards = mutableListOf<CardInfo>()
|
||||
|
||||
while (cursor.moveToNext()) {
|
||||
@@ -63,7 +88,8 @@ class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?)
|
||||
name = cursor.getString(cursor.getColumnIndexOrThrow("name")),
|
||||
codeType = BarcodeFormat.valueOf(cursor.getString(cursor.getColumnIndexOrThrow("codeType"))),
|
||||
codeValue = cursor.getString(cursor.getColumnIndexOrThrow("codeValue")),
|
||||
color = cursor.getString(cursor.getColumnIndexOrThrow("color"))
|
||||
color = cursor.getString(cursor.getColumnIndexOrThrow("color")),
|
||||
icon = cursor.getStringOrNull(cursor.getColumnIndexOrThrow("icon"))
|
||||
))
|
||||
}
|
||||
|
||||
@@ -90,7 +116,8 @@ class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?)
|
||||
name = cursor.getString(cursor.getColumnIndexOrThrow("name")),
|
||||
codeType = BarcodeFormat.valueOf(cursor.getString(cursor.getColumnIndexOrThrow("codeType"))),
|
||||
codeValue = cursor.getString(cursor.getColumnIndexOrThrow("codeValue")),
|
||||
color = cursor.getString(cursor.getColumnIndexOrThrow("color"))
|
||||
color = cursor.getString(cursor.getColumnIndexOrThrow("color")),
|
||||
icon = cursor.getStringOrNull(cursor.getColumnIndexOrThrow("icon"))
|
||||
)
|
||||
|
||||
cursor.close()
|
||||
@@ -124,6 +151,7 @@ class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?)
|
||||
values.put("codeValue", card.codeValue)
|
||||
values.put("codeType", card.codeType.name)
|
||||
values.put("color", card.color)
|
||||
values.put("icon", card.icon)
|
||||
|
||||
db.insert("cards", null, values)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package ru.omni_devel.cards
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.widget.Toast
|
||||
import androidx.activity.ComponentActivity
|
||||
@@ -7,6 +9,8 @@ import androidx.activity.compose.LocalActivity
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
@@ -19,13 +23,18 @@ import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.DropdownMenu
|
||||
import androidx.compose.material3.DropdownMenuItem
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Switch
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.Palette
|
||||
import androidx.compose.material.icons.outlined.QrCodeScanner
|
||||
import androidx.compose.material.icons.outlined.TextFields
|
||||
import androidx.compose.material.icons.outlined.Save
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.setValue
|
||||
@@ -37,10 +46,6 @@ import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.core.graphics.toColorInt
|
||||
import com.github.skydoves.colorpicker.compose.ColorEnvelope
|
||||
import com.github.skydoves.colorpicker.compose.HsvColorPicker
|
||||
import com.github.skydoves.colorpicker.compose.rememberColorPickerController
|
||||
import com.google.zxing.BarcodeFormat
|
||||
import com.journeyapps.barcodescanner.ScanContract
|
||||
import com.journeyapps.barcodescanner.ScanOptions
|
||||
@@ -79,15 +84,15 @@ fun CardEditable(
|
||||
codeType: BarcodeFormat,
|
||||
onCodeTypeChange: (BarcodeFormat) -> Unit,
|
||||
color: String?,
|
||||
onColorChange: (String?) -> Unit
|
||||
onColorChange: (String?) -> Unit,
|
||||
icon: String?,
|
||||
onIconChange: (String?) -> Unit
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
|
||||
var isCodeTypeDropdownExpanded by rememberSaveable { mutableStateOf(false) }
|
||||
var isColorPickerExpanded by rememberSaveable { mutableStateOf(false) }
|
||||
var isColorEnabled by rememberSaveable { mutableStateOf(color != null) }
|
||||
|
||||
val colorPickerController = rememberColorPickerController()
|
||||
var currentColor by rememberSaveable { mutableStateOf(color) }
|
||||
|
||||
val fieldModifier = Modifier.fillMaxWidth()
|
||||
|
||||
@@ -100,41 +105,57 @@ fun CardEditable(
|
||||
result.formatName?.let { formatName ->
|
||||
try {
|
||||
onCodeTypeChange(BarcodeFormat.valueOf(formatName))
|
||||
} catch (e: IllegalArgumentException) {}
|
||||
} catch (_: IllegalArgumentException) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val editColorLauncher = rememberLauncherForActivityResult(
|
||||
contract = ActivityResultContracts.StartActivityForResult()
|
||||
) { result ->
|
||||
if (result.resultCode == Activity.RESULT_OK) {
|
||||
val color = result.data?.getStringExtra("color")
|
||||
|
||||
currentColor = color
|
||||
onColorChange(color)
|
||||
}
|
||||
}
|
||||
|
||||
val selectTemplateLauncher = rememberLauncherForActivityResult(
|
||||
contract = ActivityResultContracts.StartActivityForResult()
|
||||
) { result ->
|
||||
if (result.resultCode == Activity.RESULT_OK) {
|
||||
val codeValue = result.data?.getStringExtra("codeValue")!!
|
||||
val codeType = BarcodeFormat.valueOf(result.data?.getStringExtra("codeType")!!)
|
||||
|
||||
onCodeValueChange(codeValue)
|
||||
onCodeTypeChange(codeType)
|
||||
}
|
||||
}
|
||||
|
||||
val selectIconLauncher = rememberLauncherForActivityResult(
|
||||
contract = ActivityResultContracts.StartActivityForResult()
|
||||
) { result ->
|
||||
if (result.resultCode == Activity.RESULT_OK) {
|
||||
val icon = result.data?.getStringExtra("icon")
|
||||
|
||||
onIconChange(icon)
|
||||
}
|
||||
}
|
||||
|
||||
OutlinedTextField(
|
||||
value = cardName,
|
||||
onValueChange = onCardNameChange,
|
||||
modifier = fieldModifier,
|
||||
label = { Text(stringResource(R.string.card_name)) }
|
||||
)
|
||||
Row(
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
|
||||
OutlinedTextField(
|
||||
value = codeValue,
|
||||
onValueChange = onCodeValueChange,
|
||||
modifier = Modifier.weight(1f),
|
||||
modifier = fieldModifier,
|
||||
label = { Text(stringResource(R.string.card_value)) }
|
||||
)
|
||||
Button(
|
||||
modifier = Modifier.align(Alignment.CenterVertically),
|
||||
onClick = {
|
||||
scanLauncher.launch(
|
||||
ScanOptions().apply {
|
||||
setPrompt(context.getString(R.string.scan_your_card))
|
||||
setBeepEnabled(true)
|
||||
setOrientationLocked(false)
|
||||
}
|
||||
)
|
||||
}
|
||||
) {
|
||||
Text(stringResource(R.string.do_scan_card))
|
||||
}
|
||||
}
|
||||
|
||||
Box(
|
||||
modifier = fieldModifier,
|
||||
@@ -170,18 +191,53 @@ fun CardEditable(
|
||||
}
|
||||
}
|
||||
|
||||
AdaptiveButton(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
icon = Icons.Outlined.QrCodeScanner,
|
||||
onClick = {
|
||||
scanLauncher.launch(
|
||||
ScanOptions().apply {
|
||||
setPrompt(context.getString(R.string.scan_your_card))
|
||||
setBeepEnabled(true)
|
||||
setOrientationLocked(true)
|
||||
}
|
||||
)
|
||||
}
|
||||
) {
|
||||
Text(stringResource(R.string.do_scan_card))
|
||||
}
|
||||
|
||||
AdaptiveButton(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
icon = Icons.Outlined.TextFields,
|
||||
onClick = {
|
||||
val intent = Intent(context, SelectTemplateActivity::class.java)
|
||||
|
||||
selectTemplateLauncher.launch(intent)
|
||||
}
|
||||
) {
|
||||
Text(stringResource(R.string.card_from_template))
|
||||
}
|
||||
|
||||
Row(
|
||||
modifier = fieldModifier,
|
||||
horizontalArrangement = Arrangement.SpaceBetween
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
Button(
|
||||
AdaptiveButton(
|
||||
modifier = Modifier.fillMaxWidth().weight(1.0f),
|
||||
icon = Icons.Outlined.Palette,
|
||||
enabled = isColorEnabled,
|
||||
onClick = {
|
||||
isColorPickerExpanded = !isColorPickerExpanded
|
||||
val intent = Intent(context, ChooseColorActivity::class.java)
|
||||
|
||||
intent.putExtra("currentColor", currentColor)
|
||||
|
||||
editColorLauncher.launch(intent)
|
||||
}
|
||||
) {
|
||||
Text(stringResource(R.string.do_choose_color))
|
||||
}
|
||||
|
||||
Switch(
|
||||
checked = isColorEnabled,
|
||||
onCheckedChange = {
|
||||
@@ -194,18 +250,27 @@ fun CardEditable(
|
||||
)
|
||||
}
|
||||
|
||||
if (isColorPickerExpanded && isColorEnabled) {
|
||||
HsvColorPicker(
|
||||
AdaptiveButton(
|
||||
modifier = Modifier.fillMaxWidth().height(64.dp),
|
||||
contentPadding = PaddingValues(horizontal = 0.dp),
|
||||
onClick = {
|
||||
val intent = Intent(context, SelectIconActivity::class.java)
|
||||
|
||||
selectIconLauncher.launch(intent)
|
||||
}
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(450.dp)
|
||||
.padding(10.dp),
|
||||
initialColor = color?.let { Color(it.toColorInt()) },
|
||||
controller = colorPickerController,
|
||||
onColorChanged = { colorEnvelope: ColorEnvelope ->
|
||||
onColorChange("#${colorEnvelope.hexCode.substring(2)}")
|
||||
.fillMaxSize()
|
||||
.background(Color.Transparent)
|
||||
.padding(horizontal = 8.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
CardIcon(if (icon == null) null else base64ToImageBitmap(icon), MaterialTheme.colorScheme.primary)
|
||||
|
||||
Text(stringResource(R.string.do_select_icon))
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -215,6 +280,7 @@ fun AddCard(innerPadding: PaddingValues, getDbFun: () -> DbHelper) {
|
||||
var codeValue by rememberSaveable { mutableStateOf("") }
|
||||
var codeType by rememberSaveable { mutableStateOf(BarcodeFormat.QR_CODE) }
|
||||
var color by rememberSaveable { mutableStateOf<String?>(null) }
|
||||
var icon by rememberSaveable { mutableStateOf<String?>(null) }
|
||||
|
||||
val context = LocalActivity.current
|
||||
|
||||
@@ -234,18 +300,21 @@ fun AddCard(innerPadding: PaddingValues, getDbFun: () -> DbHelper) {
|
||||
codeType = codeType,
|
||||
onCodeTypeChange = { codeType = it },
|
||||
color = color,
|
||||
onColorChange = { color = it }
|
||||
onColorChange = { color = it },
|
||||
icon = icon,
|
||||
onIconChange = { icon = it }
|
||||
)
|
||||
|
||||
Button(
|
||||
AdaptiveButton(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
icon = Icons.Outlined.Save,
|
||||
onClick = {
|
||||
cardName = cardName.trim()
|
||||
|
||||
val err = checkCardData(cardName, codeValue)
|
||||
|
||||
if (err == null) {
|
||||
getDbFun().addCard(cardName, codeValue, codeType, color)
|
||||
getDbFun().addCard(cardName, codeValue, codeType, color, icon)
|
||||
context!!.finish()
|
||||
} else {
|
||||
Toast.makeText(context, context!!.getString(err), Toast.LENGTH_SHORT).show()
|
||||
@@ -264,6 +333,7 @@ fun EditCard(cardInfo: CardInfo, innerPadding: PaddingValues, getDbFun: () -> Db
|
||||
var codeValue by rememberSaveable { mutableStateOf(cardInfo.codeValue) }
|
||||
var codeType by rememberSaveable { mutableStateOf(cardInfo.codeType) }
|
||||
var color by rememberSaveable { mutableStateOf(cardInfo.color) }
|
||||
var icon by rememberSaveable { mutableStateOf(cardInfo.icon) }
|
||||
|
||||
val context = LocalActivity.current
|
||||
|
||||
@@ -283,18 +353,21 @@ fun EditCard(cardInfo: CardInfo, innerPadding: PaddingValues, getDbFun: () -> Db
|
||||
codeType = codeType,
|
||||
onCodeTypeChange = { codeType = it },
|
||||
color = color,
|
||||
onColorChange = { color = it }
|
||||
onColorChange = { color = it },
|
||||
icon = icon,
|
||||
onIconChange = { icon = it }
|
||||
)
|
||||
|
||||
Button(
|
||||
AdaptiveButton(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
icon = Icons.Outlined.Save,
|
||||
onClick = {
|
||||
cardName = cardName.trim()
|
||||
|
||||
val err = checkCardData(cardName, codeValue)
|
||||
|
||||
if (err == null) {
|
||||
getDbFun().editCard(cardInfo.id, cardName, codeValue, codeType, color)
|
||||
getDbFun().editCard(cardInfo.id, cardName, codeValue, codeType, color, icon)
|
||||
context!!.finish()
|
||||
} else {
|
||||
Toast.makeText(context, context!!.getString(err), Toast.LENGTH_SHORT).show()
|
||||
|
||||
@@ -1,34 +1,69 @@
|
||||
package ru.omni_devel.cards
|
||||
|
||||
import android.content.Intent
|
||||
import androidx.activity.compose.BackHandler
|
||||
import androidx.activity.compose.LocalActivity
|
||||
import androidx.compose.animation.AnimatedVisibility
|
||||
import androidx.compose.animation.core.animateFloatAsState
|
||||
import androidx.compose.animation.fadeIn
|
||||
import androidx.compose.animation.fadeOut
|
||||
import androidx.compose.animation.scaleIn
|
||||
import androidx.compose.animation.scaleOut
|
||||
import androidx.compose.foundation.BorderStroke
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.gestures.detectTapGestures
|
||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.IntrinsicSize
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxHeight
|
||||
import androidx.compose.foundation.layout.RowScope
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.ButtonDefaults
|
||||
import androidx.compose.material3.ButtonElevation
|
||||
import androidx.compose.material3.DrawerValue
|
||||
import androidx.compose.material3.DropdownMenu
|
||||
import androidx.compose.material3.DropdownMenuItem
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material.icons.outlined.Add
|
||||
import androidx.compose.material.icons.outlined.QrCode
|
||||
import androidx.compose.material.icons.outlined.DensityMedium
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.ExtendedFloatingActionButton
|
||||
import androidx.compose.material3.FloatingActionButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.ModalDrawerSheet
|
||||
import androidx.compose.material3.ModalNavigationDrawer
|
||||
import androidx.compose.material3.NavigationDrawerItem
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.rememberDrawerState
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.ImageBitmap
|
||||
import androidx.compose.ui.graphics.graphicsLayer
|
||||
import androidx.compose.ui.graphics.vector.ImageVector
|
||||
import androidx.compose.ui.input.pointer.pointerInput
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
@@ -36,51 +71,135 @@ import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.core.graphics.toColorInt
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@Composable
|
||||
fun Page(name: String, innerPadding: PaddingValues, content: @Composable () -> Unit) {
|
||||
Column() {
|
||||
TopBar(name, innerPadding)
|
||||
|
||||
Column(
|
||||
fun Page(
|
||||
name: String,
|
||||
innerPadding: PaddingValues,
|
||||
topBarAdditionalElements: @Composable () -> Unit = {},
|
||||
floatingActionButton: @Composable (() -> Unit) = {},
|
||||
content: @Composable () -> Unit
|
||||
) {
|
||||
Scaffold(
|
||||
topBar = {
|
||||
TopBar(name, innerPadding, topBarAdditionalElements)
|
||||
},
|
||||
floatingActionButton = floatingActionButton
|
||||
) { padding ->
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.padding(padding)
|
||||
.fillMaxSize()
|
||||
.padding(horizontal = 16.dp)
|
||||
.background(MaterialTheme.colorScheme.background)
|
||||
.padding(horizontal = 16.dp)
|
||||
.padding(top = 16.dp)
|
||||
) {
|
||||
Column(
|
||||
verticalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
content()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun TopBar(text: String, innerPadding: PaddingValues) {
|
||||
fun ExpandableFab(
|
||||
items: List<NavigationPageData>
|
||||
) {
|
||||
val activityContext = LocalActivity.current
|
||||
var isExpanded by remember { mutableStateOf(false) }
|
||||
|
||||
val rotation by animateFloatAsState(if (isExpanded) 45f else 0f)
|
||||
|
||||
BackHandler(
|
||||
enabled = isExpanded
|
||||
) {
|
||||
isExpanded = false
|
||||
}
|
||||
|
||||
Column(
|
||||
horizontalAlignment = Alignment.End,
|
||||
verticalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
items.forEach { page ->
|
||||
AnimatedVisibility(
|
||||
visible = isExpanded,
|
||||
enter = fadeIn() + scaleIn(),
|
||||
exit = fadeOut() + scaleOut()
|
||||
) {
|
||||
ExtendedFloatingActionButton(
|
||||
text = { Text(page.name) },
|
||||
icon = { Icon(page.icon, contentDescription = null) },
|
||||
onClick = {
|
||||
isExpanded = false
|
||||
|
||||
if (page.pageClass != null) {
|
||||
val intent = Intent(activityContext, page.pageClass)
|
||||
activityContext!!.startActivity(intent)
|
||||
} else if (page.onClick != null) {
|
||||
page.onClick()
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
FloatingActionButton(
|
||||
onClick = { isExpanded = !isExpanded }
|
||||
) {
|
||||
Icon(
|
||||
modifier = Modifier.graphicsLayer { rotationZ = rotation },
|
||||
imageVector = Icons.Outlined.Add,
|
||||
contentDescription = null
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun TopBar(
|
||||
text: String,
|
||||
innerPadding: PaddingValues,
|
||||
topBarAdditionalElements: @Composable () -> Unit = {}
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.background(MaterialTheme.colorScheme.primaryContainer)
|
||||
.padding(top = innerPadding.calculateTopPadding())
|
||||
.padding(horizontal = 8.dp)
|
||||
.padding(bottom = 8.dp)
|
||||
.padding(vertical = 12.dp, horizontal = 12.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.SpaceBetween
|
||||
) {
|
||||
Text(
|
||||
text,
|
||||
modifier = Modifier.padding(vertical = 4.dp, horizontal = 12.dp),
|
||||
modifier = Modifier,
|
||||
fontSize = 24.sp,
|
||||
color = MaterialTheme.colorScheme.onPrimaryContainer
|
||||
)
|
||||
|
||||
topBarAdditionalElements()
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun CardButton(cardInfo: CardInfo, onDeleteCard: (Int) -> Unit) {
|
||||
val context = LocalContext.current
|
||||
|
||||
var menuExpanded by remember { mutableStateOf(false) }
|
||||
|
||||
val cardColor = if (cardInfo.color == null)
|
||||
MaterialTheme.colorScheme.primaryContainer
|
||||
else
|
||||
Color(cardInfo.color.toColorInt())
|
||||
|
||||
Box(modifier = Modifier.fillMaxWidth()) {
|
||||
Surface(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(64.dp)
|
||||
.pointerInput(Unit) {
|
||||
detectTapGestures(
|
||||
onTap = {
|
||||
@@ -93,32 +212,28 @@ fun CardButton(cardInfo: CardInfo, onDeleteCard: (Int) -> Unit) {
|
||||
}
|
||||
)
|
||||
},
|
||||
shape = ButtonDefaults.shape,
|
||||
color = MaterialTheme.colorScheme.primaryContainer,
|
||||
tonalElevation = 2.dp
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
color = MaterialTheme.colorScheme.primaryContainer.copy(0.625f),
|
||||
tonalElevation = 2.dp,
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth().height(IntrinsicSize.Min),
|
||||
horizontalArrangement = Arrangement.SpaceBetween
|
||||
) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxHeight()
|
||||
.width(32.dp)
|
||||
.background(
|
||||
if (cardInfo.color == null) MaterialTheme.colorScheme.secondaryContainer.copy(alpha = 0.625f)
|
||||
else Color(cardInfo.color.toColorInt()).copy(alpha = 0.625f)
|
||||
)
|
||||
.fillMaxSize()
|
||||
.background(Color.Transparent)
|
||||
.padding(horizontal = 8.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
CardIcon(
|
||||
if (cardInfo.icon == null) null else base64ToImageBitmap(cardInfo.icon),
|
||||
cardColor = cardColor
|
||||
)
|
||||
|
||||
Text(
|
||||
cardInfo.name,
|
||||
fontSize = 16.sp,
|
||||
color = MaterialTheme.colorScheme.onPrimaryContainer,
|
||||
modifier = Modifier
|
||||
.padding(16.dp)
|
||||
.fillMaxWidth()
|
||||
.weight(1f),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
textAlign = TextAlign.Start
|
||||
)
|
||||
}
|
||||
@@ -153,11 +268,84 @@ fun CardButton(cardInfo: CardInfo, onDeleteCard: (Int) -> Unit) {
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun FullWidthButton(onClick: () -> Unit, content: @Composable () -> Unit) {
|
||||
Button(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
onClick = onClick
|
||||
fun CardIcon(icon: ImageBitmap?, cardColor: Color, isLoading: Boolean = false) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.background(
|
||||
cardColor.copy(0.525f),
|
||||
RoundedCornerShape(12.dp)
|
||||
)
|
||||
.padding(8.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.Center
|
||||
) {
|
||||
content()
|
||||
if (isLoading) {
|
||||
CircularProgressIndicator(
|
||||
modifier = Modifier.size(32.dp),
|
||||
color = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
} else if (icon == null) {
|
||||
Icon(
|
||||
modifier = Modifier.size(32.dp),
|
||||
imageVector = Icons.Outlined.QrCode,
|
||||
tint = MaterialTheme.colorScheme.onPrimaryContainer,
|
||||
contentDescription = null
|
||||
)
|
||||
} else {
|
||||
Image(
|
||||
bitmap = icon,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(36.dp).clip(RoundedCornerShape(12.dp))
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun AdaptiveButton(
|
||||
onClick: () -> Unit,
|
||||
modifier: Modifier = Modifier,
|
||||
enabled: Boolean = true,
|
||||
icon: ImageVector? = null,
|
||||
elevation: ButtonElevation? = ButtonDefaults.buttonElevation(),
|
||||
border: BorderStroke? = null,
|
||||
contentPadding: PaddingValues = ButtonDefaults.ContentPadding,
|
||||
interactionSource: MutableInteractionSource? = null,
|
||||
isSecondary: Boolean = false,
|
||||
content: @Composable RowScope.() -> Unit
|
||||
) {
|
||||
val colors = if (isSecondary)
|
||||
ButtonDefaults.buttonColors(
|
||||
containerColor = MaterialTheme.colorScheme.secondaryContainer,
|
||||
contentColor = MaterialTheme.colorScheme.onSecondaryContainer
|
||||
)
|
||||
else
|
||||
ButtonDefaults.buttonColors(
|
||||
containerColor = MaterialTheme.colorScheme.primaryContainer,
|
||||
contentColor = MaterialTheme.colorScheme.onPrimaryContainer
|
||||
)
|
||||
|
||||
Button(
|
||||
onClick = onClick,
|
||||
modifier = modifier,
|
||||
enabled = enabled,
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
colors = colors,
|
||||
elevation = elevation,
|
||||
border = border,
|
||||
contentPadding = contentPadding,
|
||||
interactionSource = interactionSource,
|
||||
content = {
|
||||
if (icon != null) {
|
||||
Icon(
|
||||
imageVector = icon,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(18.dp)
|
||||
)
|
||||
Spacer(modifier = Modifier.width(8.dp))
|
||||
}
|
||||
|
||||
content()
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
package ru.omni_devel.cards
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.widget.Toast
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.LocalActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import ru.omni_devel.cards.ui.theme.OmniCardsTheme
|
||||
|
||||
class FillTemplateActivity : ComponentActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
enableEdgeToEdge()
|
||||
|
||||
val templateJson = intent.getStringExtra("templateJson") ?: ""
|
||||
val template = Template.parseTemplateFromJson(templateJson)
|
||||
|
||||
setContent {
|
||||
OmniCardsTheme {
|
||||
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
|
||||
FillTemplatePage(template, innerPadding)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun FillTemplatePage(template: Template, innerPadding: PaddingValues) {
|
||||
val activityContext = LocalActivity.current
|
||||
val context = LocalContext.current
|
||||
|
||||
val language = getLanguage(context)
|
||||
|
||||
var filledFields by rememberSaveable { mutableStateOf(template.fields.associate { field ->
|
||||
field.id to (field.default ?: "")
|
||||
}) }
|
||||
|
||||
Page(
|
||||
selectItemNameViaLanguage(template.names, language),
|
||||
innerPadding
|
||||
) {
|
||||
for (field in template.fields) {
|
||||
OutlinedTextField(
|
||||
value = filledFields[field.id]!!,
|
||||
onValueChange = { newValue ->
|
||||
filledFields = filledFields.toMutableMap().apply {
|
||||
this[field.id] = newValue
|
||||
}
|
||||
},
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
label = { Text((if (field.maybeEmpty) "" else "*") + selectItemNameViaLanguage(field.names, language)) }
|
||||
)
|
||||
}
|
||||
|
||||
AdaptiveButton(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
onClick = {
|
||||
var isSuccess = true
|
||||
|
||||
for (field in template.fields) {
|
||||
if (filledFields[field.id]!!.isEmpty() && !field.maybeEmpty) {
|
||||
isSuccess = false
|
||||
}
|
||||
}
|
||||
|
||||
if (!isSuccess) {
|
||||
Toast.makeText(context, context.getString(R.string.error_in_data), Toast.LENGTH_LONG).show()
|
||||
|
||||
return@AdaptiveButton
|
||||
}
|
||||
|
||||
val result = try {
|
||||
processQrScript(template.template, filledFields)
|
||||
} catch (e: Exception) {
|
||||
Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show()
|
||||
|
||||
return@AdaptiveButton
|
||||
}
|
||||
|
||||
val resultIntent = Intent()
|
||||
|
||||
resultIntent.putExtra("codeValue", result)
|
||||
resultIntent.putExtra("codeType", template.codeType.name)
|
||||
|
||||
activityContext!!.setResult(Activity.RESULT_OK, resultIntent)
|
||||
activityContext.finish()
|
||||
}
|
||||
) {
|
||||
Text(stringResource(R.string.do_save))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,36 +1,43 @@
|
||||
package ru.omni_devel.cards
|
||||
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.widget.Toast
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.animation.AnimatedVisibility
|
||||
import androidx.compose.animation.expandVertically
|
||||
import androidx.compose.animation.fadeIn
|
||||
import androidx.compose.animation.fadeOut
|
||||
import androidx.compose.animation.scaleIn
|
||||
import androidx.compose.animation.scaleOut
|
||||
import androidx.compose.animation.shrinkVertically
|
||||
import androidx.compose.foundation.clickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.Add
|
||||
import androidx.compose.material.icons.outlined.Sync
|
||||
import androidx.compose.material.icons.outlined.Backup
|
||||
import androidx.compose.material.icons.outlined.Code
|
||||
import androidx.compose.material.icons.outlined.Search
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateListOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.platform.LocalUriHandler
|
||||
import androidx.compose.ui.res.stringResource
|
||||
@@ -56,6 +63,7 @@ class MainActivity : ComponentActivity() {
|
||||
MainPage(innerPadding, cards, onDeleteCard = { cardId ->
|
||||
db.removeCard(cardId)
|
||||
cards.removeIf { it.id == cardId }
|
||||
syncCardsWithWatch(this, db.getCards())
|
||||
}, getDbFun = {
|
||||
return@MainPage db
|
||||
})
|
||||
@@ -69,6 +77,8 @@ class MainActivity : ComponentActivity() {
|
||||
|
||||
cards.clear()
|
||||
cards.addAll(db.getCards())
|
||||
|
||||
syncCardsWithWatch(this, db.getCards())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,110 +92,111 @@ fun MainPage(
|
||||
val context = LocalContext.current
|
||||
val uriHandler = LocalUriHandler.current
|
||||
|
||||
var isActionBarShowed by remember { mutableStateOf(false) }
|
||||
var searchQuery by rememberSaveable { mutableStateOf("") }
|
||||
var isSearchFieldShowed by rememberSaveable { mutableStateOf(false) }
|
||||
|
||||
Box(
|
||||
modifier = Modifier.fillMaxSize()
|
||||
) {
|
||||
Page(
|
||||
stringResource(R.string.app_name),
|
||||
innerPadding
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier.verticalScroll(rememberScrollState()).padding(bottom = 64.dp).background(Color.Transparent).padding(top = 16.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
if (cards.isEmpty()) {
|
||||
Text(
|
||||
stringResource(R.string.add_card_to_get_started)
|
||||
innerPadding,
|
||||
topBarAdditionalElements = {
|
||||
Icon(
|
||||
Icons.Outlined.Search,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.clickable {
|
||||
isSearchFieldShowed = !isSearchFieldShowed
|
||||
searchQuery = ""
|
||||
}
|
||||
)
|
||||
} else {
|
||||
for (card in cards) {
|
||||
CardButton(card, onDeleteCard)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isActionBarShowed) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.align(Alignment.BottomEnd)
|
||||
.padding(innerPadding)
|
||||
.padding(16.dp)
|
||||
.background(
|
||||
MaterialTheme.colorScheme.inversePrimary,
|
||||
RoundedCornerShape(24.dp)
|
||||
)
|
||||
.padding(16.dp)
|
||||
.fillMaxWidth()
|
||||
) {
|
||||
FullWidthButton(
|
||||
onClick = {
|
||||
isActionBarShowed = false
|
||||
|
||||
val intent = Intent(context, EditCardActivity::class.java)
|
||||
|
||||
context.startActivity(intent)
|
||||
}
|
||||
) {
|
||||
Text(stringResource(R.string.do_add_card))
|
||||
}
|
||||
FullWidthButton(
|
||||
onClick = {
|
||||
},
|
||||
floatingActionButton = {
|
||||
ExpandableFab(
|
||||
items = listOf(
|
||||
NavigationPageData(stringResource(R.string.do_add_card), Icons.Outlined.Add, EditCardActivity::class.java),
|
||||
NavigationPageData(stringResource(R.string.do_sync), Icons.Outlined.Sync) {
|
||||
Toast.makeText(context, context.getString(R.string.sync_in_progress), Toast.LENGTH_SHORT).show()
|
||||
|
||||
sendToWatch(context, "/updateCards", Json.encodeToString(getDbFun().getCards())) { isSuccess ->
|
||||
val isSuccess = syncCardsWithWatch(context, getDbFun().getCards())
|
||||
|
||||
if (isSuccess) {
|
||||
Toast.makeText(context, context.getString(R.string.sync_is_successful), Toast.LENGTH_SHORT).show()
|
||||
} else {
|
||||
Toast.makeText(context, context.getString(R.string.sync_is_fail), Toast.LENGTH_LONG).show()
|
||||
}
|
||||
}
|
||||
|
||||
isActionBarShowed = false
|
||||
}
|
||||
) {
|
||||
Text(stringResource(R.string.do_sync))
|
||||
}
|
||||
FullWidthButton(
|
||||
onClick = {
|
||||
val intent = Intent(context, BackupActivity::class.java)
|
||||
|
||||
context.startActivity(intent)
|
||||
|
||||
isActionBarShowed = false
|
||||
}
|
||||
) {
|
||||
Text(stringResource(R.string.open_backup_menu))
|
||||
}
|
||||
FullWidthButton(
|
||||
onClick = {
|
||||
},
|
||||
NavigationPageData(stringResource(R.string.open_backup_menu), Icons.Outlined.Backup, BackupActivity::class.java),
|
||||
NavigationPageData(stringResource(R.string.source_code), Icons.Outlined.Code) {
|
||||
uriHandler.openUri("https://github.com/omni-devel/OmniCards")
|
||||
},
|
||||
)
|
||||
)
|
||||
}
|
||||
) {
|
||||
var queryCleaned = searchQuery.lowercase()
|
||||
|
||||
isActionBarShowed = false
|
||||
for ((from, to) in SEARCH_REPLACE_SYMBOLS) {
|
||||
queryCleaned = queryCleaned.replace(from, to)
|
||||
}
|
||||
|
||||
val searchResults = cards.filter { cardInfo ->
|
||||
if (queryCleaned.isEmpty()) {
|
||||
return@filter true
|
||||
}
|
||||
|
||||
var cardNameCleaned = cardInfo.name.lowercase()
|
||||
|
||||
for ((from, to) in SEARCH_REPLACE_SYMBOLS) {
|
||||
cardNameCleaned = cardNameCleaned.replace(from, to)
|
||||
}
|
||||
|
||||
if (cardNameCleaned.contains(queryCleaned)) {
|
||||
return@filter true
|
||||
}
|
||||
|
||||
return@filter false
|
||||
}
|
||||
|
||||
LazyColumn(
|
||||
verticalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
Text(stringResource(R.string.source_code))
|
||||
}
|
||||
FullWidthButton(
|
||||
onClick = {
|
||||
isActionBarShowed = false
|
||||
}
|
||||
item {
|
||||
AnimatedVisibility(
|
||||
visible = isSearchFieldShowed,
|
||||
enter = fadeIn() + expandVertically(),
|
||||
exit = fadeOut() + shrinkVertically()
|
||||
) {
|
||||
Text(">")
|
||||
OutlinedTextField(
|
||||
value = searchQuery,
|
||||
onValueChange = {
|
||||
searchQuery = it
|
||||
},
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
label = { Text(stringResource(R.string.search)) }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (cards.isEmpty()) {
|
||||
item {
|
||||
Text(
|
||||
stringResource(R.string.add_card_to_get_started)
|
||||
)
|
||||
}
|
||||
} else if (searchResults.isEmpty()) {
|
||||
item {
|
||||
Text(
|
||||
stringResource(R.string.cards_not_found)
|
||||
)
|
||||
}
|
||||
} else {
|
||||
Button(
|
||||
modifier = Modifier
|
||||
.align(Alignment.BottomEnd)
|
||||
.padding(innerPadding).padding(32.dp),
|
||||
onClick = {
|
||||
isActionBarShowed = true
|
||||
for (card in searchResults) {
|
||||
item {
|
||||
CardButton(card, onDeleteCard)
|
||||
}
|
||||
}
|
||||
}
|
||||
) {
|
||||
Text("<")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package ru.omni_devel.cards
|
||||
|
||||
import android.app.Activity
|
||||
import androidx.compose.ui.graphics.vector.ImageVector
|
||||
|
||||
data class NavigationPageData(
|
||||
val name: String,
|
||||
val icon: ImageVector,
|
||||
val pageClass: Class<out Activity>? = null,
|
||||
val onClick: (() -> Unit)? = null
|
||||
)
|
||||
@@ -0,0 +1,237 @@
|
||||
package ru.omni_devel.cards
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.graphics.BitmapFactory
|
||||
import android.os.Bundle
|
||||
import android.widget.Toast
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.LocalActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.activity.result.ActivityResult
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.QrCode
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateMapOf
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.ImageBitmap
|
||||
import androidx.compose.ui.graphics.asImageBitmap
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.call.body
|
||||
import io.ktor.client.engine.cio.CIO
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.statement.HttpResponse
|
||||
import io.ktor.client.statement.bodyAsText
|
||||
import kotlinx.coroutines.launch
|
||||
import ru.omni_devel.cards.ui.theme.OmniCardsTheme
|
||||
|
||||
class SelectIconActivity : ComponentActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
enableEdgeToEdge()
|
||||
setContent {
|
||||
OmniCardsTheme {
|
||||
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
|
||||
SelectIconPage(innerPadding)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun SelectIconPage(innerPadding: PaddingValues) {
|
||||
val context = LocalContext.current
|
||||
val activityContext = LocalActivity.current
|
||||
|
||||
val language = getLanguage(context)
|
||||
|
||||
var searchQuery by rememberSaveable { mutableStateOf("") }
|
||||
|
||||
var iconsData by remember { mutableStateOf<List<CardIcon>?>(null) }
|
||||
val loadedIcons = remember { mutableStateMapOf<String, ImageBitmap?>() }
|
||||
var loadingError by remember { mutableStateOf<String?>(null) }
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
try {
|
||||
val client = HttpClient(CIO)
|
||||
|
||||
val response: HttpResponse = client.get("$REPO_BASE_URL/$ICONS_REPO_FILE")
|
||||
|
||||
val loadedIconsData = CardIcon.parseIconsDataFromJson(response.bodyAsText())
|
||||
iconsData = loadedIconsData
|
||||
|
||||
for (iconData in loadedIconsData) {
|
||||
if (iconData.iconId != null) {
|
||||
launch {
|
||||
val response = client.get("$REPO_BASE_URL/icons/${iconData.iconId}.png")
|
||||
val bytes: ByteArray = response.body()
|
||||
|
||||
BitmapFactory.decodeByteArray(bytes, 0, bytes.size)?.let { bitmap ->
|
||||
loadedIcons[iconData.iconId] = bitmap.asImageBitmap()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
loadingError = e.toString()
|
||||
}
|
||||
}
|
||||
|
||||
var queryCleaned = searchQuery.lowercase()
|
||||
|
||||
for ((from, to) in SEARCH_REPLACE_SYMBOLS) {
|
||||
queryCleaned = queryCleaned.replace(from, to)
|
||||
}
|
||||
|
||||
val searchResults = iconsData?.filter { iconData ->
|
||||
if (queryCleaned.isEmpty()) {
|
||||
return@filter true
|
||||
}
|
||||
|
||||
for (name in iconData.names.values) {
|
||||
var nameCleaned = name.lowercase()
|
||||
|
||||
for ((from, to) in SEARCH_REPLACE_SYMBOLS) {
|
||||
nameCleaned = nameCleaned.replace(from, to)
|
||||
}
|
||||
|
||||
if (nameCleaned.contains(queryCleaned)) {
|
||||
return@filter true
|
||||
}
|
||||
}
|
||||
|
||||
return@filter false
|
||||
} ?: emptyList()
|
||||
|
||||
Page(context.getString(R.string.choosing_icon), innerPadding) {
|
||||
OutlinedTextField(
|
||||
value = searchQuery,
|
||||
onValueChange = { newValue ->
|
||||
searchQuery = newValue
|
||||
},
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
label = { Text(stringResource(R.string.search)) }
|
||||
)
|
||||
|
||||
LazyColumn(
|
||||
verticalArrangement = Arrangement.spacedBy(8.dp),
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
if (loadingError != null) {
|
||||
item {
|
||||
Text(loadingError!!)
|
||||
}
|
||||
} else if (iconsData == null) {
|
||||
item {
|
||||
Column(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
CircularProgressIndicator(
|
||||
modifier = Modifier.size(48.dp),
|
||||
color = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (searchResults.isEmpty()) {
|
||||
item {
|
||||
Text(stringResource(R.string.icons_not_found))
|
||||
}
|
||||
} else {
|
||||
for (iconData in searchResults) {
|
||||
val loadedIcon = loadedIcons[iconData.iconId]
|
||||
|
||||
item {
|
||||
AdaptiveButton(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(64.dp),
|
||||
contentPadding = PaddingValues(0.dp),
|
||||
onClick = {
|
||||
if (loadedIcon == null && iconData.iconId != null) {
|
||||
Toast.makeText(
|
||||
context,
|
||||
context.getString(R.string.wait_for_icon_load),
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
} else {
|
||||
val intent = Intent()
|
||||
|
||||
if (loadedIcon != null) {
|
||||
intent.putExtra(
|
||||
"icon",
|
||||
imageBitmapToBase64(loadedIcon)
|
||||
)
|
||||
}
|
||||
|
||||
activityContext!!.setResult(
|
||||
Activity.RESULT_OK,
|
||||
intent
|
||||
)
|
||||
activityContext.finish()
|
||||
}
|
||||
}
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.background(Color.Transparent)
|
||||
.padding(horizontal = 8.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
CardIcon(
|
||||
loadedIcon,
|
||||
MaterialTheme.colorScheme.primary,
|
||||
loadedIcon == null && iconData.iconId != null
|
||||
)
|
||||
|
||||
Text(
|
||||
selectItemNameViaLanguage(
|
||||
iconData.names,
|
||||
language
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
package ru.omni_devel.cards
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.LocalActivity
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.engine.cio.CIO
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.statement.HttpResponse
|
||||
import io.ktor.client.statement.bodyAsText
|
||||
import kotlinx.serialization.json.Json
|
||||
import ru.omni_devel.cards.ui.theme.OmniCardsTheme
|
||||
|
||||
class SelectTemplateActivity : ComponentActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
enableEdgeToEdge()
|
||||
|
||||
setContent {
|
||||
OmniCardsTheme {
|
||||
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
|
||||
SelectTemplatePage(innerPadding)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun SelectTemplatePage(innerPadding: PaddingValues) {
|
||||
val activityContext = LocalActivity.current
|
||||
val context = LocalContext.current
|
||||
|
||||
val language = getLanguage(context)
|
||||
|
||||
var searchQuery by rememberSaveable { mutableStateOf("") }
|
||||
|
||||
var templates by remember { mutableStateOf<List<Template>?>(null) }
|
||||
var loadingError by remember { mutableStateOf<String?>(null) }
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
try {
|
||||
val client = HttpClient(CIO)
|
||||
|
||||
val response: HttpResponse = client.get("$REPO_BASE_URL/$TEMPLATES_REPO_FILE")
|
||||
|
||||
templates = Template.parseTemplatesFromJson(response.bodyAsText())
|
||||
} catch (e: Exception) {
|
||||
loadingError = e.toString()
|
||||
}
|
||||
}
|
||||
|
||||
val fillTemplateLauncher = rememberLauncherForActivityResult(
|
||||
contract = ActivityResultContracts.StartActivityForResult()
|
||||
) { result ->
|
||||
if (result.resultCode == Activity.RESULT_OK) {
|
||||
val codeValue = result.data?.getStringExtra("codeValue")
|
||||
val codeTypeString = result.data?.getStringExtra("codeType")
|
||||
|
||||
val resultIntent = Intent()
|
||||
|
||||
resultIntent.putExtra("codeValue", codeValue)
|
||||
resultIntent.putExtra("codeType", codeTypeString)
|
||||
|
||||
activityContext!!.setResult(Activity.RESULT_OK, resultIntent)
|
||||
activityContext.finish()
|
||||
}
|
||||
}
|
||||
|
||||
Page(
|
||||
stringResource(R.string.choosing_template),
|
||||
innerPadding
|
||||
) {
|
||||
OutlinedTextField(
|
||||
value = searchQuery,
|
||||
onValueChange = { newValue ->
|
||||
searchQuery = newValue
|
||||
},
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
label = { Text(stringResource(R.string.search)) }
|
||||
)
|
||||
|
||||
LazyColumn(
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
val currentTemplates = templates
|
||||
|
||||
if (loadingError != null) {
|
||||
item {
|
||||
Text(loadingError!!)
|
||||
}
|
||||
} else if (currentTemplates == null) {
|
||||
item {
|
||||
Column(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
CircularProgressIndicator(
|
||||
modifier = Modifier.size(48.dp),
|
||||
color = MaterialTheme.colorScheme.primary
|
||||
)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
var queryCleaned = searchQuery.lowercase()
|
||||
|
||||
for ((from, to) in SEARCH_REPLACE_SYMBOLS) {
|
||||
queryCleaned = queryCleaned.replace(from, to)
|
||||
}
|
||||
|
||||
val searchResults = currentTemplates.filter { template ->
|
||||
if (queryCleaned.isEmpty()) {
|
||||
return@filter true
|
||||
}
|
||||
|
||||
for (name in template.names.values) {
|
||||
var nameCleaned = name.lowercase()
|
||||
|
||||
for ((from, to) in SEARCH_REPLACE_SYMBOLS) {
|
||||
nameCleaned = nameCleaned.replace(from, to)
|
||||
}
|
||||
|
||||
if (nameCleaned.contains(queryCleaned)) {
|
||||
return@filter true
|
||||
}
|
||||
}
|
||||
|
||||
return@filter false
|
||||
}
|
||||
|
||||
if (searchResults.isEmpty()) {
|
||||
item {
|
||||
Text(stringResource(R.string.templates_not_found))
|
||||
}
|
||||
} else {
|
||||
for (template in searchResults) {
|
||||
item {
|
||||
AdaptiveButton(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
onClick = {
|
||||
val intent = Intent(context, FillTemplateActivity::class.java)
|
||||
|
||||
intent.putExtra("templateJson", Json.encodeToString(template))
|
||||
|
||||
fillTemplateLauncher.launch(intent)
|
||||
}
|
||||
) {
|
||||
Text(selectItemNameViaLanguage(template.names, language))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,12 +7,14 @@ import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
@@ -20,6 +22,8 @@ import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.asImageBitmap
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.res.stringResource
|
||||
@@ -95,10 +99,10 @@ fun ShowCardPage(innerPadding: PaddingValues, cardInfo: CardInfo) {
|
||||
contentDescription = cardInfo.codeValue,
|
||||
modifier = Modifier
|
||||
.padding(bottom = 8.dp)
|
||||
.fillMaxWidth()
|
||||
.weight(1f, fill = false),
|
||||
.clip(RoundedCornerShape(12.dp)),
|
||||
contentScale = ContentScale.Fit
|
||||
)
|
||||
|
||||
Text(
|
||||
cardInfo.codeValue,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
package ru.omni_devel.cards
|
||||
|
||||
import com.google.zxing.BarcodeFormat
|
||||
import kotlinx.serialization.Serializable
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
|
||||
@Serializable
|
||||
class Field(
|
||||
val id: String,
|
||||
val names: LinkedHashMap<String, String>,
|
||||
val default: String? = null,
|
||||
val maybeEmpty: Boolean = false
|
||||
)
|
||||
|
||||
@Serializable
|
||||
class Template(
|
||||
val names: LinkedHashMap<String, String>,
|
||||
val fields: List<Field>,
|
||||
val template: String,
|
||||
val codeType: BarcodeFormat = BarcodeFormat.QR_CODE
|
||||
) {
|
||||
companion object {
|
||||
fun parseTemplateFromJson(json: String): Template {
|
||||
val obj = JSONObject(json)
|
||||
|
||||
val fieldsArray = obj.getJSONArray("fields")
|
||||
val fields = mutableListOf<Field>()
|
||||
|
||||
for (fi in 0 until fieldsArray.length()) {
|
||||
val fieldObj = fieldsArray.getJSONObject(fi)
|
||||
|
||||
fields.add(Field(
|
||||
id = fieldObj.getString("id"),
|
||||
names = jsonObjectToMap(fieldObj.getJSONObject("names")),
|
||||
default = fieldObj.optString("default"),
|
||||
maybeEmpty = fieldObj.optBoolean("maybeEmpty")
|
||||
))
|
||||
}
|
||||
|
||||
return Template(
|
||||
names = jsonObjectToMap(obj.getJSONObject("names")),
|
||||
fields = fields,
|
||||
template = obj.getString("template"),
|
||||
codeType = parseCodeType(obj)
|
||||
)
|
||||
}
|
||||
|
||||
fun parseTemplatesFromJson(json: String): List<Template> {
|
||||
val array = JSONArray(json)
|
||||
val templates = mutableListOf<Template>()
|
||||
|
||||
for (i in 0 until array.length()) {
|
||||
val obj = array.getJSONObject(i)
|
||||
|
||||
val fieldsArray = obj.getJSONArray("fields")
|
||||
val fields = mutableListOf<Field>()
|
||||
|
||||
for (fi in 0 until fieldsArray.length()) {
|
||||
val fieldObj = fieldsArray.getJSONObject(fi)
|
||||
|
||||
fields.add(Field(
|
||||
id = fieldObj.getString("id"),
|
||||
names = jsonObjectToMap(fieldObj.getJSONObject("names")),
|
||||
default = fieldObj.optString("default"),
|
||||
maybeEmpty = fieldObj.optBoolean("maybeEmpty")
|
||||
))
|
||||
}
|
||||
|
||||
templates.add(Template(
|
||||
names = jsonObjectToMap(obj.getJSONObject("names")),
|
||||
fields = fields,
|
||||
template = obj.getString("template"),
|
||||
codeType = parseCodeType(obj)
|
||||
))
|
||||
}
|
||||
|
||||
return templates
|
||||
}
|
||||
|
||||
private fun parseCodeType(obj: JSONObject): BarcodeFormat {
|
||||
val raw = obj.optString("codeType", "")
|
||||
return if (raw.isBlank()) {
|
||||
BarcodeFormat.QR_CODE
|
||||
} else {
|
||||
BarcodeFormat.valueOf(raw)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,25 +2,47 @@ package ru.omni_devel.cards
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
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 matrix = writer.encode(value, format, 800, 400)
|
||||
|
||||
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, onResult: (Boolean) -> Unit) {
|
||||
fun sendToWatch(context: Context, path: String, message: String): Boolean {
|
||||
val nodeClient = Wearable.getNodeClient(context)
|
||||
|
||||
var isSuccess = false
|
||||
|
||||
nodeClient.connectedNodes.addOnSuccessListener { nodes ->
|
||||
if (nodes.isEmpty()) {
|
||||
onResult(false)
|
||||
isSuccess = false
|
||||
|
||||
return@addOnSuccessListener
|
||||
}
|
||||
@@ -41,13 +63,19 @@ fun sendToWatch(context: Context, path: String, message: String, onResult: (Bool
|
||||
}
|
||||
|
||||
if (completedCount == nodes.size) {
|
||||
onResult(isAnySuccess)
|
||||
isSuccess = isAnySuccess
|
||||
}
|
||||
}.addOnFailureListener(context.mainExecutor) {
|
||||
onResult(false)
|
||||
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? {
|
||||
@@ -61,3 +89,83 @@ fun checkCardData(name: String, value: String): Int? {
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -11,7 +11,8 @@
|
||||
<string name="sync_is_successful">Успешная синхронизация</string>
|
||||
<string name="sync_is_fail">Ошибка синхронизации. Проверьте подключение к часам</string>
|
||||
|
||||
<string name="open_backup_menu">Бэкап/восстановление</string>
|
||||
<string name="backup">Резервное копирование</string>
|
||||
<string name="open_backup_menu">Резервное копирование</string>
|
||||
<string name="do_backup">Сохранить данные</string>
|
||||
<string name="do_restore">Восстановить данные</string>
|
||||
<string name="ask_do_restore">Восстановить данные?</string>
|
||||
@@ -30,9 +31,20 @@
|
||||
<string name="card_name">Название карты</string>
|
||||
<string name="card_value">Значение карты</string>
|
||||
<string name="do_scan_card">Сканировать карту</string>
|
||||
<string name="card_from_template">Из шаблона</string>
|
||||
<string name="scan_your_card">Отсканируйте вашу карту</string>
|
||||
<string name="card_code_type">Тип кода карты</string>
|
||||
<string name="do_choose_color">Выбрать цвет</string>
|
||||
<string name="choosing_color">Выбор цвета</string>
|
||||
<string name="choosing_template">Выбор шаблона</string>
|
||||
<string name="error_in_data">Ошибка в данных. Пожалуйста, заполните все необходимые (*) поля</string>
|
||||
<string name="templates_not_found">Шаблоны не найдены</string>
|
||||
<string name="search">Поиск</string>
|
||||
<string name="cards_not_found">Карты не найдены</string>
|
||||
<string name="choosing_icon">Выбор иконки</string>
|
||||
<string name="icons_not_found">Иконки не найдены</string>
|
||||
<string name="do_select_icon">Выбрать иконку</string>
|
||||
<string name="wait_for_icon_load">Дождитесь загрузки иконки</string>
|
||||
<string name="card_name_should_not_be_empty">Имя карты не должно быть пустым</string>
|
||||
<string name="card_value_should_not_be_empty">Значение карты не должно быть пустым</string>
|
||||
<string name="name_must_be_shorter_than_32_symbols">Имя карты должно быть короче 32 символов</string>
|
||||
|
||||
@@ -16,7 +16,8 @@
|
||||
<string name="sync_is_successful">Sync successful</string>
|
||||
<string name="sync_is_fail">Sync failed. Check your watch connection</string>
|
||||
|
||||
<string name="open_backup_menu">Backup / Restore</string>
|
||||
<string name="backup">Backup</string>
|
||||
<string name="open_backup_menu">Backup</string>
|
||||
<string name="do_backup">Save data</string>
|
||||
<string name="do_restore">Restore data</string>
|
||||
<string name="ask_do_restore">Restore data?</string>
|
||||
@@ -35,12 +36,24 @@
|
||||
<string name="card_name">Card name</string>
|
||||
<string name="card_value">Card value</string>
|
||||
<string name="do_scan_card">Scan card</string>
|
||||
<string name="card_from_template">From template</string>
|
||||
<string name="scan_your_card">Scan your card</string>
|
||||
<string name="card_code_type">Card code type</string>
|
||||
<string name="do_choose_color">Choose color</string>
|
||||
<string name="choosing_color">Choosing color</string>
|
||||
<string name="choosing_template">Choosing template</string>
|
||||
<string name="error_in_data">Error in data. Please, fill all required (*) fields</string>
|
||||
<string name="templates_not_found">Templates not found</string>
|
||||
<string name="search">Search</string>
|
||||
<string name="cards_not_found">Cards not found</string>
|
||||
<string name="choosing_icon">Choosing icon</string>
|
||||
<string name="icons_not_found">Icons not found</string>
|
||||
<string name="do_select_icon">Select icon</string>
|
||||
<string name="wait_for_icon_load">Wait for icon load</string>
|
||||
<string name="card_name_should_not_be_empty">Card name must not be empty</string>
|
||||
<string name="card_value_should_not_be_empty">Card value must not be empty</string>
|
||||
<string name="name_must_be_shorter_than_32_symbols">Card name must be shorter than 32 characters</string>
|
||||
|
||||
<string name="failed_to_generate_card_code">Failed to generate card code. Check the card data</string>
|
||||
<string name="title_activity_select_icon">SelectIconActivity</string>
|
||||
</resources>
|
||||
@@ -23,17 +23,20 @@ android {
|
||||
minSdk = 30
|
||||
targetSdk = 36
|
||||
versionCode = generateVersionCode()
|
||||
versionName = "1.3.0"
|
||||
versionName = "3.3.0"
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
isMinifyEnabled = false
|
||||
isMinifyEnabled = true
|
||||
proguardFiles(
|
||||
getDefaultProguardFile("proguard-android-optimize.txt"),
|
||||
"proguard-rules.pro"
|
||||
)
|
||||
}
|
||||
debug {
|
||||
applicationIdSuffix = ".debug"
|
||||
}
|
||||
}
|
||||
compileOptions {
|
||||
sourceCompatibility = JavaVersion.VERSION_11
|
||||
@@ -68,4 +71,6 @@ dependencies {
|
||||
implementation(libs.zxing.android)
|
||||
implementation(libs.material)
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.9.0")
|
||||
implementation("androidx.compose.material:material-icons-extended")
|
||||
implementation("androidx.lifecycle:lifecycle-process:2.8.7")
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
@@ -9,5 +9,6 @@ class CardInfo (
|
||||
val name: String,
|
||||
val codeType: BarcodeFormat,
|
||||
val codeValue: String,
|
||||
val color: String?
|
||||
val color: String?,
|
||||
val icon: String?
|
||||
)
|
||||
@@ -4,11 +4,14 @@ import android.content.ContentValues
|
||||
import android.content.Context
|
||||
import android.database.sqlite.SQLiteDatabase
|
||||
import android.database.sqlite.SQLiteOpenHelper
|
||||
import androidx.core.database.getStringOrNull
|
||||
import com.google.zxing.BarcodeFormat
|
||||
|
||||
class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?) : SQLiteOpenHelper(context, "omni_cards", factory, 2) {
|
||||
class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?) : SQLiteOpenHelper(context, "omni_cards", factory, 5) {
|
||||
override fun onCreate(db: SQLiteDatabase?) {
|
||||
db!!.execSQL("CREATE TABLE IF NOT EXISTS cards (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, codeType TEXT, codeValue TEXT, color TEXT)")
|
||||
db!!.execSQL(
|
||||
"CREATE TABLE IF NOT EXISTS cards (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, codeType TEXT, codeValue TEXT, color TEXT, icon TEXT)"
|
||||
)
|
||||
}
|
||||
|
||||
override fun onUpgrade(
|
||||
@@ -19,6 +22,21 @@ class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?)
|
||||
if (oldVersion < 2) {
|
||||
db!!.execSQL("ALTER TABLE cards ADD COLUMN color TEXT")
|
||||
}
|
||||
|
||||
if (oldVersion < 3) {
|
||||
db!!.execSQL("ALTER TABLE cards ADD COLUMN position INTEGER DEFAULT 0")
|
||||
db.execSQL("ALTER TABLE cards ADD COLUMN iconPath TEXT")
|
||||
db.execSQL("UPDATE cards SET position = id")
|
||||
}
|
||||
|
||||
if (oldVersion < 4) {
|
||||
db!!.execSQL("ALTER TABLE cards DROP COLUMN position")
|
||||
db.execSQL("ALTER TABLE cards DROP COLUMN iconPath")
|
||||
}
|
||||
|
||||
if (oldVersion < 5) {
|
||||
db!!.execSQL("ALTER TABLE cards ADD COLUMN icon TEXT")
|
||||
}
|
||||
}
|
||||
|
||||
fun clearDatabase() {
|
||||
@@ -40,6 +58,7 @@ class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?)
|
||||
values.put("codeValue", card.codeValue)
|
||||
values.put("codeType", card.codeType.name)
|
||||
values.put("color", card.color)
|
||||
values.put("icon", card.icon)
|
||||
|
||||
db.insert("cards", null, values)
|
||||
}
|
||||
@@ -59,7 +78,8 @@ class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?)
|
||||
name = cursor.getString(cursor.getColumnIndexOrThrow("name")),
|
||||
codeType = BarcodeFormat.valueOf(cursor.getString(cursor.getColumnIndexOrThrow("codeType"))),
|
||||
codeValue = cursor.getString(cursor.getColumnIndexOrThrow("codeValue")),
|
||||
color = cursor.getString(cursor.getColumnIndexOrThrow("color"))
|
||||
color = cursor.getString(cursor.getColumnIndexOrThrow("color")),
|
||||
icon = cursor.getStringOrNull(cursor.getColumnIndexOrThrow("icon"))
|
||||
))
|
||||
}
|
||||
|
||||
@@ -86,7 +106,8 @@ class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?)
|
||||
name = cursor.getString(cursor.getColumnIndexOrThrow("name")),
|
||||
codeType = BarcodeFormat.valueOf(cursor.getString(cursor.getColumnIndexOrThrow("codeType"))),
|
||||
codeValue = cursor.getString(cursor.getColumnIndexOrThrow("codeValue")),
|
||||
color = cursor.getString(cursor.getColumnIndexOrThrow("color"))
|
||||
color = cursor.getString(cursor.getColumnIndexOrThrow("color")),
|
||||
icon = cursor.getStringOrNull(cursor.getColumnIndexOrThrow("icon"))
|
||||
)
|
||||
|
||||
cursor.close()
|
||||
|
||||
@@ -1,20 +1,26 @@
|
||||
package ru.omni_devel.cards.presentation
|
||||
|
||||
import android.content.Intent
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.IntrinsicSize
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.fillMaxHeight
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.QrCode
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.ImageBitmap
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.dp
|
||||
@@ -22,6 +28,7 @@ import androidx.compose.ui.unit.sp
|
||||
import androidx.core.graphics.toColorInt
|
||||
import androidx.wear.compose.material3.Button
|
||||
import androidx.wear.compose.material3.ButtonDefaults
|
||||
import androidx.wear.compose.material3.Icon
|
||||
import androidx.wear.compose.material3.MaterialTheme
|
||||
import androidx.wear.compose.material3.Text
|
||||
|
||||
@@ -29,12 +36,18 @@ import androidx.wear.compose.material3.Text
|
||||
fun CardButton(cardInfo: CardInfo) {
|
||||
val context = LocalContext.current
|
||||
|
||||
val cardColor = if (cardInfo.color == null)
|
||||
MaterialTheme.colorScheme.primaryContainer
|
||||
else
|
||||
Color(cardInfo.color.toColorInt())
|
||||
|
||||
Box(modifier = Modifier.fillMaxWidth()) {
|
||||
Button(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
modifier = Modifier.fillMaxWidth().height(64.dp),
|
||||
colors = ButtonDefaults.buttonColors(
|
||||
containerColor = MaterialTheme.colorScheme.primaryContainer
|
||||
containerColor = MaterialTheme.colorScheme.primaryContainer.copy(0.625f)
|
||||
),
|
||||
shape = RoundedCornerShape(12.dp),
|
||||
contentPadding = PaddingValues(0.dp),
|
||||
onClick = {
|
||||
val intent = Intent(context, ShowCardActivity::class.java)
|
||||
@@ -46,32 +59,54 @@ fun CardButton(cardInfo: CardInfo) {
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(IntrinsicSize.Min)
|
||||
.clip(ButtonDefaults.shape)
|
||||
.fillMaxSize()
|
||||
.background(Color.Transparent)
|
||||
.padding(horizontal = 8.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxHeight()
|
||||
.width(32.dp)
|
||||
.background(
|
||||
if (cardInfo.color == null)
|
||||
MaterialTheme.colorScheme.secondaryContainer.copy(alpha = 0.625f)
|
||||
else
|
||||
Color(cardInfo.color.toColorInt()).copy(alpha = 0.625f)
|
||||
)
|
||||
CardIcon(
|
||||
if (cardInfo.icon == null) null else base64ToImageBitmap(cardInfo.icon),
|
||||
cardColor = cardColor
|
||||
)
|
||||
|
||||
Text(
|
||||
cardInfo.name,
|
||||
fontSize = 16.sp,
|
||||
color = MaterialTheme.colorScheme.onPrimaryContainer,
|
||||
modifier = Modifier
|
||||
.padding(16.dp)
|
||||
.fillMaxWidth()
|
||||
.weight(1f),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
textAlign = TextAlign.Start
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun CardIcon(icon: ImageBitmap?, cardColor: Color) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.background(
|
||||
cardColor.copy(0.525f),
|
||||
RoundedCornerShape(12.dp)
|
||||
)
|
||||
.padding(8.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.Center
|
||||
) {
|
||||
if (icon == null) {
|
||||
Icon(
|
||||
modifier = Modifier.size(32.dp),
|
||||
imageVector = Icons.Outlined.QrCode,
|
||||
tint = MaterialTheme.colorScheme.onPrimaryContainer,
|
||||
contentDescription = null
|
||||
)
|
||||
} else {
|
||||
Image(
|
||||
bitmap = icon,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(36.dp).clip(RoundedCornerShape(12.dp))
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,38 +4,50 @@ import android.annotation.SuppressLint
|
||||
import android.os.Bundle
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.BoxWithConstraints
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.wear.compose.foundation.lazy.TransformingLazyColumn
|
||||
import androidx.wear.compose.material3.MaterialTheme
|
||||
import androidx.wear.compose.material3.Text
|
||||
import ru.omni_devel.cards.R
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
private lateinit var db: DbHelper
|
||||
private val cardsState = mutableStateOf<List<CardInfo>>(emptyList())
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
db = DbHelper(this, null)
|
||||
|
||||
cardsState.value = db.getCards()
|
||||
|
||||
setContent {
|
||||
App(db.getCards())
|
||||
App(cardsState.value)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
|
||||
cardsState.value = db.getCards()
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("UnusedBoxWithConstraintsScope")
|
||||
@Composable
|
||||
fun App(cards: List<CardInfo>) {
|
||||
BoxWithConstraints(modifier = Modifier.fillMaxSize()) {
|
||||
BoxWithConstraints(modifier = Modifier.fillMaxSize().background(MaterialTheme.colorScheme.background)) {
|
||||
val topBottomPadding = maxHeight * 0.4f
|
||||
|
||||
if (cards.isEmpty()) {
|
||||
|
||||
@@ -8,6 +8,8 @@ import com.google.android.gms.wearable.MessageEvent
|
||||
import com.google.android.gms.wearable.WearableListenerService
|
||||
import kotlinx.serialization.json.Json
|
||||
import ru.omni_devel.cards.R
|
||||
import androidx.lifecycle.ProcessLifecycleOwner
|
||||
import androidx.lifecycle.Lifecycle
|
||||
|
||||
class MessagesReceiver : WearableListenerService() {
|
||||
private lateinit var db: DbHelper
|
||||
@@ -28,8 +30,17 @@ class MessagesReceiver : WearableListenerService() {
|
||||
db.clearDatabase()
|
||||
db.addCards(cards)
|
||||
|
||||
val isAppInForeground = ProcessLifecycleOwner.get().lifecycle.currentState.isAtLeast(
|
||||
Lifecycle.State.STARTED
|
||||
)
|
||||
|
||||
if (isAppInForeground) {
|
||||
Handler(Looper.getMainLooper()).post {
|
||||
Toast.makeText(this, this.getString(R.string.sync_is_successful), Toast.LENGTH_SHORT).show()
|
||||
Toast.makeText(
|
||||
this,
|
||||
this.getString(R.string.sync_is_successful),
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
|
||||
val intent = Intent(this, MainActivity::class.java).apply {
|
||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
|
||||
@@ -39,4 +50,5 @@ class MessagesReceiver : WearableListenerService() {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,10 @@
|
||||
package ru.omni_devel.cards.presentation
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.util.Base64
|
||||
import androidx.compose.ui.graphics.ImageBitmap
|
||||
import androidx.compose.ui.graphics.asImageBitmap
|
||||
import com.google.zxing.BarcodeFormat
|
||||
import com.google.zxing.MultiFormatWriter
|
||||
import com.journeyapps.barcodescanner.BarcodeEncoder
|
||||
@@ -11,3 +15,10 @@ fun generateCode(value: String, format: BarcodeFormat, width: Int, height: Int):
|
||||
|
||||
return BarcodeEncoder().createBitmap(matrix)
|
||||
}
|
||||
|
||||
fun base64ToImageBitmap(base64String: String): ImageBitmap {
|
||||
val decodedBytes = Base64.decode(base64String, Base64.DEFAULT)
|
||||
val bitmap = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.size)
|
||||
|
||||
return bitmap.asImageBitmap()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user