mirror of
https://github.com/johrpan/musicus_mobile.git
synced 2025-10-26 18:57:25 +01:00
Make Backend a stateful widget
This commit is contained in:
parent
ad7499051b
commit
3f0567e688
5 changed files with 35 additions and 15 deletions
|
|
@ -15,7 +15,7 @@ class _AppState extends State<App> with SingleTickerProviderStateMixin {
|
|||
final nestedNavigator = GlobalKey<NavigatorState>();
|
||||
|
||||
AnimationController playerBarAnimation;
|
||||
Backend backend;
|
||||
BackendState backend;
|
||||
StreamSubscription<bool> playerActiveSubscription;
|
||||
|
||||
@override
|
||||
|
|
|
|||
|
|
@ -3,21 +3,34 @@ import 'package:rxdart/rxdart.dart';
|
|||
|
||||
import 'database.dart';
|
||||
|
||||
class Backend extends InheritedWidget {
|
||||
static Backend of(BuildContext context) =>
|
||||
context.dependOnInheritedWidgetOfExactType<Backend>();
|
||||
|
||||
class Backend extends StatefulWidget {
|
||||
final Widget child;
|
||||
|
||||
Backend({
|
||||
@required this.child,
|
||||
});
|
||||
|
||||
@override
|
||||
BackendState createState() => BackendState();
|
||||
|
||||
static BackendState of(BuildContext context) =>
|
||||
context.dependOnInheritedWidgetOfExactType<_InheritedBackend>().state;
|
||||
}
|
||||
|
||||
class BackendState extends State<Backend> {
|
||||
final db = Database('musicus.sqlite');
|
||||
|
||||
final playerActive = BehaviorSubject.seeded(false);
|
||||
final playing = BehaviorSubject.seeded(false);
|
||||
final position = BehaviorSubject.seeded(0.0);
|
||||
|
||||
Backend({
|
||||
@required this.child,
|
||||
}) : super(child: child);
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return _InheritedBackend(
|
||||
child: widget.child,
|
||||
state: this,
|
||||
);
|
||||
}
|
||||
|
||||
void startPlayer() {
|
||||
playerActive.add(true);
|
||||
|
|
@ -44,10 +57,17 @@ class Backend extends InheritedWidget {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class _InheritedBackend extends InheritedWidget {
|
||||
final Widget child;
|
||||
final BackendState state;
|
||||
|
||||
_InheritedBackend({
|
||||
@required this.child,
|
||||
@required this.state,
|
||||
}) : super(child: child);
|
||||
|
||||
@override
|
||||
bool updateShouldNotify(Backend old) =>
|
||||
playerActive != old.playerActive ||
|
||||
playing != old.playing ||
|
||||
position != old.position;
|
||||
bool updateShouldNotify(_InheritedBackend old) => state != old.state;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -209,7 +209,7 @@ class WorkEditor extends StatefulWidget {
|
|||
class _WorkEditorState extends State<WorkEditor> {
|
||||
final titleController = TextEditingController();
|
||||
|
||||
Backend backend;
|
||||
BackendState backend;
|
||||
|
||||
String title = '';
|
||||
Person composer;
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ class ProgramScreen extends StatefulWidget {
|
|||
}
|
||||
|
||||
class _ProgramScreenState extends State<ProgramScreen> {
|
||||
Backend backend;
|
||||
BackendState backend;
|
||||
StreamSubscription<double> positionSubscription;
|
||||
double position = 0.0;
|
||||
bool seeking = false;
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ class PlayPauseButton extends StatefulWidget {
|
|||
class _PlayPauseButtonState extends State<PlayPauseButton>
|
||||
with SingleTickerProviderStateMixin {
|
||||
AnimationController playPauseAnimation;
|
||||
Backend backend;
|
||||
BackendState backend;
|
||||
StreamSubscription<bool> playingSubscription;
|
||||
|
||||
@override
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue