musicus_mobile/lib/backend.dart

74 lines
1.5 KiB
Dart
Raw Normal View History

2019-12-02 21:05:49 +01:00
import 'package:flutter/widgets.dart';
import 'package:rxdart/rxdart.dart';
2019-12-03 12:44:24 +01:00
import 'database.dart';
2020-03-28 08:51:45 +01:00
class Backend extends StatefulWidget {
2019-12-02 21:05:49 +01:00
final Widget child;
2020-03-28 08:51:45 +01:00
Backend({
@required this.child,
});
@override
BackendState createState() => BackendState();
static BackendState of(BuildContext context) =>
context.dependOnInheritedWidgetOfExactType<_InheritedBackend>().state;
}
class BackendState extends State<Backend> {
2019-12-03 12:44:24 +01:00
final db = Database('musicus.sqlite');
2019-12-02 21:05:49 +01:00
final playerActive = BehaviorSubject.seeded(false);
final playing = BehaviorSubject.seeded(false);
final position = BehaviorSubject.seeded(0.0);
2020-03-28 08:51:45 +01:00
@override
Widget build(BuildContext context) {
return _InheritedBackend(
child: widget.child,
state: this,
);
}
2019-12-02 21:05:49 +01:00
void startPlayer() {
playerActive.add(true);
}
void playPause() {
playing.add(!playing.value);
if (playing.value) {
simulatePlay();
}
}
void seekTo(double pos) {
position.add(pos);
}
Future<void> simulatePlay() async {
while (playing.value) {
await Future.delayed(Duration(milliseconds: 200));
if (position.value >= 0.99) {
position.add(0.0);
} else {
position.add(position.value + 0.01);
}
}
}
2020-03-28 08:51:45 +01:00
}
class _InheritedBackend extends InheritedWidget {
final Widget child;
final BackendState state;
_InheritedBackend({
@required this.child,
@required this.state,
}) : super(child: child);
2019-12-02 21:05:49 +01:00
@override
2020-03-28 08:51:45 +01:00
bool updateShouldNotify(_InheritedBackend old) => state != old.state;
2019-12-02 21:05:49 +01:00
}