193 lines
6.5 KiB
Kotlin
193 lines
6.5 KiB
Kotlin
package ru.omni_devel.cards
|
|
|
|
import android.content.Intent
|
|
import android.os.Bundle
|
|
import android.widget.Toast
|
|
import androidx.activity.ComponentActivity
|
|
import androidx.activity.compose.setContent
|
|
import androidx.activity.enableEdgeToEdge
|
|
import androidx.compose.foundation.background
|
|
import androidx.compose.foundation.layout.Arrangement
|
|
import androidx.compose.foundation.layout.Box
|
|
import androidx.compose.foundation.layout.Column
|
|
import androidx.compose.foundation.layout.PaddingValues
|
|
import androidx.compose.foundation.layout.fillMaxSize
|
|
import androidx.compose.foundation.layout.fillMaxWidth
|
|
import androidx.compose.foundation.layout.padding
|
|
import androidx.compose.foundation.rememberScrollState
|
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
|
import androidx.compose.foundation.verticalScroll
|
|
import androidx.compose.material3.Button
|
|
import androidx.compose.material3.MaterialTheme
|
|
import androidx.compose.material3.Scaffold
|
|
import androidx.compose.material3.Text
|
|
import androidx.compose.runtime.Composable
|
|
import androidx.compose.runtime.mutableStateOf
|
|
import androidx.compose.runtime.remember
|
|
import androidx.compose.ui.Modifier
|
|
import androidx.compose.ui.unit.dp
|
|
import androidx.compose.runtime.getValue
|
|
import androidx.compose.runtime.mutableStateListOf
|
|
import androidx.compose.runtime.setValue
|
|
import androidx.compose.ui.Alignment
|
|
import androidx.compose.ui.graphics.Color
|
|
import androidx.compose.ui.platform.LocalContext
|
|
import androidx.compose.ui.platform.LocalUriHandler
|
|
import androidx.compose.ui.res.stringResource
|
|
import kotlinx.serialization.json.Json
|
|
import ru.omni_devel.cards.ui.theme.OmniCardsTheme
|
|
|
|
class MainActivity : ComponentActivity() {
|
|
private lateinit var db: DbHelper
|
|
|
|
private val cards = mutableStateListOf<CardInfo>()
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
enableEdgeToEdge()
|
|
|
|
db = DbHelper(this, null)
|
|
|
|
setContent {
|
|
OmniCardsTheme {
|
|
Scaffold(
|
|
modifier = Modifier.fillMaxSize()
|
|
) { innerPadding ->
|
|
MainPage(innerPadding, cards, onDeleteCard = { cardId ->
|
|
db.removeCard(cardId)
|
|
cards.removeIf { it.id == cardId }
|
|
}, getDbFun = {
|
|
return@MainPage db
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
override fun onResume() {
|
|
super.onResume()
|
|
|
|
cards.clear()
|
|
cards.addAll(db.getCards())
|
|
}
|
|
}
|
|
|
|
@Composable
|
|
fun MainPage(
|
|
innerPadding: PaddingValues,
|
|
cards: List<CardInfo>,
|
|
onDeleteCard: (Int) -> Unit,
|
|
getDbFun: () -> DbHelper
|
|
) {
|
|
val context = LocalContext.current
|
|
val uriHandler = LocalUriHandler.current
|
|
|
|
var isActionBarShowed by remember { mutableStateOf(false) }
|
|
|
|
Box(
|
|
modifier = Modifier.fillMaxSize()
|
|
) {
|
|
Page(
|
|
stringResource(R.string.app_name),
|
|
innerPadding
|
|
) {
|
|
Column(
|
|
modifier = Modifier.verticalScroll(rememberScrollState()).padding(bottom = 64.dp).background(Color.Transparent).padding(top = 16.dp),
|
|
verticalArrangement = Arrangement.spacedBy(8.dp)
|
|
) {
|
|
if (cards.isEmpty()) {
|
|
Text(
|
|
stringResource(R.string.add_card_to_get_started)
|
|
)
|
|
} else {
|
|
for (card in cards) {
|
|
CardButton(card, onDeleteCard)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (isActionBarShowed) {
|
|
Column(
|
|
modifier = Modifier
|
|
.align(Alignment.BottomEnd)
|
|
.padding(innerPadding)
|
|
.padding(16.dp)
|
|
.background(
|
|
MaterialTheme.colorScheme.inversePrimary,
|
|
RoundedCornerShape(24.dp)
|
|
)
|
|
.padding(16.dp)
|
|
.fillMaxWidth()
|
|
) {
|
|
FullWidthButton(
|
|
onClick = {
|
|
isActionBarShowed = false
|
|
|
|
val intent = Intent(context, EditCardActivity::class.java)
|
|
|
|
context.startActivity(intent)
|
|
}
|
|
) {
|
|
Text(stringResource(R.string.do_add_card))
|
|
}
|
|
FullWidthButton(
|
|
onClick = {
|
|
Toast.makeText(context, context.getString(R.string.sync_in_progress), Toast.LENGTH_SHORT).show()
|
|
|
|
sendToWatch(context, "/updateCards", Json.encodeToString(getDbFun().getCards())) { isSuccess ->
|
|
if (isSuccess) {
|
|
Toast.makeText(context, context.getString(R.string.sync_is_successful), Toast.LENGTH_SHORT).show()
|
|
} else {
|
|
Toast.makeText(context, context.getString(R.string.sync_is_fail), Toast.LENGTH_LONG).show()
|
|
}
|
|
}
|
|
|
|
isActionBarShowed = false
|
|
}
|
|
) {
|
|
Text(stringResource(R.string.do_sync))
|
|
}
|
|
FullWidthButton(
|
|
onClick = {
|
|
val intent = Intent(context, BackupActivity::class.java)
|
|
|
|
context.startActivity(intent)
|
|
|
|
isActionBarShowed = false
|
|
}
|
|
) {
|
|
Text(stringResource(R.string.open_backup_menu))
|
|
}
|
|
FullWidthButton(
|
|
onClick = {
|
|
uriHandler.openUri("https://github.com/omni-devel/OmniCards")
|
|
|
|
isActionBarShowed = false
|
|
}
|
|
) {
|
|
Text(stringResource(R.string.source_code))
|
|
}
|
|
FullWidthButton(
|
|
onClick = {
|
|
isActionBarShowed = false
|
|
}
|
|
) {
|
|
Text(">")
|
|
}
|
|
}
|
|
} else {
|
|
Button(
|
|
modifier = Modifier
|
|
.align(Alignment.BottomEnd)
|
|
.padding(innerPadding).padding(32.dp),
|
|
onClick = {
|
|
isActionBarShowed = true
|
|
}
|
|
) {
|
|
Text("<")
|
|
}
|
|
}
|
|
}
|
|
}
|