2019-12-02 21:38:47 +01:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
2019-12-02 21:05:49 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2020-04-24 22:41:52 +02:00
|
|
|
import 'package:musicus_database/musicus_database.dart';
|
2019-12-02 21:05:49 +01:00
|
|
|
|
2019-12-02 21:38:47 +01:00
|
|
|
import '../backend.dart';
|
2020-04-22 10:01:50 +02:00
|
|
|
import '../music_library.dart';
|
2019-12-02 22:05:07 +01:00
|
|
|
import '../widgets/play_pause_button.dart';
|
2020-04-22 10:32:36 +02:00
|
|
|
import '../widgets/recording_tile.dart';
|
2019-12-02 21:38:47 +01:00
|
|
|
|
|
|
|
|
class ProgramScreen extends StatefulWidget {
|
|
|
|
|
@override
|
|
|
|
|
_ProgramScreenState createState() => _ProgramScreenState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _ProgramScreenState extends State<ProgramScreen> {
|
2020-03-28 08:51:45 +01:00
|
|
|
BackendState backend;
|
2020-04-24 19:53:29 +02:00
|
|
|
|
2020-04-24 19:56:14 +02:00
|
|
|
StreamSubscription<bool> playerActiveSubscription;
|
|
|
|
|
|
2020-04-24 19:53:29 +02:00
|
|
|
StreamSubscription<List<InternalTrack>> playlistSubscription;
|
2020-04-26 15:35:45 +02:00
|
|
|
List<Widget> widgets = [];
|
2020-04-24 19:53:29 +02:00
|
|
|
|
2019-12-02 21:38:47 +01:00
|
|
|
StreamSubscription<double> positionSubscription;
|
|
|
|
|
double position = 0.0;
|
|
|
|
|
bool seeking = false;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void didChangeDependencies() {
|
|
|
|
|
super.didChangeDependencies();
|
|
|
|
|
|
|
|
|
|
backend = Backend.of(context);
|
|
|
|
|
|
2020-04-24 19:56:14 +02:00
|
|
|
if (playerActiveSubscription != null) {
|
|
|
|
|
playerActiveSubscription.cancel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close the program screen, if the player is no longer active.
|
|
|
|
|
playerActiveSubscription = backend.player.active.listen((active) {
|
|
|
|
|
if (!active) {
|
|
|
|
|
Navigator.pop(context);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2020-04-24 19:53:29 +02:00
|
|
|
if (playlistSubscription != null) {
|
|
|
|
|
playlistSubscription.cancel();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
playlistSubscription = backend.player.playlist.listen((playlist) {
|
|
|
|
|
updateProgram(playlist);
|
|
|
|
|
});
|
|
|
|
|
|
2019-12-02 21:38:47 +01:00
|
|
|
if (positionSubscription != null) {
|
|
|
|
|
positionSubscription.cancel();
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-18 13:50:38 +02:00
|
|
|
positionSubscription = backend.player.normalizedPosition.listen((pos) {
|
2019-12-02 21:38:47 +01:00
|
|
|
if (!seeking) {
|
|
|
|
|
setState(() {
|
|
|
|
|
position = pos;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 19:53:29 +02:00
|
|
|
/// Go through the tracks of [playlist] and preprocess them for displaying.
|
|
|
|
|
Future<void> updateProgram(List<InternalTrack> playlist) async {
|
2020-04-26 15:35:45 +02:00
|
|
|
List<Widget> newWidgets = [];
|
2020-04-24 19:53:29 +02:00
|
|
|
|
|
|
|
|
// The following variables exist to adapt the resulting ProgramItem to its
|
|
|
|
|
// predecessor.
|
|
|
|
|
|
|
|
|
|
// If the previous recording was the same, we won't need to include the
|
|
|
|
|
// recording data again.
|
|
|
|
|
int lastRecordingId;
|
|
|
|
|
|
|
|
|
|
// If the previous work was the same, we won't need to retrieve its parts
|
|
|
|
|
// from the database again.
|
|
|
|
|
int lastWorkId;
|
|
|
|
|
|
2020-04-26 15:35:45 +02:00
|
|
|
// This will contain information on the last new work.
|
|
|
|
|
WorkInfo workInfo;
|
2020-04-24 19:53:29 +02:00
|
|
|
|
|
|
|
|
for (var i = 0; i < playlist.length; i++) {
|
2020-04-26 15:35:45 +02:00
|
|
|
// The widgets displayed for this track.
|
|
|
|
|
List<Widget> children = [];
|
2020-04-24 19:53:29 +02:00
|
|
|
|
|
|
|
|
final track = playlist[i];
|
|
|
|
|
final recordingId = track.track.recordingId;
|
|
|
|
|
final partIds = track.track.partIds;
|
|
|
|
|
|
2020-04-26 15:35:45 +02:00
|
|
|
// If the recording is the same, the work will also be the same, so
|
|
|
|
|
// workInfo doesn't have to be updated either.
|
2020-04-24 19:53:29 +02:00
|
|
|
if (recordingId != lastRecordingId) {
|
|
|
|
|
lastRecordingId = recordingId;
|
|
|
|
|
|
2020-04-26 15:35:45 +02:00
|
|
|
final recordingInfo = await backend.db.getRecording(recordingId);
|
2020-04-24 19:53:29 +02:00
|
|
|
|
2020-04-26 15:35:45 +02:00
|
|
|
if (recordingInfo.recording.work != lastWorkId) {
|
|
|
|
|
lastWorkId = recordingInfo.recording.work;
|
|
|
|
|
workInfo = await backend.db.getWork(lastWorkId);
|
2020-04-24 19:53:29 +02:00
|
|
|
}
|
|
|
|
|
|
2020-04-26 15:35:45 +02:00
|
|
|
children.addAll([
|
|
|
|
|
RecordingTile(
|
|
|
|
|
workInfo: workInfo,
|
|
|
|
|
recordingInfo: recordingInfo,
|
|
|
|
|
),
|
|
|
|
|
SizedBox(
|
|
|
|
|
height: 8.0,
|
|
|
|
|
),
|
|
|
|
|
]);
|
2020-04-24 19:53:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (final partId in partIds) {
|
2020-04-26 15:35:45 +02:00
|
|
|
final partInfo = workInfo.parts[partId];
|
|
|
|
|
|
|
|
|
|
children.add(Padding(
|
|
|
|
|
padding: const EdgeInsets.only(
|
|
|
|
|
left: 8.0,
|
|
|
|
|
),
|
|
|
|
|
child: Text(
|
|
|
|
|
partInfo.work.title,
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
fontStyle: FontStyle.italic,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
));
|
2020-04-24 19:53:29 +02:00
|
|
|
}
|
|
|
|
|
|
2020-04-26 15:35:45 +02:00
|
|
|
newWidgets.add(Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
|
|
|
children: children,
|
2020-04-24 19:53:29 +02:00
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check, whether we are still a part of the widget tree, because this
|
|
|
|
|
// function might take some time.
|
|
|
|
|
if (mounted) {
|
|
|
|
|
setState(() {
|
2020-04-26 15:35:45 +02:00
|
|
|
widgets = newWidgets;
|
2020-04-24 19:53:29 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-02 21:05:49 +01:00
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
return Scaffold(
|
|
|
|
|
appBar: AppBar(
|
|
|
|
|
leading: IconButton(
|
|
|
|
|
icon: const Icon(Icons.keyboard_arrow_down),
|
|
|
|
|
onPressed: () => Navigator.pop(context),
|
|
|
|
|
),
|
|
|
|
|
title: Text('Program'),
|
|
|
|
|
),
|
2020-04-24 19:53:29 +02:00
|
|
|
body: StreamBuilder<int>(
|
|
|
|
|
stream: backend.player.currentIndex,
|
2020-04-22 10:01:50 +02:00
|
|
|
builder: (context, snapshot) {
|
2020-04-24 19:53:29 +02:00
|
|
|
if (snapshot.hasData) {
|
|
|
|
|
return ListView.builder(
|
2020-04-26 15:35:45 +02:00
|
|
|
itemCount: widgets.length,
|
2020-04-24 19:53:29 +02:00
|
|
|
itemBuilder: (context, index) {
|
|
|
|
|
return InkWell(
|
2020-04-26 15:35:45 +02:00
|
|
|
child: Row(
|
|
|
|
|
children: <Widget>[
|
|
|
|
|
Padding(
|
|
|
|
|
padding: const EdgeInsets.all(4.0),
|
|
|
|
|
child: index == snapshot.data
|
|
|
|
|
? const Icon(Icons.play_arrow)
|
|
|
|
|
: SizedBox(
|
|
|
|
|
width: 24.0,
|
|
|
|
|
height: 24.0,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
Expanded(
|
|
|
|
|
child: Padding(
|
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
|
child: widgets[index],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
2020-04-24 19:53:29 +02:00
|
|
|
),
|
|
|
|
|
onTap: () {
|
|
|
|
|
backend.player.skipTo(index);
|
|
|
|
|
},
|
|
|
|
|
);
|
2020-04-22 10:01:50 +02:00
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
return Container();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
),
|
2019-12-02 21:38:47 +01:00
|
|
|
bottomNavigationBar: BottomAppBar(
|
|
|
|
|
child: Column(
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
children: <Widget>[
|
|
|
|
|
Slider(
|
|
|
|
|
value: position,
|
|
|
|
|
onChangeStart: (_) {
|
|
|
|
|
seeking = true;
|
|
|
|
|
},
|
|
|
|
|
onChangeEnd: (pos) {
|
|
|
|
|
seeking = false;
|
2020-04-18 13:50:38 +02:00
|
|
|
backend.player.seekTo(pos);
|
2019-12-02 21:38:47 +01:00
|
|
|
},
|
|
|
|
|
onChanged: (pos) {
|
|
|
|
|
setState(() {
|
|
|
|
|
position = pos;
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
),
|
2019-12-02 22:05:07 +01:00
|
|
|
Row(
|
|
|
|
|
children: <Widget>[
|
2019-12-03 10:24:23 +01:00
|
|
|
Padding(
|
|
|
|
|
padding: const EdgeInsets.only(left: 24.0),
|
2020-04-18 13:50:38 +02:00
|
|
|
child: StreamBuilder<Duration>(
|
|
|
|
|
stream: backend.player.position,
|
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
|
if (snapshot.hasData) {
|
|
|
|
|
return DurationText(snapshot.data);
|
|
|
|
|
} else {
|
|
|
|
|
return Container();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
),
|
2019-12-03 10:24:23 +01:00
|
|
|
),
|
|
|
|
|
Spacer(),
|
2019-12-02 22:05:07 +01:00
|
|
|
IconButton(
|
|
|
|
|
icon: const Icon(Icons.skip_previous),
|
2020-04-22 10:01:50 +02:00
|
|
|
onPressed: () {
|
|
|
|
|
backend.player.skipToPrevious();
|
|
|
|
|
},
|
2019-12-02 22:05:07 +01:00
|
|
|
),
|
|
|
|
|
PlayPauseButton(),
|
|
|
|
|
IconButton(
|
|
|
|
|
icon: const Icon(Icons.skip_next),
|
2020-04-22 10:01:50 +02:00
|
|
|
onPressed: () {
|
|
|
|
|
backend.player.skipToNext();
|
|
|
|
|
},
|
2019-12-02 22:05:07 +01:00
|
|
|
),
|
2019-12-03 10:24:23 +01:00
|
|
|
Spacer(),
|
|
|
|
|
Padding(
|
|
|
|
|
padding: const EdgeInsets.only(right: 20.0),
|
2020-04-18 13:50:38 +02:00
|
|
|
child: StreamBuilder<Duration>(
|
|
|
|
|
stream: backend.player.duration,
|
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
|
if (snapshot.hasData) {
|
|
|
|
|
return DurationText(snapshot.data);
|
|
|
|
|
} else {
|
|
|
|
|
return Container();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
),
|
2019-12-03 10:24:23 +01:00
|
|
|
),
|
2019-12-02 22:05:07 +01:00
|
|
|
],
|
|
|
|
|
),
|
2019-12-02 21:38:47 +01:00
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
2019-12-02 21:05:49 +01:00
|
|
|
);
|
|
|
|
|
}
|
2019-12-02 21:38:47 +01:00
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
super.dispose();
|
2020-04-24 19:56:14 +02:00
|
|
|
playerActiveSubscription.cancel();
|
2020-04-24 19:53:29 +02:00
|
|
|
playlistSubscription.cancel();
|
2019-12-02 21:38:47 +01:00
|
|
|
positionSubscription.cancel();
|
|
|
|
|
}
|
2019-12-02 21:05:49 +01:00
|
|
|
}
|
2020-04-18 13:50:38 +02:00
|
|
|
|
|
|
|
|
class DurationText extends StatelessWidget {
|
|
|
|
|
final Duration duration;
|
|
|
|
|
|
|
|
|
|
DurationText(this.duration);
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
final minutes = duration.inMinutes;
|
|
|
|
|
final seconds = (duration - Duration(minutes: minutes)).inSeconds;
|
|
|
|
|
|
|
|
|
|
final secondsString = seconds >= 10 ? seconds.toString() : '0$seconds';
|
|
|
|
|
|
|
|
|
|
return Text('$minutes:$secondsString');
|
|
|
|
|
}
|
|
|
|
|
}
|