Improve server settings page

pull/116/head
NaiJi ✨ 2022-09-12 20:38:22 +03:00
parent b3ba7d959f
commit c5eed6ace9
7 changed files with 73 additions and 37 deletions

View File

@ -101,6 +101,13 @@
"month": "Month", "month": "Month",
"day": "Day", "day": "Day",
"hour": "Hour" "hour": "Hour"
},
"settings": {
"allow_autoupgrade": "Allow auto-upgrade",
"allow_autoupgrade_hint": "Allow automatic packages upgrades on server",
"reboot_after_upgrade": "Reboot after upgrade",
"reboot_after_upgrade_hint": "Reboot without prompt after applying changes on server",
"server_timezone": "Server timezone"
} }
}, },
"domain": { "domain": {

View File

@ -101,6 +101,13 @@
"month": "Месяц", "month": "Месяц",
"day": "День", "day": "День",
"hour": "Час" "hour": "Час"
},
"settings": {
"allow_autoupgrade": "Разрешить авто-обноления",
"allow_autoupgrade_hint": "Разрешить автоматичесую установку обновлений на сервер",
"reboot_after_upgrade": "Перезагружать после обновлений",
"reboot_after_upgrade_hint": "Автоматически перезагружать сервер после применения обновлений",
"server_timezone": "Часовой пояс сервера"
} }
}, },
"domain": { "domain": {

View File

@ -126,4 +126,26 @@ class ServerApi extends ApiMap
print(e); print(e);
} }
} }
Future<void> setAutoUpgradeSettings(
final bool allowReboot,
final bool enableAutoUpgrade,
) async {
try {
final GraphQLClient client = await getClient();
final input = Input$AutoUpgradeSettingsInput(
allowReboot: allowReboot,
enableAutoUpgrade: enableAutoUpgrade,
);
final variables = Variables$Mutation$ChangeAutoUpgradeSettings(
settings: input,
);
final mutation = Options$Mutation$ChangeAutoUpgradeSettings(
variables: variables,
);
await client.mutate$ChangeAutoUpgradeSettings(mutation);
} catch (e) {
print(e);
}
}
} }

View File

@ -603,13 +603,20 @@ class ServerApi extends ApiMap {
} }
Future<TimeZoneSettings> getServerTimezone() async { Future<TimeZoneSettings> getServerTimezone() async {
// I am not sure how to initialize TimeZoneSettings with default value... TimeZoneSettings settings = TimeZoneSettings();
final Dio client = await getClient(); final Dio client = await getClient();
final Response response = try {
await client.get('/system/configuration/timezone'); final Response response = await client.get(
close(client); '/system/configuration/timezone',
);
settings = TimeZoneSettings.fromString(response.data);
} catch (e) {
print(e);
} finally {
close(client);
}
return TimeZoneSettings.fromString(response.data); return settings;
} }
Future<void> updateServerTimezone(final TimeZoneSettings settings) async { Future<void> updateServerTimezone(final TimeZoneSettings settings) async {

View File

@ -5,17 +5,14 @@ import 'package:selfprivacy/logic/models/json/hetzner_server_info.dart';
import 'package:selfprivacy/logic/models/timezone_settings.dart'; import 'package:selfprivacy/logic/models/timezone_settings.dart';
class ServerDetailsRepository { class ServerDetailsRepository {
HetznerApi hetznerAPi = HetznerApi(); HetznerApi hetzner = HetznerApi();
ServerApi selfprivacyServer = ServerApi(); ServerApi server = ServerApi();
Future<ServerDetailsRepositoryDto> load() async { Future<ServerDetailsRepositoryDto> load() async => ServerDetailsRepositoryDto(
print('load'); autoUpgradeSettings: await server.getAutoUpgradeSettings(),
return ServerDetailsRepositoryDto( hetznerServerInfo: await hetzner.getInfo(),
autoUpgradeSettings: await selfprivacyServer.getAutoUpgradeSettings(), serverTimezone: await server.getServerTimezone(),
hetznerServerInfo: await hetznerAPi.getInfo(), );
serverTimezone: await selfprivacyServer.getServerTimezone(),
);
}
} }
class ServerDetailsRepositoryDto { class ServerDetailsRepositoryDto {

View File

@ -3,13 +3,16 @@ import 'package:timezone/timezone.dart';
class TimeZoneSettings { class TimeZoneSettings {
factory TimeZoneSettings.fromString(final String string) { factory TimeZoneSettings.fromString(final String string) {
final Location location = timeZoneDatabase.locations[string]!; final Location location = timeZoneDatabase.locations[string]!;
return TimeZoneSettings(location); return TimeZoneSettings(timezone: location);
} }
TimeZoneSettings(this.timezone); TimeZoneSettings({this.timezone});
final Location timezone; final Location? timezone;
Map<String, dynamic> toJson() => { Map<String, dynamic> toJson() => {
'timezone': timezone.name, 'timezone': timezone?.name ?? 'Unknown',
}; };
@override
String toString() => timezone?.name ?? 'Unknown';
} }

View File

@ -11,7 +11,7 @@ class _ServerSettings extends StatelessWidget {
Widget build(final BuildContext context) { Widget build(final BuildContext context) {
final serverDetailsState = context.watch<ServerDetailsCubit>().state; final serverDetailsState = context.watch<ServerDetailsCubit>().state;
if (serverDetailsState is ServerDetailsNotReady) { if (serverDetailsState is ServerDetailsNotReady) {
return const Text('not ready'); return Text('basis.loading'.tr());
} else if (serverDetailsState is! Loaded) { } else if (serverDetailsState is! Loaded) {
return BrandLoader.horizontal(); return BrandLoader.horizontal();
} }
@ -38,19 +38,17 @@ class _ServerSettings extends StatelessWidget {
SwitcherBlock( SwitcherBlock(
onChange: (final _) {}, onChange: (final _) {},
isActive: serverDetailsState.autoUpgradeSettings.enable, isActive: serverDetailsState.autoUpgradeSettings.enable,
child: const _TextColumn( child: _TextColumn(
title: 'Allow Auto-upgrade', title: 'providers.server.settings.allow_autoupgrade'.tr(),
value: 'Wether to allow automatic packages upgrades', value: 'providers.server.settings.allow_autoupgrade_hint'.tr(),
hasWarning: false,
), ),
), ),
SwitcherBlock( SwitcherBlock(
onChange: (final _) {}, onChange: (final _) {},
isActive: serverDetailsState.autoUpgradeSettings.allowReboot, isActive: serverDetailsState.autoUpgradeSettings.allowReboot,
child: const _TextColumn( child: _TextColumn(
title: 'Reboot after upgrade', title: 'providers.server.settings.reboot_after_upgrade'.tr(),
value: 'Reboot without prompt after applying updates', value: 'providers.server.settings.reboot_after_upgrade_hint'.tr(),
hasWarning: false,
), ),
), ),
_Button( _Button(
@ -58,9 +56,8 @@ class _ServerSettings extends StatelessWidget {
Navigator.of(context).push(materialRoute(const SelectTimezone())); Navigator.of(context).push(materialRoute(const SelectTimezone()));
}, },
child: _TextColumn( child: _TextColumn(
title: 'Server Timezone', title: 'providers.server.settings.server_timezone'.tr(),
value: serverDetailsState.serverTimezone.timezone.name, value: serverDetailsState.serverTimezone.toString(),
hasWarning: false,
), ),
), ),
], ],
@ -108,16 +105,12 @@ class _TextColumn extends StatelessWidget {
children: [ children: [
BrandText.body1( BrandText.body1(
title, title,
style: TextStyle(color: hasWarning ? BrandColors.warning : null), style: Theme.of(context).textTheme.bodyLarge,
), ),
const SizedBox(height: 5), const SizedBox(height: 5),
BrandText.body1( BrandText.body1(
value, value,
style: TextStyle( style: Theme.of(context).textTheme.bodyMedium,
fontSize: 13,
height: 1.53,
color: hasWarning ? BrandColors.warning : BrandColors.gray1,
),
), ),
], ],
); );