diff --git a/lib/core/models/authResponse.dart b/lib/core/models/authResponse.dart new file mode 100644 index 0000000..8b60a15 --- /dev/null +++ b/lib/core/models/authResponse.dart @@ -0,0 +1,39 @@ +import 'user.dart'; + +class AuthResponse { + final AuthBody body; + final int status; + final bool operation; + + + AuthResponse( {this.body, this.status, this.operation}); + + factory AuthResponse.fromJson(Map json) { + return AuthResponse( + operation: json['operation'], + status: json['status'], + body: AuthBody.fromJson(json['body']) + ); + } +} + +List parseListString(json){ + if(json==null) return null; + return new List.from(json); +} + +class AuthBody { + final List email; + final List password; + final String message; + final User user; + AuthBody({this.message, this.user, this.email, this.password}); + factory AuthBody.fromJson(Map json) { + return AuthBody( + email: json['email']!=null ? parseListString(json['email']): null, + message: json['message'], + password: json['password']!=null ? parseListString(json['password']): null, + user: json['user']!=null ? User.fromJson(json['user']) : null + ); + } +} diff --git a/lib/core/models/dialog_models.dart b/lib/core/models/dialog_models.dart new file mode 100644 index 0000000..3dda7e7 --- /dev/null +++ b/lib/core/models/dialog_models.dart @@ -0,0 +1,26 @@ +import 'package:flutter/foundation.dart'; + +class DialogRequest { + final String title; + final String description; + final String buttonTitle; + final String cancelTitle; + + DialogRequest( + {@required this.title, + @required this.description, + @required this.buttonTitle, + this.cancelTitle}); +} + +class DialogResponse { + final String fieldOne; + final String fieldTwo; + final bool confirmed; + + DialogResponse({ + this.fieldOne, + this.fieldTwo, + this.confirmed, + }); +} diff --git a/lib/core/services/dialog_service.dart b/lib/core/services/dialog_service.dart new file mode 100644 index 0000000..f7e2c56 --- /dev/null +++ b/lib/core/services/dialog_service.dart @@ -0,0 +1,54 @@ +import 'dart:async'; + +import 'package:aman_kassa_flutter/core/models/dialog_models.dart'; +import 'package:flutter/cupertino.dart'; + +class DialogService { + GlobalKey _dialogNavigationKey = GlobalKey(); + Function(DialogRequest) _showDialogListener; + Completer _dialogCompleter; + + GlobalKey get dialogNavigationKey => _dialogNavigationKey; + + /// Registers a callback function. Typically to show the dialog + void registerDialogListener(Function(DialogRequest) showDialogListener) { + _showDialogListener = showDialogListener; + } + + /// Calls the dialog listener and returns a Future that will wait for dialogComplete. + Future showDialog({ + String title, + String description, + String buttonTitle = 'Ok', + }) { + _dialogCompleter = Completer(); + _showDialogListener(DialogRequest( + title: title, + description: description, + buttonTitle: buttonTitle, + )); + return _dialogCompleter.future; + } + + /// Shows a confirmation dialog + Future showConfirmationDialog( + {String title, + String description, + String confirmationTitle = 'Ok', + String cancelTitle = 'Cancel'}) { + _dialogCompleter = Completer(); + _showDialogListener(DialogRequest( + title: title, + description: description, + buttonTitle: confirmationTitle, + cancelTitle: cancelTitle)); + return _dialogCompleter.future; + } + + /// Completes the _dialogCompleter to resume the Future's execution call + void dialogComplete(DialogResponse response) { + _dialogNavigationKey.currentState.pop(); + _dialogCompleter.complete(response); + _dialogCompleter = null; + } +} diff --git a/lib/widgets/dialog_manager.dart b/lib/widgets/dialog_manager.dart new file mode 100644 index 0000000..130fba9 --- /dev/null +++ b/lib/widgets/dialog_manager.dart @@ -0,0 +1,53 @@ +import 'package:aman_kassa_flutter/core/locator.dart'; +import 'package:aman_kassa_flutter/core/models/dialog_models.dart'; +import 'package:aman_kassa_flutter/core/services/dialog_service.dart'; +import 'package:flutter/material.dart'; + +class DialogManager extends StatefulWidget { + final Widget child; + DialogManager({Key key, this.child}) : super(key: key); + + _DialogManagerState createState() => _DialogManagerState(); +} + +class _DialogManagerState extends State { + DialogService _dialogService = locator(); + + @override + void initState() { + super.initState(); + _dialogService.registerDialogListener(_showDialog); + } + + @override + Widget build(BuildContext context) { + return widget.child; + } + + void _showDialog(DialogRequest request) { + var isConfirmationDialog = request.cancelTitle != null; + showDialog( + context: context, + builder: (context) => AlertDialog( + title: Text(request.title), + content: Text(request.description), + actions: [ + if (isConfirmationDialog) + FlatButton( + child: Text(request.cancelTitle), + onPressed: () { + _dialogService + .dialogComplete(DialogResponse(confirmed: false)); + }, + ), + FlatButton( + child: Text(request.buttonTitle), + onPressed: () { + _dialogService + .dialogComplete(DialogResponse(confirmed: true)); + }, + ), + ], + )); + } +}