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 {
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<TokenT?> getToken() async {
var completer = Completer<TokenT?>();
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._();

View File

@ -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

View File

@ -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';

View File

@ -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,11 +19,13 @@ class _LoginContainerState extends State<LoginContainer> {
@override
void initState() {
super.initState();
_init();
}
final token = Token.getInstance();
token.getToken().then((value) {
Log.i("The token value is $value");
if (value != null) {
void _init() async {
final token = await Token.getInstance().getToken();
Log.i("The token value is $token");
if (token != null) {
setState(() {
_loggedIn = true;
_loading = false;
@ -33,7 +36,6 @@ class _LoginContainerState extends State<LoginContainer> {
_loading = false;
});
}
});
}
@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: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<LoginScreen> {
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<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
void dispose() {
_domainTextController.dispose();
@ -132,33 +158,7 @@ class _LoginScreenState extends State<LoginScreen> {
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,
)),

View File

@ -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 {

View File

@ -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<SettingsScreen> {
ElevatedButton(
onPressed: () {
loginCtx.onLoggin(false);
Token.getInstance().setToken("", "");
Token.getInstance().setToken("", "", "");
Db().clear();
},
child: Text("Logout"))

View File

@ -37,9 +37,7 @@ class _VideoScreenState extends State<VideoScreen> {
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;
}