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",
"apply": "Apply",
"done": "Done",
"continue": "Continue"
"continue": "Continue",
"alert": "Alert"
},
"more_page": {
"configuration_wizard": "Setup wizard",

View File

@ -31,7 +31,8 @@
"remove": "Удалить",
"apply": "Применить",
"done": "Готово",
"continue": "Продолжить"
"continue": "Продолжить",
"alert": "Уведомление"
},
"more_page": {
"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/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<NavigationService>();
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<ApiConfigModel>().serverType!,
);
} catch (e) {
print(e);
}
ServerHostingDetails? serverDetails;
try {
serverDetails = await api.createServer(
dnsApiToken: cloudFlareKey,
rootUser: rootUser,
domainName: domainName,
serverType: getIt<ApiConfigModel>().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<NavigationService>();
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<ApiConfigModel>().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<ApiConfigModel>().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<NavigationService>();
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;
}

View File

@ -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<NavigationService>();
nav.showPopUpDialog(
BrandAlert(
title: 'jobs.reboot_server'.tr(),
contentText: 'modals.are_you_sure'.tr(),
actions: [
ActionButton(
text: 'basis.cancel'.tr(),
),
ActionButton(
onPressed: () =>
{context.read<JobsCubit>().rebootServer()},
text: 'modals.reboot'.tr(),
)
],
),
showPopUpAlert(
alertTitle: 'jobs.reboot_server'.tr(),
description: 'modals.are_you_sure'.tr(),
actionButtonTitle: 'modals.reboot'.tr(),
actionButtonOnPressed: () =>
{context.read<JobsCubit>().rebootServer()},
);
},
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: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>({
required final BuildContext context,
@ -12,3 +16,30 @@ Future<T?> showBrandBottomSheet<T>({
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<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: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<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
@ -157,28 +155,17 @@ class _BackupDetailsState extends State<BackupDetails>
onTap: preventActions
? null
: () {
final NavigationService nav =
getIt<NavigationService>();
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<BackupsCubit>()
.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<BackupsCubit>()
.restoreBackup(backup.id)
},
);
},
title: Text(