package com.vgmlr.wedge
import android.content.Context
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DataView(vm: MainViewModel, prefs: PreferenceManager, onBack: () -> Unit) {
val context = LocalContext.current
val aHeaderFmt by prefs.headerFormat.collectAsState(WedgeConfig.HEADER_FORMAT_DEFAULT)
val aBkStamp by prefs.backupStamp.collectAsState(initial = 0L)
val noteEntity by vm.dao.getNote().collectAsState(initial = null)
val content = noteEntity?.content ?: ""
val charCount = content.length
val lineCount = if (content.isEmpty()) 0 else content.count { it == '\n' } + 1
val modifiedStr = WedgeDate.getOtcMetadata(
noteEntity?.lastModified ?: System.currentTimeMillis()
)
val syncStamp = remember {
context.getSharedPreferences("sync_prefs", Context.MODE_PRIVATE)
.getLong("sync_last_stamp", 0L)
}
val syncedStr = if (syncStamp == 0L) "[none]" else WedgeDate.getOtcMetadata(syncStamp)
val archivedStr = if (aBkStamp == 0L) "[none]" else WedgeDate.getOtcMetadata(aBkStamp)
val headerPreview = remember(aHeaderFmt) {
try {
WedgeHeader.render(
aHeaderFmt.ifBlank { WedgeConfig.HEADER_FORMAT_DEFAULT },
System.currentTimeMillis()
)
} catch (_: Exception) {
""
}
}
Scaffold(
topBar = {
TopAppBar(
title = {
Text(
"Data",
color = colorResource(id = R.color.title_color),
fontSize = 18.sp
)
},
colors = TopAppBarDefaults.topAppBarColors(containerColor = Color(0xFF486860)),
navigationIcon = {
IconButton(onClick = onBack) {
Icon(
Icons.AutoMirrored.Filled.ArrowBack,
null,
tint = colorResource(id = R.color.title_color)
)
}
}
)
}
) { p ->
Column(
Modifier.padding(p).fillMaxSize().background(CBg)
.padding(horizontal = 16.dp).verticalScroll(rememberScrollState())
) {
Spacer(Modifier.height(24.dp))
// note
WgCard {
DataRow("Lines", "$lineCount")
WgDivider()
DataRow("Characters", "$charCount")
WgDivider()
DataRow("Modified", modifiedStr)
WgDivider()
DataRow("Synced", syncedStr)
WgDivider()
DataRow("Archived", archivedStr)
}
Spacer(Modifier.height(24.dp))
// syntax
WgSection("Syntax") {
DataRow("Math", "123+456=")
WgDivider()
DataRow("Days", "20mar()=")
WgDivider()
DataRow("Focus", "text!")
WgDivider()
DataRow("Italic", "text?")
WgDivider()
DataRow("Under-Line", "text*")
WgDivider()
DataRow("Strike-Thru", "text~")
WgDivider()
DataRow("Comment", "// text")
WgDivider()
DataRow("HR", "hr(#)")
WgDivider()
DataRow("Dash", "dash(#)")
}
// list
WgSection("Logic") {
DataRow("List", "1. text[enter]")
WgDivider()
DataRow("Sort", "(hierarchic)")
}
// calendar
WgSection("Calendar") {
DataRow("Header", headerPreview)
WgDivider()
DataRow("Alarm", "00:00 text()")
WgDivider()
DataRow("Count Down", "00:00 text[]")
}
// security
WgSection("Security") {
DataRow("Encryption", "AES-256 [wdg:]")
}
Spacer(Modifier.height(24.dp))
}
}
}
@Composable
private fun DataRow(label: String, value: String) {
WgRow(label) {
WgValue(
text = value,
mono = true,
color = CText,
maxLines = 2,
modifier = Modifier.weight(1f, fill = false)
)
}
}