some changes in display options

This commit is contained in:
Daniel Borges 2019-08-16 18:09:31 -03:00
parent 7e4c623ae2
commit 419302a77b
3 changed files with 130 additions and 95 deletions

View File

@ -26,6 +26,8 @@ import 'package:meta/meta.dart';
import '../network/api.dart' as api; import '../network/api.dart' as api;
enum DisplayOptionsState { none, success, failure }
@immutable @immutable
abstract class SetDisplayTypeEvent extends Equatable { abstract class SetDisplayTypeEvent extends Equatable {
final String type; final String type;
@ -49,11 +51,10 @@ class SetDisplayTypeShowEvent extends SetDisplayTypeEvent {
SetDisplayTypeShowEvent() : super('show'); SetDisplayTypeShowEvent() : super('show');
} }
class DisplayOptionDialogBloc extends Bloc<SetDisplayTypeEvent, void> { class DisplayOptionsDialogBloc
extends Bloc<SetDisplayTypeEvent, DisplayOptionsState> {
@override @override
void get initialState { DisplayOptionsState get initialState => DisplayOptionsState.none;
return;
}
void setBlankScreen() { void setBlankScreen() {
dispatch(SetDisplayTypeBlankEvent()); dispatch(SetDisplayTypeBlankEvent());
@ -72,7 +73,14 @@ class DisplayOptionDialogBloc extends Bloc<SetDisplayTypeEvent, void> {
} }
@override @override
Stream<void> mapEventToState(SetDisplayTypeEvent event) async* { Stream<DisplayOptionsState> mapEventToState(
api.setDisplayState(event.type); SetDisplayTypeEvent event) async* {
try {
await api.setDisplayState(event.type);
yield DisplayOptionsState.success;
} catch (e) {
yield DisplayOptionsState.failure;
}
yield DisplayOptionsState.none;
} }
} }

View File

@ -55,84 +55,88 @@ Uri _uri(String path, {Map<String, String> queryParams}) {
); );
} }
Future<http.Response> _get(String path, Future<String> _get(String path, {Map<String, String> queryParams}) {
{Map<String, String> queryParams}) async {
Uri uri = _uri(path, queryParams: queryParams); Uri uri = _uri(path, queryParams: queryParams);
final response = await http.get(uri, headers: _headers()); return http.get(uri, headers: _headers()).then((response) {
return response; if (response.statusCode == 200) {
} return response.body;
Future<PollState> pollState() async {
final response = await _get('api/poll');
if (response.statusCode != 200) {
return null;
} }
final jsonResponse = convert.jsonDecode(response.body); throw Exception('Invalid request.');
return PollState.fromJson(jsonResponse['results']);
}
Future<List<SearchResult>> search(Plugin plugin, String query) async {
final response = await _get('api/${plugin.id}/search', queryParams: {
'data': '{"request":{"text":"$query"}}',
}); });
if (response.statusCode != 200) { // final response = await http.get(uri, headers: _headers());
return []; // return response;
}
final jsonResponse = convert.jsonDecode(response.body);
return (jsonResponse['results']['items'] as List)
.map((i) => SearchResult.fromJson(i))
.toList();
} }
Future<void> projectItem(Plugin plugin, String itemId) async { // Future<PollState> pollState() async {
final response = await _get('api/${plugin.id}/live', queryParams: { // final response = await _get('api/poll');
'data': '{"request": {"id": $itemId}}', // if (response.statusCode != 200) {
// return null;
// }
// final jsonResponse = convert.jsonDecode(response.body);
// return PollState.fromJson(jsonResponse['results']);
// }
// Future<List<SearchResult>> 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<void> 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<void> 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<bool> alert(String text) async {
// final response = await _get('api/alert', queryParams: {
// 'data': '{"request": {"text": "$text"}}',
// });
// if (response.statusCode != 200) {
// return false;
// }
// return true;
// }
Future<void> setDisplayState(String state) {
return _get('api/display/$state').then((_) {
return;
}); });
if (response.statusCode != 200) {
// Do some stuff...
}
} }
Future<void> addItemToService(Plugin plugin, String itemId) async { // Future<void> setServiceItem(int itemIndex) async {
final response = await _get('api/${plugin.id}/add', queryParams: { // final response = await _get('api/service/set', queryParams: {
'data': '{"request": {"id": $itemId}}', // 'data': '{"request": {"id": $itemIndex}}',
}); // });
if (response.statusCode != 200) { // if (response.statusCode != 200) {
// Do some stuff... // // Do some stuff...
} // }
} // }
Future<bool> alert(String text) async { // Future<void> setLiveSlide(int slideIndex) async {
final response = await _get('api/alert', queryParams: { // final response = await _get('api/controller/live/set', queryParams: {
'data': '{"request": {"text": "$text"}}', // 'data': '{"request": {"id": $slideIndex}}',
}); // });
if (response.statusCode != 200) { // if (response.statusCode != 200) {
return false; // // Do some stuff...
} // }
return true; // }
}
Future<void> setDisplayState(String state) async {
final response = await _get('api/display/$state');
if (response.statusCode != 200) {
// Do some stuff...
}
}
Future<void> setServiceItem(int itemIndex) async {
final response = await _get('api/service/set', queryParams: {
'data': '{"request": {"id": $itemIndex}}',
});
if (response.statusCode != 200) {
// Do some stuff...
}
}
Future<void> setLiveSlide(int slideIndex) async {
final response = await _get('api/controller/live/set', queryParams: {
'data': '{"request": {"id": $slideIndex}}',
});
if (response.statusCode != 200) {
// Do some stuff...
}
}

View File

@ -21,13 +21,14 @@
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../bloc/display_options_dialog_bloc.dart'; import '../bloc/display_options_dialog_bloc.dart';
import '../configurations/app_localizations.dart'; import '../configurations/app_localizations.dart';
class _Action { class _Action {
String titleKey; String titleKey;
Function(DisplayOptionDialogBloc) callback; Function(DisplayOptionsDialogBloc) callback;
_Action({@required this.titleKey, @required this.callback}); _Action({@required this.titleKey, @required this.callback});
} }
@ -61,8 +62,29 @@ class DisplayOptionsDialog extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final bloc = DisplayOptionDialogBloc(); final bloc = DisplayOptionsDialogBloc();
return SimpleDialog( return BlocListener<DisplayOptionsDialogBloc, DisplayOptionsState>(
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), contentPadding: EdgeInsets.all(20.0),
children: _actions children: _actions
.map((action) => OutlineButton( .map((action) => OutlineButton(
@ -76,6 +98,7 @@ class DisplayOptionsDialog extends StatelessWidget {
}, },
)) ))
.toList(), .toList(),
),
); );
} }
} }