diff --git a/lib/controller/stillbox.dart b/lib/controller/stillbox.dart index 0f08e75..9930727 100644 --- a/lib/controller/stillbox.dart +++ b/lib/controller/stillbox.dart @@ -1,24 +1,22 @@ import 'package:flutter/foundation.dart'; -import 'package:web_socket_channel/io.dart'; import 'package:http/http.dart' as http; import '../pb/stillbox.pb.dart'; import 'play.dart'; -import 'storage_none.dart' - if (dart.library.io) 'storage_secure.dart' - if (dart.library.html) 'storage_web.dart'; +import 'stillbox_none.dart' + if (dart.library.io) 'stillbox_io.dart' + if (dart.library.html) 'stillbox_web.dart'; class BadAuthException implements Exception {} class Stillbox extends ChangeNotifier { final storage = Storer(); Player player = Player(); - late IOWebSocketChannel channel; + final channel = Socketer(); bool connected = false; late Uri _wsUri; LiveState _state = LiveState.LS_LIVE; Filter? currentFilter; Call? _currentCall; - Map headers = {}; Uri? baseUri = Uri.base; set state(LiveState newState) { @@ -54,15 +52,6 @@ class Stillbox extends ChangeNotifier { } } - void updateCookie(http.Response response) { - String? rawCookie = response.headers['set-cookie']; - if (rawCookie != null) { - int index = rawCookie.indexOf(';'); - headers['cookie'] = - (index == -1) ? rawCookie : rawCookie.substring(0, index); - } - } - Future doLogin(String uri, String username, String password) async { baseUri = Uri.parse(uri); setUris(); @@ -80,10 +69,10 @@ class Stillbox extends ChangeNotifier { body: form, ); if (response.statusCode == 200) { - updateCookie(response); + String? token = channel.updateCookie(response); storage.setKey('baseURL', uri); - if (!kIsWeb) { - storage.setKey('token', headers['cookie']!); + if (!kIsWeb && token != null) { + storage.setKey('token', token); } await connect(); return true; @@ -95,7 +84,7 @@ class Stillbox extends ChangeNotifier { if (connected == true) { return; } - channel = IOWebSocketChannel.connect(_wsUri, headers: headers); + channel.connect(_wsUri); channel.stream.listen((event) => _handleData(event), onDone: () { connected = false; }, onError: (error) { @@ -112,7 +101,7 @@ class Stillbox extends ChangeNotifier { if (storedToken == null || storedUri == null) { throw (BadAuthException); } - headers['cookie'] = storedToken; + channel.setCookie(storedToken); baseUri = Uri.parse(storedUri); setUris(); } diff --git a/lib/controller/stillbox_io.dart b/lib/controller/stillbox_io.dart new file mode 100644 index 0000000..6d38261 --- /dev/null +++ b/lib/controller/stillbox_io.dart @@ -0,0 +1,47 @@ +import 'package:flutter_secure_storage/flutter_secure_storage.dart'; +import 'package:web_socket_channel/io.dart'; +import 'package:web_socket_channel/web_socket_channel.dart'; +import 'package:http/http.dart' as http; + +class Storer { + final storage = const FlutterSecureStorage(); + Future getKey(String key) async { + return await storage.read(key: key); + } + + void setKey(String key, String value) async { + await storage.write(key: key, value: value); + } +} + +class Socketer { + late IOWebSocketChannel channel; + Map headers = {}; + + WebSocketSink get sink { + return channel.sink; + } + + Stream get stream { + return channel.stream; + } + + String? updateCookie(http.Response response) { + String? rawCookie = response.headers['set-cookie']; + if (rawCookie != null) { + int index = rawCookie.indexOf(';'); + headers['cookie'] = + (index == -1) ? rawCookie : rawCookie.substring(0, index); + } + + return headers['cookie']; + } + + void setCookie(String token) { + headers['cookie'] = token; + } + + void connect(Uri uri) { + channel = IOWebSocketChannel.connect(uri, headers: headers); + } +} diff --git a/lib/controller/stillbox_none.dart b/lib/controller/stillbox_none.dart new file mode 100644 index 0000000..a682223 --- /dev/null +++ b/lib/controller/stillbox_none.dart @@ -0,0 +1,34 @@ +import 'package:web_socket_channel/web_socket_channel.dart'; +import 'package:http/http.dart' as http; + +class Storer { + Future getKey(String key) async { + return null; + } + + void setKey(String key, String value) { + return; + } +} + +class Socketer { + late WebSocketChannel channel; + + WebSocketSink get sink { + return channel.sink; + } + + Stream get stream { + return channel.stream; + } + + String? updateCookie(http.Response response) { + return null; + } + + void setCookie(String token) {} + + void connect(Uri uri) { + channel = WebSocketChannel.connect(uri); + } +} diff --git a/lib/controller/stillbox_web.dart b/lib/controller/stillbox_web.dart new file mode 100644 index 0000000..62ba41f --- /dev/null +++ b/lib/controller/stillbox_web.dart @@ -0,0 +1,35 @@ +import 'dart:html'; +import 'package:web_socket_channel/web_socket_channel.dart'; +import 'package:http/http.dart' as http; + +class Storer { + Future getKey(String key) async { + return window.localStorage[key]; + } + + void setKey(String key, String value) { + window.localStorage[key] = value; + } +} + +class Socketer { + late WebSocketChannel channel; + + WebSocketSink get sink { + return channel.sink; + } + + Stream get stream { + return channel.stream; + } + + String? updateCookie(http.Response response) { + return response.headers['set-cookie']; + } + + void setCookie(String token) {} + + void connect(Uri uri) { + channel = WebSocketChannel.connect(uri); + } +} diff --git a/lib/controller/storage_none.dart b/lib/controller/storage_none.dart deleted file mode 100644 index ec3d078..0000000 --- a/lib/controller/storage_none.dart +++ /dev/null @@ -1,9 +0,0 @@ -class Storer { - Future getKey(String key) async { - return null; - } - - void setKey(String key, String value) { - return; - } -} diff --git a/lib/controller/storage_secure.dart b/lib/controller/storage_secure.dart deleted file mode 100644 index 06acd2e..0000000 --- a/lib/controller/storage_secure.dart +++ /dev/null @@ -1,12 +0,0 @@ -import 'package:flutter_secure_storage/flutter_secure_storage.dart'; - -class Storer { - final storage = const FlutterSecureStorage(); - Future getKey(String key) async { - return await storage.read(key: 'token'); - } - - void setKey(String key, String value) async { - await storage.write(key: 'token', value: value); - } -} diff --git a/lib/controller/storage_web.dart b/lib/controller/storage_web.dart deleted file mode 100644 index a0ef055..0000000 --- a/lib/controller/storage_web.dart +++ /dev/null @@ -1,11 +0,0 @@ -import 'dart:html'; - -class Storer { - Future getKey(String key) async { - return window.localStorage[key]; - } - - void setKey(String key, String value) { - window.localStorage[key] = value; - } -}