Hardcode production license server URL in app.

Remove editable server URL from settings; only admin token is configured by user.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Ivan Fontosh
2026-06-28 13:52:32 +08:00
parent 2037905976
commit f2e0c61010
3 changed files with 25 additions and 25 deletions
@@ -17,30 +17,29 @@ 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 serverUrl: String = SERVER_URL,
val adminToken: String = "",
) {
companion object {
const val DEFAULT_SERVER_URL = "https://license.mailib.ru/"
/** Продакшен-сервер лицензий (захардкожен, не меняется в UI). */
const val SERVER_URL = "https://license.mailib.ru/"
@Deprecated("Use SERVER_URL", ReplaceWith("SERVER_URL"))
const val DEFAULT_SERVER_URL = SERVER_URL
}
}
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,
serverUrl = AppSettings.SERVER_URL,
adminToken = prefs[adminTokenKey].orEmpty(),
)
}
suspend fun save(serverUrl: String, adminToken: String) {
suspend fun save(adminToken: String) {
context.dataStore.edit { prefs ->
prefs[serverUrlKey] = serverUrl.trim().let { url ->
if (url.endsWith("/")) url else "$url/"
}
prefs[adminTokenKey] = adminToken.trim()
}
}
@@ -37,7 +37,6 @@ data class GenerateKeyUiState(
)
data class SettingsUiState(
val serverUrl: String = AppSettings.DEFAULT_SERVER_URL,
val adminToken: String = "",
val savedMessage: String? = null,
)
@@ -62,10 +61,7 @@ class LicenseViewModel(application: Application) : AndroidViewModel(application)
viewModelScope.launch {
settings.collect { appSettings ->
_settingsState.update {
it.copy(
serverUrl = appSettings.serverUrl,
adminToken = appSettings.adminToken,
)
it.copy(adminToken = appSettings.adminToken)
}
}
}
@@ -159,16 +155,16 @@ class LicenseViewModel(application: Application) : AndroidViewModel(application)
}
}
fun updateSettingsDraft(serverUrl: String, adminToken: String) {
fun updateAdminToken(value: String) {
_settingsState.update {
it.copy(serverUrl = serverUrl, adminToken = adminToken, savedMessage = null)
it.copy(adminToken = value, savedMessage = null)
}
}
fun saveSettings() {
val draft = _settingsState.value
viewModelScope.launch {
settingsRepository.save(draft.serverUrl, draft.adminToken)
settingsRepository.save(draft.adminToken)
_settingsState.update { it.copy(savedMessage = "Сохранено") }
}
}
@@ -16,6 +16,7 @@ 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.data.AppSettings
import ru.mailib.ttrpg.licensemanager.ui.LicenseViewModel
@Composable
@@ -29,23 +30,27 @@ fun SettingsScreen(viewModel: LicenseViewModel, onBack: () -> Unit) {
verticalArrangement = Arrangement.spacedBy(12.dp),
) {
Text(
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,
Text(
text = "Сервер: ${AppSettings.SERVER_URL}",
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
Text(
text = "Токен администратора (LICENSE_ADMIN_TOKEN) — см. инструкцию в README или спросите у администратора сервера.",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
OutlinedTextField(
value = state.adminToken,
onValueChange = { value -> viewModel.updateSettingsDraft(state.serverUrl, value) },
label = { Text("Admin token (LICENSE_ADMIN_TOKEN)") },
onValueChange = viewModel::updateAdminToken,
label = { Text("Admin token") },
modifier = Modifier.fillMaxWidth(),
singleLine = true,
visualTransformation = PasswordVisualTransformation(),