refactor(ui): Move all pop up dialogs into general utils function

To not import get_it everywhere and encapsulate all the related dirt into utils.
pull/140/head
NaiJi ✨ 2022-11-09 19:49:37 +04:00
parent 6eb49fa8f1
commit cdc47ecdb3
6 changed files with 119 additions and 146 deletions

View File

@ -31,7 +31,8 @@
"remove": "Remove", "remove": "Remove",
"apply": "Apply", "apply": "Apply",
"done": "Done", "done": "Done",
"continue": "Continue" "continue": "Continue",
"alert": "Alert"
}, },
"more_page": { "more_page": {
"configuration_wizard": "Setup wizard", "configuration_wizard": "Setup wizard",

View File

@ -31,7 +31,8 @@
"remove": "Удалить", "remove": "Удалить",
"apply": "Применить", "apply": "Применить",
"done": "Готово", "done": "Готово",
"continue": "Продолжить" "continue": "Продолжить",
"alert": "Уведомление"
}, },
"more_page": { "more_page": {
"configuration_wizard": "Мастер настройки", "configuration_wizard": "Мастер настройки",

View File

@ -27,8 +27,7 @@ import 'package:selfprivacy/logic/models/json/dns_records.dart';
import 'package:selfprivacy/logic/models/message.dart'; import 'package:selfprivacy/logic/models/message.dart';
import 'package:selfprivacy/logic/models/server_basic_info.dart'; import 'package:selfprivacy/logic/models/server_basic_info.dart';
import 'package:selfprivacy/logic/models/server_type.dart'; import 'package:selfprivacy/logic/models/server_type.dart';
import 'package:selfprivacy/ui/components/action_button/action_button.dart'; import 'package:selfprivacy/ui/helpers/modals.dart';
import 'package:selfprivacy/ui/components/brand_alert/brand_alert.dart';
import 'package:selfprivacy/utils/network_utils.dart'; import 'package:selfprivacy/utils/network_utils.dart';
class IpNotFoundException implements Exception { class IpNotFoundException implements Exception {
@ -262,84 +261,62 @@ class ServerInstallationRepository {
onSuccess(serverDetails); onSuccess(serverDetails);
} on DioError catch (e) { } on DioError catch (e) {
if (e.response!.data['error']['code'] == 'uniqueness_error') { if (e.response!.data['error']['code'] == 'uniqueness_error') {
final NavigationService nav = getIt.get<NavigationService>(); showPopUpAlert(
nav.showPopUpDialog( alertTitle: 'modals.already_exists'.tr(),
BrandAlert( description: 'modals.destroy_server'.tr(),
title: 'modals.already_exists'.tr(), actionButtonTitle: 'modals.yes'.tr(),
contentText: 'modals.destroy_server'.tr(), actionButtonOnPressed: () async {
actions: [ await api.deleteServer(
ActionButton( domainName: domainName,
text: 'basis.delete'.tr(), );
isRed: true,
onPressed: () async {
await api.deleteServer(
domainName: domainName,
);
ServerHostingDetails? serverDetails; ServerHostingDetails? serverDetails;
try { try {
serverDetails = await api.createServer( serverDetails = await api.createServer(
dnsApiToken: cloudFlareKey, dnsApiToken: cloudFlareKey,
rootUser: rootUser, rootUser: rootUser,
domainName: domainName, domainName: domainName,
serverType: getIt<ApiConfigModel>().serverType!, serverType: getIt<ApiConfigModel>().serverType!,
); );
} catch (e) { } catch (e) {
print(e); print(e);
} }
if (serverDetails == null) { if (serverDetails == null) {
print('Server is not initialized!'); print('Server is not initialized!');
return; return;
} }
await saveServerDetails(serverDetails); await saveServerDetails(serverDetails);
onSuccess(serverDetails); onSuccess(serverDetails);
}, },
), cancelButtonOnPressed: onCancel,
ActionButton(
text: 'basis.cancel'.tr(),
onPressed: onCancel,
),
],
),
); );
} else { } else {
final NavigationService nav = getIt.get<NavigationService>(); showPopUpAlert(
nav.showPopUpDialog( alertTitle: 'modals.unexpected_error'.tr(),
BrandAlert( description: 'modals.try_again'.tr(),
title: 'modals.unexpected_error'.tr(), actionButtonTitle: 'modals.yes'.tr(),
contentText: 'modals.try_again'.tr(), actionButtonOnPressed: () async {
actions: [ ServerHostingDetails? serverDetails;
ActionButton( try {
text: 'modals.yes'.tr(), serverDetails = await api.createServer(
isRed: true, dnsApiToken: cloudFlareKey,
onPressed: () async { rootUser: rootUser,
ServerHostingDetails? serverDetails; domainName: domainName,
try { serverType: getIt<ApiConfigModel>().serverType!,
serverDetails = await api.createServer( );
dnsApiToken: cloudFlareKey, } catch (e) {
rootUser: rootUser, print(e);
domainName: domainName, }
serverType: getIt<ApiConfigModel>().serverType!,
);
} catch (e) {
print(e);
}
if (serverDetails == null) { if (serverDetails == null) {
print('Server is not initialized!'); print('Server is not initialized!');
return; return;
} }
await saveServerDetails(serverDetails); await saveServerDetails(serverDetails);
onSuccess(serverDetails); onSuccess(serverDetails);
}, },
), cancelButtonOnPressed: onCancel,
ActionButton(
text: 'basis.cancel'.tr(),
onPressed: onCancel,
),
],
),
); );
} }
} }
@ -366,31 +343,19 @@ class ServerInstallationRepository {
domain: domain, domain: domain,
); );
} on DioError catch (e) { } on DioError catch (e) {
final NavigationService nav = getIt.get<NavigationService>(); showPopUpAlert(
nav.showPopUpDialog( alertTitle: e.response!.data['errors'][0]['code'] == 1038
BrandAlert( ? 'modals.you_cant_use_this_api'.tr()
title: e.response!.data['errors'][0]['code'] == 1038 : 'domain.error'.tr(),
? 'modals.you_cant_use_this_api'.tr() description: 'modals.delete_server_volume'.tr(),
: 'domain.error'.tr(), cancelButtonOnPressed: onCancel,
contentText: 'modals.delete_server_volume'.tr(), actionButtonTitle: 'basis.delete'.tr(),
actions: [ actionButtonOnPressed: () async {
ActionButton( await serverApi.deleteServer(
text: 'basis.delete'.tr(), domainName: domain.domainName,
isRed: true, );
onPressed: () async { onCancel();
await serverApi.deleteServer( },
domainName: domain.domainName,
);
onCancel();
},
),
ActionButton(
text: 'basis.cancel'.tr(),
onPressed: onCancel,
),
],
),
); );
return false; return false;
} }

View File

@ -2,18 +2,16 @@ import 'package:flutter/material.dart';
import 'package:easy_localization/easy_localization.dart'; import 'package:easy_localization/easy_localization.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:selfprivacy/config/brand_theme.dart'; import 'package:selfprivacy/config/brand_theme.dart';
import 'package:selfprivacy/config/get_it_config.dart';
import 'package:selfprivacy/logic/cubit/client_jobs/client_jobs_cubit.dart'; import 'package:selfprivacy/logic/cubit/client_jobs/client_jobs_cubit.dart';
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart'; import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart';
import 'package:selfprivacy/logic/cubit/server_jobs/server_jobs_cubit.dart'; import 'package:selfprivacy/logic/cubit/server_jobs/server_jobs_cubit.dart';
import 'package:selfprivacy/logic/models/json/server_job.dart'; import 'package:selfprivacy/logic/models/json/server_job.dart';
import 'package:selfprivacy/ui/components/action_button/action_button.dart';
import 'package:selfprivacy/ui/components/brand_alert/brand_alert.dart';
import 'package:selfprivacy/ui/components/brand_button/brand_button.dart'; import 'package:selfprivacy/ui/components/brand_button/brand_button.dart';
import 'package:selfprivacy/ui/components/brand_cards/brand_cards.dart'; import 'package:selfprivacy/ui/components/brand_cards/brand_cards.dart';
import 'package:selfprivacy/ui/components/brand_loader/brand_loader.dart'; import 'package:selfprivacy/ui/components/brand_loader/brand_loader.dart';
import 'package:selfprivacy/ui/components/brand_text/brand_text.dart'; import 'package:selfprivacy/ui/components/brand_text/brand_text.dart';
import 'package:selfprivacy/ui/components/jobs_content/server_job_card.dart'; import 'package:selfprivacy/ui/components/jobs_content/server_job_card.dart';
import 'package:selfprivacy/ui/helpers/modals.dart';
class JobsContent extends StatelessWidget { class JobsContent extends StatelessWidget {
const JobsContent({super.key}); const JobsContent({super.key});
@ -47,26 +45,16 @@ class JobsContent extends StatelessWidget {
), ),
const SizedBox(height: 10), const SizedBox(height: 10),
BrandButton.text( BrandButton.text(
title: 'jobs.reboot_server'.tr(),
onPressed: () { onPressed: () {
final NavigationService nav = getIt<NavigationService>(); showPopUpAlert(
nav.showPopUpDialog( alertTitle: 'jobs.reboot_server'.tr(),
BrandAlert( description: 'modals.are_you_sure'.tr(),
title: 'jobs.reboot_server'.tr(), actionButtonTitle: 'modals.reboot'.tr(),
contentText: 'modals.are_you_sure'.tr(), actionButtonOnPressed: () =>
actions: [ {context.read<JobsCubit>().rebootServer()},
ActionButton(
text: 'basis.cancel'.tr(),
),
ActionButton(
onPressed: () =>
{context.read<JobsCubit>().rebootServer()},
text: 'modals.reboot'.tr(),
)
],
),
); );
}, },
title: 'jobs.reboot_server'.tr(),
), ),
]; ];
} }

View File

@ -1,5 +1,9 @@
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart'; import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
import 'package:selfprivacy/config/get_it_config.dart';
import 'package:selfprivacy/ui/components/action_button/action_button.dart';
import 'package:selfprivacy/ui/components/brand_alert/brand_alert.dart';
Future<T?> showBrandBottomSheet<T>({ Future<T?> showBrandBottomSheet<T>({
required final BuildContext context, required final BuildContext context,
@ -12,3 +16,30 @@ Future<T?> showBrandBottomSheet<T>({
shadow: const BoxShadow(color: Colors.transparent), shadow: const BoxShadow(color: Colors.transparent),
backgroundColor: Colors.transparent, backgroundColor: Colors.transparent,
); );
void showPopUpAlert({
required final String description,
required final String actionButtonTitle,
required final void Function() actionButtonOnPressed,
final void Function()? cancelButtonOnPressed,
final String? alertTitle,
final String? cancelButtonTitle,
}) {
getIt.get<NavigationService>().showPopUpDialog(
BrandAlert(
title: alertTitle ?? 'basis.alert'.tr(),
contentText: description,
actions: [
ActionButton(
text: actionButtonTitle,
isRed: true,
onPressed: actionButtonOnPressed,
),
ActionButton(
text: cancelButtonTitle ?? 'basis.cancel'.tr(),
onPressed: cancelButtonOnPressed,
),
],
),
);
}

View File

@ -1,17 +1,15 @@
import 'package:easy_localization/easy_localization.dart'; import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:selfprivacy/config/get_it_config.dart';
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart'; import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart';
import 'package:selfprivacy/logic/cubit/backups/backups_cubit.dart'; import 'package:selfprivacy/logic/cubit/backups/backups_cubit.dart';
import 'package:selfprivacy/logic/models/json/backup.dart'; import 'package:selfprivacy/logic/models/json/backup.dart';
import 'package:selfprivacy/logic/models/state_types.dart'; import 'package:selfprivacy/logic/models/state_types.dart';
import 'package:selfprivacy/ui/components/action_button/action_button.dart';
import 'package:selfprivacy/ui/components/brand_alert/brand_alert.dart';
import 'package:selfprivacy/ui/components/brand_button/brand_button.dart'; import 'package:selfprivacy/ui/components/brand_button/brand_button.dart';
import 'package:selfprivacy/ui/components/brand_cards/outlined_card.dart'; import 'package:selfprivacy/ui/components/brand_cards/outlined_card.dart';
import 'package:selfprivacy/ui/components/brand_hero_screen/brand_hero_screen.dart'; import 'package:selfprivacy/ui/components/brand_hero_screen/brand_hero_screen.dart';
import 'package:selfprivacy/ui/components/brand_icons/brand_icons.dart'; import 'package:selfprivacy/ui/components/brand_icons/brand_icons.dart';
import 'package:selfprivacy/ui/components/brand_text/brand_text.dart'; import 'package:selfprivacy/ui/components/brand_text/brand_text.dart';
import 'package:selfprivacy/ui/helpers/modals.dart';
GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>(); GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
@ -157,28 +155,17 @@ class _BackupDetailsState extends State<BackupDetails>
onTap: preventActions onTap: preventActions
? null ? null
: () { : () {
final NavigationService nav = showPopUpAlert(
getIt<NavigationService>(); alertTitle: 'backup.restoring'.tr(),
nav.showPopUpDialog( description: 'backup.restore_alert'.tr(
BrandAlert( args: [backup.time.toString()],
title: 'backup.restoring'.tr(),
contentText: 'backup.restore_alert'.tr(
args: [backup.time.toString()],
),
actions: [
ActionButton(
text: 'basis.cancel'.tr(),
),
ActionButton(
onPressed: () => {
context
.read<BackupsCubit>()
.restoreBackup(backup.id)
},
text: 'modals.yes'.tr(),
)
],
), ),
actionButtonTitle: 'modals.yes'.tr(),
actionButtonOnPressed: () => {
context
.read<BackupsCubit>()
.restoreBackup(backup.id)
},
); );
}, },
title: Text( title: Text(