From 7e4c623ae2cd8d45bbcc0bac82606574e7633ec1 Mon Sep 17 00:00:00 2001 From: Daniel Borges Date: Wed, 14 Aug 2019 18:36:59 -0300 Subject: [PATCH] display options api integration --- lib/src/bloc/display_options_dialog_bloc.dart | 78 +++++++++++++++++++ lib/src/network/api.dart | 5 +- lib/src/widgets/display_options_dialog.dart | 22 +++--- 3 files changed, 92 insertions(+), 13 deletions(-) create mode 100644 lib/src/bloc/display_options_dialog_bloc.dart diff --git a/lib/src/bloc/display_options_dialog_bloc.dart b/lib/src/bloc/display_options_dialog_bloc.dart new file mode 100644 index 0000000..959fac7 --- /dev/null +++ b/lib/src/bloc/display_options_dialog_bloc.dart @@ -0,0 +1,78 @@ +// OpenLP Mobile Remote +// --------------------------------------------------------------------------- +// Copyright (c) 2008-2019 OpenLP Developers +// --------------------------------------------------------------------------- +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +import 'package:bloc/bloc.dart'; +import 'package:equatable/equatable.dart'; +import 'package:meta/meta.dart'; + +import '../network/api.dart' as api; + +@immutable +abstract class SetDisplayTypeEvent extends Equatable { + final String type; + SetDisplayTypeEvent(this.type, [List props = const []]) + : super(props); +} + +class SetDisplayTypeBlankEvent extends SetDisplayTypeEvent { + SetDisplayTypeBlankEvent() : super('blank'); +} + +class SetDisplayTypeThemeEvent extends SetDisplayTypeEvent { + SetDisplayTypeThemeEvent() : super('theme'); +} + +class SetDisplayTypeDesktopEvent extends SetDisplayTypeEvent { + SetDisplayTypeDesktopEvent() : super('desktop'); +} + +class SetDisplayTypeShowEvent extends SetDisplayTypeEvent { + SetDisplayTypeShowEvent() : super('show'); +} + +class DisplayOptionDialogBloc extends Bloc { + @override + void get initialState { + return; + } + + void setBlankScreen() { + dispatch(SetDisplayTypeBlankEvent()); + } + + void setThemeScreen() { + dispatch(SetDisplayTypeThemeEvent()); + } + + void setDesktopScreen() { + dispatch(SetDisplayTypeDesktopEvent()); + } + + void setFullProjection() { + dispatch(SetDisplayTypeShowEvent()); + } + + @override + Stream mapEventToState(SetDisplayTypeEvent event) async* { + api.setDisplayState(event.type); + } +} diff --git a/lib/src/network/api.dart b/lib/src/network/api.dart index 73b4f9a..8a615e5 100644 --- a/lib/src/network/api.dart +++ b/lib/src/network/api.dart @@ -112,12 +112,11 @@ Future alert(String text) async { return true; } -Future changeDisplayState(String state) async { +Future setDisplayState(String state) async { final response = await _get('api/display/$state'); if (response.statusCode != 200) { - return false; + // Do some stuff... } - return true; } Future setServiceItem(int itemIndex) async { diff --git a/lib/src/widgets/display_options_dialog.dart b/lib/src/widgets/display_options_dialog.dart index 8ed43e4..0e344e5 100644 --- a/lib/src/widgets/display_options_dialog.dart +++ b/lib/src/widgets/display_options_dialog.dart @@ -22,11 +22,12 @@ import 'package:flutter/material.dart'; +import '../bloc/display_options_dialog_bloc.dart'; import '../configurations/app_localizations.dart'; class _Action { String titleKey; - VoidCallback callback; + Function(DisplayOptionDialogBloc) callback; _Action({@required this.titleKey, @required this.callback}); } @@ -34,32 +35,33 @@ class DisplayOptionsDialog extends StatelessWidget { final List<_Action> _actions = [ _Action( titleKey: 'display_option_blank', - callback: () { - print('Blank screen'); + callback: (bloc) { + bloc.setBlankScreen(); }, ), _Action( titleKey: 'display_option_theme', - callback: () { - print('Theme screen'); + callback: (bloc) { + bloc.setThemeScreen(); }, ), _Action( titleKey: 'display_option_desktop', - callback: () { - print('Desktop screen'); + callback: (bloc) { + bloc.setDesktopScreen(); }, ), _Action( titleKey: 'display_option_show', - callback: () { - print('Show screen'); + callback: (bloc) { + bloc.setFullProjection(); }, ), ]; @override Widget build(BuildContext context) { + final bloc = DisplayOptionDialogBloc(); return SimpleDialog( contentPadding: EdgeInsets.all(20.0), children: _actions @@ -69,7 +71,7 @@ class DisplayOptionsDialog extends StatelessWidget { child: Text( AppLocalizations.of(context).translate(action.titleKey)), onPressed: () { - action.callback(); + action.callback(bloc); Navigator.of(context).pop(); }, ))