From cdc47ecdb3624769b64ddb5f294f208d3ee39923 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Wed, 9 Nov 2022 19:49:37 +0400 Subject: [PATCH] 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. --- assets/translations/en.json | 3 +- assets/translations/ru.json | 3 +- .../server_installation_repository.dart | 165 +++++++----------- .../components/jobs_content/jobs_content.dart | 28 +-- lib/ui/helpers/modals.dart | 31 ++++ .../pages/backup_details/backup_details.dart | 35 ++-- 6 files changed, 119 insertions(+), 146 deletions(-) diff --git a/assets/translations/en.json b/assets/translations/en.json index 2958663e..dd6e2ca3 100644 --- a/assets/translations/en.json +++ b/assets/translations/en.json @@ -31,7 +31,8 @@ "remove": "Remove", "apply": "Apply", "done": "Done", - "continue": "Continue" + "continue": "Continue", + "alert": "Alert" }, "more_page": { "configuration_wizard": "Setup wizard", diff --git a/assets/translations/ru.json b/assets/translations/ru.json index 0fe5001b..71f5c0ef 100644 --- a/assets/translations/ru.json +++ b/assets/translations/ru.json @@ -31,7 +31,8 @@ "remove": "Удалить", "apply": "Применить", "done": "Готово", - "continue": "Продолжить" + "continue": "Продолжить", + "alert": "Уведомление" }, "more_page": { "configuration_wizard": "Мастер настройки", diff --git a/lib/logic/cubit/server_installation/server_installation_repository.dart b/lib/logic/cubit/server_installation/server_installation_repository.dart index 386bc08c..f736310c 100644 --- a/lib/logic/cubit/server_installation/server_installation_repository.dart +++ b/lib/logic/cubit/server_installation/server_installation_repository.dart @@ -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/server_basic_info.dart'; import 'package:selfprivacy/logic/models/server_type.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/helpers/modals.dart'; import 'package:selfprivacy/utils/network_utils.dart'; class IpNotFoundException implements Exception { @@ -262,84 +261,62 @@ class ServerInstallationRepository { onSuccess(serverDetails); } on DioError catch (e) { if (e.response!.data['error']['code'] == 'uniqueness_error') { - final NavigationService nav = getIt.get(); - nav.showPopUpDialog( - BrandAlert( - title: 'modals.already_exists'.tr(), - contentText: 'modals.destroy_server'.tr(), - actions: [ - ActionButton( - text: 'basis.delete'.tr(), - isRed: true, - onPressed: () async { - await api.deleteServer( - domainName: domainName, - ); + showPopUpAlert( + alertTitle: 'modals.already_exists'.tr(), + description: 'modals.destroy_server'.tr(), + actionButtonTitle: 'modals.yes'.tr(), + actionButtonOnPressed: () async { + await api.deleteServer( + domainName: domainName, + ); - ServerHostingDetails? serverDetails; - try { - serverDetails = await api.createServer( - dnsApiToken: cloudFlareKey, - rootUser: rootUser, - domainName: domainName, - serverType: getIt().serverType!, - ); - } catch (e) { - print(e); - } + ServerHostingDetails? serverDetails; + try { + serverDetails = await api.createServer( + dnsApiToken: cloudFlareKey, + rootUser: rootUser, + domainName: domainName, + serverType: getIt().serverType!, + ); + } catch (e) { + print(e); + } - if (serverDetails == null) { - print('Server is not initialized!'); - return; - } - await saveServerDetails(serverDetails); - onSuccess(serverDetails); - }, - ), - ActionButton( - text: 'basis.cancel'.tr(), - onPressed: onCancel, - ), - ], - ), + if (serverDetails == null) { + print('Server is not initialized!'); + return; + } + await saveServerDetails(serverDetails); + onSuccess(serverDetails); + }, + cancelButtonOnPressed: onCancel, ); } else { - final NavigationService nav = getIt.get(); - nav.showPopUpDialog( - BrandAlert( - title: 'modals.unexpected_error'.tr(), - contentText: 'modals.try_again'.tr(), - actions: [ - ActionButton( - text: 'modals.yes'.tr(), - isRed: true, - onPressed: () async { - ServerHostingDetails? serverDetails; - try { - serverDetails = await api.createServer( - dnsApiToken: cloudFlareKey, - rootUser: rootUser, - domainName: domainName, - serverType: getIt().serverType!, - ); - } catch (e) { - print(e); - } + showPopUpAlert( + alertTitle: 'modals.unexpected_error'.tr(), + description: 'modals.try_again'.tr(), + actionButtonTitle: 'modals.yes'.tr(), + actionButtonOnPressed: () async { + ServerHostingDetails? serverDetails; + try { + serverDetails = await api.createServer( + dnsApiToken: cloudFlareKey, + rootUser: rootUser, + domainName: domainName, + serverType: getIt().serverType!, + ); + } catch (e) { + print(e); + } - if (serverDetails == null) { - print('Server is not initialized!'); - return; - } - await saveServerDetails(serverDetails); - onSuccess(serverDetails); - }, - ), - ActionButton( - text: 'basis.cancel'.tr(), - onPressed: onCancel, - ), - ], - ), + if (serverDetails == null) { + print('Server is not initialized!'); + return; + } + await saveServerDetails(serverDetails); + onSuccess(serverDetails); + }, + cancelButtonOnPressed: onCancel, ); } } @@ -366,31 +343,19 @@ class ServerInstallationRepository { domain: domain, ); } on DioError catch (e) { - final NavigationService nav = getIt.get(); - nav.showPopUpDialog( - BrandAlert( - title: e.response!.data['errors'][0]['code'] == 1038 - ? 'modals.you_cant_use_this_api'.tr() - : 'domain.error'.tr(), - contentText: 'modals.delete_server_volume'.tr(), - actions: [ - ActionButton( - text: 'basis.delete'.tr(), - isRed: true, - onPressed: () async { - await serverApi.deleteServer( - domainName: domain.domainName, - ); - - onCancel(); - }, - ), - ActionButton( - text: 'basis.cancel'.tr(), - onPressed: onCancel, - ), - ], - ), + showPopUpAlert( + alertTitle: e.response!.data['errors'][0]['code'] == 1038 + ? 'modals.you_cant_use_this_api'.tr() + : 'domain.error'.tr(), + description: 'modals.delete_server_volume'.tr(), + cancelButtonOnPressed: onCancel, + actionButtonTitle: 'basis.delete'.tr(), + actionButtonOnPressed: () async { + await serverApi.deleteServer( + domainName: domain.domainName, + ); + onCancel(); + }, ); return false; } diff --git a/lib/ui/components/jobs_content/jobs_content.dart b/lib/ui/components/jobs_content/jobs_content.dart index 25fb3612..bafe94a7 100644 --- a/lib/ui/components/jobs_content/jobs_content.dart +++ b/lib/ui/components/jobs_content/jobs_content.dart @@ -2,18 +2,16 @@ import 'package:flutter/material.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:flutter_bloc/flutter_bloc.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/server_installation/server_installation_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/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_cards/brand_cards.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/jobs_content/server_job_card.dart'; +import 'package:selfprivacy/ui/helpers/modals.dart'; class JobsContent extends StatelessWidget { const JobsContent({super.key}); @@ -47,26 +45,16 @@ class JobsContent extends StatelessWidget { ), const SizedBox(height: 10), BrandButton.text( + title: 'jobs.reboot_server'.tr(), onPressed: () { - final NavigationService nav = getIt(); - nav.showPopUpDialog( - BrandAlert( - title: 'jobs.reboot_server'.tr(), - contentText: 'modals.are_you_sure'.tr(), - actions: [ - ActionButton( - text: 'basis.cancel'.tr(), - ), - ActionButton( - onPressed: () => - {context.read().rebootServer()}, - text: 'modals.reboot'.tr(), - ) - ], - ), + showPopUpAlert( + alertTitle: 'jobs.reboot_server'.tr(), + description: 'modals.are_you_sure'.tr(), + actionButtonTitle: 'modals.reboot'.tr(), + actionButtonOnPressed: () => + {context.read().rebootServer()}, ); }, - title: 'jobs.reboot_server'.tr(), ), ]; } diff --git a/lib/ui/helpers/modals.dart b/lib/ui/helpers/modals.dart index 8867885f..1750c2aa 100644 --- a/lib/ui/helpers/modals.dart +++ b/lib/ui/helpers/modals.dart @@ -1,5 +1,9 @@ +import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/material.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 showBrandBottomSheet({ required final BuildContext context, @@ -12,3 +16,30 @@ Future showBrandBottomSheet({ shadow: const BoxShadow(color: 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().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, + ), + ], + ), + ); +} diff --git a/lib/ui/pages/backup_details/backup_details.dart b/lib/ui/pages/backup_details/backup_details.dart index e982a3d4..268b0870 100644 --- a/lib/ui/pages/backup_details/backup_details.dart +++ b/lib/ui/pages/backup_details/backup_details.dart @@ -1,17 +1,15 @@ import 'package:easy_localization/easy_localization.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/backups/backups_cubit.dart'; import 'package:selfprivacy/logic/models/json/backup.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_cards/outlined_card.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_text/brand_text.dart'; +import 'package:selfprivacy/ui/helpers/modals.dart'; GlobalKey navigatorKey = GlobalKey(); @@ -157,28 +155,17 @@ class _BackupDetailsState extends State onTap: preventActions ? null : () { - final NavigationService nav = - getIt(); - nav.showPopUpDialog( - BrandAlert( - title: 'backup.restoring'.tr(), - contentText: 'backup.restore_alert'.tr( - args: [backup.time.toString()], - ), - actions: [ - ActionButton( - text: 'basis.cancel'.tr(), - ), - ActionButton( - onPressed: () => { - context - .read() - .restoreBackup(backup.id) - }, - text: 'modals.yes'.tr(), - ) - ], + showPopUpAlert( + alertTitle: 'backup.restoring'.tr(), + description: 'backup.restore_alert'.tr( + args: [backup.time.toString()], ), + actionButtonTitle: 'modals.yes'.tr(), + actionButtonOnPressed: () => { + context + .read() + .restoreBackup(backup.id) + }, ); }, title: Text(