From cb42db80af291f650ecc56427e0d16b23b27b539 Mon Sep 17 00:00:00 2001 From: lukas-heiligenbrunner Date: Fri, 2 Sep 2022 18:42:38 +0200 Subject: [PATCH] load videoprefix on login --- lib/api/settings_api.dart | 42 ++++++++++++ lib/api/token.dart | 22 ++++--- lib/app.dart | 2 +- lib/drawer/drawer_page.dart | 2 +- ...logincontext.dart => login_container.dart} | 56 +++++----------- lib/login/login_context.dart | 25 +++++++ lib/login/login_screen.dart | 66 +++++++++---------- lib/main.dart | 2 +- lib/navigation/settings_screen.dart | 6 +- ...shufflescreen.dart => shuffle_screen.dart} | 0 ...FileFormatter.dart => file_formatter.dart} | 0 lib/video_screen/videoscreen.dart | 4 +- 12 files changed, 136 insertions(+), 91 deletions(-) create mode 100644 lib/api/settings_api.dart rename lib/login/{logincontext.dart => login_container.dart} (51%) create mode 100644 lib/login/login_context.dart rename lib/navigation/{shufflescreen.dart => shuffle_screen.dart} (100%) rename lib/utils/{FileFormatter.dart => file_formatter.dart} (100%) diff --git a/lib/api/settings_api.dart b/lib/api/settings_api.dart new file mode 100644 index 0000000..8262de4 --- /dev/null +++ b/lib/api/settings_api.dart @@ -0,0 +1,42 @@ +import 'dart:convert'; + +import 'api.dart'; + +class InitialData { + bool darkMode; + bool password; + String mediacenterName; + String videoPath; + String tvShowPath; + bool tvShowEnabled; + bool fullDeleteEnabled; + + InitialData( + this.darkMode, + this.password, + this.mediacenterName, + this.videoPath, + this.tvShowPath, + this.tvShowEnabled, + this.fullDeleteEnabled); + + factory InitialData.fromJson(dynamic json) { + return InitialData( + json['DarkMode'] as bool, + json['Pasword'] as bool, + json['MediacenterName'] as String, + json['VideoPath'] as String, + json['TVShowPath'] as String, + json['TVShowEnabled'] as bool, + json['FullDeleteEnabled'] as bool, + ); + } +} + +Future loadInitialData() async { + final data = await API.query("settings", "loadInitialData", {}); + + final d = jsonDecode(data); + final video = InitialData.fromJson(d); + return video; +} diff --git a/lib/api/token.dart b/lib/api/token.dart index ca314f7..0fed2bb 100644 --- a/lib/api/token.dart +++ b/lib/api/token.dart @@ -8,8 +8,9 @@ import '../log/log.dart'; class TokenT { String token; String domain; + String videoPath; - TokenT(this.token, this.domain); + TokenT(this.token, this.domain, this.videoPath); } class Token { @@ -18,40 +19,41 @@ class Token { String _tokenval = ""; String _domain = ""; + String _vPath = ""; static Token getInstance() { return _token; } Future getToken() async { - var completer = Completer(); - - if (_tokenval == "" || _domain == "") { + if (_tokenval == "" || _domain == "" || _vPath == "") { Log.d("reading token store"); WidgetsFlutterBinding.ensureInitialized(); final token = await _storage.read(key: 'jwt'); final domain = await _storage.read(key: 'domain'); + final vPath = await _storage.read(key: 'videoPath'); // check if value is defined in phone store - if (token != null && domain != null) { + if (token != null && domain != null && vPath != null) { _tokenval = token; _domain = domain; - completer.complete(TokenT(token, domain)); + return TokenT(token, domain, vPath); } else { Log.d("no token defined"); - completer.complete(null); + return null; } } else { - completer.complete(TokenT(_tokenval, _domain)); + return TokenT(_tokenval, _domain, _vPath); } - return completer.future; } - void setToken(String token, String domain) { + void setToken(String token, String domain, String videoPath) { _tokenval = token; _domain = domain; + _vPath = videoPath; _storage.write(key: 'jwt', value: token); _storage.write(key: 'domain', value: domain); + _storage.write(key: 'videoPath', value: videoPath); } Token._(); diff --git a/lib/app.dart b/lib/app.dart index 9988eb3..b82e6b8 100644 --- a/lib/app.dart +++ b/lib/app.dart @@ -5,7 +5,7 @@ import 'package:openmediacentermobile/log/log.dart'; import 'package:openmediacentermobile/login/login_screen.dart'; import 'drawer/drawer_page.dart'; -import 'login/logincontext.dart'; +import 'login/login_context.dart'; class AppScrollBehavior extends MaterialScrollBehavior { @override diff --git a/lib/drawer/drawer_page.dart b/lib/drawer/drawer_page.dart index 97b9706..cececf1 100644 --- a/lib/drawer/drawer_page.dart +++ b/lib/drawer/drawer_page.dart @@ -2,7 +2,7 @@ import 'package:flutter/material.dart'; import 'package:openmediacentermobile/navigation/settings_screen.dart'; import '../navigation/actor_screen.dart'; import '../navigation/categorie_screen.dart'; -import '../navigation/shufflescreen.dart'; +import '../navigation/shuffle_screen.dart'; import '../navigation/video_feed.dart'; import 'drawer_context.dart'; diff --git a/lib/login/logincontext.dart b/lib/login/login_container.dart similarity index 51% rename from lib/login/logincontext.dart rename to lib/login/login_container.dart index a1e6b2a..fcfb2af 100644 --- a/lib/login/logincontext.dart +++ b/lib/login/login_container.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import '../api/token.dart'; import '../log/log.dart'; +import 'login_context.dart'; class LoginContainer extends StatefulWidget { const LoginContainer({Key? key, required this.child}) : super(key: key); @@ -18,22 +19,23 @@ class _LoginContainerState extends State { @override void initState() { super.initState(); + _init(); + } - final token = Token.getInstance(); - token.getToken().then((value) { - Log.i("The token value is $value"); - if (value != null) { - setState(() { - _loggedIn = true; - _loading = false; - }); - } else { - setState(() { - _loggedIn = false; - _loading = false; - }); - } - }); + void _init() async { + final token = await Token.getInstance().getToken(); + Log.i("The token value is $token"); + if (token != null) { + setState(() { + _loggedIn = true; + _loading = false; + }); + } else { + setState(() { + _loggedIn = false; + _loading = false; + }); + } } @override @@ -58,27 +60,3 @@ class _LoginContainerState extends State { ); } } - -class LoginContext extends InheritedWidget { - const LoginContext( - {Key? key, - required Widget child, - required this.loggedIn, - required this.onLoggin}) - : super(key: key, child: child); - - final bool loggedIn; - final void Function(bool) onLoggin; - - static LoginContext of(BuildContext context) { - final LoginContext? result = - context.dependOnInheritedWidgetOfExactType(); - assert(result != null, 'No LoginContext found in context'); - return result!; - } - - @override - bool updateShouldNotify(LoginContext old) { - return loggedIn != old.loggedIn; - } -} diff --git a/lib/login/login_context.dart b/lib/login/login_context.dart new file mode 100644 index 0000000..fd513b2 --- /dev/null +++ b/lib/login/login_context.dart @@ -0,0 +1,25 @@ +import 'package:flutter/material.dart'; + +class LoginContext extends InheritedWidget { + const LoginContext( + {Key? key, + required Widget child, + required this.loggedIn, + required this.onLoggin}) + : super(key: key, child: child); + + final bool loggedIn; + final void Function(bool) onLoggin; + + static LoginContext of(BuildContext context) { + final LoginContext? result = + context.dependOnInheritedWidgetOfExactType(); + assert(result != null, 'No LoginContext found in context'); + return result!; + } + + @override + bool updateShouldNotify(LoginContext old) { + return loggedIn != old.loggedIn; + } +} diff --git a/lib/login/login_screen.dart b/lib/login/login_screen.dart index c8bd6c9..ac108cc 100644 --- a/lib/login/login_screen.dart +++ b/lib/login/login_screen.dart @@ -3,9 +3,10 @@ import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; -import 'package:openmediacentermobile/api/token.dart'; -import 'package:openmediacentermobile/log/log.dart'; -import 'package:openmediacentermobile/login/logincontext.dart'; +import '../api/settings_api.dart'; +import '../api/token.dart'; +import '../log/log.dart'; +import 'login_context.dart'; class LoginScreen extends StatefulWidget { const LoginScreen({Key? key}) : super(key: key); @@ -39,19 +40,44 @@ class _LoginScreenState extends State { if (resp.statusCode != 200) { return "error" + resp.body; - - // compl.complete(resp.body); } else { final json = jsonDecode(resp.body); final token = json["Token"]; - Token.getInstance().setToken(token, domain); + // todo bit hacky + Token.getInstance().setToken(token, domain, "temp"); + // we need to call this twice because we need for the loadInitialData an api token + Token.getInstance() + .setToken(token, domain, (await loadInitialData()).videoPath); LoginContext.of(context).onLoggin(true); return ""; } } + Future onLoginClick() async { + Log.d("logging in"); + final pwd = _passwordTextController.value.text; + final domain = _domainTextController.value.text; + + var err = ""; + if (domain.startsWith("https://") || domain.startsWith("http://")) { + err = await login(pwd, domain); + if (err.isEmpty) return; + } else { + // try to auto infering domain prefix + err = await login(pwd, "https://" + domain); + if (err.isEmpty) return; + err = await login(pwd, "http://" + domain); + if (err.isEmpty) return; + } + + Log.i(err); + setState(() { + error = err; + }); + } + @override void dispose() { _domainTextController.dispose(); @@ -132,33 +158,7 @@ class _LoginScreenState extends State { backgroundColor: const Color(0xff4c505b), child: IconButton( color: Colors.white, - onPressed: () async { - Log.d("clickkked"); - final pwd = - _passwordTextController.value.text; - final domain = - _domainTextController.value.text; - - var err = ""; - if (domain.startsWith("https://") || - domain.startsWith("http://")) { - err = await login(pwd, domain); - if (err.isEmpty) return; - } else { - // try to auto infering domain prefix - err = await login( - pwd, "https://" + domain); - if (err.isEmpty) return; - err = await login( - pwd, "http://" + domain); - if (err.isEmpty) return; - } - - Log.i(err); - setState(() { - error = err; - }); - }, + onPressed: () async => await onLoginClick(), icon: const Icon( Icons.arrow_forward, )), diff --git a/lib/main.dart b/lib/main.dart index 8ec9872..2d39da8 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -5,7 +5,7 @@ import 'package:flutter/services.dart'; import 'app.dart'; import 'db/database.dart'; import 'log/log.dart'; -import 'login/logincontext.dart'; +import 'login/login_container.dart'; import 'utils/platform.dart'; void main() async { diff --git a/lib/navigation/settings_screen.dart b/lib/navigation/settings_screen.dart index 8d7e6e3..d0779de 100644 --- a/lib/navigation/settings_screen.dart +++ b/lib/navigation/settings_screen.dart @@ -1,10 +1,10 @@ import 'package:flutter/material.dart'; -import 'package:openmediacentermobile/utils/FileFormatter.dart'; +import 'package:openmediacentermobile/utils/file_formatter.dart'; import '../api/token.dart'; import '../db/database.dart'; import '../drawer/my_drawer.dart'; -import '../login/logincontext.dart'; +import '../login/login_context.dart'; class SettingsScreen extends StatefulWidget { const SettingsScreen({Key? key}) : super(key: key); @@ -47,7 +47,7 @@ class _SettingsScreenState extends State { ElevatedButton( onPressed: () { loginCtx.onLoggin(false); - Token.getInstance().setToken("", ""); + Token.getInstance().setToken("", "", ""); Db().clear(); }, child: Text("Logout")) diff --git a/lib/navigation/shufflescreen.dart b/lib/navigation/shuffle_screen.dart similarity index 100% rename from lib/navigation/shufflescreen.dart rename to lib/navigation/shuffle_screen.dart diff --git a/lib/utils/FileFormatter.dart b/lib/utils/file_formatter.dart similarity index 100% rename from lib/utils/FileFormatter.dart rename to lib/utils/file_formatter.dart diff --git a/lib/video_screen/videoscreen.dart b/lib/video_screen/videoscreen.dart index 2c242ea..f5d1c08 100644 --- a/lib/video_screen/videoscreen.dart +++ b/lib/video_screen/videoscreen.dart @@ -37,9 +37,7 @@ class _VideoScreenState extends State { final token = await Token.getInstance().getToken(); if (token == null) return; - final baseurl = token.domain; - // todo not static middle path - final path = baseurl + "/videos/vids/" + videodata.movieUrl; + final path = token.domain + token.videoPath + videodata.movieUrl; url = path; }