diff --git a/.gitlab-ci.yaml b/.gitlab-ci.yaml new file mode 100644 index 0000000..8678591 --- /dev/null +++ b/.gitlab-ci.yaml @@ -0,0 +1,17 @@ +image: cirrusci/flutter + +stages: + - build + +flutter_build_android: #Job name + stage: build # kind of job + before_script: + - flutter packages get + - flutter clean + script: + - flutter build apk + artifacts: + paths: + - build/app/outputs/apk/release/app-release.apk + tags: + - flutter diff --git a/lib/token.dart b/lib/api/token.dart similarity index 100% rename from lib/token.dart rename to lib/api/token.dart diff --git a/lib/app.dart b/lib/app.dart index 5043a1d..07ee3c2 100644 --- a/lib/app.dart +++ b/lib/app.dart @@ -1,57 +1,62 @@ -import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:logger/logger.dart'; +import 'package:openmediacentermobile/api/token.dart'; import 'package:openmediacentermobile/login_screen.dart'; -import 'package:openmediacentermobile/token.dart'; +import 'logincontext.dart'; import 'video_feed.dart'; class App extends StatefulWidget { - App({Key? key}) : super(key: key) {} - + const App({Key? key}) : super(key: key); @override - State createState() { - return AppState(); - } + State createState() => AppState(); } class AppState extends State { - String? loggedin = null; - - onLogin() { - setState(() { - loggedin = "ddd"; - }); - } - @override Widget build(BuildContext context) { - if (loggedin == null) { - return const CircularProgressIndicator(); - } else if (loggedin == "") { + var loginctx = LoginContext.of(context); + + Logger().d("We are logged in: ${loginctx.LoggedIn}"); + + if (!loginctx.LoggedIn) { return MaterialApp( home: Scaffold( appBar: AppBar( title: const Text("Login"), ), - body: LoginScreen(onLogin: onLogin))); + body: LoginScreen())); } else { return MaterialApp( home: Scaffold( appBar: AppBar( - title: const Text("Openmediacenter"), + title: Row( + children: [ + Text("Openmediacenter"), + ElevatedButton( + onPressed: () { + loginctx.onLoggin(false); + }, + child: Text("logout")) + ], + ), ), body: VideoFeed())); } } - AppState() { + @override + void initState() { final token = Token.getInstance(); token.getToken().then((value) { + // todo this context call might occur before app rendered correctly! + final loginctx = LoginContext.of(context); Logger().i("The token value is $value"); - setState(() { - loggedin = value; - }); + if (value != null && value != "") { + loginctx.onLoggin(false); + } else { + loginctx.onLoggin(true); + } }); } } diff --git a/lib/login_screen.dart b/lib/login_screen.dart index b106a42..301c9a5 100644 --- a/lib/login_screen.dart +++ b/lib/login_screen.dart @@ -1,5 +1,7 @@ import 'package:flutter/material.dart'; +import 'logincontext.dart'; + class LoginScreen extends StatefulWidget { const LoginScreen({Key? key, this.onLogin}) : super(key: key); @@ -13,6 +15,9 @@ class _LoginScreenState extends State { @override Widget build(BuildContext context) { return ElevatedButton( - onPressed: this.widget.onLogin, child: Text("klick meee")); + onPressed: () { + LoginContext.of(context).onLoggin(true); + }, + child: Text("klick meee")); } } diff --git a/lib/logincontext.dart b/lib/logincontext.dart new file mode 100644 index 0000000..be75090 --- /dev/null +++ b/lib/logincontext.dart @@ -0,0 +1,56 @@ +import 'package:flutter/material.dart'; + +class LoginContainer extends StatefulWidget { + const LoginContainer({Key? key, required this.child}) : super(key: key); + final Widget child; + + @override + _LoginContainerState createState() => _LoginContainerState(); +} + +class _LoginContainerState extends State { + bool loggedIn = false; + + @override + Widget build(BuildContext context) { + return LoginContext( + LoggedIn: loggedIn, + child: widget.child, + onLoggin: (bool login) { + if (loggedIn != login) { + setState(() { + loggedIn = login; + }); + } + }, + ); + } +} + +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; + + setLoggedIn(bool login) { + onLoggin(login); + } + + 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/main.dart b/lib/main.dart index 761a675..4954050 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,9 +1,10 @@ import 'package:flutter/material.dart'; import 'package:logger/logger.dart'; import 'package:openmediacentermobile/app.dart'; +import 'package:openmediacentermobile/logincontext.dart'; // main app entry point void main() { Logger().i("App init!"); - runApp(App()); + runApp(const LoginContainer(child: App())); }