use a Drawercontext to be able to have a specific scaffold for each page

This commit is contained in:
lukas-heiligenbrunner 2022-08-29 17:16:51 +02:00
parent 9867b913f4
commit 01049d9381
11 changed files with 255 additions and 198 deletions

View File

@ -1,5 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:openmediacentermobile/navigation/settings_screen.dart'; import 'package:openmediacentermobile/navigation/settings_screen.dart';
import 'db/database.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/shufflescreen.dart';
@ -17,7 +18,7 @@ class DrawerPage extends StatefulWidget {
_DrawerPageState createState() => _DrawerPageState(); _DrawerPageState createState() => _DrawerPageState();
} }
enum Section { HOME, SHUFFLE, LOGOUT, CATEGORIE, ACTOR } enum Section { HOME, SHUFFLE, SETTING, CATEGORIE, ACTOR }
class _DrawerPageState extends State<DrawerPage> { class _DrawerPageState extends State<DrawerPage> {
Section _sec = Section.HOME; Section _sec = Section.HOME;
@ -38,7 +39,7 @@ class _DrawerPageState extends State<DrawerPage> {
title = "Shuffle"; title = "Shuffle";
break; break;
case Section.LOGOUT: case Section.SETTING:
body = SettingsScreen(); body = SettingsScreen();
title = "Settings"; title = "Settings";
break; break;
@ -54,30 +55,28 @@ class _DrawerPageState extends State<DrawerPage> {
break; break;
} }
final loginCtx = LoginContext.of(context); return DrawerContext((newPage) {
setState(() {
_sec = newPage;
});
}, child: body);
}
}
return Scaffold( class MyDrawer extends StatelessWidget {
appBar: AppBar( const MyDrawer({Key? key}) : super(key: key);
title: Text(title),
actions: [ @override
IconButton( Widget build(BuildContext context) {
onPressed: () { final ctx = DrawerContext.of(context);
loginCtx.onLoggin(false);
Token.getInstance().setToken("", ""); return Drawer(
},
icon: const Icon(Icons.logout))
],
),
body: body,
drawer: Drawer(
child: ListView(children: [ child: ListView(children: [
ListTile( ListTile(
title: const Text('Home'), title: const Text('Home'),
leading: const Icon(Icons.home), leading: const Icon(Icons.home),
onTap: () { onTap: () {
setState(() { ctx.onChangePage(Section.HOME);
_sec = Section.HOME;
});
Navigator.pop(context); Navigator.pop(context);
}, },
), ),
@ -85,9 +84,7 @@ class _DrawerPageState extends State<DrawerPage> {
title: const Text('Shuffle'), title: const Text('Shuffle'),
leading: const Icon(Icons.update), leading: const Icon(Icons.update),
onTap: () { onTap: () {
setState(() { ctx.onChangePage(Section.SHUFFLE);
_sec = Section.SHUFFLE;
});
Navigator.pop(context); Navigator.pop(context);
}, },
), ),
@ -95,9 +92,7 @@ class _DrawerPageState extends State<DrawerPage> {
title: const Text('Categories'), title: const Text('Categories'),
leading: const Icon(Icons.category), leading: const Icon(Icons.category),
onTap: () { onTap: () {
setState(() { ctx.onChangePage(Section.CATEGORIE);
_sec = Section.CATEGORIE;
});
Navigator.pop(context); Navigator.pop(context);
}, },
), ),
@ -105,9 +100,7 @@ class _DrawerPageState extends State<DrawerPage> {
title: const Text('Actors'), title: const Text('Actors'),
leading: const Icon(Icons.people), leading: const Icon(Icons.people),
onTap: () { onTap: () {
setState(() { ctx.onChangePage(Section.ACTOR);
_sec = Section.ACTOR;
});
Navigator.pop(context); Navigator.pop(context);
}, },
), ),
@ -115,14 +108,36 @@ class _DrawerPageState extends State<DrawerPage> {
title: const Text('Settings'), title: const Text('Settings'),
leading: const Icon(Icons.settings), leading: const Icon(Icons.settings),
onTap: () { onTap: () {
setState(() { ctx.onChangePage(Section.SETTING);
_sec = Section.LOGOUT;
});
Navigator.pop(context); 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<DrawerContext>();
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;
// }
}

View File

@ -28,7 +28,7 @@ class Db {
// Set the path to the database. Note: Using the `join` function from the // Set the path to the database. Note: Using the `join` function from the
// `path` package is best practice to ensure the path is correctly // `path` package is best practice to ensure the path is correctly
// constructed for each platform. // constructed for each platform.
join(await getDatabasesPath(), 'previews.db'), dbpath,
onCreate: (db, version) { onCreate: (db, version) {
// Run the CREATE TABLE statement on the database. // Run the CREATE TABLE statement on the database.
return db.execute( return db.execute(
@ -41,6 +41,22 @@ class Db {
); );
} }
/// delete all data but keep structure
Future<void> clear() async {
await _db.delete("previews");
// shrink the db file size
await _db.execute("VACUUM");
}
/// get db size in bytes
Future<int> 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() { Database db() {
return _db; return _db;
} }

View File

@ -1,8 +1,8 @@
import "package:dart_vlc/dart_vlc.dart"; import "package:dart_vlc/dart_vlc.dart";
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:openmediacentermobile/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/logincontext.dart';
@ -10,29 +10,14 @@ import 'platform.dart';
void main() async { void main() async {
Log.i("App init!"); Log.i("App init!");
DartVLC.initialize();
if (isDesktop()) { if (isDesktop()) {
DartVLC.initialize();
} else { } else {
await loadDeviceInfo(); await loadDeviceInfo();
} }
Db().init(); 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, Intent>{ runApp(Shortcuts(shortcuts: <LogicalKeySet, Intent>{
LogicalKeySet(LogicalKeyboardKey.select): ActivateIntent(), LogicalKeySet(LogicalKeyboardKey.select): ActivateIntent(),
}, child: const LoginContainer(child: App()))); }, child: const LoginContainer(child: App())));
// runApp(const LoginContainer(child: App()));
} }

View File

@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:openmediacentermobile/types/actor.dart'; import 'package:openmediacentermobile/types/actor.dart';
import 'package:openmediacentermobile/preview/actor_tile.dart'; import 'package:openmediacentermobile/preview/actor_tile.dart';
import '../DrawerPage.dart';
import '../api/api.dart'; import '../api/api.dart';
import '../screen_loading.dart'; import '../screen_loading.dart';
@ -33,7 +34,11 @@ class _ActorScreenState extends State<ActorScreen> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return FutureBuilder( return Scaffold(
appBar: AppBar(
title: Text("Temp"),
),
body: FutureBuilder(
future: _categories, future: _categories,
builder: (context, AsyncSnapshot<List<Actor>> snapshot) { builder: (context, AsyncSnapshot<List<Actor>> snapshot) {
if (snapshot.connectionState != ConnectionState.done) { if (snapshot.connectionState != ConnectionState.done) {
@ -60,6 +65,7 @@ class _ActorScreenState extends State<ActorScreen> {
return ScreenLoading(); return ScreenLoading();
} }
}, },
); ),
drawer: MyDrawer());
} }
} }

View File

@ -2,6 +2,7 @@ import 'dart:convert';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:openmediacentermobile/preview/tag_tile.dart'; import 'package:openmediacentermobile/preview/tag_tile.dart';
import '../DrawerPage.dart';
import '../screen_loading.dart'; import '../screen_loading.dart';
import '../api/api.dart'; import '../api/api.dart';
@ -33,7 +34,11 @@ class _CategorieScreenState extends State<CategorieScreen> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return FutureBuilder( return Scaffold(
appBar: AppBar(
title: Text("Temp"),
),
body: FutureBuilder(
future: _categories, future: _categories,
builder: (context, AsyncSnapshot<List<Tag>> snapshot) { builder: (context, AsyncSnapshot<List<Tag>> snapshot) {
if (snapshot.connectionState != ConnectionState.done) { if (snapshot.connectionState != ConnectionState.done) {
@ -62,6 +67,7 @@ class _CategorieScreenState extends State<CategorieScreen> {
return ScreenLoading(); return ScreenLoading();
} }
}, },
); ),
drawer: MyDrawer());
} }
} }

View File

@ -1,6 +1,9 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import '../DrawerPage.dart';
import '../api/token.dart';
import '../db/database.dart'; import '../db/database.dart';
import '../login/logincontext.dart';
class SettingsScreen extends StatefulWidget { class SettingsScreen extends StatefulWidget {
const SettingsScreen({Key? key}) : super(key: key); const SettingsScreen({Key? key}) : super(key: key);
@ -15,33 +18,37 @@ class _SettingsScreenState extends State<SettingsScreen> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
loadDBSize(); Db().getDbSize().then((v) => setState(() {
} dbsize = v;
}));
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;
});
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Column( final loginCtx = LoginContext.of(context);
return Scaffold(
appBar: AppBar(
title: Text("Temp"),
),
body: Column(
children: [ children: [
ElevatedButton( ElevatedButton(
onPressed: () async { onPressed: () async {
await Db().db().delete("previews"); await Db().clear();
// shrink the db file size Db().getDbSize().then((v) => setState(() {
await Db().db().execute("VACUUM"); dbsize = v;
loadDBSize(); }));
}, },
child: const Text("Delete cache!")), child: const Text("Delete cache!")),
Text("db size: ${dbsize / 1024} kb") Text("db size: ${dbsize / 1024} kb"),
ElevatedButton(onPressed: () {
loginCtx.onLoggin(false);
Token.getInstance().setToken("", "");
Db().clear();
}, child: Text("Logout"))
], ],
); ),
drawer: MyDrawer());
} }
} }

View File

@ -2,6 +2,7 @@ import 'dart:convert';
import 'dart:math'; import 'dart:math';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import '../DrawerPage.dart';
import '../preview/preview_grid.dart'; import '../preview/preview_grid.dart';
import '../api/api.dart'; import '../api/api.dart';
import '../platform.dart'; import '../platform.dart';
@ -30,7 +31,12 @@ class _ShuffleScreenState extends State<ShuffleScreen> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
double width = MediaQuery.of(context).size.width; double width = MediaQuery.of(context).size.width;
return PreviewGrid(
return Scaffold(
appBar: AppBar(
title: Text("Temp"),
),
body: PreviewGrid(
videoLoader: () { videoLoader: () {
return loadData((isTV() ? width ~/ 200 : width ~/ 275) * 2); return loadData((isTV() ? width ~/ 200 : width ~/ 275) * 2);
}, },
@ -51,6 +57,7 @@ class _ShuffleScreenState extends State<ShuffleScreen> {
), ),
], ],
), ),
); ),
drawer: MyDrawer());
} }
} }

View File

@ -1,6 +1,7 @@
import 'dart:convert'; import 'dart:convert';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:openmediacentermobile/DrawerPage.dart';
import '../api/api.dart'; import '../api/api.dart';
import '../log/log.dart'; import '../log/log.dart';
@ -36,8 +37,13 @@ class VideoFeedState extends State<VideoFeed> {
double width = MediaQuery.of(context).size.width; double width = MediaQuery.of(context).size.width;
Log.d(width); Log.d(width);
return PreviewGrid( return Scaffold(
appBar: AppBar(
title: Text(widget.tag?.tagName ?? "OpenMediaCenter"),
),
body: PreviewGrid(
videoLoader: () => loadData(), videoLoader: () => loadData(),
); ),
drawer: widget.tag == null ? MyDrawer() : null);
} }
} }

View File

@ -14,10 +14,7 @@ class TagTile extends StatelessWidget {
Navigator.push( Navigator.push(
context, context,
MaterialPageRoute( MaterialPageRoute(
builder: (context) => Scaffold( builder: (context) => VideoFeed(tag: tag),
appBar: AppBar(title: Text(tag.tagName)),
body: VideoFeed(tag: tag),
),
), ),
); );
}, },

View File

@ -7,6 +7,7 @@ import 'package:openmediacentermobile/types/video_data.dart';
import 'package:openmediacentermobile/preview/actor_tile.dart'; import 'package:openmediacentermobile/preview/actor_tile.dart';
import '../api/api.dart'; import '../api/api.dart';
import '../log/log.dart';
import '../types/actor.dart'; import '../types/actor.dart';
class InfoView extends StatefulWidget { class InfoView extends StatefulWidget {
@ -51,11 +52,21 @@ class _InfoViewState extends State<InfoView> {
} else if (snapshot.hasData) { } else if (snapshot.hasData) {
final actors = snapshot.data; final actors = snapshot.data;
return Padding( return Padding(
padding: EdgeInsets.only(left: 10, right: 10, top: 30), padding: EdgeInsets.only(left: 10, right: 10, top: 60),
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
Text("Likes: ${widget.vdata.likes}"), 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("Quality: ${widget.vdata.quality}"),
Text("Length: ${widget.vdata.length}sec"), Text("Length: ${widget.vdata.length}sec"),
Text("Actors:"), Text("Actors:"),

View File

@ -1,6 +1,7 @@
import 'dart:async'; import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../api/api.dart'; import '../api/api.dart';
import '../api/token.dart'; import '../api/token.dart';
@ -64,9 +65,9 @@ class _VideoScreenState extends State<VideoScreen> {
@override @override
void dispose() { void dispose() {
super.dispose();
_controller.dispose(); _controller.dispose();
_appBarTimer?.cancel(); _appBarTimer?.cancel();
super.dispose();
} }
void _setAppBarTimer() { void _setAppBarTimer() {