diff --git a/lib/src/models/poll_state.dart b/lib/src/models/poll_state.dart new file mode 100644 index 0000000..a673d85 --- /dev/null +++ b/lib/src/models/poll_state.dart @@ -0,0 +1,34 @@ +// 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. + +class PollState { + final String chordNotation; + final int serviceItemCount; + final int slideIndex; + final String currentItem; + + PollState.fromJson(Map json) + : serviceItemCount = json['service'], + slideIndex = json['slide'], + currentItem = json['item'], + chordNotation = json['chordNotation']; +} diff --git a/lib/src/models/search_result.dart b/lib/src/models/search_result.dart new file mode 100644 index 0000000..4de3051 --- /dev/null +++ b/lib/src/models/search_result.dart @@ -0,0 +1,32 @@ +// 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. + +class SearchResult { + final int id; + final String text; + + SearchResult({this.id, this.text}); + + SearchResult.fromJson(List json) + : id = json[0], + text = json[1]; +} diff --git a/lib/src/network/api.dart b/lib/src/network/api.dart new file mode 100644 index 0000000..73b4f9a --- /dev/null +++ b/lib/src/network/api.dart @@ -0,0 +1,139 @@ +// 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 'dart:convert' as convert; + +import 'package:http/http.dart' as http; + +import '../models/plugin.dart'; +import '../models/poll_state.dart'; +import '../models/search_result.dart'; + +Map _headers() { + final needsAuth = false; + final username = 'openlp'; + final password = 'password'; + + Map headers = {}; + if (needsAuth) { + headers['Authorization'] = + 'Basic ${convert.base64.encode('$username:$password'.codeUnits)}'; + } + return headers; +} + +Uri _uri(String path, {Map queryParams}) { + final useHttps = false; + final address = '192.168.0.184'; + final port = 4316; + + return Uri( + scheme: useHttps ? 'https' : 'http', + host: address, + port: port, + path: path, + queryParameters: queryParams, + ); +} + +Future _get(String path, + {Map queryParams}) async { + 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"}}', + }); + 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 changeDisplayState(String state) async { + final response = await _get('api/display/$state'); + if (response.statusCode != 200) { + return false; + } + return true; +} + +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... + } +} diff --git a/pubspec.yaml b/pubspec.yaml index 118ec49..9d4bea6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -37,6 +37,7 @@ dependencies: bloc: ^0.14.0 flutter_bloc: ^0.20.0 equatable: ^0.4.0 + http: ^0.12.0+2 dev_dependencies: flutter_test: