load videoprefix on login

This commit is contained in:
lukas-heiligenbrunner 2022-09-02 18:42:38 +02:00
parent 7ff0d387b5
commit cb42db80af
12 changed files with 136 additions and 91 deletions

42
lib/api/settings_api.dart Normal file
View File

@ -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<InitialData> loadInitialData() async {
final data = await API.query("settings", "loadInitialData", {});
final d = jsonDecode(data);
final video = InitialData.fromJson(d);
return video;
}

View File

@ -8,8 +8,9 @@ import '../log/log.dart';
class TokenT { class TokenT {
String token; String token;
String domain; String domain;
String videoPath;
TokenT(this.token, this.domain); TokenT(this.token, this.domain, this.videoPath);
} }
class Token { class Token {
@ -18,40 +19,41 @@ class Token {
String _tokenval = ""; String _tokenval = "";
String _domain = ""; String _domain = "";
String _vPath = "";
static Token getInstance() { static Token getInstance() {
return _token; return _token;
} }
Future<TokenT?> getToken() async { Future<TokenT?> getToken() async {
var completer = Completer<TokenT?>(); if (_tokenval == "" || _domain == "" || _vPath == "") {
if (_tokenval == "" || _domain == "") {
Log.d("reading token store"); Log.d("reading token store");
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
final token = await _storage.read(key: 'jwt'); final token = await _storage.read(key: 'jwt');
final domain = await _storage.read(key: 'domain'); final domain = await _storage.read(key: 'domain');
final vPath = await _storage.read(key: 'videoPath');
// check if value is defined in phone store // check if value is defined in phone store
if (token != null && domain != null) { if (token != null && domain != null && vPath != null) {
_tokenval = token; _tokenval = token;
_domain = domain; _domain = domain;
completer.complete(TokenT(token, domain)); return TokenT(token, domain, vPath);
} else { } else {
Log.d("no token defined"); Log.d("no token defined");
completer.complete(null); return null;
} }
} else { } 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; _tokenval = token;
_domain = domain; _domain = domain;
_vPath = videoPath;
_storage.write(key: 'jwt', value: token); _storage.write(key: 'jwt', value: token);
_storage.write(key: 'domain', value: domain); _storage.write(key: 'domain', value: domain);
_storage.write(key: 'videoPath', value: videoPath);
} }
Token._(); Token._();

View File

@ -5,7 +5,7 @@ import 'package:openmediacentermobile/log/log.dart';
import 'package:openmediacentermobile/login/login_screen.dart'; import 'package:openmediacentermobile/login/login_screen.dart';
import 'drawer/drawer_page.dart'; import 'drawer/drawer_page.dart';
import 'login/logincontext.dart'; import 'login/login_context.dart';
class AppScrollBehavior extends MaterialScrollBehavior { class AppScrollBehavior extends MaterialScrollBehavior {
@override @override

View File

@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:openmediacentermobile/navigation/settings_screen.dart'; import 'package:openmediacentermobile/navigation/settings_screen.dart';
import '../navigation/actor_screen.dart'; import '../navigation/actor_screen.dart';
import '../navigation/categorie_screen.dart'; import '../navigation/categorie_screen.dart';
import '../navigation/shufflescreen.dart'; import '../navigation/shuffle_screen.dart';
import '../navigation/video_feed.dart'; import '../navigation/video_feed.dart';
import 'drawer_context.dart'; import 'drawer_context.dart';

View File

@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import '../api/token.dart'; import '../api/token.dart';
import '../log/log.dart'; import '../log/log.dart';
import 'login_context.dart';
class LoginContainer extends StatefulWidget { class LoginContainer extends StatefulWidget {
const LoginContainer({Key? key, required this.child}) : super(key: key); const LoginContainer({Key? key, required this.child}) : super(key: key);
@ -18,22 +19,23 @@ class _LoginContainerState extends State<LoginContainer> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_init();
}
final token = Token.getInstance(); void _init() async {
token.getToken().then((value) { final token = await Token.getInstance().getToken();
Log.i("The token value is $value"); Log.i("The token value is $token");
if (value != null) { if (token != null) {
setState(() { setState(() {
_loggedIn = true; _loggedIn = true;
_loading = false; _loading = false;
}); });
} else { } else {
setState(() { setState(() {
_loggedIn = false; _loggedIn = false;
_loading = false; _loading = false;
}); });
} }
});
} }
@override @override
@ -58,27 +60,3 @@ class _LoginContainerState extends State<LoginContainer> {
); );
} }
} }
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<LoginContext>();
assert(result != null, 'No LoginContext found in context');
return result!;
}
@override
bool updateShouldNotify(LoginContext old) {
return loggedIn != old.loggedIn;
}
}

View File

@ -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<LoginContext>();
assert(result != null, 'No LoginContext found in context');
return result!;
}
@override
bool updateShouldNotify(LoginContext old) {
return loggedIn != old.loggedIn;
}
}

View File

@ -3,9 +3,10 @@ import 'dart:convert';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'package:openmediacentermobile/api/token.dart'; import '../api/settings_api.dart';
import 'package:openmediacentermobile/log/log.dart'; import '../api/token.dart';
import 'package:openmediacentermobile/login/logincontext.dart'; import '../log/log.dart';
import 'login_context.dart';
class LoginScreen extends StatefulWidget { class LoginScreen extends StatefulWidget {
const LoginScreen({Key? key}) : super(key: key); const LoginScreen({Key? key}) : super(key: key);
@ -39,19 +40,44 @@ class _LoginScreenState extends State<LoginScreen> {
if (resp.statusCode != 200) { if (resp.statusCode != 200) {
return "error" + resp.body; return "error" + resp.body;
// compl.complete(resp.body);
} else { } else {
final json = jsonDecode(resp.body); final json = jsonDecode(resp.body);
final token = json["Token"]; 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); LoginContext.of(context).onLoggin(true);
return ""; return "";
} }
} }
Future<void> 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 @override
void dispose() { void dispose() {
_domainTextController.dispose(); _domainTextController.dispose();
@ -132,33 +158,7 @@ class _LoginScreenState extends State<LoginScreen> {
backgroundColor: const Color(0xff4c505b), backgroundColor: const Color(0xff4c505b),
child: IconButton( child: IconButton(
color: Colors.white, color: Colors.white,
onPressed: () async { onPressed: () async => await onLoginClick(),
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;
});
},
icon: const Icon( icon: const Icon(
Icons.arrow_forward, Icons.arrow_forward,
)), )),

View File

@ -5,7 +5,7 @@ import 'package:flutter/services.dart';
import 'app.dart'; import 'app.dart';
import 'db/database.dart'; import 'db/database.dart';
import 'log/log.dart'; import 'log/log.dart';
import 'login/logincontext.dart'; import 'login/login_container.dart';
import 'utils/platform.dart'; import 'utils/platform.dart';
void main() async { void main() async {

View File

@ -1,10 +1,10 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:openmediacentermobile/utils/FileFormatter.dart'; import 'package:openmediacentermobile/utils/file_formatter.dart';
import '../api/token.dart'; import '../api/token.dart';
import '../db/database.dart'; import '../db/database.dart';
import '../drawer/my_drawer.dart'; import '../drawer/my_drawer.dart';
import '../login/logincontext.dart'; import '../login/login_context.dart';
class SettingsScreen extends StatefulWidget { class SettingsScreen extends StatefulWidget {
const SettingsScreen({Key? key}) : super(key: key); const SettingsScreen({Key? key}) : super(key: key);
@ -47,7 +47,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
ElevatedButton( ElevatedButton(
onPressed: () { onPressed: () {
loginCtx.onLoggin(false); loginCtx.onLoggin(false);
Token.getInstance().setToken("", ""); Token.getInstance().setToken("", "", "");
Db().clear(); Db().clear();
}, },
child: Text("Logout")) child: Text("Logout"))

View File

@ -37,9 +37,7 @@ class _VideoScreenState extends State<VideoScreen> {
final token = await Token.getInstance().getToken(); final token = await Token.getInstance().getToken();
if (token == null) return; if (token == null) return;
final baseurl = token.domain; final path = token.domain + token.videoPath + videodata.movieUrl;
// todo not static middle path
final path = baseurl + "/videos/vids/" + videodata.movieUrl;
url = path; url = path;
} }