From 419302a77b0bfb82bfdb795424fcfde370e5b813 Mon Sep 17 00:00:00 2001 From: Daniel Borges Date: Fri, 16 Aug 2019 18:09:31 -0300 Subject: [PATCH] some changes in display options --- lib/src/bloc/display_options_dialog_bloc.dart | 20 ++- lib/src/network/api.dart | 150 +++++++++--------- lib/src/widgets/display_options_dialog.dart | 55 +++++-- 3 files changed, 130 insertions(+), 95 deletions(-) diff --git a/lib/src/bloc/display_options_dialog_bloc.dart b/lib/src/bloc/display_options_dialog_bloc.dart index 959fac7..68eed19 100644 --- a/lib/src/bloc/display_options_dialog_bloc.dart +++ b/lib/src/bloc/display_options_dialog_bloc.dart @@ -26,6 +26,8 @@ import 'package:meta/meta.dart'; import '../network/api.dart' as api; +enum DisplayOptionsState { none, success, failure } + @immutable abstract class SetDisplayTypeEvent extends Equatable { final String type; @@ -49,11 +51,10 @@ class SetDisplayTypeShowEvent extends SetDisplayTypeEvent { SetDisplayTypeShowEvent() : super('show'); } -class DisplayOptionDialogBloc extends Bloc { +class DisplayOptionsDialogBloc + extends Bloc { @override - void get initialState { - return; - } + DisplayOptionsState get initialState => DisplayOptionsState.none; void setBlankScreen() { dispatch(SetDisplayTypeBlankEvent()); @@ -72,7 +73,14 @@ class DisplayOptionDialogBloc extends Bloc { } @override - Stream mapEventToState(SetDisplayTypeEvent event) async* { - api.setDisplayState(event.type); + Stream mapEventToState( + SetDisplayTypeEvent event) async* { + try { + await api.setDisplayState(event.type); + yield DisplayOptionsState.success; + } catch (e) { + yield DisplayOptionsState.failure; + } + yield DisplayOptionsState.none; } } diff --git a/lib/src/network/api.dart b/lib/src/network/api.dart index 8a615e5..e8c0d8f 100644 --- a/lib/src/network/api.dart +++ b/lib/src/network/api.dart @@ -55,84 +55,88 @@ Uri _uri(String path, {Map queryParams}) { ); } -Future _get(String path, - {Map queryParams}) async { +Future _get(String path, {Map queryParams}) { Uri uri = _uri(path, queryParams: queryParams); - final response = await http.get(uri, headers: _headers()); - return response; -} - -Future pollState() async { - final response = await _get('api/poll'); - if (response.statusCode != 200) { - return null; - } - final jsonResponse = convert.jsonDecode(response.body); - return PollState.fromJson(jsonResponse['results']); -} - -Future> search(Plugin plugin, String query) async { - final response = await _get('api/${plugin.id}/search', queryParams: { - 'data': '{"request":{"text":"$query"}}', + return http.get(uri, headers: _headers()).then((response) { + if (response.statusCode == 200) { + return response.body; + } + throw Exception('Invalid request.'); }); - if (response.statusCode != 200) { - return []; - } - final jsonResponse = convert.jsonDecode(response.body); - return (jsonResponse['results']['items'] as List) - .map((i) => SearchResult.fromJson(i)) - .toList(); + // final response = await http.get(uri, headers: _headers()); + // return response; } -Future projectItem(Plugin plugin, String itemId) async { - final response = await _get('api/${plugin.id}/live', queryParams: { - 'data': '{"request": {"id": $itemId}}', +// Future pollState() async { +// final response = await _get('api/poll'); +// if (response.statusCode != 200) { +// return null; +// } +// final jsonResponse = convert.jsonDecode(response.body); +// return PollState.fromJson(jsonResponse['results']); +// } + +// Future> search(Plugin plugin, String query) async { +// final response = await _get('api/${plugin.id}/search', queryParams: { +// 'data': '{"request":{"text":"$query"}}', +// }); +// if (response.statusCode != 200) { +// return []; +// } +// final jsonResponse = convert.jsonDecode(response.body); +// return (jsonResponse['results']['items'] as List) +// .map((i) => SearchResult.fromJson(i)) +// .toList(); +// } + +// Future projectItem(Plugin plugin, String itemId) async { +// final response = await _get('api/${plugin.id}/live', queryParams: { +// 'data': '{"request": {"id": $itemId}}', +// }); +// if (response.statusCode != 200) { +// // Do some stuff... +// } +// } + +// Future addItemToService(Plugin plugin, String itemId) async { +// final response = await _get('api/${plugin.id}/add', queryParams: { +// 'data': '{"request": {"id": $itemId}}', +// }); +// if (response.statusCode != 200) { +// // Do some stuff... +// } +// } + +// Future alert(String text) async { +// final response = await _get('api/alert', queryParams: { +// 'data': '{"request": {"text": "$text"}}', +// }); +// if (response.statusCode != 200) { +// return false; +// } +// return true; +// } + +Future setDisplayState(String state) { + return _get('api/display/$state').then((_) { + return; }); - if (response.statusCode != 200) { - // Do some stuff... - } } -Future addItemToService(Plugin plugin, String itemId) async { - final response = await _get('api/${plugin.id}/add', queryParams: { - 'data': '{"request": {"id": $itemId}}', - }); - if (response.statusCode != 200) { - // Do some stuff... - } -} +// Future setServiceItem(int itemIndex) async { +// final response = await _get('api/service/set', queryParams: { +// 'data': '{"request": {"id": $itemIndex}}', +// }); +// if (response.statusCode != 200) { +// // Do some stuff... +// } +// } -Future alert(String text) async { - final response = await _get('api/alert', queryParams: { - 'data': '{"request": {"text": "$text"}}', - }); - if (response.statusCode != 200) { - return false; - } - return true; -} - -Future setDisplayState(String state) async { - final response = await _get('api/display/$state'); - if (response.statusCode != 200) { - // Do some stuff... - } -} - -Future setServiceItem(int itemIndex) async { - final response = await _get('api/service/set', queryParams: { - 'data': '{"request": {"id": $itemIndex}}', - }); - if (response.statusCode != 200) { - // Do some stuff... - } -} - -Future setLiveSlide(int slideIndex) async { - final response = await _get('api/controller/live/set', queryParams: { - 'data': '{"request": {"id": $slideIndex}}', - }); - if (response.statusCode != 200) { - // Do some stuff... - } -} +// Future setLiveSlide(int slideIndex) async { +// final response = await _get('api/controller/live/set', queryParams: { +// 'data': '{"request": {"id": $slideIndex}}', +// }); +// if (response.statusCode != 200) { +// // Do some stuff... +// } +// } diff --git a/lib/src/widgets/display_options_dialog.dart b/lib/src/widgets/display_options_dialog.dart index 0e344e5..e48555a 100644 --- a/lib/src/widgets/display_options_dialog.dart +++ b/lib/src/widgets/display_options_dialog.dart @@ -21,13 +21,14 @@ // DEALINGS IN THE SOFTWARE. import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; import '../bloc/display_options_dialog_bloc.dart'; import '../configurations/app_localizations.dart'; class _Action { String titleKey; - Function(DisplayOptionDialogBloc) callback; + Function(DisplayOptionsDialogBloc) callback; _Action({@required this.titleKey, @required this.callback}); } @@ -61,21 +62,43 @@ class DisplayOptionsDialog extends StatelessWidget { @override Widget build(BuildContext context) { - final bloc = DisplayOptionDialogBloc(); - return SimpleDialog( - contentPadding: EdgeInsets.all(20.0), - children: _actions - .map((action) => OutlineButton( - borderSide: BorderSide(color: Theme.of(context).primaryColor), - textColor: Theme.of(context).primaryColor, - child: Text( - AppLocalizations.of(context).translate(action.titleKey)), - onPressed: () { - action.callback(bloc); - Navigator.of(context).pop(); - }, - )) - .toList(), + final bloc = DisplayOptionsDialogBloc(); + return BlocListener( + bloc: bloc, + condition: (previous, current) => current != DisplayOptionsState.none, + listener: (context, state) { + print(state); + switch (state) { + case DisplayOptionsState.success: + Scaffold.of(context).showSnackBar(SnackBar( + content: Text('Sucesso!'), + )); + break; + case DisplayOptionsState.failure: + Scaffold.of(context).showSnackBar(SnackBar( + content: Text('Falha!'), + backgroundColor: Colors.red, + )); + break; + default: + break; + } + }, + child: SimpleDialog( + contentPadding: EdgeInsets.all(20.0), + children: _actions + .map((action) => OutlineButton( + borderSide: BorderSide(color: Theme.of(context).primaryColor), + textColor: Theme.of(context).primaryColor, + child: Text( + AppLocalizations.of(context).translate(action.titleKey)), + onPressed: () { + action.callback(bloc); + Navigator.of(context).pop(); + }, + )) + .toList(), + ), ); } }