Refactor profiles
This commit is contained in:
97
lib/core/notification/in_app_notification_controller.dart
Normal file
97
lib/core/notification/in_app_notification_controller.dart
Normal file
@@ -0,0 +1,97 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hiddify/domain/failures.dart';
|
||||
import 'package:hiddify/features/common/adaptive_root_scaffold.dart';
|
||||
import 'package:hiddify/utils/utils.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:toastification/toastification.dart';
|
||||
|
||||
part 'in_app_notification_controller.g.dart';
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
InAppNotificationController inAppNotificationController(
|
||||
InAppNotificationControllerRef ref,
|
||||
) {
|
||||
return InAppNotificationController();
|
||||
}
|
||||
|
||||
enum NotificationType {
|
||||
info,
|
||||
error,
|
||||
success,
|
||||
}
|
||||
|
||||
class InAppNotificationController with AppLogger {
|
||||
void showToast(
|
||||
BuildContext context,
|
||||
String message, {
|
||||
NotificationType type = NotificationType.info,
|
||||
Duration duration = const Duration(seconds: 3),
|
||||
}) {
|
||||
toastification.show(
|
||||
context: context,
|
||||
title: message,
|
||||
type: type._toastificationType,
|
||||
alignment: Alignment.bottomLeft,
|
||||
autoCloseDuration: duration,
|
||||
style: ToastificationStyle.fillColored,
|
||||
pauseOnHover: true,
|
||||
showProgressBar: false,
|
||||
dragToClose: true,
|
||||
closeOnClick: true,
|
||||
closeButtonShowType: CloseButtonShowType.onHover,
|
||||
);
|
||||
}
|
||||
|
||||
void showErrorToast(String message) {
|
||||
final context = RootScaffold.stateKey.currentContext;
|
||||
if (context == null) {
|
||||
loggy.warning("context is null");
|
||||
return;
|
||||
}
|
||||
showToast(
|
||||
context,
|
||||
message,
|
||||
type: NotificationType.error,
|
||||
duration: const Duration(seconds: 5),
|
||||
);
|
||||
}
|
||||
|
||||
void showSuccessToast(String message) {
|
||||
final context = RootScaffold.stateKey.currentContext;
|
||||
if (context == null) {
|
||||
loggy.warning("context is null");
|
||||
return;
|
||||
}
|
||||
showToast(
|
||||
context,
|
||||
message,
|
||||
type: NotificationType.success,
|
||||
);
|
||||
}
|
||||
|
||||
void showInfoToast(String message) {
|
||||
final context = RootScaffold.stateKey.currentContext;
|
||||
if (context == null) {
|
||||
loggy.warning("context is null");
|
||||
return;
|
||||
}
|
||||
showToast(context, message);
|
||||
}
|
||||
|
||||
Future<void> showErrorDialog(PresentableError error) async {
|
||||
final context = RootScaffold.stateKey.currentContext;
|
||||
if (context == null) {
|
||||
loggy.warning("context is null");
|
||||
return;
|
||||
}
|
||||
CustomAlertDialog.fromErr(error).show(context);
|
||||
}
|
||||
}
|
||||
|
||||
extension NotificationTypeX on NotificationType {
|
||||
ToastificationType get _toastificationType => switch (this) {
|
||||
NotificationType.success => ToastificationType.success,
|
||||
NotificationType.error => ToastificationType.error,
|
||||
NotificationType.info => ToastificationType.info,
|
||||
};
|
||||
}
|
||||
@@ -3,8 +3,9 @@ import 'package:go_router/go_router.dart';
|
||||
import 'package:hiddify/core/router/app_router.dart';
|
||||
import 'package:hiddify/features/home/view/view.dart';
|
||||
import 'package:hiddify/features/intro/intro_page.dart';
|
||||
import 'package:hiddify/features/profile_detail/view/view.dart';
|
||||
import 'package:hiddify/features/profiles/view/view.dart';
|
||||
import 'package:hiddify/features/profile/add/add_profile_modal.dart';
|
||||
import 'package:hiddify/features/profile/details/profile_details_page.dart';
|
||||
import 'package:hiddify/features/profile/overview/profiles_overview_page.dart';
|
||||
import 'package:hiddify/features/proxies/view/view.dart';
|
||||
import 'package:hiddify/utils/utils.dart';
|
||||
|
||||
@@ -86,7 +87,8 @@ class ProfilesRoute extends GoRouteData {
|
||||
Page<void> buildPage(BuildContext context, GoRouterState state) {
|
||||
return BottomSheetPage(
|
||||
name: name,
|
||||
builder: (controller) => ProfilesModal(scrollController: controller),
|
||||
builder: (controller) =>
|
||||
ProfilesOverviewModal(scrollController: controller),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -103,7 +105,7 @@ class NewProfileRoute extends GoRouteData {
|
||||
return const MaterialPage(
|
||||
fullscreenDialog: true,
|
||||
name: name,
|
||||
child: ProfileDetailPage("new"),
|
||||
child: ProfileDetailsPage("new"),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -121,7 +123,7 @@ class ProfileDetailsRoute extends GoRouteData {
|
||||
return MaterialPage(
|
||||
fullscreenDialog: true,
|
||||
name: name,
|
||||
child: ProfileDetailPage(id),
|
||||
child: ProfileDetailsPage(id),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
50
lib/core/widget/custom_alert_dialog.dart
Normal file
50
lib/core/widget/custom_alert_dialog.dart
Normal file
@@ -0,0 +1,50 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hiddify/domain/failures.dart';
|
||||
|
||||
class CustomAlertDialog extends StatelessWidget {
|
||||
const CustomAlertDialog({
|
||||
super.key,
|
||||
this.title,
|
||||
required this.message,
|
||||
});
|
||||
|
||||
final String? title;
|
||||
final String message;
|
||||
|
||||
factory CustomAlertDialog.fromError(PresentableError error) =>
|
||||
CustomAlertDialog(
|
||||
title: error.message == null ? null : error.type,
|
||||
message: error.message ?? error.type,
|
||||
);
|
||||
|
||||
Future<void> show(BuildContext context) async {
|
||||
await showDialog(
|
||||
context: context,
|
||||
useRootNavigator: true,
|
||||
builder: (context) => this,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final localizations = MaterialLocalizations.of(context);
|
||||
|
||||
return AlertDialog(
|
||||
title: title != null ? Text(title!) : null,
|
||||
content: SingleChildScrollView(
|
||||
child: SizedBox(
|
||||
width: 468,
|
||||
child: Text(message),
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
child: Text(localizations.okButtonLabel),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user