Initial Android app: license list and key generation.
Kotlin Compose admin client for TTRPG Player license server with branding and settings for server URL and admin token. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:icon="@drawable/ic_app_logo"
|
||||
android:label="@string/app_name"
|
||||
android:roundIcon="@drawable/ic_app_logo"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/Theme.TTRPGLicenseManager"
|
||||
android:usesCleartextTraffic="true">
|
||||
<activity
|
||||
android:name=".MainActivity"
|
||||
android:exported="true"
|
||||
android:theme="@style/Theme.TTRPGLicenseManager">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
@@ -0,0 +1,125 @@
|
||||
package ru.mailib.ttrpg.licensemanager
|
||||
|
||||
import android.os.Bundle
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Row
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.Add
|
||||
import androidx.compose.material.icons.filled.List
|
||||
import androidx.compose.material.icons.filled.Settings
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.NavigationBar
|
||||
import androidx.compose.material3.NavigationBarItem
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import androidx.navigation.compose.NavHost
|
||||
import androidx.navigation.compose.composable
|
||||
import androidx.navigation.compose.currentBackStackEntryAsState
|
||||
import androidx.navigation.compose.rememberNavController
|
||||
import ru.mailib.ttrpg.licensemanager.ui.AppLogo
|
||||
import ru.mailib.ttrpg.licensemanager.ui.LicenseManagerTheme
|
||||
import ru.mailib.ttrpg.licensemanager.ui.LicenseViewModel
|
||||
import ru.mailib.ttrpg.licensemanager.ui.screens.GenerateKeyScreen
|
||||
import ru.mailib.ttrpg.licensemanager.ui.screens.LicenseListScreen
|
||||
import ru.mailib.ttrpg.licensemanager.ui.screens.SettingsScreen
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
enableEdgeToEdge()
|
||||
setContent {
|
||||
LicenseManagerTheme {
|
||||
LicenseManagerApp()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private object Routes {
|
||||
const val LIST = "list"
|
||||
const val GENERATE = "generate"
|
||||
const val SETTINGS = "settings"
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun LicenseManagerApp(viewModel: LicenseViewModel = viewModel()) {
|
||||
val navController = rememberNavController()
|
||||
val backStack by navController.currentBackStackEntryAsState()
|
||||
val currentRoute = backStack?.destination?.route
|
||||
|
||||
val topLevelRoutes = setOf(Routes.LIST, Routes.GENERATE)
|
||||
val showBottomBar = currentRoute in topLevelRoutes
|
||||
|
||||
Scaffold(
|
||||
topBar = {
|
||||
TopAppBar(
|
||||
title = {
|
||||
Row(
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
horizontalArrangement = Arrangement.spacedBy(12.dp),
|
||||
) {
|
||||
AppLogo(size = 32.dp)
|
||||
Text(stringResource(R.string.app_name))
|
||||
}
|
||||
},
|
||||
actions = {
|
||||
IconButton(onClick = { navController.navigate(Routes.SETTINGS) }) {
|
||||
Icon(Icons.Default.Settings, contentDescription = stringResource(R.string.settings))
|
||||
}
|
||||
},
|
||||
)
|
||||
},
|
||||
bottomBar = {
|
||||
if (showBottomBar) {
|
||||
NavigationBar {
|
||||
NavigationBarItem(
|
||||
selected = currentRoute == Routes.LIST,
|
||||
onClick = { navController.navigate(Routes.LIST) },
|
||||
icon = { Icon(Icons.Default.List, contentDescription = null) },
|
||||
label = { Text(stringResource(R.string.tab_licenses)) },
|
||||
)
|
||||
NavigationBarItem(
|
||||
selected = currentRoute == Routes.GENERATE,
|
||||
onClick = { navController.navigate(Routes.GENERATE) },
|
||||
icon = { Icon(Icons.Default.Add, contentDescription = null) },
|
||||
label = { Text(stringResource(R.string.tab_generate)) },
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
) { padding ->
|
||||
NavHost(
|
||||
navController = navController,
|
||||
startDestination = Routes.LIST,
|
||||
modifier = Modifier.padding(padding),
|
||||
) {
|
||||
composable(Routes.LIST) {
|
||||
LicenseListScreen(viewModel)
|
||||
}
|
||||
composable(Routes.GENERATE) {
|
||||
GenerateKeyScreen(viewModel)
|
||||
}
|
||||
composable(Routes.SETTINGS) {
|
||||
SettingsScreen(
|
||||
viewModel = viewModel,
|
||||
onBack = { navController.popBackStack() },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package ru.mailib.ttrpg.licensemanager.data
|
||||
|
||||
import retrofit2.http.Body
|
||||
import retrofit2.http.GET
|
||||
import retrofit2.http.POST
|
||||
|
||||
interface LicenseApi {
|
||||
@GET("v1/admin/licenses")
|
||||
suspend fun listLicenses(): LicensesResponse
|
||||
|
||||
@POST("v1/admin/product-keys")
|
||||
suspend fun createProductKey(@Body body: CreateProductKeyRequest): ProductKeyResponse
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package ru.mailib.ttrpg.licensemanager.data
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
data class LicenseEntry(
|
||||
val key: String,
|
||||
val sub: String,
|
||||
val pid: String,
|
||||
val maxDevices: Int,
|
||||
val expiresAtSec: Long,
|
||||
val revoked: Boolean,
|
||||
val activatedDevices: List<String>,
|
||||
val activatedCount: Int,
|
||||
)
|
||||
|
||||
data class LicensesResponse(
|
||||
val licenses: List<LicenseEntry>,
|
||||
)
|
||||
|
||||
data class CreateProductKeyRequest(
|
||||
val pid: String = "dnd_player",
|
||||
val maxDevices: Int,
|
||||
val expiresAtSec: Long,
|
||||
)
|
||||
|
||||
data class ProductKeyResponse(
|
||||
val key: String,
|
||||
val sub: String,
|
||||
val pid: String,
|
||||
val maxDevices: Int,
|
||||
val expiresAtSec: Long,
|
||||
)
|
||||
|
||||
data class ApiErrorBody(
|
||||
@SerializedName("error") val error: String?,
|
||||
)
|
||||
@@ -0,0 +1,90 @@
|
||||
package ru.mailib.ttrpg.licensemanager.data
|
||||
|
||||
import android.content.Context
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.preferences.core.Preferences
|
||||
import androidx.datastore.preferences.core.edit
|
||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||
import androidx.datastore.preferences.preferencesDataStore
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
|
||||
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")
|
||||
|
||||
data class AppSettings(
|
||||
val serverUrl: String = DEFAULT_SERVER_URL,
|
||||
val adminToken: String = "",
|
||||
) {
|
||||
companion object {
|
||||
const val DEFAULT_SERVER_URL = "https://license.mailib.ru/"
|
||||
}
|
||||
}
|
||||
|
||||
class SettingsRepository(private val context: Context) {
|
||||
private val serverUrlKey = stringPreferencesKey("server_url")
|
||||
private val adminTokenKey = stringPreferencesKey("admin_token")
|
||||
|
||||
val settings: Flow<AppSettings> = context.dataStore.data.map { prefs ->
|
||||
AppSettings(
|
||||
serverUrl = prefs[serverUrlKey] ?: AppSettings.DEFAULT_SERVER_URL,
|
||||
adminToken = prefs[adminTokenKey].orEmpty(),
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun save(serverUrl: String, adminToken: String) {
|
||||
context.dataStore.edit { prefs ->
|
||||
prefs[serverUrlKey] = serverUrl.trim().let { url ->
|
||||
if (url.endsWith("/")) url else "$url/"
|
||||
}
|
||||
prefs[adminTokenKey] = adminToken.trim()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class LicenseRepository(
|
||||
private val settingsRepository: SettingsRepository,
|
||||
) {
|
||||
suspend fun listLicenses(settings: AppSettings): Result<List<LicenseEntry>> = runCatching {
|
||||
createApi(settings).listLicenses().licenses
|
||||
}
|
||||
|
||||
suspend fun createProductKey(
|
||||
settings: AppSettings,
|
||||
request: CreateProductKeyRequest,
|
||||
): Result<ProductKeyResponse> = runCatching {
|
||||
createApi(settings).createProductKey(request)
|
||||
}
|
||||
|
||||
private fun createApi(settings: AppSettings): LicenseApi {
|
||||
require(settings.adminToken.isNotBlank()) { "Укажите admin token в настройках" }
|
||||
val baseUrl = settings.serverUrl
|
||||
|
||||
val authInterceptor = Interceptor { chain ->
|
||||
val request = chain.request().newBuilder()
|
||||
.header("Authorization", "Bearer ${settings.adminToken}")
|
||||
.build()
|
||||
chain.proceed(request)
|
||||
}
|
||||
|
||||
val logging = HttpLoggingInterceptor().apply {
|
||||
level = HttpLoggingInterceptor.Level.BASIC
|
||||
}
|
||||
|
||||
val client = OkHttpClient.Builder()
|
||||
.addInterceptor(authInterceptor)
|
||||
.addInterceptor(logging)
|
||||
.build()
|
||||
|
||||
return Retrofit.Builder()
|
||||
.baseUrl(baseUrl)
|
||||
.client(client)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build()
|
||||
.create(LicenseApi::class.java)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package ru.mailib.ttrpg.licensemanager.ui
|
||||
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.unit.Dp
|
||||
import ru.mailib.ttrpg.licensemanager.R
|
||||
|
||||
@Composable
|
||||
fun AppLogo(modifier: Modifier = Modifier, size: Dp) {
|
||||
Icon(
|
||||
painter = painterResource(R.drawable.ic_app_logo),
|
||||
contentDescription = null,
|
||||
modifier = modifier.size(size),
|
||||
tint = androidx.compose.ui.graphics.Color.Unspecified,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
package ru.mailib.ttrpg.licensemanager.ui
|
||||
|
||||
import android.app.Application
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import ru.mailib.ttrpg.licensemanager.data.AppSettings
|
||||
import ru.mailib.ttrpg.licensemanager.data.CreateProductKeyRequest
|
||||
import ru.mailib.ttrpg.licensemanager.data.LicenseEntry
|
||||
import ru.mailib.ttrpg.licensemanager.data.LicenseRepository
|
||||
import ru.mailib.ttrpg.licensemanager.data.ProductKeyResponse
|
||||
import ru.mailib.ttrpg.licensemanager.data.SettingsRepository
|
||||
import java.time.Instant
|
||||
import java.time.LocalDate
|
||||
import java.time.ZoneOffset
|
||||
|
||||
data class LicenseListUiState(
|
||||
val licenses: List<LicenseEntry> = emptyList(),
|
||||
val isLoading: Boolean = false,
|
||||
val error: String? = null,
|
||||
)
|
||||
|
||||
data class GenerateKeyUiState(
|
||||
val pid: String = "dnd_player",
|
||||
val maxDevices: String = "3",
|
||||
val expiryDate: LocalDate = LocalDate.of(2027, 12, 31),
|
||||
val isSubmitting: Boolean = false,
|
||||
val error: String? = null,
|
||||
val createdKey: ProductKeyResponse? = null,
|
||||
)
|
||||
|
||||
data class SettingsUiState(
|
||||
val serverUrl: String = AppSettings.DEFAULT_SERVER_URL,
|
||||
val adminToken: String = "",
|
||||
val savedMessage: String? = null,
|
||||
)
|
||||
|
||||
class LicenseViewModel(application: Application) : AndroidViewModel(application) {
|
||||
private val settingsRepository = SettingsRepository(application)
|
||||
private val licenseRepository = LicenseRepository(settingsRepository)
|
||||
|
||||
val settings: StateFlow<AppSettings> = settingsRepository.settings
|
||||
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), AppSettings())
|
||||
|
||||
private val _listState = MutableStateFlow(LicenseListUiState())
|
||||
val listState: StateFlow<LicenseListUiState> = _listState.asStateFlow()
|
||||
|
||||
private val _generateState = MutableStateFlow(GenerateKeyUiState())
|
||||
val generateState: StateFlow<GenerateKeyUiState> = _generateState.asStateFlow()
|
||||
|
||||
private val _settingsState = MutableStateFlow(SettingsUiState())
|
||||
val settingsState: StateFlow<SettingsUiState> = _settingsState.asStateFlow()
|
||||
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
settings.collect { appSettings ->
|
||||
_settingsState.update {
|
||||
it.copy(
|
||||
serverUrl = appSettings.serverUrl,
|
||||
adminToken = appSettings.adminToken,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun loadLicenses() {
|
||||
viewModelScope.launch {
|
||||
val currentSettings = settings.value
|
||||
_listState.update { it.copy(isLoading = true, error = null) }
|
||||
licenseRepository.listLicenses(currentSettings)
|
||||
.onSuccess { licenses ->
|
||||
_listState.update { it.copy(isLoading = false, licenses = licenses, error = null) }
|
||||
}
|
||||
.onFailure { e ->
|
||||
_listState.update { it.copy(isLoading = false, error = e.message ?: "Ошибка загрузки") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun updatePid(value: String) {
|
||||
_generateState.update { it.copy(pid = value, error = null) }
|
||||
}
|
||||
|
||||
fun updateMaxDevices(value: String) {
|
||||
_generateState.update { it.copy(maxDevices = value.filter { ch -> ch.isDigit() }.take(2), error = null) }
|
||||
}
|
||||
|
||||
fun updateExpiryDate(date: LocalDate) {
|
||||
_generateState.update { it.copy(expiryDate = date, error = null) }
|
||||
}
|
||||
|
||||
fun clearCreatedKey() {
|
||||
_generateState.update { it.copy(createdKey = null) }
|
||||
}
|
||||
|
||||
fun generateKey() {
|
||||
val state = _generateState.value
|
||||
val maxDevices = state.maxDevices.toIntOrNull()
|
||||
if (maxDevices == null || maxDevices < 1) {
|
||||
_generateState.update { it.copy(error = "Укажите число устройств (минимум 1)") }
|
||||
return
|
||||
}
|
||||
if (state.pid.isBlank()) {
|
||||
_generateState.update { it.copy(error = "Укажите product id") }
|
||||
return
|
||||
}
|
||||
|
||||
val expiresAtSec = state.expiryDate
|
||||
.plusDays(1)
|
||||
.atStartOfDay()
|
||||
.toEpochSecond(ZoneOffset.UTC) - 1
|
||||
|
||||
viewModelScope.launch {
|
||||
_generateState.update { it.copy(isSubmitting = true, error = null, createdKey = null) }
|
||||
licenseRepository.createProductKey(
|
||||
settings.value,
|
||||
CreateProductKeyRequest(
|
||||
pid = state.pid.trim(),
|
||||
maxDevices = maxDevices,
|
||||
expiresAtSec = expiresAtSec,
|
||||
),
|
||||
)
|
||||
.onSuccess { created ->
|
||||
_generateState.update { it.copy(isSubmitting = false, createdKey = created) }
|
||||
loadLicenses()
|
||||
}
|
||||
.onFailure { e ->
|
||||
_generateState.update {
|
||||
it.copy(isSubmitting = false, error = e.message ?: "Не удалось создать ключ")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun updateSettingsDraft(serverUrl: String, adminToken: String) {
|
||||
_settingsState.update {
|
||||
it.copy(serverUrl = serverUrl, adminToken = adminToken, savedMessage = null)
|
||||
}
|
||||
}
|
||||
|
||||
fun saveSettings() {
|
||||
val draft = _settingsState.value
|
||||
viewModelScope.launch {
|
||||
settingsRepository.save(draft.serverUrl, draft.adminToken)
|
||||
_settingsState.update { it.copy(savedMessage = "Сохранено") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun formatEpochSec(sec: Long): String =
|
||||
Instant.ofEpochSecond(sec).atZone(ZoneOffset.UTC).toLocalDate().toString()
|
||||
@@ -0,0 +1,35 @@
|
||||
package ru.mailib.ttrpg.licensemanager.ui
|
||||
|
||||
import androidx.compose.foundation.isSystemInDarkTheme
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.darkColorScheme
|
||||
import androidx.compose.material3.lightColorScheme
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.graphics.Color
|
||||
|
||||
private val BrandPurple = Color(0xFF8B5CF6)
|
||||
private val BrandPurpleDark = Color(0xFF6D28D9)
|
||||
|
||||
private val LightColors = lightColorScheme(
|
||||
primary = BrandPurple,
|
||||
onPrimary = Color.White,
|
||||
primaryContainer = Color(0xFFEDE9FE),
|
||||
onPrimaryContainer = BrandPurpleDark,
|
||||
secondary = Color(0xFF64748B),
|
||||
)
|
||||
|
||||
private val DarkColors = darkColorScheme(
|
||||
primary = BrandPurple,
|
||||
onPrimary = Color.White,
|
||||
primaryContainer = BrandPurpleDark,
|
||||
onPrimaryContainer = Color(0xFFEDE9FE),
|
||||
secondary = Color(0xFF94A3B8),
|
||||
)
|
||||
|
||||
@Composable
|
||||
fun LicenseManagerTheme(content: @Composable () -> Unit) {
|
||||
MaterialTheme(
|
||||
colorScheme = if (isSystemInDarkTheme()) DarkColors else LightColors,
|
||||
content = content,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
package ru.mailib.ttrpg.licensemanager.ui.screens
|
||||
|
||||
import android.app.DatePickerDialog
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.ContentCopy
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CardDefaults
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedButton
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalClipboardManager
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.text.AnnotatedString
|
||||
import androidx.compose.ui.text.font.FontFamily
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import ru.mailib.ttrpg.licensemanager.ui.LicenseViewModel
|
||||
import ru.mailib.ttrpg.licensemanager.ui.formatEpochSec
|
||||
import java.time.LocalDate
|
||||
import java.util.Calendar
|
||||
|
||||
@Composable
|
||||
fun GenerateKeyScreen(viewModel: LicenseViewModel) {
|
||||
val state by viewModel.generateState.collectAsStateWithLifecycle()
|
||||
val context = LocalContext.current
|
||||
val clipboard = LocalClipboardManager.current
|
||||
|
||||
val showDatePicker = remember(state.expiryDate) {
|
||||
{
|
||||
val cal = Calendar.getInstance().apply {
|
||||
set(state.expiryDate.year, state.expiryDate.monthValue - 1, state.expiryDate.dayOfMonth)
|
||||
}
|
||||
DatePickerDialog(
|
||||
context,
|
||||
{ _, year, month, day ->
|
||||
viewModel.updateExpiryDate(LocalDate.of(year, month + 1, day))
|
||||
},
|
||||
cal.get(Calendar.YEAR),
|
||||
cal.get(Calendar.MONTH),
|
||||
cal.get(Calendar.DAY_OF_MONTH),
|
||||
).show()
|
||||
}
|
||||
}
|
||||
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.verticalScroll(rememberScrollState())
|
||||
.padding(16.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp),
|
||||
) {
|
||||
Text(
|
||||
text = "Новый продуктовый ключ",
|
||||
style = MaterialTheme.typography.headlineSmall,
|
||||
fontWeight = FontWeight.SemiBold,
|
||||
)
|
||||
Text(
|
||||
text = "Ключ будет добавлен в data.json на сервере. Передайте его пользователю для активации в TTRPG Player.",
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
|
||||
OutlinedTextField(
|
||||
value = state.pid,
|
||||
onValueChange = viewModel::updatePid,
|
||||
label = { Text("Product ID (pid)") },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
singleLine = true,
|
||||
)
|
||||
|
||||
OutlinedTextField(
|
||||
value = state.maxDevices,
|
||||
onValueChange = viewModel::updateMaxDevices,
|
||||
label = { Text("Макс. устройств") },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
singleLine = true,
|
||||
)
|
||||
|
||||
OutlinedButton(onClick = showDatePicker, modifier = Modifier.fillMaxWidth()) {
|
||||
Text("Срок действия: ${state.expiryDate}")
|
||||
}
|
||||
|
||||
if (state.error != null) {
|
||||
ErrorMessage(message = state.error ?: "")
|
||||
}
|
||||
|
||||
Button(
|
||||
onClick = viewModel::generateKey,
|
||||
enabled = !state.isSubmitting,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
) {
|
||||
if (state.isSubmitting) {
|
||||
CircularProgressIndicator(
|
||||
modifier = Modifier.height(20.dp),
|
||||
strokeWidth = 2.dp,
|
||||
)
|
||||
} else {
|
||||
Text("Сгенерировать ключ")
|
||||
}
|
||||
}
|
||||
|
||||
state.createdKey?.let { created ->
|
||||
Spacer(modifier = Modifier.height(8.dp))
|
||||
Card(
|
||||
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.primaryContainer),
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
) {
|
||||
Column(modifier = Modifier.padding(16.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
Text("Ключ создан", style = MaterialTheme.typography.titleMedium, fontWeight = FontWeight.SemiBold)
|
||||
Text(
|
||||
text = created.key,
|
||||
fontFamily = FontFamily.Monospace,
|
||||
style = MaterialTheme.typography.bodyLarge,
|
||||
)
|
||||
Text("sub: ${created.sub}")
|
||||
Text("Срок до: ${formatEpochSec(created.expiresAtSec)}")
|
||||
IconButton(
|
||||
onClick = { clipboard.setText(AnnotatedString(created.key)) },
|
||||
) {
|
||||
Icon(Icons.Default.ContentCopy, contentDescription = "Копировать ключ")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
package ru.mailib.ttrpg.licensemanager.ui.screens
|
||||
|
||||
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.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.material.ExperimentalMaterialApi
|
||||
import androidx.compose.material.pullrefresh.PullRefreshIndicator
|
||||
import androidx.compose.material.pullrefresh.pullRefresh
|
||||
import androidx.compose.material.pullrefresh.rememberPullRefreshState
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CardDefaults
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.text.font.FontFamily
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import ru.mailib.ttrpg.licensemanager.data.LicenseEntry
|
||||
import ru.mailib.ttrpg.licensemanager.ui.LicenseViewModel
|
||||
import ru.mailib.ttrpg.licensemanager.ui.formatEpochSec
|
||||
|
||||
@OptIn(ExperimentalMaterialApi::class)
|
||||
@Composable
|
||||
fun LicenseListScreen(viewModel: LicenseViewModel) {
|
||||
val state by viewModel.listState.collectAsStateWithLifecycle()
|
||||
val refreshState = rememberPullRefreshState(
|
||||
refreshing = state.isLoading,
|
||||
onRefresh = viewModel::loadLicenses,
|
||||
)
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
viewModel.loadLicenses()
|
||||
}
|
||||
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.pullRefresh(refreshState),
|
||||
) {
|
||||
when {
|
||||
state.error != null && state.licenses.isEmpty() -> {
|
||||
ErrorMessage(
|
||||
message = state.error ?: "",
|
||||
modifier = Modifier.align(Alignment.Center),
|
||||
)
|
||||
}
|
||||
|
||||
state.licenses.isEmpty() && state.isLoading -> {
|
||||
CircularProgressIndicator(modifier = Modifier.align(Alignment.Center))
|
||||
}
|
||||
|
||||
state.licenses.isEmpty() -> {
|
||||
Text(
|
||||
text = "Лицензий пока нет",
|
||||
modifier = Modifier.align(Alignment.Center),
|
||||
style = MaterialTheme.typography.bodyLarge,
|
||||
)
|
||||
}
|
||||
|
||||
else -> {
|
||||
LazyColumn(
|
||||
contentPadding = PaddingValues(16.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp),
|
||||
) {
|
||||
items(state.licenses, key = { it.sub }) { license ->
|
||||
LicenseCard(license)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PullRefreshIndicator(
|
||||
refreshing = state.isLoading,
|
||||
state = refreshState,
|
||||
modifier = Modifier.align(Alignment.TopCenter),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun LicenseCard(license: LicenseEntry) {
|
||||
Card(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
colors = CardDefaults.cardColors(
|
||||
containerColor = if (license.revoked) {
|
||||
MaterialTheme.colorScheme.errorContainer
|
||||
} else {
|
||||
MaterialTheme.colorScheme.surfaceContainerHigh
|
||||
},
|
||||
),
|
||||
) {
|
||||
Column(modifier = Modifier.padding(16.dp), verticalArrangement = Arrangement.spacedBy(6.dp)) {
|
||||
Text(
|
||||
text = license.key,
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
fontFamily = FontFamily.Monospace,
|
||||
fontWeight = FontWeight.SemiBold,
|
||||
)
|
||||
DetailRow("sub", license.sub)
|
||||
DetailRow("pid", license.pid)
|
||||
DetailRow("Устройства", "${license.activatedCount} / ${license.maxDevices}")
|
||||
DetailRow("Срок до", formatEpochSec(license.expiresAtSec))
|
||||
DetailRow("Статус", if (license.revoked) "Отозвана" else "Активна")
|
||||
if (license.activatedDevices.isNotEmpty()) {
|
||||
DetailRow("deviceId", license.activatedDevices.joinToString(", "))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun DetailRow(label: String, value: String) {
|
||||
Text(
|
||||
text = "$label: $value",
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun ErrorMessage(message: String, modifier: Modifier = Modifier) {
|
||||
Surface(
|
||||
modifier = modifier.padding(24.dp),
|
||||
color = MaterialTheme.colorScheme.errorContainer,
|
||||
shape = MaterialTheme.shapes.medium,
|
||||
) {
|
||||
Text(
|
||||
text = message,
|
||||
modifier = Modifier.padding(16.dp),
|
||||
color = MaterialTheme.colorScheme.onErrorContainer,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package ru.mailib.ttrpg.licensemanager.ui.screens
|
||||
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.input.PasswordVisualTransformation
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import ru.mailib.ttrpg.licensemanager.ui.LicenseViewModel
|
||||
|
||||
@Composable
|
||||
fun SettingsScreen(viewModel: LicenseViewModel, onBack: () -> Unit) {
|
||||
val state by viewModel.settingsState.collectAsStateWithLifecycle()
|
||||
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.padding(16.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp),
|
||||
) {
|
||||
Text(
|
||||
text = "Настройки сервера",
|
||||
style = MaterialTheme.typography.headlineSmall,
|
||||
fontWeight = FontWeight.SemiBold,
|
||||
)
|
||||
|
||||
OutlinedTextField(
|
||||
value = state.serverUrl,
|
||||
onValueChange = { value -> viewModel.updateSettingsDraft(value, state.adminToken) },
|
||||
label = { Text("URL сервера") },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
singleLine = true,
|
||||
)
|
||||
|
||||
OutlinedTextField(
|
||||
value = state.adminToken,
|
||||
onValueChange = { value -> viewModel.updateSettingsDraft(state.serverUrl, value) },
|
||||
label = { Text("Admin token (LICENSE_ADMIN_TOKEN)") },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
singleLine = true,
|
||||
visualTransformation = PasswordVisualTransformation(),
|
||||
)
|
||||
|
||||
if (state.savedMessage != null) {
|
||||
Text(
|
||||
text = state.savedMessage ?: "",
|
||||
color = MaterialTheme.colorScheme.primary,
|
||||
)
|
||||
}
|
||||
|
||||
Button(
|
||||
onClick = {
|
||||
viewModel.saveSettings()
|
||||
onBack()
|
||||
},
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
) {
|
||||
Text("Сохранить")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<svg width="32" height="32" viewBox="0 0 32 32" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||||
<rect width="32" height="32" rx="12" fill="#8B5CF6" />
|
||||
<path
|
||||
d="M9.33333 17.6667C9.01146 17.6678 8.71773 17.4834 8.57879 17.193C8.43985 16.9027 8.48055 16.5583 8.68333 16.3083L16.9333 7.80833C17.0608 7.66121 17.2731 7.62194 17.4448 7.71375C17.6164 7.80556 17.7016 8.00398 17.65 8.19167L16.05 13.2083C15.9542 13.4646 15.9904 13.7516 16.1467 13.9762C16.3031 14.2007 16.5597 14.3342 16.8333 14.3333H22.6667C22.9885 14.3322 23.2823 14.5166 23.4212 14.807C23.5601 15.0973 23.5195 15.4417 23.3167 15.6917L15.0667 24.1917C14.9392 24.3388 14.7269 24.3781 14.5552 24.2862C14.3836 24.1944 14.2984 23.996 14.35 23.8083L15.95 18.7917C16.0458 18.5354 16.0096 18.2484 15.8533 18.0238C15.6969 17.7993 15.4403 17.6658 15.1667 17.6667H9.33333"
|
||||
stroke="white"
|
||||
stroke-width="1.66667"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 944 B |
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="108dp"
|
||||
android:height="108dp"
|
||||
android:viewportWidth="32"
|
||||
android:viewportHeight="32">
|
||||
<path
|
||||
android:fillColor="#8B5CF6"
|
||||
android:pathData="M0,0h32v32h-32z" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M9.333,17.667c-0.322,0.002 -0.615,-0.182 -0.754,-0.474c-0.139,-0.291 -0.098,-0.635 0.104,-0.885l8.25,-8.5c0.128,-0.147 0.34,-0.186 0.512,-0.094c0.172,0.092 0.257,0.29 0.206,0.478l-1.6,5.017c-0.096,0.256 -0.06,0.543 0.097,0.768c0.156,0.225 0.413,0.358 0.687,0.357h5.833c0.322,-0.001 0.616,0.183 0.755,0.474c0.139,0.29 0.098,0.635 -0.105,0.885l-8.25,8.5c-0.127,0.147 -0.34,0.186 -0.512,0.094c-0.172,-0.092 -0.257,-0.29 -0.206,-0.478l1.6,-5.017c0.096,-0.256 0.06,-0.543 -0.097,-0.768c-0.156,-0.225 -0.413,-0.358 -0.687,-0.357z"
|
||||
android:strokeWidth="1.667"
|
||||
android:strokeColor="#FFFFFF"
|
||||
android:strokeLineCap="round"
|
||||
android:strokeLineJoin="round" />
|
||||
</vector>
|
||||
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="108dp"
|
||||
android:height="108dp"
|
||||
android:viewportWidth="108"
|
||||
android:viewportHeight="108">
|
||||
<path
|
||||
android:fillColor="#8B5CF6"
|
||||
android:pathData="M0,0h108v108h-108z" />
|
||||
</vector>
|
||||
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="108dp"
|
||||
android:height="108dp"
|
||||
android:viewportWidth="32"
|
||||
android:viewportHeight="32">
|
||||
<group
|
||||
android:translateX="38"
|
||||
android:translateY="38"
|
||||
android:scaleX="1"
|
||||
android:scaleY="1">
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M9.333,17.667c-0.322,0.002 -0.615,-0.182 -0.754,-0.474c-0.139,-0.291 -0.098,-0.635 0.104,-0.885l8.25,-8.5c0.128,-0.147 0.34,-0.186 0.512,-0.094c0.172,0.092 0.257,0.29 0.206,0.478l-1.6,5.017c-0.096,0.256 -0.06,0.543 0.097,0.768c0.156,0.225 0.413,0.358 0.687,0.357h5.833c0.322,-0.001 0.616,0.183 0.755,0.474c0.139,0.29 0.098,0.635 -0.105,0.885l-8.25,8.5c-0.127,0.147 -0.34,0.186 -0.512,0.094c-0.172,-0.092 -0.257,-0.29 -0.206,-0.478l1.6,-5.017c0.096,-0.256 0.06,-0.543 -0.097,-0.768c-0.156,-0.225 -0.413,-0.358 -0.687,-0.357z"
|
||||
android:strokeWidth="1.667"
|
||||
android:strokeColor="#FFFFFF"
|
||||
android:strokeLineCap="round"
|
||||
android:strokeLineJoin="round" />
|
||||
</group>
|
||||
</vector>
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<background android:drawable="@drawable/ic_launcher_background" />
|
||||
<foreground android:drawable="@drawable/ic_launcher_foreground" />
|
||||
</adaptive-icon>
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<background android:drawable="@drawable/ic_launcher_background" />
|
||||
<foreground android:drawable="@drawable/ic_launcher_foreground" />
|
||||
</adaptive-icon>
|
||||
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="app_name">TTRPG License Manager</string>
|
||||
<string name="tab_licenses">Лицензии</string>
|
||||
<string name="tab_generate">Новый ключ</string>
|
||||
<string name="settings">Настройки</string>
|
||||
</resources>
|
||||
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<style name="Theme.TTRPGLicenseManager" parent="android:Theme.Material.Light.NoActionBar" />
|
||||
</resources>
|
||||
Reference in New Issue
Block a user