From 01049d93811ecfe3014cb4e4f222cfe82b58eaa0 Mon Sep 17 00:00:00 2001 From: lukas-heiligenbrunner Date: Mon, 29 Aug 2022 17:16:51 +0200 Subject: [PATCH] use a Drawercontext to be able to have a specific scaffold for each page --- lib/DrawerPage.dart | 155 +++++++++++++++------------ lib/db/database.dart | 18 +++- lib/main.dart | 19 +--- lib/navigation/actor_screen.dart | 60 ++++++----- lib/navigation/categorie_screen.dart | 64 ++++++----- lib/navigation/settings_screen.dart | 55 +++++----- lib/navigation/shufflescreen.dart | 49 +++++---- lib/navigation/video_feed.dart | 12 ++- lib/preview/tag_tile.dart | 5 +- lib/video_screen/info_view.dart | 13 ++- lib/video_screen/videoscreen.dart | 3 +- 11 files changed, 255 insertions(+), 198 deletions(-) diff --git a/lib/DrawerPage.dart b/lib/DrawerPage.dart index 9496370..4fdd490 100644 --- a/lib/DrawerPage.dart +++ b/lib/DrawerPage.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:openmediacentermobile/navigation/settings_screen.dart'; +import 'db/database.dart'; import 'navigation/actor_screen.dart'; import 'navigation/categorie_screen.dart'; import 'navigation/shufflescreen.dart'; @@ -17,7 +18,7 @@ class DrawerPage extends StatefulWidget { _DrawerPageState createState() => _DrawerPageState(); } -enum Section { HOME, SHUFFLE, LOGOUT, CATEGORIE, ACTOR } +enum Section { HOME, SHUFFLE, SETTING, CATEGORIE, ACTOR } class _DrawerPageState extends State { Section _sec = Section.HOME; @@ -38,7 +39,7 @@ class _DrawerPageState extends State { title = "Shuffle"; break; - case Section.LOGOUT: + case Section.SETTING: body = SettingsScreen(); title = "Settings"; break; @@ -54,75 +55,89 @@ class _DrawerPageState extends State { break; } - final loginCtx = LoginContext.of(context); + return DrawerContext((newPage) { + setState(() { + _sec = newPage; + }); + }, child: body); + } +} - return Scaffold( - appBar: AppBar( - title: Text(title), - actions: [ - IconButton( - onPressed: () { - loginCtx.onLoggin(false); - Token.getInstance().setToken("", ""); - }, - icon: const Icon(Icons.logout)) - ], - ), - body: body, - drawer: Drawer( - child: ListView(children: [ - ListTile( - title: const Text('Home'), - leading: const Icon(Icons.home), - onTap: () { - setState(() { - _sec = Section.HOME; - }); - Navigator.pop(context); - }, - ), - ListTile( - title: const Text('Shuffle'), - leading: const Icon(Icons.update), - onTap: () { - setState(() { - _sec = Section.SHUFFLE; - }); - Navigator.pop(context); - }, - ), - ListTile( - title: const Text('Categories'), - leading: const Icon(Icons.category), - onTap: () { - setState(() { - _sec = Section.CATEGORIE; - }); - Navigator.pop(context); - }, - ), - ListTile( - title: const Text('Actors'), - leading: const Icon(Icons.people), - onTap: () { - setState(() { - _sec = Section.ACTOR; - }); - Navigator.pop(context); - }, - ), - ListTile( - title: const Text('Settings'), - leading: const Icon(Icons.settings), - onTap: () { - setState(() { - _sec = Section.LOGOUT; - }); - Navigator.pop(context); - }, - ), - ]), - ), +class MyDrawer extends StatelessWidget { + const MyDrawer({Key? key}) : super(key: key); + + @override + Widget build(BuildContext context) { + final ctx = DrawerContext.of(context); + + return Drawer( + child: ListView(children: [ + ListTile( + title: const Text('Home'), + leading: const Icon(Icons.home), + onTap: () { + ctx.onChangePage(Section.HOME); + Navigator.pop(context); + }, + ), + ListTile( + title: const Text('Shuffle'), + leading: const Icon(Icons.update), + onTap: () { + ctx.onChangePage(Section.SHUFFLE); + Navigator.pop(context); + }, + ), + ListTile( + title: const Text('Categories'), + leading: const Icon(Icons.category), + onTap: () { + ctx.onChangePage(Section.CATEGORIE); + Navigator.pop(context); + }, + ), + ListTile( + title: const Text('Actors'), + leading: const Icon(Icons.people), + onTap: () { + ctx.onChangePage(Section.ACTOR); + Navigator.pop(context); + }, + ), + ListTile( + title: const Text('Settings'), + leading: const Icon(Icons.settings), + onTap: () { + ctx.onChangePage(Section.SETTING); + Navigator.pop(context); + }, + ), + ]), ); } } + + +class DrawerContext extends InheritedWidget { + const DrawerContext(this.onChangePage, {Key? key, required Widget child}) + : super(key: key, child: child); + + final void Function(Section) onChangePage; + + static DrawerContext of(BuildContext context) { + final DrawerContext? result = + context.dependOnInheritedWidgetOfExactType(); + assert(result != null, 'No DrawerContext found in context'); + return result!; + } + + @override + bool updateShouldNotify(covariant InheritedWidget oldWidget) { + return false; + } + +// @override +// bool updateShouldNotify(LoginContext old) { +// return loggedIn != old.loggedIn; +// } +} diff --git a/lib/db/database.dart b/lib/db/database.dart index 48d31b5..6fc543f 100644 --- a/lib/db/database.dart +++ b/lib/db/database.dart @@ -28,7 +28,7 @@ class Db { // Set the path to the database. Note: Using the `join` function from the // `path` package is best practice to ensure the path is correctly // constructed for each platform. - join(await getDatabasesPath(), 'previews.db'), + dbpath, onCreate: (db, version) { // Run the CREATE TABLE statement on the database. return db.execute( @@ -41,6 +41,22 @@ class Db { ); } + /// delete all data but keep structure + Future clear() async { + await _db.delete("previews"); + // shrink the db file size + await _db.execute("VACUUM"); + } + + /// get db size in bytes + Future getDbSize() async { + final int cnt = (await Db().db().rawQuery("pragma page_count;"))[0] + ["page_count"] as int; + final int pagesize = + (await Db().db().rawQuery("pragma page_size;"))[0]["page_size"] as int; + return cnt * pagesize; + } + Database db() { return _db; } diff --git a/lib/main.dart b/lib/main.dart index 77dddf0..d1d2bfc 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,8 +1,8 @@ import "package:dart_vlc/dart_vlc.dart"; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; -import 'package:openmediacentermobile/app.dart'; +import 'app.dart'; import 'db/database.dart'; import 'log/log.dart'; import 'login/logincontext.dart'; @@ -10,29 +10,14 @@ import 'platform.dart'; void main() async { Log.i("App init!"); - DartVLC.initialize(); if (isDesktop()) { + DartVLC.initialize(); } else { await loadDeviceInfo(); } - Db().init(); - // RawKeyboard.instance.addListener((event) { - // if (LogicalKeyboardKey.arrowLeft == event.logicalKey) { - // FocusManager.instance.primaryFocus?.focusInDirection(TraversalDirection.left); - // } else if (LogicalKeyboardKey.arrowRight == event.logicalKey) { - // FocusManager.instance.primaryFocus?.focusInDirection(TraversalDirection.right); - // } else if (LogicalKeyboardKey.arrowDown == event.logicalKey) { - // FocusManager.instance.primaryFocus?.focusInDirection(TraversalDirection.down); - // } else if (LogicalKeyboardKey.arrowUp == event.logicalKey) { - // FocusManager.instance.primaryFocus?.focusInDirection(TraversalDirection.up); - // } - // }); - runApp(Shortcuts(shortcuts: { LogicalKeySet(LogicalKeyboardKey.select): ActivateIntent(), }, child: const LoginContainer(child: App()))); - - // runApp(const LoginContainer(child: App())); } diff --git a/lib/navigation/actor_screen.dart b/lib/navigation/actor_screen.dart index 35c1549..1fd16e4 100644 --- a/lib/navigation/actor_screen.dart +++ b/lib/navigation/actor_screen.dart @@ -4,6 +4,7 @@ import 'package:flutter/material.dart'; import 'package:openmediacentermobile/types/actor.dart'; import 'package:openmediacentermobile/preview/actor_tile.dart'; +import '../DrawerPage.dart'; import '../api/api.dart'; import '../screen_loading.dart'; @@ -33,33 +34,38 @@ class _ActorScreenState extends State { @override Widget build(BuildContext context) { - return FutureBuilder( - future: _categories, - builder: (context, AsyncSnapshot> snapshot) { - if (snapshot.connectionState != ConnectionState.done) { - return ScreenLoading(); - } + return Scaffold( + appBar: AppBar( + title: Text("Temp"), + ), + body: FutureBuilder( + future: _categories, + builder: (context, AsyncSnapshot> snapshot) { + if (snapshot.connectionState != ConnectionState.done) { + return ScreenLoading(); + } - if (snapshot.hasError) { - return Text("Error"); - } else if (snapshot.hasData) { - return Padding( - padding: EdgeInsets.all(5), - child: SingleChildScrollView( - child: Wrap( - spacing: 5, - runSpacing: 5, - alignment: WrapAlignment.start, - children: snapshot.data! - .map((e) => ActorTile(actor: e)) - .toList(growable: false), - ), - ), - ); - } else { - return ScreenLoading(); - } - }, - ); + if (snapshot.hasError) { + return Text("Error"); + } else if (snapshot.hasData) { + return Padding( + padding: EdgeInsets.all(5), + child: SingleChildScrollView( + child: Wrap( + spacing: 5, + runSpacing: 5, + alignment: WrapAlignment.start, + children: snapshot.data! + .map((e) => ActorTile(actor: e)) + .toList(growable: false), + ), + ), + ); + } else { + return ScreenLoading(); + } + }, + ), + drawer: MyDrawer()); } } diff --git a/lib/navigation/categorie_screen.dart b/lib/navigation/categorie_screen.dart index 342ac3a..f3a9d27 100644 --- a/lib/navigation/categorie_screen.dart +++ b/lib/navigation/categorie_screen.dart @@ -2,6 +2,7 @@ import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:openmediacentermobile/preview/tag_tile.dart'; +import '../DrawerPage.dart'; import '../screen_loading.dart'; import '../api/api.dart'; @@ -33,35 +34,40 @@ class _CategorieScreenState extends State { @override Widget build(BuildContext context) { - return FutureBuilder( - future: _categories, - builder: (context, AsyncSnapshot> snapshot) { - if (snapshot.connectionState != ConnectionState.done) { - return ScreenLoading(); - } + return Scaffold( + appBar: AppBar( + title: Text("Temp"), + ), + body: FutureBuilder( + future: _categories, + builder: (context, AsyncSnapshot> snapshot) { + if (snapshot.connectionState != ConnectionState.done) { + return ScreenLoading(); + } - if (snapshot.hasError) { - return Text("Error"); - } else if (snapshot.hasData) { - return Padding( - padding: EdgeInsets.all(5), - child: SingleChildScrollView( - child: Wrap( - spacing: 5, - runSpacing: 5, - alignment: WrapAlignment.start, - children: snapshot.data! - .map((e) => TagTile( - tag: e, - )) - .toList(growable: false), - ), - ), - ); - } else { - return ScreenLoading(); - } - }, - ); + if (snapshot.hasError) { + return Text("Error"); + } else if (snapshot.hasData) { + return Padding( + padding: EdgeInsets.all(5), + child: SingleChildScrollView( + child: Wrap( + spacing: 5, + runSpacing: 5, + alignment: WrapAlignment.start, + children: snapshot.data! + .map((e) => TagTile( + tag: e, + )) + .toList(growable: false), + ), + ), + ); + } else { + return ScreenLoading(); + } + }, + ), + drawer: MyDrawer()); } } diff --git a/lib/navigation/settings_screen.dart b/lib/navigation/settings_screen.dart index 9d72a8d..489a0b0 100644 --- a/lib/navigation/settings_screen.dart +++ b/lib/navigation/settings_screen.dart @@ -1,6 +1,9 @@ import 'package:flutter/material.dart'; +import '../DrawerPage.dart'; +import '../api/token.dart'; import '../db/database.dart'; +import '../login/logincontext.dart'; class SettingsScreen extends StatefulWidget { const SettingsScreen({Key? key}) : super(key: key); @@ -15,33 +18,37 @@ class _SettingsScreenState extends State { @override void initState() { super.initState(); - loadDBSize(); - } - - void loadDBSize() async { - final int cnt = (await Db().db().rawQuery("pragma page_count;"))[0] - ["page_count"] as int; - final int pagesize = - (await Db().db().rawQuery("pragma page_size;"))[0]["page_size"] as int; - setState(() { - dbsize = cnt * pagesize; - }); + Db().getDbSize().then((v) => setState(() { + dbsize = v; + })); } @override Widget build(BuildContext context) { - return Column( - children: [ - ElevatedButton( - onPressed: () async { - await Db().db().delete("previews"); - // shrink the db file size - await Db().db().execute("VACUUM"); - loadDBSize(); - }, - child: const Text("Delete cache!")), - Text("db size: ${dbsize / 1024} kb") - ], - ); + final loginCtx = LoginContext.of(context); + + return Scaffold( + appBar: AppBar( + title: Text("Temp"), + ), + body: Column( + children: [ + ElevatedButton( + onPressed: () async { + await Db().clear(); + Db().getDbSize().then((v) => setState(() { + dbsize = v; + })); + }, + child: const Text("Delete cache!")), + Text("db size: ${dbsize / 1024} kb"), + ElevatedButton(onPressed: () { + loginCtx.onLoggin(false); + Token.getInstance().setToken("", ""); + Db().clear(); + }, child: Text("Logout")) + ], + ), + drawer: MyDrawer()); } } diff --git a/lib/navigation/shufflescreen.dart b/lib/navigation/shufflescreen.dart index 7d100e4..a07d345 100644 --- a/lib/navigation/shufflescreen.dart +++ b/lib/navigation/shufflescreen.dart @@ -2,6 +2,7 @@ import 'dart:convert'; import 'dart:math'; import 'package:flutter/material.dart'; +import '../DrawerPage.dart'; import '../preview/preview_grid.dart'; import '../api/api.dart'; import '../platform.dart'; @@ -30,27 +31,33 @@ class _ShuffleScreenState extends State { @override Widget build(BuildContext context) { double width = MediaQuery.of(context).size.width; - return PreviewGrid( - videoLoader: () { - return loadData((isTV() ? width ~/ 200 : width ~/ 275) * 2); - }, - footerBuilder: (state) => Column( - children: [ - const SizedBox( - height: 25, + + return Scaffold( + appBar: AppBar( + title: Text("Temp"), + ), + body: PreviewGrid( + videoLoader: () { + return loadData((isTV() ? width ~/ 200 : width ~/ 275) * 2); + }, + footerBuilder: (state) => Column( + children: [ + const SizedBox( + height: 25, + ), + TextButton.icon( + onPressed: () { + state.loadData(); + }, + icon: const Icon(Icons.update), + label: const Text("Shuffle"), + ), + const SizedBox( + height: 25, + ), + ], ), - TextButton.icon( - onPressed: () { - state.loadData(); - }, - icon: const Icon(Icons.update), - label: const Text("Shuffle"), - ), - const SizedBox( - height: 25, - ), - ], - ), - ); + ), + drawer: MyDrawer()); } } diff --git a/lib/navigation/video_feed.dart b/lib/navigation/video_feed.dart index 4afff40..cf63a2e 100644 --- a/lib/navigation/video_feed.dart +++ b/lib/navigation/video_feed.dart @@ -1,6 +1,7 @@ import 'dart:convert'; import 'package:flutter/material.dart'; +import 'package:openmediacentermobile/DrawerPage.dart'; import '../api/api.dart'; import '../log/log.dart'; @@ -36,8 +37,13 @@ class VideoFeedState extends State { double width = MediaQuery.of(context).size.width; Log.d(width); - return PreviewGrid( - videoLoader: () => loadData(), - ); + return Scaffold( + appBar: AppBar( + title: Text(widget.tag?.tagName ?? "OpenMediaCenter"), + ), + body: PreviewGrid( + videoLoader: () => loadData(), + ), + drawer: widget.tag == null ? MyDrawer() : null); } } diff --git a/lib/preview/tag_tile.dart b/lib/preview/tag_tile.dart index f5ddae7..86d8084 100644 --- a/lib/preview/tag_tile.dart +++ b/lib/preview/tag_tile.dart @@ -14,10 +14,7 @@ class TagTile extends StatelessWidget { Navigator.push( context, MaterialPageRoute( - builder: (context) => Scaffold( - appBar: AppBar(title: Text(tag.tagName)), - body: VideoFeed(tag: tag), - ), + builder: (context) => VideoFeed(tag: tag), ), ); }, diff --git a/lib/video_screen/info_view.dart b/lib/video_screen/info_view.dart index a3a018d..8dd3d4f 100644 --- a/lib/video_screen/info_view.dart +++ b/lib/video_screen/info_view.dart @@ -7,6 +7,7 @@ import 'package:openmediacentermobile/types/video_data.dart'; import 'package:openmediacentermobile/preview/actor_tile.dart'; import '../api/api.dart'; +import '../log/log.dart'; import '../types/actor.dart'; class InfoView extends StatefulWidget { @@ -51,11 +52,21 @@ class _InfoViewState extends State { } else if (snapshot.hasData) { final actors = snapshot.data; return Padding( - padding: EdgeInsets.only(left: 10, right: 10, top: 30), + padding: EdgeInsets.only(left: 10, right: 10, top: 60), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text("Likes: ${widget.vdata.likes}"), + IconButton(onPressed: () async { + final data = await API + .query("video", "addLike", {'MovieId': widget.vdata.movieId}); + final d = jsonDecode(data); + if (d["result"] != 'success') { + Log.w(d); + } + // bit hacky but it works + widget.vdata.likes += 1; + }, icon: Icon(Icons.thumb_up)), Text("Quality: ${widget.vdata.quality}"), Text("Length: ${widget.vdata.length}sec"), Text("Actors:"), diff --git a/lib/video_screen/videoscreen.dart b/lib/video_screen/videoscreen.dart index 7409a37..6b99ee2 100644 --- a/lib/video_screen/videoscreen.dart +++ b/lib/video_screen/videoscreen.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'dart:convert'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import '../api/api.dart'; import '../api/token.dart'; @@ -64,9 +65,9 @@ class _VideoScreenState extends State { @override void dispose() { + super.dispose(); _controller.dispose(); _appBarTimer?.cancel(); - super.dispose(); } void _setAppBarTimer() {