Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1c76705551 | ||
|
|
24a1e704ca | ||
|
|
a7b3097a03 | ||
|
|
ff64f431f6 | ||
|
|
50b2a965b4 |
@@ -23,7 +23,7 @@ android {
|
|||||||
minSdk = 30
|
minSdk = 30
|
||||||
targetSdk = 36
|
targetSdk = 36
|
||||||
versionCode = generateVersionCode()
|
versionCode = generateVersionCode()
|
||||||
versionName = "4.3.1"
|
versionName = "2.6.0"
|
||||||
|
|
||||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||||
}
|
}
|
||||||
@@ -33,7 +33,7 @@ android {
|
|||||||
applicationIdSuffix = ".debug"
|
applicationIdSuffix = ".debug"
|
||||||
}
|
}
|
||||||
release {
|
release {
|
||||||
isMinifyEnabled = true
|
isMinifyEnabled = false
|
||||||
proguardFiles(
|
proguardFiles(
|
||||||
getDefaultProguardFile("proguard-android-optimize.txt"),
|
getDefaultProguardFile("proguard-android-optimize.txt"),
|
||||||
"proguard-rules.pro"
|
"proguard-rules.pro"
|
||||||
@@ -47,7 +47,6 @@ android {
|
|||||||
buildFeatures {
|
buildFeatures {
|
||||||
compose = true
|
compose = true
|
||||||
viewBinding = true
|
viewBinding = true
|
||||||
buildConfig = true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,8 +77,4 @@ dependencies {
|
|||||||
implementation("com.google.android.gms:play-services-wearable:18.1.0")
|
implementation("com.google.android.gms:play-services-wearable:18.1.0")
|
||||||
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.9.0")
|
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.9.0")
|
||||||
implementation(libs.compose.colorpicker)
|
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")
|
|
||||||
implementation("sh.calvin.reorderable:reorderable:2.4.3")
|
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,5 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
xmlns:tools="http://schemas.android.com/tools">
|
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.INTERNET" />
|
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
@@ -11,31 +8,6 @@
|
|||||||
android:roundIcon="@mipmap/ic_launcher_round"
|
android:roundIcon="@mipmap/ic_launcher_round"
|
||||||
android:supportsRtl="true"
|
android:supportsRtl="true"
|
||||||
android:theme="@style/Theme.OmniCards">
|
android:theme="@style/Theme.OmniCards">
|
||||||
<activity
|
|
||||||
android:name="com.journeyapps.barcodescanner.CaptureActivity"
|
|
||||||
android:screenOrientation="portrait"
|
|
||||||
android:stateNotNeeded="true"
|
|
||||||
android:theme="@style/zxing_CaptureTheme"
|
|
||||||
android:windowSoftInputMode="stateAlwaysHidden"
|
|
||||||
tools:replace="android:screenOrientation" />
|
|
||||||
|
|
||||||
<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
|
<activity
|
||||||
android:name=".BackupActivity"
|
android:name=".BackupActivity"
|
||||||
android:exported="false"
|
android:exported="false"
|
||||||
|
|||||||
@@ -11,10 +11,6 @@ import androidx.activity.enableEdgeToEdge
|
|||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.compose.foundation.layout.PaddingValues
|
import androidx.compose.foundation.layout.PaddingValues
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
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.AlertDialog
|
||||||
import androidx.compose.material3.Scaffold
|
import androidx.compose.material3.Scaffold
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
@@ -136,18 +132,14 @@ fun BackupPage(innerPadding: PaddingValues, getDbFun: () -> DbHelper) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Page(stringResource(R.string.backup), innerPadding) {
|
Page(stringResource(R.string.backup), innerPadding) {
|
||||||
AdaptiveButton(
|
FullWidthButton(
|
||||||
modifier = Modifier.fillMaxWidth(),
|
|
||||||
icon = Icons.Outlined.Save,
|
|
||||||
onClick = {
|
onClick = {
|
||||||
createBackupLauncher.launch("omnicards-backup.json")
|
createBackupLauncher.launch("omnicards-backup.json")
|
||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
Text(stringResource(R.string.do_backup))
|
Text(stringResource(R.string.do_backup))
|
||||||
}
|
}
|
||||||
AdaptiveButton(
|
FullWidthButton(
|
||||||
modifier = Modifier.fillMaxWidth(),
|
|
||||||
icon = Icons.Outlined.Restore,
|
|
||||||
onClick = {
|
onClick = {
|
||||||
restoreFromBackupLauncher.launch("application/json")
|
restoreFromBackupLauncher.launch("application/json")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,28 +0,0 @@
|
|||||||
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,6 +15,4 @@ class CardInfo (
|
|||||||
val codeType: BarcodeFormat,
|
val codeType: BarcodeFormat,
|
||||||
val codeValue: String,
|
val codeValue: String,
|
||||||
val color: String?,
|
val color: String?,
|
||||||
val icon: String?,
|
|
||||||
val position: Int
|
|
||||||
) {}
|
) {}
|
||||||
|
|||||||
@@ -1,118 +0,0 @@
|
|||||||
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))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
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 "е"
|
|
||||||
)
|
|
||||||
@@ -2,18 +2,13 @@ package ru.omni_devel.cards
|
|||||||
|
|
||||||
import android.content.ContentValues
|
import android.content.ContentValues
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.database.Cursor
|
|
||||||
import android.database.sqlite.SQLiteDatabase
|
import android.database.sqlite.SQLiteDatabase
|
||||||
import android.database.sqlite.SQLiteOpenHelper
|
import android.database.sqlite.SQLiteOpenHelper
|
||||||
import androidx.core.database.getStringOrNull
|
|
||||||
import androidx.core.database.sqlite.transaction
|
|
||||||
import com.google.zxing.BarcodeFormat
|
import com.google.zxing.BarcodeFormat
|
||||||
|
|
||||||
class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?) : SQLiteOpenHelper(context, "omni_cards", factory, 8) {
|
class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?) : SQLiteOpenHelper(context, "omni_cards", factory, 2) {
|
||||||
override fun onCreate(db: SQLiteDatabase?) {
|
override fun onCreate(db: SQLiteDatabase?) {
|
||||||
db!!.execSQL(
|
db!!.execSQL("CREATE TABLE IF NOT EXISTS cards (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, codeType TEXT, codeValue TEXT, color TEXT)")
|
||||||
"CREATE TABLE IF NOT EXISTS cards (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, codeType TEXT, codeValue TEXT, color TEXT, icon TEXT, position DEFAULT 0)"
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onUpgrade(
|
override fun onUpgrade(
|
||||||
@@ -24,79 +19,30 @@ class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?)
|
|||||||
if (oldVersion < 2) {
|
if (oldVersion < 2) {
|
||||||
db!!.execSQL("ALTER TABLE cards ADD COLUMN color TEXT")
|
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 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")
|
|
||||||
}
|
|
||||||
|
|
||||||
if (oldVersion < 7) {
|
|
||||||
db!!.execSQL("ALTER TABLE cards ADD COLUMN position INTEGER DEFAULT 0")
|
|
||||||
db.execSQL("UPDATE cards SET position = id")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun readCard(cursor: Cursor): CardInfo {
|
fun addCard(name: String, codeValue: String, codeType: BarcodeFormat, color: String?) {
|
||||||
return CardInfo(
|
|
||||||
id = cursor.getInt(cursor.getColumnIndexOrThrow("id")),
|
|
||||||
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")),
|
|
||||||
icon = cursor.getStringOrNull(cursor.getColumnIndexOrThrow("icon")),
|
|
||||||
position = cursor.getInt(cursor.getColumnIndexOrThrow("position"))
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun nextPosition(db: SQLiteDatabase): Int {
|
|
||||||
val cursor = db.rawQuery("SELECT COALESCE(MAX(position), -1) + 1 FROM cards", null)
|
|
||||||
cursor.moveToFirst()
|
|
||||||
val next = cursor.getInt(0)
|
|
||||||
cursor.close()
|
|
||||||
|
|
||||||
return next
|
|
||||||
}
|
|
||||||
|
|
||||||
fun addCard(name: String, codeValue: String, codeType: BarcodeFormat, color: String?, icon: String?) {
|
|
||||||
val db = this.writableDatabase
|
|
||||||
|
|
||||||
val values = ContentValues()
|
val values = ContentValues()
|
||||||
|
|
||||||
values.put("name", name)
|
values.put("name", name)
|
||||||
values.put("codeValue", codeValue)
|
values.put("codeValue", codeValue)
|
||||||
values.put("codeType", codeType.name)
|
values.put("codeType", codeType.name)
|
||||||
values.put("color", color)
|
values.put("color", color)
|
||||||
values.put("icon", icon)
|
|
||||||
values.put("position", nextPosition(db))
|
val db = this.writableDatabase
|
||||||
|
|
||||||
db.insert("cards", null, values)
|
db.insert("cards", null, values)
|
||||||
|
|
||||||
db.close()
|
db.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun editCard(id: Int, name: String, codeValue: String, codeType: BarcodeFormat, color: String?, icon: String?) {
|
fun editCard(id: Int, name: String, codeValue: String, codeType: BarcodeFormat, color: String?) {
|
||||||
val values = ContentValues()
|
val values = ContentValues()
|
||||||
|
|
||||||
values.put("name", name)
|
values.put("name", name)
|
||||||
values.put("codeValue", codeValue)
|
values.put("codeValue", codeValue)
|
||||||
values.put("codeType", codeType.name)
|
values.put("codeType", codeType.name)
|
||||||
values.put("color", color)
|
values.put("color", color)
|
||||||
values.put("icon", icon)
|
|
||||||
|
|
||||||
val db = this.writableDatabase
|
val db = this.writableDatabase
|
||||||
|
|
||||||
@@ -108,11 +54,17 @@ class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?)
|
|||||||
fun getCards(): List<CardInfo> {
|
fun getCards(): List<CardInfo> {
|
||||||
val db = this.readableDatabase
|
val db = this.readableDatabase
|
||||||
|
|
||||||
val cursor = db.rawQuery("SELECT * FROM cards ORDER BY position ASC", null)
|
val cursor = db.rawQuery("SELECT * FROM cards", null)
|
||||||
val cards = mutableListOf<CardInfo>()
|
val cards = mutableListOf<CardInfo>()
|
||||||
|
|
||||||
while (cursor.moveToNext()) {
|
while (cursor.moveToNext()) {
|
||||||
cards.add(readCard(cursor))
|
cards.add(CardInfo(
|
||||||
|
id = cursor.getInt(cursor.getColumnIndexOrThrow("id")),
|
||||||
|
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"))
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
cursor.close()
|
cursor.close()
|
||||||
@@ -133,7 +85,13 @@ class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?)
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
val cardInfo = readCard(cursor)
|
val cardInfo = CardInfo(
|
||||||
|
id = cursor.getInt(cursor.getColumnIndexOrThrow("id")),
|
||||||
|
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"))
|
||||||
|
)
|
||||||
|
|
||||||
cursor.close()
|
cursor.close()
|
||||||
db.close()
|
db.close()
|
||||||
@@ -158,38 +116,18 @@ class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?)
|
|||||||
fun addCards(cards: List<CardInfo>) {
|
fun addCards(cards: List<CardInfo>) {
|
||||||
val db = this.writableDatabase
|
val db = this.writableDatabase
|
||||||
|
|
||||||
db.use { db ->
|
for (card in cards) {
|
||||||
db.transaction {
|
val values = ContentValues()
|
||||||
for (card in cards) {
|
|
||||||
val values = ContentValues()
|
|
||||||
|
|
||||||
values.put("id", card.id)
|
values.put("id", card.id)
|
||||||
values.put("name", card.name)
|
values.put("name", card.name)
|
||||||
values.put("codeValue", card.codeValue)
|
values.put("codeValue", card.codeValue)
|
||||||
values.put("codeType", card.codeType.name)
|
values.put("codeType", card.codeType.name)
|
||||||
values.put("color", card.color)
|
values.put("color", card.color)
|
||||||
values.put("icon", card.icon)
|
|
||||||
values.put("position", nextPosition(db))
|
|
||||||
|
|
||||||
db.insert("cards", null, values)
|
db.insert("cards", null, values)
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db.close()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
fun updatePositions(orderedIds: List<Int>) {
|
|
||||||
val db = this.writableDatabase
|
|
||||||
|
|
||||||
db.use { db ->
|
|
||||||
db.transaction {
|
|
||||||
orderedIds.forEachIndexed { index, id ->
|
|
||||||
val values = ContentValues()
|
|
||||||
|
|
||||||
values.put("position", index)
|
|
||||||
|
|
||||||
db.update("cards", values, "id = ?", arrayOf(id.toString()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,5 @@
|
|||||||
package ru.omni_devel.cards
|
package ru.omni_devel.cards
|
||||||
|
|
||||||
import android.app.Activity
|
|
||||||
import android.content.Intent
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.activity.ComponentActivity
|
import androidx.activity.ComponentActivity
|
||||||
@@ -9,8 +7,6 @@ import androidx.activity.compose.LocalActivity
|
|||||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||||
import androidx.activity.compose.setContent
|
import androidx.activity.compose.setContent
|
||||||
import androidx.activity.enableEdgeToEdge
|
import androidx.activity.enableEdgeToEdge
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
|
||||||
import androidx.compose.foundation.background
|
|
||||||
import androidx.compose.foundation.clickable
|
import androidx.compose.foundation.clickable
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Box
|
import androidx.compose.foundation.layout.Box
|
||||||
@@ -23,18 +19,13 @@ import androidx.compose.foundation.layout.height
|
|||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.foundation.rememberScrollState
|
import androidx.compose.foundation.rememberScrollState
|
||||||
import androidx.compose.foundation.verticalScroll
|
import androidx.compose.foundation.verticalScroll
|
||||||
|
import androidx.compose.material3.Button
|
||||||
import androidx.compose.material3.DropdownMenu
|
import androidx.compose.material3.DropdownMenu
|
||||||
import androidx.compose.material3.DropdownMenuItem
|
import androidx.compose.material3.DropdownMenuItem
|
||||||
import androidx.compose.material3.MaterialTheme
|
|
||||||
import androidx.compose.material3.OutlinedTextField
|
import androidx.compose.material3.OutlinedTextField
|
||||||
import androidx.compose.material3.Scaffold
|
import androidx.compose.material3.Scaffold
|
||||||
import androidx.compose.material3.Switch
|
import androidx.compose.material3.Switch
|
||||||
import androidx.compose.material3.Text
|
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.Composable
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
@@ -47,6 +38,9 @@ import androidx.compose.ui.platform.LocalContext
|
|||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.core.graphics.toColorInt
|
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.google.zxing.BarcodeFormat
|
||||||
import com.journeyapps.barcodescanner.ScanContract
|
import com.journeyapps.barcodescanner.ScanContract
|
||||||
import com.journeyapps.barcodescanner.ScanOptions
|
import com.journeyapps.barcodescanner.ScanOptions
|
||||||
@@ -85,23 +79,18 @@ fun CardEditable(
|
|||||||
codeType: BarcodeFormat,
|
codeType: BarcodeFormat,
|
||||||
onCodeTypeChange: (BarcodeFormat) -> Unit,
|
onCodeTypeChange: (BarcodeFormat) -> Unit,
|
||||||
color: String?,
|
color: String?,
|
||||||
onColorChange: (String?) -> Unit,
|
onColorChange: (String?) -> Unit
|
||||||
icon: String?,
|
|
||||||
onIconChange: (String?) -> Unit
|
|
||||||
) {
|
) {
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
|
|
||||||
var isCodeTypeDropdownExpanded by rememberSaveable { mutableStateOf(false) }
|
var isCodeTypeDropdownExpanded by rememberSaveable { mutableStateOf(false) }
|
||||||
|
var isColorPickerExpanded by rememberSaveable { mutableStateOf(false) }
|
||||||
var isColorEnabled by rememberSaveable { mutableStateOf(color != null) }
|
var isColorEnabled by rememberSaveable { mutableStateOf(color != null) }
|
||||||
var currentColor by rememberSaveable { mutableStateOf(color) }
|
|
||||||
|
val colorPickerController = rememberColorPickerController()
|
||||||
|
|
||||||
val fieldModifier = Modifier.fillMaxWidth()
|
val fieldModifier = Modifier.fillMaxWidth()
|
||||||
|
|
||||||
val cardColor = if (color == null)
|
|
||||||
MaterialTheme.colorScheme.primary
|
|
||||||
else
|
|
||||||
Color(color.toColorInt())
|
|
||||||
|
|
||||||
val scanLauncher = rememberLauncherForActivityResult(
|
val scanLauncher = rememberLauncherForActivityResult(
|
||||||
contract = ScanContract()
|
contract = ScanContract()
|
||||||
) { result ->
|
) { result ->
|
||||||
@@ -111,57 +100,41 @@ fun CardEditable(
|
|||||||
result.formatName?.let { formatName ->
|
result.formatName?.let { formatName ->
|
||||||
try {
|
try {
|
||||||
onCodeTypeChange(BarcodeFormat.valueOf(formatName))
|
onCodeTypeChange(BarcodeFormat.valueOf(formatName))
|
||||||
} catch (_: IllegalArgumentException) {}
|
} catch (e: 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(
|
OutlinedTextField(
|
||||||
value = cardName,
|
value = cardName,
|
||||||
onValueChange = onCardNameChange,
|
onValueChange = onCardNameChange,
|
||||||
modifier = fieldModifier,
|
modifier = fieldModifier,
|
||||||
label = { Text(stringResource(R.string.card_name)) }
|
label = { Text(stringResource(R.string.card_name)) }
|
||||||
)
|
)
|
||||||
|
Row(
|
||||||
OutlinedTextField(
|
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
||||||
value = codeValue,
|
) {
|
||||||
onValueChange = onCodeValueChange,
|
OutlinedTextField(
|
||||||
modifier = fieldModifier,
|
value = codeValue,
|
||||||
label = { Text(stringResource(R.string.card_value)) }
|
onValueChange = onCodeValueChange,
|
||||||
)
|
modifier = Modifier.weight(1f),
|
||||||
|
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(
|
Box(
|
||||||
modifier = fieldModifier,
|
modifier = fieldModifier,
|
||||||
@@ -197,53 +170,18 @@ 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(
|
Row(
|
||||||
modifier = fieldModifier,
|
modifier = fieldModifier,
|
||||||
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
horizontalArrangement = Arrangement.SpaceBetween
|
||||||
) {
|
) {
|
||||||
AdaptiveButton(
|
Button(
|
||||||
modifier = Modifier.fillMaxWidth().weight(1.0f),
|
|
||||||
icon = Icons.Outlined.Palette,
|
|
||||||
enabled = isColorEnabled,
|
enabled = isColorEnabled,
|
||||||
onClick = {
|
onClick = {
|
||||||
val intent = Intent(context, ChooseColorActivity::class.java)
|
isColorPickerExpanded = !isColorPickerExpanded
|
||||||
|
|
||||||
intent.putExtra("currentColor", currentColor)
|
|
||||||
|
|
||||||
editColorLauncher.launch(intent)
|
|
||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
Text(stringResource(R.string.do_choose_color))
|
Text(stringResource(R.string.do_choose_color))
|
||||||
}
|
}
|
||||||
|
|
||||||
Switch(
|
Switch(
|
||||||
checked = isColorEnabled,
|
checked = isColorEnabled,
|
||||||
onCheckedChange = {
|
onCheckedChange = {
|
||||||
@@ -256,27 +194,18 @@ fun CardEditable(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
AdaptiveButton(
|
if (isColorPickerExpanded && isColorEnabled) {
|
||||||
modifier = Modifier.fillMaxWidth().height(64.dp),
|
HsvColorPicker(
|
||||||
contentPadding = PaddingValues(horizontal = 0.dp),
|
|
||||||
onClick = {
|
|
||||||
val intent = Intent(context, SelectIconActivity::class.java)
|
|
||||||
|
|
||||||
selectIconLauncher.launch(intent)
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
Row(
|
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxSize()
|
.fillMaxWidth()
|
||||||
.background(Color.Transparent)
|
.height(450.dp)
|
||||||
.padding(horizontal = 8.dp),
|
.padding(10.dp),
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
initialColor = color?.let { Color(it.toColorInt()) },
|
||||||
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
controller = colorPickerController,
|
||||||
) {
|
onColorChanged = { colorEnvelope: ColorEnvelope ->
|
||||||
CardIcon(if (icon == null) null else base64ToImageBitmap(icon), cardColor)
|
onColorChange("#${colorEnvelope.hexCode.substring(2)}")
|
||||||
|
}
|
||||||
Text(stringResource(R.string.do_select_icon))
|
)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -286,7 +215,6 @@ fun AddCard(innerPadding: PaddingValues, getDbFun: () -> DbHelper) {
|
|||||||
var codeValue by rememberSaveable { mutableStateOf("") }
|
var codeValue by rememberSaveable { mutableStateOf("") }
|
||||||
var codeType by rememberSaveable { mutableStateOf(BarcodeFormat.QR_CODE) }
|
var codeType by rememberSaveable { mutableStateOf(BarcodeFormat.QR_CODE) }
|
||||||
var color by rememberSaveable { mutableStateOf<String?>(null) }
|
var color by rememberSaveable { mutableStateOf<String?>(null) }
|
||||||
var icon by rememberSaveable { mutableStateOf<String?>(null) }
|
|
||||||
|
|
||||||
val context = LocalActivity.current
|
val context = LocalActivity.current
|
||||||
|
|
||||||
@@ -306,21 +234,18 @@ fun AddCard(innerPadding: PaddingValues, getDbFun: () -> DbHelper) {
|
|||||||
codeType = codeType,
|
codeType = codeType,
|
||||||
onCodeTypeChange = { codeType = it },
|
onCodeTypeChange = { codeType = it },
|
||||||
color = color,
|
color = color,
|
||||||
onColorChange = { color = it },
|
onColorChange = { color = it }
|
||||||
icon = icon,
|
|
||||||
onIconChange = { icon = it }
|
|
||||||
)
|
)
|
||||||
|
|
||||||
AdaptiveButton(
|
Button(
|
||||||
modifier = Modifier.fillMaxWidth(),
|
modifier = Modifier.fillMaxWidth(),
|
||||||
icon = Icons.Outlined.Save,
|
|
||||||
onClick = {
|
onClick = {
|
||||||
cardName = cardName.trim()
|
cardName = cardName.trim()
|
||||||
|
|
||||||
val err = checkCardData(cardName, codeValue)
|
val err = checkCardData(cardName, codeValue)
|
||||||
|
|
||||||
if (err == null) {
|
if (err == null) {
|
||||||
getDbFun().addCard(cardName, codeValue, codeType, color, icon)
|
getDbFun().addCard(cardName, codeValue, codeType, color)
|
||||||
context!!.finish()
|
context!!.finish()
|
||||||
} else {
|
} else {
|
||||||
Toast.makeText(context, context!!.getString(err), Toast.LENGTH_SHORT).show()
|
Toast.makeText(context, context!!.getString(err), Toast.LENGTH_SHORT).show()
|
||||||
@@ -339,7 +264,6 @@ fun EditCard(cardInfo: CardInfo, innerPadding: PaddingValues, getDbFun: () -> Db
|
|||||||
var codeValue by rememberSaveable { mutableStateOf(cardInfo.codeValue) }
|
var codeValue by rememberSaveable { mutableStateOf(cardInfo.codeValue) }
|
||||||
var codeType by rememberSaveable { mutableStateOf(cardInfo.codeType) }
|
var codeType by rememberSaveable { mutableStateOf(cardInfo.codeType) }
|
||||||
var color by rememberSaveable { mutableStateOf(cardInfo.color) }
|
var color by rememberSaveable { mutableStateOf(cardInfo.color) }
|
||||||
var icon by rememberSaveable { mutableStateOf(cardInfo.icon) }
|
|
||||||
|
|
||||||
val context = LocalActivity.current
|
val context = LocalActivity.current
|
||||||
|
|
||||||
@@ -359,21 +283,18 @@ fun EditCard(cardInfo: CardInfo, innerPadding: PaddingValues, getDbFun: () -> Db
|
|||||||
codeType = codeType,
|
codeType = codeType,
|
||||||
onCodeTypeChange = { codeType = it },
|
onCodeTypeChange = { codeType = it },
|
||||||
color = color,
|
color = color,
|
||||||
onColorChange = { color = it },
|
onColorChange = { color = it }
|
||||||
icon = icon,
|
|
||||||
onIconChange = { icon = it }
|
|
||||||
)
|
)
|
||||||
|
|
||||||
AdaptiveButton(
|
Button(
|
||||||
modifier = Modifier.fillMaxWidth(),
|
modifier = Modifier.fillMaxWidth(),
|
||||||
icon = Icons.Outlined.Save,
|
|
||||||
onClick = {
|
onClick = {
|
||||||
cardName = cardName.trim()
|
cardName = cardName.trim()
|
||||||
|
|
||||||
val err = checkCardData(cardName, codeValue)
|
val err = checkCardData(cardName, codeValue)
|
||||||
|
|
||||||
if (err == null) {
|
if (err == null) {
|
||||||
getDbFun().editCard(cardInfo.id, cardName, codeValue, codeType, color, icon)
|
getDbFun().editCard(cardInfo.id, cardName, codeValue, codeType, color)
|
||||||
context!!.finish()
|
context!!.finish()
|
||||||
} else {
|
} else {
|
||||||
Toast.makeText(context, context!!.getString(err), Toast.LENGTH_SHORT).show()
|
Toast.makeText(context, context!!.getString(err), Toast.LENGTH_SHORT).show()
|
||||||
|
|||||||
@@ -3,47 +3,26 @@ package ru.omni_devel.cards
|
|||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import androidx.activity.compose.BackHandler
|
import androidx.activity.compose.BackHandler
|
||||||
import androidx.activity.compose.LocalActivity
|
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.background
|
||||||
import androidx.compose.foundation.clickable
|
import androidx.compose.foundation.clickable
|
||||||
import androidx.compose.foundation.gestures.detectTapGestures
|
import androidx.compose.foundation.gestures.detectTapGestures
|
||||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Box
|
import androidx.compose.foundation.layout.Box
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
|
import androidx.compose.foundation.layout.IntrinsicSize
|
||||||
import androidx.compose.foundation.layout.PaddingValues
|
import androidx.compose.foundation.layout.PaddingValues
|
||||||
import androidx.compose.foundation.layout.Row
|
import androidx.compose.foundation.layout.Row
|
||||||
import androidx.compose.foundation.layout.RowScope
|
import androidx.compose.foundation.layout.fillMaxHeight
|
||||||
import androidx.compose.foundation.layout.Spacer
|
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
import androidx.compose.foundation.layout.height
|
import androidx.compose.foundation.layout.height
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.foundation.layout.size
|
|
||||||
import androidx.compose.foundation.layout.width
|
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.Button
|
||||||
import androidx.compose.material3.ButtonDefaults
|
import androidx.compose.material3.ButtonDefaults
|
||||||
import androidx.compose.material3.ButtonElevation
|
|
||||||
import androidx.compose.material3.DrawerValue
|
import androidx.compose.material3.DrawerValue
|
||||||
import androidx.compose.material3.DropdownMenu
|
import androidx.compose.material3.DropdownMenu
|
||||||
import androidx.compose.material3.DropdownMenuItem
|
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.material.icons.outlined.DragHandle
|
|
||||||
import androidx.compose.material3.CircularProgressIndicator
|
|
||||||
import androidx.compose.material3.ExtendedFloatingActionButton
|
|
||||||
import androidx.compose.material3.FloatingActionButton
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.material3.ModalDrawerSheet
|
import androidx.compose.material3.ModalDrawerSheet
|
||||||
import androidx.compose.material3.ModalNavigationDrawer
|
import androidx.compose.material3.ModalNavigationDrawer
|
||||||
@@ -60,11 +39,7 @@ import androidx.compose.runtime.rememberCoroutineScope
|
|||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.draw.clip
|
|
||||||
import androidx.compose.ui.graphics.Color
|
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.input.pointer.pointerInput
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
@@ -75,96 +50,84 @@ import androidx.core.graphics.toColorInt
|
|||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun Page(
|
fun Page(name: String, innerPadding: PaddingValues, pages: List<NavigationPageData>? = null, content: @Composable () -> Unit) {
|
||||||
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()
|
|
||||||
.background(MaterialTheme.colorScheme.background)
|
|
||||||
.padding(horizontal = 16.dp)
|
|
||||||
.padding(top = 16.dp)
|
|
||||||
) {
|
|
||||||
Column(
|
|
||||||
verticalArrangement = Arrangement.spacedBy(8.dp)
|
|
||||||
) {
|
|
||||||
content()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Composable
|
|
||||||
fun ExpandableFab(
|
|
||||||
items: List<NavigationPageData>
|
|
||||||
) {
|
|
||||||
val activityContext = LocalActivity.current
|
val activityContext = LocalActivity.current
|
||||||
var isExpanded by remember { mutableStateOf(false) }
|
|
||||||
|
|
||||||
val rotation by animateFloatAsState(if (isExpanded) 45f else 0f)
|
val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
|
||||||
|
val scope = rememberCoroutineScope()
|
||||||
|
|
||||||
BackHandler(
|
BackHandler(
|
||||||
enabled = isExpanded
|
enabled = pages != null && drawerState.isOpen
|
||||||
) {
|
) {
|
||||||
isExpanded = false
|
scope.launch {
|
||||||
|
drawerState.close()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Column(
|
ModalNavigationDrawer(
|
||||||
horizontalAlignment = Alignment.End,
|
drawerState = drawerState,
|
||||||
verticalArrangement = Arrangement.spacedBy(8.dp)
|
drawerContent = {
|
||||||
) {
|
if (pages != null) {
|
||||||
items.forEach { page ->
|
ModalDrawerSheet(
|
||||||
AnimatedVisibility(
|
modifier = Modifier.background(MaterialTheme.colorScheme.background).padding(8.dp)
|
||||||
visible = isExpanded,
|
) {
|
||||||
enter = fadeIn() + scaleIn(),
|
for (page in pages) {
|
||||||
exit = fadeOut() + scaleOut()
|
NavigationDrawerItem(
|
||||||
) {
|
label = { Text(page.name) },
|
||||||
ExtendedFloatingActionButton(
|
selected = false,
|
||||||
text = { Text(page.name) },
|
onClick = {
|
||||||
icon = { Icon(page.icon, contentDescription = null) },
|
scope.launch { drawerState.close() }
|
||||||
onClick = {
|
|
||||||
isExpanded = false
|
|
||||||
|
|
||||||
if (page.pageClass != null) {
|
if (page.pageClass != null) {
|
||||||
val intent = Intent(activityContext, page.pageClass)
|
val intent = Intent(activityContext, page.pageClass)
|
||||||
activityContext!!.startActivity(intent)
|
|
||||||
} else if (page.onClick != null) {
|
activityContext!!.startActivity(intent)
|
||||||
page.onClick()
|
} else if (page.onClick != null) {
|
||||||
}
|
page.onClick()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
}
|
}
|
||||||
)
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
) {
|
||||||
FloatingActionButton(
|
Scaffold(
|
||||||
onClick = { isExpanded = !isExpanded }
|
topBar = {
|
||||||
) {
|
TopBar(name, innerPadding, if (pages != null) {
|
||||||
Icon(
|
{
|
||||||
modifier = Modifier.graphicsLayer { rotationZ = rotation },
|
Text(
|
||||||
imageVector = Icons.Outlined.Add,
|
"☰",
|
||||||
contentDescription = null
|
fontSize = 24.sp,
|
||||||
)
|
color = MaterialTheme.colorScheme.onPrimaryContainer,
|
||||||
|
modifier = Modifier.clickable() {
|
||||||
|
scope.launch {
|
||||||
|
drawerState.open()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else null)
|
||||||
|
}
|
||||||
|
) { padding ->
|
||||||
|
Box(
|
||||||
|
modifier = Modifier
|
||||||
|
.padding(padding)
|
||||||
|
.fillMaxSize()
|
||||||
|
.background(MaterialTheme.colorScheme.background)
|
||||||
|
.padding(horizontal = 16.dp)
|
||||||
|
.padding(top = 16.dp)
|
||||||
|
) {
|
||||||
|
Column() {
|
||||||
|
content()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun TopBar(
|
fun TopBar(text: String, innerPadding: PaddingValues, openMenuButton: (@Composable () -> Unit)? = null) {
|
||||||
text: String,
|
|
||||||
innerPadding: PaddingValues,
|
|
||||||
topBarAdditionalElements: @Composable () -> Unit = {}
|
|
||||||
) {
|
|
||||||
Row(
|
Row(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
@@ -172,35 +135,30 @@ fun TopBar(
|
|||||||
.padding(top = innerPadding.calculateTopPadding())
|
.padding(top = innerPadding.calculateTopPadding())
|
||||||
.padding(vertical = 12.dp, horizontal = 12.dp),
|
.padding(vertical = 12.dp, horizontal = 12.dp),
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
horizontalArrangement = Arrangement.SpaceBetween
|
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
||||||
) {
|
) {
|
||||||
|
if (openMenuButton != null) {
|
||||||
|
openMenuButton()
|
||||||
|
}
|
||||||
|
|
||||||
Text(
|
Text(
|
||||||
text,
|
text,
|
||||||
modifier = Modifier,
|
modifier = Modifier,
|
||||||
fontSize = 24.sp,
|
fontSize = 24.sp,
|
||||||
color = MaterialTheme.colorScheme.onPrimaryContainer
|
color = MaterialTheme.colorScheme.onPrimaryContainer
|
||||||
)
|
)
|
||||||
|
|
||||||
topBarAdditionalElements()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun CardButton(cardInfo: CardInfo, onDeleteCard: (Int) -> Unit, reorderIcon: (@Composable () -> Unit)? = null) {
|
fun CardButton(cardInfo: CardInfo, onDeleteCard: (Int) -> Unit) {
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
|
|
||||||
var menuExpanded by remember { mutableStateOf(false) }
|
var menuExpanded by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
val cardColor = if (cardInfo.color == null)
|
|
||||||
MaterialTheme.colorScheme.primaryContainer
|
|
||||||
else
|
|
||||||
Color(cardInfo.color.toColorInt())
|
|
||||||
|
|
||||||
Box(modifier = Modifier.fillMaxWidth()) {
|
Box(modifier = Modifier.fillMaxWidth()) {
|
||||||
Surface(
|
Surface(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
.height(64.dp)
|
|
||||||
.pointerInput(Unit) {
|
.pointerInput(Unit) {
|
||||||
detectTapGestures(
|
detectTapGestures(
|
||||||
onTap = {
|
onTap = {
|
||||||
@@ -213,40 +171,34 @@ fun CardButton(cardInfo: CardInfo, onDeleteCard: (Int) -> Unit, reorderIcon: (@C
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
shape = RoundedCornerShape(12.dp),
|
shape = ButtonDefaults.shape,
|
||||||
color = MaterialTheme.colorScheme.primaryContainer.copy(0.625f),
|
color = MaterialTheme.colorScheme.primaryContainer,
|
||||||
tonalElevation = 2.dp,
|
tonalElevation = 2.dp
|
||||||
) {
|
) {
|
||||||
Row(
|
Row(
|
||||||
modifier = Modifier
|
modifier = Modifier.fillMaxWidth().height(IntrinsicSize.Min),
|
||||||
.fillMaxSize()
|
|
||||||
.background(Color.Transparent)
|
|
||||||
.padding(horizontal = 8.dp),
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
horizontalArrangement = Arrangement.SpaceBetween
|
horizontalArrangement = Arrangement.SpaceBetween
|
||||||
) {
|
) {
|
||||||
Row(
|
Box(
|
||||||
modifier = Modifier.weight(1f),
|
modifier = Modifier
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
.fillMaxHeight()
|
||||||
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
.width(32.dp)
|
||||||
) {
|
.background(
|
||||||
CardIcon(
|
if (cardInfo.color == null) MaterialTheme.colorScheme.secondaryContainer.copy(alpha = 0.625f)
|
||||||
if (cardInfo.icon == null) null else base64ToImageBitmap(cardInfo.icon),
|
else Color(cardInfo.color.toColorInt()).copy(alpha = 0.625f)
|
||||||
cardColor = cardColor
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
Text(
|
Text(
|
||||||
cardInfo.name,
|
cardInfo.name,
|
||||||
fontSize = 16.sp,
|
fontSize = 16.sp,
|
||||||
color = MaterialTheme.colorScheme.onPrimaryContainer,
|
color = MaterialTheme.colorScheme.onPrimaryContainer,
|
||||||
modifier = Modifier.fillMaxWidth(),
|
modifier = Modifier
|
||||||
textAlign = TextAlign.Start
|
.padding(16.dp)
|
||||||
)
|
.fillMaxWidth()
|
||||||
}
|
.weight(1f),
|
||||||
|
textAlign = TextAlign.Start
|
||||||
if (reorderIcon != null) {
|
)
|
||||||
reorderIcon()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -279,84 +231,11 @@ fun CardButton(cardInfo: CardInfo, onDeleteCard: (Int) -> Unit, reorderIcon: (@C
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun CardIcon(icon: ImageBitmap?, cardColor: Color, isLoading: Boolean = false) {
|
fun FullWidthButton(onClick: () -> Unit, content: @Composable () -> Unit) {
|
||||||
Row(
|
Button(
|
||||||
modifier = Modifier
|
modifier = Modifier.fillMaxWidth(),
|
||||||
.background(
|
onClick = onClick
|
||||||
cardColor.copy(0.525f),
|
|
||||||
RoundedCornerShape(12.dp)
|
|
||||||
)
|
|
||||||
.padding(8.dp),
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
horizontalArrangement = Arrangement.Center
|
|
||||||
) {
|
) {
|
||||||
if (isLoading) {
|
content()
|
||||||
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()
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,110 +0,0 @@
|
|||||||
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,54 +1,56 @@
|
|||||||
package ru.omni_devel.cards
|
package ru.omni_devel.cards
|
||||||
|
|
||||||
|
import android.content.Intent
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.activity.ComponentActivity
|
import androidx.activity.ComponentActivity
|
||||||
|
import androidx.activity.compose.BackHandler
|
||||||
import androidx.activity.compose.setContent
|
import androidx.activity.compose.setContent
|
||||||
import androidx.activity.enableEdgeToEdge
|
import androidx.activity.enableEdgeToEdge
|
||||||
import androidx.compose.animation.AnimatedVisibility
|
import androidx.compose.animation.AnimatedVisibility
|
||||||
import androidx.compose.animation.expandVertically
|
|
||||||
import androidx.compose.animation.fadeIn
|
import androidx.compose.animation.fadeIn
|
||||||
import androidx.compose.animation.fadeOut
|
import androidx.compose.animation.fadeOut
|
||||||
import androidx.compose.animation.shrinkVertically
|
import androidx.compose.animation.scaleIn
|
||||||
|
import androidx.compose.animation.scaleOut
|
||||||
|
import androidx.compose.animation.slideInVertically
|
||||||
|
import androidx.compose.animation.slideOutVertically
|
||||||
|
import androidx.compose.foundation.background
|
||||||
import androidx.compose.foundation.clickable
|
import androidx.compose.foundation.clickable
|
||||||
|
import androidx.compose.foundation.interaction.MutableInteractionSource
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Box
|
import androidx.compose.foundation.layout.Box
|
||||||
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.foundation.layout.PaddingValues
|
import androidx.compose.foundation.layout.PaddingValues
|
||||||
import androidx.compose.foundation.layout.Row
|
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.foundation.lazy.LazyColumn
|
import androidx.compose.foundation.rememberScrollState
|
||||||
import androidx.compose.foundation.lazy.rememberLazyListState
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||||
import androidx.compose.material.icons.Icons
|
import androidx.compose.foundation.verticalScroll
|
||||||
import androidx.compose.material.icons.outlined.Add
|
import androidx.compose.material3.Button
|
||||||
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.DragHandle
|
|
||||||
import androidx.compose.material.icons.outlined.Search
|
|
||||||
import androidx.compose.material3.Icon
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.material3.OutlinedTextField
|
|
||||||
import androidx.compose.material3.Scaffold
|
import androidx.compose.material3.Scaffold
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.mutableStateListOf
|
import androidx.compose.runtime.mutableStateListOf
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.rememberCoroutineScope
|
||||||
import androidx.compose.runtime.saveable.rememberSaveable
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.compose.runtime.snapshots.SnapshotStateList
|
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.draw.alpha
|
import androidx.compose.ui.graphics.Color
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.platform.LocalUriHandler
|
import androidx.compose.ui.platform.LocalUriHandler
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
|
import kotlinx.coroutines.delay
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
import ru.omni_devel.cards.ui.theme.OmniCardsTheme
|
import ru.omni_devel.cards.ui.theme.OmniCardsTheme
|
||||||
import sh.calvin.reorderable.ReorderableItem
|
|
||||||
import sh.calvin.reorderable.rememberReorderableLazyListState
|
private const val ANIMATION_DELAY = 300L
|
||||||
|
|
||||||
class MainActivity : ComponentActivity() {
|
class MainActivity : ComponentActivity() {
|
||||||
private lateinit var db: DbHelper
|
private lateinit var db: DbHelper
|
||||||
@@ -66,22 +68,12 @@ class MainActivity : ComponentActivity() {
|
|||||||
Scaffold(
|
Scaffold(
|
||||||
modifier = Modifier.fillMaxSize()
|
modifier = Modifier.fillMaxSize()
|
||||||
) { innerPadding ->
|
) { innerPadding ->
|
||||||
MainPage(
|
MainPage(innerPadding, cards, onDeleteCard = { cardId ->
|
||||||
innerPadding,
|
db.removeCard(cardId)
|
||||||
cards,
|
cards.removeIf { it.id == cardId }
|
||||||
onDeleteCard = { cardId ->
|
}, getDbFun = {
|
||||||
db.removeCard(cardId)
|
return@MainPage db
|
||||||
cards.removeIf { it.id == cardId }
|
})
|
||||||
syncCardsWithWatch(this, db.getCards())
|
|
||||||
},
|
|
||||||
onReorderCards = {
|
|
||||||
db.updatePositions(it)
|
|
||||||
syncCardsWithWatch(this, db.getCards())
|
|
||||||
},
|
|
||||||
getDbFun = {
|
|
||||||
return@MainPage db
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -92,167 +84,55 @@ class MainActivity : ComponentActivity() {
|
|||||||
|
|
||||||
cards.clear()
|
cards.clear()
|
||||||
cards.addAll(db.getCards())
|
cards.addAll(db.getCards())
|
||||||
|
|
||||||
syncCardsWithWatch(this, db.getCards())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun MainPage(
|
fun MainPage(
|
||||||
innerPadding: PaddingValues,
|
innerPadding: PaddingValues,
|
||||||
cards: SnapshotStateList<CardInfo>,
|
cards: List<CardInfo>,
|
||||||
onDeleteCard: (Int) -> Unit,
|
onDeleteCard: (Int) -> Unit,
|
||||||
onReorderCards: (List<Int>) -> Unit,
|
|
||||||
getDbFun: () -> DbHelper
|
getDbFun: () -> DbHelper
|
||||||
) {
|
) {
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
val uriHandler = LocalUriHandler.current
|
val uriHandler = LocalUriHandler.current
|
||||||
|
|
||||||
var searchQuery by rememberSaveable { mutableStateOf("") }
|
|
||||||
var isSearchFieldShowed by rememberSaveable { mutableStateOf(false) }
|
|
||||||
|
|
||||||
val lazyListState = rememberLazyListState()
|
|
||||||
val reorderableLazyListState = rememberReorderableLazyListState(lazyListState) { from, to ->
|
|
||||||
val fromId = from.key as? Int ?: return@rememberReorderableLazyListState
|
|
||||||
val toId = to.key as? Int ?: return@rememberReorderableLazyListState
|
|
||||||
|
|
||||||
val fromIndex = cards.indexOfFirst { it.id == fromId }
|
|
||||||
val toIndex = cards.indexOfFirst { it.id == toId }
|
|
||||||
|
|
||||||
if (fromIndex != -1 && toIndex != -1) {
|
|
||||||
cards.add(toIndex, cards.removeAt(fromIndex))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Box(
|
Box(
|
||||||
modifier = Modifier.fillMaxSize()
|
modifier = Modifier.fillMaxSize()
|
||||||
) {
|
) {
|
||||||
Page(
|
Page(
|
||||||
stringResource(R.string.app_name),
|
stringResource(R.string.app_name),
|
||||||
innerPadding,
|
innerPadding,
|
||||||
topBarAdditionalElements = {
|
pages = listOf(
|
||||||
Icon(
|
NavigationPageData(stringResource(R.string.do_add_card), EditCardActivity::class.java),
|
||||||
Icons.Outlined.Search,
|
NavigationPageData(stringResource(R.string.do_sync)) {
|
||||||
contentDescription = null,
|
Toast.makeText(context, context.getString(R.string.sync_in_progress), Toast.LENGTH_SHORT).show()
|
||||||
modifier = Modifier.clickable {
|
|
||||||
isSearchFieldShowed = !isSearchFieldShowed
|
sendToWatch(context, "/updateCards", Json.encodeToString(getDbFun().getCards())) { isSuccess ->
|
||||||
searchQuery = ""
|
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()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
)
|
},
|
||||||
},
|
NavigationPageData(stringResource(R.string.open_backup_menu), BackupActivity::class.java),
|
||||||
floatingActionButton = {
|
NavigationPageData(stringResource(R.string.source_code)) {
|
||||||
ExpandableFab(
|
uriHandler.openUri("https://github.com/omni-devel/OmniCards")
|
||||||
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()
|
|
||||||
|
|
||||||
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()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
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()
|
Column(
|
||||||
|
modifier = Modifier.verticalScroll(rememberScrollState()).padding(bottom = 64.dp).background(Color.Transparent),
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
return@filter cardNameCleaned.contains(queryCleaned)
|
|
||||||
}
|
|
||||||
|
|
||||||
LazyColumn(
|
|
||||||
state = lazyListState,
|
|
||||||
verticalArrangement = Arrangement.spacedBy(8.dp)
|
verticalArrangement = Arrangement.spacedBy(8.dp)
|
||||||
) {
|
) {
|
||||||
item {
|
|
||||||
AnimatedVisibility(
|
|
||||||
visible = isSearchFieldShowed,
|
|
||||||
enter = fadeIn() + expandVertically(),
|
|
||||||
exit = fadeOut() + shrinkVertically()
|
|
||||||
) {
|
|
||||||
OutlinedTextField(
|
|
||||||
value = searchQuery,
|
|
||||||
onValueChange = {
|
|
||||||
searchQuery = it
|
|
||||||
},
|
|
||||||
modifier = Modifier.fillMaxWidth(),
|
|
||||||
label = { Text(stringResource(R.string.search)) }
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cards.isEmpty()) {
|
if (cards.isEmpty()) {
|
||||||
item {
|
Text(
|
||||||
Text(
|
stringResource(R.string.add_card_to_get_started)
|
||||||
stringResource(R.string.add_card_to_get_started)
|
)
|
||||||
)
|
|
||||||
}
|
|
||||||
} else if (searchResults.isEmpty()) {
|
|
||||||
item {
|
|
||||||
Text(
|
|
||||||
stringResource(R.string.cards_not_found)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
for (card in searchResults) {
|
for (card in cards) {
|
||||||
item(key = card.id) {
|
CardButton(card, onDeleteCard)
|
||||||
ReorderableItem(reorderableLazyListState, key = card.id) { isDragging ->
|
|
||||||
Row(
|
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.alpha(if (isDragging) 0.7f else 1f)
|
|
||||||
) {
|
|
||||||
var reorderIcon: (@Composable () -> Unit)? = null
|
|
||||||
|
|
||||||
if (queryCleaned.isEmpty()) {
|
|
||||||
reorderIcon = {
|
|
||||||
Icon(
|
|
||||||
imageVector = Icons.Outlined.DragHandle,
|
|
||||||
contentDescription = null,
|
|
||||||
tint = MaterialTheme.colorScheme.onPrimaryContainer,
|
|
||||||
modifier = Modifier
|
|
||||||
.draggableHandle(
|
|
||||||
onDragStopped = {
|
|
||||||
onReorderCards(cards.map { it.id })
|
|
||||||
}
|
|
||||||
)
|
|
||||||
.padding(8.dp)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
CardButton(
|
|
||||||
card,
|
|
||||||
onDeleteCard,
|
|
||||||
reorderIcon
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
package ru.omni_devel.cards
|
package ru.omni_devel.cards
|
||||||
|
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
import androidx.compose.ui.graphics.vector.ImageVector
|
|
||||||
|
|
||||||
data class NavigationPageData(
|
data class NavigationPageData(
|
||||||
val name: String,
|
val name: String,
|
||||||
val icon: ImageVector,
|
|
||||||
val pageClass: Class<out Activity>? = null,
|
val pageClass: Class<out Activity>? = null,
|
||||||
val onClick: (() -> Unit)? = null
|
val onClick: (() -> Unit)? = null
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,237 +0,0 @@
|
|||||||
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
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,185 +0,0 @@
|
|||||||
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,14 +7,12 @@ import androidx.activity.ComponentActivity
|
|||||||
import androidx.activity.compose.setContent
|
import androidx.activity.compose.setContent
|
||||||
import androidx.activity.enableEdgeToEdge
|
import androidx.activity.enableEdgeToEdge
|
||||||
import androidx.compose.foundation.Image
|
import androidx.compose.foundation.Image
|
||||||
import androidx.compose.foundation.background
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.foundation.layout.PaddingValues
|
import androidx.compose.foundation.layout.PaddingValues
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.material3.Scaffold
|
import androidx.compose.material3.Scaffold
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
@@ -22,8 +20,6 @@ import androidx.compose.runtime.Composable
|
|||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
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.graphics.asImageBitmap
|
||||||
import androidx.compose.ui.layout.ContentScale
|
import androidx.compose.ui.layout.ContentScale
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
@@ -99,10 +95,10 @@ fun ShowCardPage(innerPadding: PaddingValues, cardInfo: CardInfo) {
|
|||||||
contentDescription = cardInfo.codeValue,
|
contentDescription = cardInfo.codeValue,
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.padding(bottom = 8.dp)
|
.padding(bottom = 8.dp)
|
||||||
.clip(RoundedCornerShape(12.dp)),
|
.fillMaxWidth()
|
||||||
|
.weight(1f, fill = false),
|
||||||
contentScale = ContentScale.Fit
|
contentScale = ContentScale.Fit
|
||||||
)
|
)
|
||||||
|
|
||||||
Text(
|
Text(
|
||||||
cardInfo.codeValue,
|
cardInfo.codeValue,
|
||||||
modifier = Modifier.fillMaxWidth(),
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
|||||||
@@ -1,90 +0,0 @@
|
|||||||
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,21 +2,10 @@ package ru.omni_devel.cards
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.graphics.Bitmap
|
import android.graphics.Bitmap
|
||||||
import android.graphics.BitmapFactory
|
|
||||||
import android.os.LocaleList
|
|
||||||
import androidx.compose.ui.graphics.ImageBitmap
|
|
||||||
import androidx.compose.ui.graphics.asAndroidBitmap
|
|
||||||
import com.google.android.gms.wearable.Wearable
|
import com.google.android.gms.wearable.Wearable
|
||||||
import com.google.zxing.BarcodeFormat
|
import com.google.zxing.BarcodeFormat
|
||||||
import com.google.zxing.EncodeHintType
|
|
||||||
import com.google.zxing.MultiFormatWriter
|
import com.google.zxing.MultiFormatWriter
|
||||||
import com.journeyapps.barcodescanner.BarcodeEncoder
|
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 {
|
fun generateCode(value: String, format: BarcodeFormat): Bitmap {
|
||||||
val writer = MultiFormatWriter()
|
val writer = MultiFormatWriter()
|
||||||
@@ -26,23 +15,17 @@ fun generateCode(value: String, format: BarcodeFormat): Bitmap {
|
|||||||
else -> 800 to 400
|
else -> 800 to 400
|
||||||
}
|
}
|
||||||
|
|
||||||
val hints = mapOf(
|
val matrix = writer.encode(value, format, width, height)
|
||||||
EncodeHintType.CHARACTER_SET to "UTF-8"
|
|
||||||
)
|
|
||||||
|
|
||||||
val matrix = writer.encode(value, format, width, height, hints)
|
|
||||||
|
|
||||||
return BarcodeEncoder().createBitmap(matrix)
|
return BarcodeEncoder().createBitmap(matrix)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun sendToWatch(context: Context, path: String, message: String): Boolean {
|
fun sendToWatch(context: Context, path: String, message: String, onResult: (Boolean) -> Unit) {
|
||||||
val nodeClient = Wearable.getNodeClient(context)
|
val nodeClient = Wearable.getNodeClient(context)
|
||||||
|
|
||||||
var isSuccess = false
|
|
||||||
|
|
||||||
nodeClient.connectedNodes.addOnSuccessListener { nodes ->
|
nodeClient.connectedNodes.addOnSuccessListener { nodes ->
|
||||||
if (nodes.isEmpty()) {
|
if (nodes.isEmpty()) {
|
||||||
isSuccess = false
|
onResult(false)
|
||||||
|
|
||||||
return@addOnSuccessListener
|
return@addOnSuccessListener
|
||||||
}
|
}
|
||||||
@@ -63,19 +46,13 @@ fun sendToWatch(context: Context, path: String, message: String): Boolean {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (completedCount == nodes.size) {
|
if (completedCount == nodes.size) {
|
||||||
isSuccess = isAnySuccess
|
onResult(isAnySuccess)
|
||||||
}
|
}
|
||||||
}.addOnFailureListener(context.mainExecutor) {
|
}.addOnFailureListener(context.mainExecutor) {
|
||||||
isSuccess = false
|
onResult(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? {
|
fun checkCardData(name: String, value: String): Int? {
|
||||||
@@ -89,83 +66,3 @@ fun checkCardData(name: String, value: String): Int? {
|
|||||||
|
|
||||||
return null
|
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
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -31,20 +31,9 @@
|
|||||||
<string name="card_name">Название карты</string>
|
<string name="card_name">Название карты</string>
|
||||||
<string name="card_value">Значение карты</string>
|
<string name="card_value">Значение карты</string>
|
||||||
<string name="do_scan_card">Сканировать карту</string>
|
<string name="do_scan_card">Сканировать карту</string>
|
||||||
<string name="card_from_template">Из шаблона</string>
|
|
||||||
<string name="scan_your_card">Отсканируйте вашу карту</string>
|
<string name="scan_your_card">Отсканируйте вашу карту</string>
|
||||||
<string name="card_code_type">Тип кода карты</string>
|
<string name="card_code_type">Тип кода карты</string>
|
||||||
<string name="do_choose_color">Выбрать цвет</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_name_should_not_be_empty">Имя карты не должно быть пустым</string>
|
||||||
<string name="card_value_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>
|
<string name="name_must_be_shorter_than_32_symbols">Имя карты должно быть короче 32 символов</string>
|
||||||
|
|||||||
@@ -36,24 +36,12 @@
|
|||||||
<string name="card_name">Card name</string>
|
<string name="card_name">Card name</string>
|
||||||
<string name="card_value">Card value</string>
|
<string name="card_value">Card value</string>
|
||||||
<string name="do_scan_card">Scan card</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="scan_your_card">Scan your card</string>
|
||||||
<string name="card_code_type">Card code type</string>
|
<string name="card_code_type">Card code type</string>
|
||||||
<string name="do_choose_color">Choose color</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_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="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="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="failed_to_generate_card_code">Failed to generate card code. Check the card data</string>
|
||||||
<string name="title_activity_select_icon">SelectIconActivity</string>
|
|
||||||
</resources>
|
</resources>
|
||||||
@@ -23,20 +23,17 @@ android {
|
|||||||
minSdk = 30
|
minSdk = 30
|
||||||
targetSdk = 36
|
targetSdk = 36
|
||||||
versionCode = generateVersionCode()
|
versionCode = generateVersionCode()
|
||||||
versionName = "3.3.0"
|
versionName = "1.4.0"
|
||||||
}
|
}
|
||||||
|
|
||||||
buildTypes {
|
buildTypes {
|
||||||
release {
|
release {
|
||||||
isMinifyEnabled = true
|
isMinifyEnabled = false
|
||||||
proguardFiles(
|
proguardFiles(
|
||||||
getDefaultProguardFile("proguard-android-optimize.txt"),
|
getDefaultProguardFile("proguard-android-optimize.txt"),
|
||||||
"proguard-rules.pro"
|
"proguard-rules.pro"
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
debug {
|
|
||||||
applicationIdSuffix = ".debug"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
compileOptions {
|
compileOptions {
|
||||||
sourceCompatibility = JavaVersion.VERSION_11
|
sourceCompatibility = JavaVersion.VERSION_11
|
||||||
@@ -71,6 +68,4 @@ dependencies {
|
|||||||
implementation(libs.zxing.android)
|
implementation(libs.zxing.android)
|
||||||
implementation(libs.material)
|
implementation(libs.material)
|
||||||
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.9.0")
|
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")
|
|
||||||
}
|
}
|
||||||
@@ -9,7 +9,5 @@ class CardInfo (
|
|||||||
val name: String,
|
val name: String,
|
||||||
val codeType: BarcodeFormat,
|
val codeType: BarcodeFormat,
|
||||||
val codeValue: String,
|
val codeValue: String,
|
||||||
val color: String?,
|
val color: String?
|
||||||
val icon: String?,
|
|
||||||
val position: Int
|
|
||||||
)
|
)
|
||||||
@@ -2,18 +2,13 @@ package ru.omni_devel.cards.presentation
|
|||||||
|
|
||||||
import android.content.ContentValues
|
import android.content.ContentValues
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.database.Cursor
|
|
||||||
import android.database.sqlite.SQLiteDatabase
|
import android.database.sqlite.SQLiteDatabase
|
||||||
import android.database.sqlite.SQLiteOpenHelper
|
import android.database.sqlite.SQLiteOpenHelper
|
||||||
import androidx.core.database.getStringOrNull
|
|
||||||
import androidx.core.database.sqlite.transaction
|
|
||||||
import com.google.zxing.BarcodeFormat
|
import com.google.zxing.BarcodeFormat
|
||||||
|
|
||||||
class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?) : SQLiteOpenHelper(context, "omni_cards", factory, 6) {
|
class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?) : SQLiteOpenHelper(context, "omni_cards", factory, 2) {
|
||||||
override fun onCreate(db: SQLiteDatabase?) {
|
override fun onCreate(db: SQLiteDatabase?) {
|
||||||
db!!.execSQL(
|
db!!.execSQL("CREATE TABLE IF NOT EXISTS cards (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, codeType TEXT, codeValue TEXT, color TEXT)")
|
||||||
"CREATE TABLE IF NOT EXISTS cards (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, codeType TEXT, codeValue TEXT, color TEXT, icon TEXT)"
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onUpgrade(
|
override fun onUpgrade(
|
||||||
@@ -24,38 +19,6 @@ class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?)
|
|||||||
if (oldVersion < 2) {
|
if (oldVersion < 2) {
|
||||||
db!!.execSQL("ALTER TABLE cards ADD COLUMN color TEXT")
|
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")
|
|
||||||
}
|
|
||||||
|
|
||||||
if (oldVersion < 6) {
|
|
||||||
db!!.execSQL("ALTER TABLE cards ADD COLUMN position INTEGER DEFAULT 0")
|
|
||||||
db.execSQL("UPDATE cards SET position = id")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun readCard(cursor: Cursor): CardInfo {
|
|
||||||
return CardInfo(
|
|
||||||
id = cursor.getInt(cursor.getColumnIndexOrThrow("id")),
|
|
||||||
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")),
|
|
||||||
icon = cursor.getStringOrNull(cursor.getColumnIndexOrThrow("icon")),
|
|
||||||
position = cursor.getInt(cursor.getColumnIndexOrThrow("position"))
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun clearDatabase() {
|
fun clearDatabase() {
|
||||||
@@ -69,33 +32,35 @@ class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?)
|
|||||||
fun addCards(cards: List<CardInfo>) {
|
fun addCards(cards: List<CardInfo>) {
|
||||||
val db = this.writableDatabase
|
val db = this.writableDatabase
|
||||||
|
|
||||||
db.use { db ->
|
for (card in cards) {
|
||||||
db.transaction {
|
val values = ContentValues()
|
||||||
for (card in cards) {
|
|
||||||
val values = ContentValues()
|
|
||||||
|
|
||||||
values.put("id", card.id)
|
values.put("id", card.id)
|
||||||
values.put("name", card.name)
|
values.put("name", card.name)
|
||||||
values.put("codeValue", card.codeValue)
|
values.put("codeValue", card.codeValue)
|
||||||
values.put("codeType", card.codeType.name)
|
values.put("codeType", card.codeType.name)
|
||||||
values.put("color", card.color)
|
values.put("color", card.color)
|
||||||
values.put("icon", card.icon)
|
|
||||||
values.put("position", card.position)
|
|
||||||
|
|
||||||
db.insert("cards", null, values)
|
db.insert("cards", null, values)
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
db.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getCards(): List<CardInfo> {
|
fun getCards(): List<CardInfo> {
|
||||||
val db = this.readableDatabase
|
val db = this.readableDatabase
|
||||||
|
|
||||||
val cursor = db.rawQuery("SELECT * FROM cards ORDER BY position", null)
|
val cursor = db.rawQuery("SELECT * FROM cards", null)
|
||||||
val cards = mutableListOf<CardInfo>()
|
val cards = mutableListOf<CardInfo>()
|
||||||
|
|
||||||
while (cursor.moveToNext()) {
|
while (cursor.moveToNext()) {
|
||||||
cards.add(readCard(cursor))
|
cards.add(CardInfo(
|
||||||
|
id = cursor.getInt(cursor.getColumnIndexOrThrow("id")),
|
||||||
|
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"))
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
cursor.close()
|
cursor.close()
|
||||||
@@ -116,7 +81,13 @@ class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?)
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
val cardInfo = readCard(cursor)
|
val cardInfo = CardInfo(
|
||||||
|
id = cursor.getInt(cursor.getColumnIndexOrThrow("id")),
|
||||||
|
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"))
|
||||||
|
)
|
||||||
|
|
||||||
cursor.close()
|
cursor.close()
|
||||||
db.close()
|
db.close()
|
||||||
|
|||||||
@@ -1,26 +1,20 @@
|
|||||||
package ru.omni_devel.cards.presentation
|
package ru.omni_devel.cards.presentation
|
||||||
|
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import androidx.compose.foundation.Image
|
|
||||||
import androidx.compose.foundation.background
|
import androidx.compose.foundation.background
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
|
||||||
import androidx.compose.foundation.layout.Box
|
import androidx.compose.foundation.layout.Box
|
||||||
|
import androidx.compose.foundation.layout.IntrinsicSize
|
||||||
import androidx.compose.foundation.layout.PaddingValues
|
import androidx.compose.foundation.layout.PaddingValues
|
||||||
import androidx.compose.foundation.layout.Row
|
import androidx.compose.foundation.layout.Row
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxHeight
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
import androidx.compose.foundation.layout.height
|
import androidx.compose.foundation.layout.height
|
||||||
import androidx.compose.foundation.layout.padding
|
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.material.icons.outlined.QrCode
|
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.ui.Alignment
|
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.draw.clip
|
import androidx.compose.ui.draw.clip
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
import androidx.compose.ui.graphics.ImageBitmap
|
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.text.style.TextAlign
|
import androidx.compose.ui.text.style.TextAlign
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
@@ -28,7 +22,6 @@ import androidx.compose.ui.unit.sp
|
|||||||
import androidx.core.graphics.toColorInt
|
import androidx.core.graphics.toColorInt
|
||||||
import androidx.wear.compose.material3.Button
|
import androidx.wear.compose.material3.Button
|
||||||
import androidx.wear.compose.material3.ButtonDefaults
|
import androidx.wear.compose.material3.ButtonDefaults
|
||||||
import androidx.wear.compose.material3.Icon
|
|
||||||
import androidx.wear.compose.material3.MaterialTheme
|
import androidx.wear.compose.material3.MaterialTheme
|
||||||
import androidx.wear.compose.material3.Text
|
import androidx.wear.compose.material3.Text
|
||||||
|
|
||||||
@@ -36,18 +29,12 @@ import androidx.wear.compose.material3.Text
|
|||||||
fun CardButton(cardInfo: CardInfo) {
|
fun CardButton(cardInfo: CardInfo) {
|
||||||
val context = LocalContext.current
|
val context = LocalContext.current
|
||||||
|
|
||||||
val cardColor = if (cardInfo.color == null)
|
|
||||||
MaterialTheme.colorScheme.primaryContainer
|
|
||||||
else
|
|
||||||
Color(cardInfo.color.toColorInt())
|
|
||||||
|
|
||||||
Box(modifier = Modifier.fillMaxWidth()) {
|
Box(modifier = Modifier.fillMaxWidth()) {
|
||||||
Button(
|
Button(
|
||||||
modifier = Modifier.fillMaxWidth().height(64.dp),
|
modifier = Modifier.fillMaxWidth(),
|
||||||
colors = ButtonDefaults.buttonColors(
|
colors = ButtonDefaults.buttonColors(
|
||||||
containerColor = MaterialTheme.colorScheme.primaryContainer.copy(0.625f)
|
containerColor = MaterialTheme.colorScheme.primaryContainer
|
||||||
),
|
),
|
||||||
shape = RoundedCornerShape(12.dp),
|
|
||||||
contentPadding = PaddingValues(0.dp),
|
contentPadding = PaddingValues(0.dp),
|
||||||
onClick = {
|
onClick = {
|
||||||
val intent = Intent(context, ShowCardActivity::class.java)
|
val intent = Intent(context, ShowCardActivity::class.java)
|
||||||
@@ -59,54 +46,32 @@ fun CardButton(cardInfo: CardInfo) {
|
|||||||
) {
|
) {
|
||||||
Row(
|
Row(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxSize()
|
.fillMaxWidth()
|
||||||
.background(Color.Transparent)
|
.height(IntrinsicSize.Min)
|
||||||
.padding(horizontal = 8.dp),
|
.clip(ButtonDefaults.shape)
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
|
||||||
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
|
||||||
) {
|
) {
|
||||||
CardIcon(
|
Box(
|
||||||
if (cardInfo.icon == null) null else base64ToImageBitmap(cardInfo.icon),
|
modifier = Modifier
|
||||||
cardColor = cardColor
|
.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)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
Text(
|
Text(
|
||||||
cardInfo.name,
|
cardInfo.name,
|
||||||
fontSize = 16.sp,
|
fontSize = 16.sp,
|
||||||
color = MaterialTheme.colorScheme.onPrimaryContainer,
|
color = MaterialTheme.colorScheme.onPrimaryContainer,
|
||||||
modifier = Modifier.fillMaxWidth(),
|
modifier = Modifier
|
||||||
|
.padding(16.dp)
|
||||||
|
.fillMaxWidth()
|
||||||
|
.weight(1f),
|
||||||
textAlign = TextAlign.Start
|
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,50 +4,38 @@ import android.annotation.SuppressLint
|
|||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import androidx.activity.ComponentActivity
|
import androidx.activity.ComponentActivity
|
||||||
import androidx.activity.compose.setContent
|
import androidx.activity.compose.setContent
|
||||||
import androidx.compose.foundation.background
|
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Box
|
import androidx.compose.foundation.layout.Box
|
||||||
import androidx.compose.foundation.layout.BoxWithConstraints
|
import androidx.compose.foundation.layout.BoxWithConstraints
|
||||||
import androidx.compose.foundation.layout.PaddingValues
|
import androidx.compose.foundation.layout.PaddingValues
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.mutableStateOf
|
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.wear.compose.foundation.lazy.TransformingLazyColumn
|
import androidx.wear.compose.foundation.lazy.TransformingLazyColumn
|
||||||
import androidx.wear.compose.material3.MaterialTheme
|
|
||||||
import androidx.wear.compose.material3.Text
|
import androidx.wear.compose.material3.Text
|
||||||
import ru.omni_devel.cards.R
|
import ru.omni_devel.cards.R
|
||||||
|
|
||||||
class MainActivity : ComponentActivity() {
|
class MainActivity : ComponentActivity() {
|
||||||
private lateinit var db: DbHelper
|
private lateinit var db: DbHelper
|
||||||
private val cardsState = mutableStateOf<List<CardInfo>>(emptyList())
|
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
db = DbHelper(this, null)
|
db = DbHelper(this, null)
|
||||||
|
|
||||||
cardsState.value = db.getCards()
|
|
||||||
|
|
||||||
setContent {
|
setContent {
|
||||||
App(cardsState.value)
|
App(db.getCards())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onResume() {
|
|
||||||
super.onResume()
|
|
||||||
|
|
||||||
cardsState.value = db.getCards()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("UnusedBoxWithConstraintsScope")
|
@SuppressLint("UnusedBoxWithConstraintsScope")
|
||||||
@Composable
|
@Composable
|
||||||
fun App(cards: List<CardInfo>) {
|
fun App(cards: List<CardInfo>) {
|
||||||
BoxWithConstraints(modifier = Modifier.fillMaxSize().background(MaterialTheme.colorScheme.background)) {
|
BoxWithConstraints(modifier = Modifier.fillMaxSize()) {
|
||||||
val topBottomPadding = maxHeight * 0.4f
|
val topBottomPadding = maxHeight * 0.4f
|
||||||
|
|
||||||
if (cards.isEmpty()) {
|
if (cards.isEmpty()) {
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ import com.google.android.gms.wearable.MessageEvent
|
|||||||
import com.google.android.gms.wearable.WearableListenerService
|
import com.google.android.gms.wearable.WearableListenerService
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
import ru.omni_devel.cards.R
|
import ru.omni_devel.cards.R
|
||||||
import androidx.lifecycle.ProcessLifecycleOwner
|
|
||||||
import androidx.lifecycle.Lifecycle
|
|
||||||
|
|
||||||
class MessagesReceiver : WearableListenerService() {
|
class MessagesReceiver : WearableListenerService() {
|
||||||
private lateinit var db: DbHelper
|
private lateinit var db: DbHelper
|
||||||
@@ -26,32 +24,17 @@ class MessagesReceiver : WearableListenerService() {
|
|||||||
val json = String(event.data, Charsets.UTF_8)
|
val json = String(event.data, Charsets.UTF_8)
|
||||||
|
|
||||||
val cards = Json.decodeFromString<List<CardInfo>>(json)
|
val cards = Json.decodeFromString<List<CardInfo>>(json)
|
||||||
val oldCards = db.getCards()
|
|
||||||
|
|
||||||
if (cards == oldCards) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
db.clearDatabase()
|
db.clearDatabase()
|
||||||
db.addCards(cards)
|
db.addCards(cards)
|
||||||
|
|
||||||
val isAppInForeground = ProcessLifecycleOwner.get().lifecycle.currentState.isAtLeast(
|
Handler(Looper.getMainLooper()).post {
|
||||||
Lifecycle.State.STARTED
|
Toast.makeText(this, this.getString(R.string.sync_is_successful), Toast.LENGTH_SHORT).show()
|
||||||
)
|
|
||||||
|
|
||||||
if (isAppInForeground) {
|
val intent = Intent(this, MainActivity::class.java).apply {
|
||||||
Handler(Looper.getMainLooper()).post {
|
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
|
||||||
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
|
|
||||||
}
|
|
||||||
startActivity(intent)
|
|
||||||
}
|
}
|
||||||
|
startActivity(intent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
package ru.omni_devel.cards.presentation
|
package ru.omni_devel.cards.presentation
|
||||||
|
|
||||||
import android.graphics.Bitmap
|
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.BarcodeFormat
|
||||||
import com.google.zxing.MultiFormatWriter
|
import com.google.zxing.MultiFormatWriter
|
||||||
import com.journeyapps.barcodescanner.BarcodeEncoder
|
import com.journeyapps.barcodescanner.BarcodeEncoder
|
||||||
@@ -15,10 +11,3 @@ fun generateCode(value: String, format: BarcodeFormat, width: Int, height: Int):
|
|||||||
|
|
||||||
return BarcodeEncoder().createBitmap(matrix)
|
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