2023-07-29 02:17:26 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2023-10-12 22:09:37 +00:00
|
|
|
import 'package:async/async.dart';
|
2023-07-29 02:17:26 +00:00
|
|
|
import 'package:audio_service/audio_service.dart';
|
2024-01-24 17:55:25 +00:00
|
|
|
import 'package:dio/dio.dart';
|
2023-07-29 02:17:26 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/scheduler.dart';
|
|
|
|
import 'package:freezer/api/deezer.dart';
|
|
|
|
import 'package:freezer/api/definitions.dart';
|
2023-10-18 15:08:05 +00:00
|
|
|
import 'package:freezer/api/player/audio_handler.dart';
|
2023-07-29 02:17:26 +00:00
|
|
|
import 'package:freezer/settings.dart';
|
|
|
|
import 'package:freezer/translations.i18n.dart';
|
|
|
|
import 'package:freezer/ui/error.dart';
|
|
|
|
import 'package:freezer/ui/player_bar.dart';
|
|
|
|
import 'package:freezer/ui/player_screen.dart';
|
|
|
|
|
2023-10-16 22:22:50 +00:00
|
|
|
class LyricsScreen extends StatelessWidget {
|
|
|
|
const LyricsScreen({super.key});
|
2023-07-29 02:17:26 +00:00
|
|
|
|
|
|
|
@override
|
2023-10-16 22:22:50 +00:00
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return PlayerScreenBackground(
|
|
|
|
enabled: settings.playerBackgroundOnLyrics,
|
|
|
|
appBar: AppBar(
|
|
|
|
title: Text('Lyrics'.i18n),
|
|
|
|
systemOverlayStyle: PlayerScreenBackground.getSystemUiOverlayStyle(
|
|
|
|
context,
|
|
|
|
enabled: settings.playerBackgroundOnLyrics),
|
|
|
|
backgroundColor: Colors.transparent,
|
|
|
|
),
|
|
|
|
child: const Column(
|
|
|
|
children: [
|
2023-10-16 22:57:55 +00:00
|
|
|
Expanded(child: LyricsWidget()),
|
2023-10-16 22:22:50 +00:00
|
|
|
Divider(height: 1.0, thickness: 1.0),
|
|
|
|
PlayerBar(backgroundColor: Colors.transparent),
|
|
|
|
],
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class LyricsWidget extends StatefulWidget {
|
|
|
|
const LyricsWidget({Key? key}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<LyricsWidget> createState() => _LyricsWidgetState();
|
2023-07-29 02:17:26 +00:00
|
|
|
}
|
|
|
|
|
2024-01-28 11:02:59 +00:00
|
|
|
class _LyricsWidgetState extends State<LyricsWidget>
|
|
|
|
with WidgetsBindingObserver {
|
2023-07-29 02:17:26 +00:00
|
|
|
late StreamSubscription _mediaItemSub;
|
|
|
|
late StreamSubscription _playbackStateSub;
|
|
|
|
int? _currentIndex = -1;
|
2024-01-24 17:55:25 +00:00
|
|
|
Duration _nextOffset = Duration.zero;
|
|
|
|
Duration _currentOffset = Duration.zero;
|
2024-01-28 11:02:59 +00:00
|
|
|
String? _currentTrackId;
|
2023-10-12 22:09:37 +00:00
|
|
|
final ScrollController _controller = ScrollController();
|
2023-07-29 02:17:26 +00:00
|
|
|
final double height = 90;
|
2023-10-16 22:22:50 +00:00
|
|
|
BoxConstraints? _widgetConstraints;
|
2023-10-12 22:09:37 +00:00
|
|
|
Lyrics? _lyrics;
|
2023-07-29 02:17:26 +00:00
|
|
|
bool _loading = true;
|
2024-01-24 17:55:25 +00:00
|
|
|
CancelToken? _lyricsCancelToken;
|
2023-07-29 02:17:26 +00:00
|
|
|
Object? _error;
|
|
|
|
|
|
|
|
bool _freeScroll = false;
|
|
|
|
bool _animatedScroll = false;
|
2023-09-26 00:06:59 +00:00
|
|
|
bool _syncedLyrics = false;
|
2023-07-29 02:17:26 +00:00
|
|
|
|
2023-09-26 00:06:59 +00:00
|
|
|
Future<void> _loadForId(String trackId) async {
|
2024-01-28 11:02:59 +00:00
|
|
|
if (_currentTrackId == trackId) return;
|
|
|
|
_currentTrackId = trackId;
|
|
|
|
print('cancelling req?');
|
2023-10-12 22:09:37 +00:00
|
|
|
// cancel current request, if applicable
|
2024-01-24 17:55:25 +00:00
|
|
|
_lyricsCancelToken?.cancel();
|
2024-01-28 11:02:59 +00:00
|
|
|
|
2024-01-24 17:55:25 +00:00
|
|
|
_currentIndex = -1;
|
|
|
|
_currentOffset = Duration.zero;
|
|
|
|
_nextOffset = Duration.zero;
|
2023-10-12 22:09:37 +00:00
|
|
|
|
2023-07-29 02:17:26 +00:00
|
|
|
//Fetch
|
2023-10-12 22:09:37 +00:00
|
|
|
if (_loading == false && _lyrics != null) {
|
2023-07-29 02:17:26 +00:00
|
|
|
setState(() {
|
|
|
|
_freeScroll = false;
|
|
|
|
_loading = true;
|
2023-10-12 22:09:37 +00:00
|
|
|
_lyrics = null;
|
2023-07-29 02:17:26 +00:00
|
|
|
});
|
|
|
|
}
|
2023-10-12 22:09:37 +00:00
|
|
|
|
2023-07-29 02:17:26 +00:00
|
|
|
try {
|
2024-01-24 17:55:25 +00:00
|
|
|
_lyricsCancelToken = CancelToken();
|
|
|
|
final lyrics =
|
|
|
|
await deezerAPI.lyrics(trackId, cancelToken: _lyricsCancelToken);
|
2024-01-28 11:02:59 +00:00
|
|
|
|
2023-10-12 22:09:37 +00:00
|
|
|
_syncedLyrics = lyrics.sync;
|
2023-09-26 00:06:59 +00:00
|
|
|
if (!mounted) return;
|
2023-07-29 02:17:26 +00:00
|
|
|
setState(() {
|
|
|
|
_loading = false;
|
2023-10-12 22:09:37 +00:00
|
|
|
_lyrics = lyrics;
|
2023-07-29 02:17:26 +00:00
|
|
|
});
|
2023-10-16 22:22:50 +00:00
|
|
|
|
|
|
|
SchedulerBinding.instance.addPostFrameCallback(
|
|
|
|
(_) => _updatePosition(audioHandler.playbackState.value.position));
|
2024-01-24 17:55:25 +00:00
|
|
|
} on DioException catch (e) {
|
|
|
|
if (e.type != DioExceptionType.cancel) rethrow;
|
2023-07-29 02:17:26 +00:00
|
|
|
} catch (e) {
|
2024-01-28 11:02:59 +00:00
|
|
|
_currentTrackId = null;
|
2023-09-26 00:06:59 +00:00
|
|
|
if (!mounted) return;
|
2023-07-29 02:17:26 +00:00
|
|
|
setState(() {
|
|
|
|
_error = e;
|
|
|
|
});
|
2024-01-28 11:02:59 +00:00
|
|
|
} finally {
|
|
|
|
_lyricsCancelToken =
|
|
|
|
null; // dispose of cancel token after lyrics are fetched.
|
2023-07-29 02:17:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-24 22:32:28 +00:00
|
|
|
void _scrollToLyric() {
|
2023-07-29 02:17:26 +00:00
|
|
|
if (!_controller.hasClients) return;
|
|
|
|
//Lyric height, screen height, appbar height
|
2023-10-16 22:22:50 +00:00
|
|
|
double scrollTo;
|
|
|
|
if (_widgetConstraints == null) {
|
|
|
|
scrollTo = (height * _currentIndex!) -
|
|
|
|
(MediaQuery.of(context).size.height / 4 + height / 2);
|
|
|
|
} else {
|
|
|
|
final widgetHeight = _widgetConstraints!.maxHeight;
|
|
|
|
final minScroll = height * _currentIndex!;
|
|
|
|
scrollTo = minScroll - widgetHeight / 2 + height / 2;
|
|
|
|
}
|
2023-07-29 02:17:26 +00:00
|
|
|
|
2023-10-24 22:32:28 +00:00
|
|
|
if (scrollTo < 0.0) scrollTo = 0.0;
|
2023-10-12 22:09:37 +00:00
|
|
|
if (scrollTo > _controller.position.maxScrollExtent) {
|
|
|
|
scrollTo = _controller.position.maxScrollExtent;
|
|
|
|
}
|
2023-07-29 02:17:26 +00:00
|
|
|
_animatedScroll = true;
|
2023-10-24 22:32:28 +00:00
|
|
|
_controller
|
|
|
|
.animateTo(scrollTo,
|
|
|
|
duration: const Duration(milliseconds: 250), curve: Curves.ease)
|
|
|
|
.then((_) => _animatedScroll = false);
|
2023-07-29 02:17:26 +00:00
|
|
|
}
|
|
|
|
|
2023-10-16 22:22:50 +00:00
|
|
|
void _updatePosition(Duration position) {
|
|
|
|
if (_loading) return;
|
|
|
|
if (!_syncedLyrics) return;
|
2024-01-24 17:55:25 +00:00
|
|
|
if (position < _nextOffset && position > _currentOffset) return;
|
2023-10-24 22:32:28 +00:00
|
|
|
|
2023-10-16 22:22:50 +00:00
|
|
|
_currentIndex =
|
|
|
|
_lyrics?.lyrics?.lastIndexWhere((l) => l.offset! <= position);
|
|
|
|
if (_currentIndex! < 0) return;
|
2024-01-24 17:55:25 +00:00
|
|
|
|
|
|
|
if (_currentIndex! < _lyrics!.lyrics!.length - 1) {
|
|
|
|
// update nextOffset
|
|
|
|
_nextOffset = _lyrics!.lyrics![_currentIndex! + 1].offset!;
|
2023-10-24 22:32:28 +00:00
|
|
|
} else {
|
|
|
|
// dummy position so that the before-hand condition always returns false
|
2024-01-24 17:55:25 +00:00
|
|
|
_nextOffset = const Duration(days: 69);
|
2023-10-24 22:32:28 +00:00
|
|
|
}
|
|
|
|
|
2024-01-24 17:55:25 +00:00
|
|
|
_currentOffset = _lyrics!.lyrics![_currentIndex!].offset!;
|
|
|
|
|
2023-10-24 22:32:28 +00:00
|
|
|
setState(() => _currentIndex);
|
2023-10-16 22:22:50 +00:00
|
|
|
if (_freeScroll) return;
|
|
|
|
_scrollToLyric();
|
|
|
|
}
|
|
|
|
|
2024-01-28 11:02:59 +00:00
|
|
|
void _makeSubscriptions() {
|
|
|
|
_playbackStateSub = AudioService.position.listen(_updatePosition);
|
2023-07-29 02:17:26 +00:00
|
|
|
|
2024-01-28 11:02:59 +00:00
|
|
|
/// Track change = reload new lyrics
|
2023-07-29 02:17:26 +00:00
|
|
|
_mediaItemSub = audioHandler.mediaItem.listen((mediaItem) {
|
|
|
|
if (mediaItem == null) return;
|
|
|
|
if (_controller.hasClients) _controller.jumpTo(0.0);
|
|
|
|
_loadForId(mediaItem.id);
|
|
|
|
});
|
2024-01-28 11:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
SchedulerBinding.instance.addPostFrameCallback((_) {
|
|
|
|
//Enable visualizer
|
|
|
|
// if (settings.lyricsVisualizer) playerHelper.startVisualizer();
|
|
|
|
_makeSubscriptions();
|
|
|
|
});
|
2023-07-29 02:17:26 +00:00
|
|
|
|
2024-01-28 11:02:59 +00:00
|
|
|
WidgetsBinding.instance.addObserver(this);
|
2023-07-29 02:17:26 +00:00
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2024-01-28 11:02:59 +00:00
|
|
|
@override
|
|
|
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
|
|
|
switch (state) {
|
|
|
|
case AppLifecycleState.paused:
|
|
|
|
_mediaItemSub.cancel();
|
|
|
|
_playbackStateSub.cancel();
|
|
|
|
break;
|
|
|
|
case AppLifecycleState.resumed:
|
|
|
|
_makeSubscriptions();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-29 02:17:26 +00:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_mediaItemSub.cancel();
|
|
|
|
_playbackStateSub.cancel();
|
2024-01-28 11:02:59 +00:00
|
|
|
WidgetsBinding.instance.removeObserver(this);
|
2023-07-29 02:17:26 +00:00
|
|
|
//Stop visualizer
|
|
|
|
// if (settings.lyricsVisualizer) playerHelper.stopVisualizer();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2023-10-16 22:22:50 +00:00
|
|
|
ScrollBehavior get _scrollBehavior {
|
|
|
|
if (_freeScroll) {
|
|
|
|
return ScrollConfiguration.of(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ScrollConfiguration.of(context).copyWith(scrollbars: false);
|
|
|
|
}
|
|
|
|
|
2023-07-29 02:17:26 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-10-16 22:22:50 +00:00
|
|
|
return Column(children: [
|
|
|
|
if (_freeScroll && !_loading)
|
|
|
|
Center(
|
|
|
|
child: TextButton(
|
|
|
|
onPressed: () {
|
|
|
|
setState(() => _freeScroll = false);
|
|
|
|
_scrollToLyric();
|
|
|
|
},
|
|
|
|
style: ButtonStyle(
|
|
|
|
foregroundColor: MaterialStateProperty.all(Colors.white)),
|
|
|
|
child: Text(
|
|
|
|
_currentIndex! >= 0
|
|
|
|
? (_lyrics?.lyrics?[_currentIndex!].text ?? '...')
|
|
|
|
: '...',
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
)),
|
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: _error != null
|
|
|
|
?
|
|
|
|
//Shouldn't really happen, empty lyrics have own text
|
|
|
|
ErrorScreen(message: _error.toString())
|
|
|
|
:
|
|
|
|
// Loading lyrics
|
|
|
|
_loading
|
|
|
|
? const Center(child: CircularProgressIndicator())
|
|
|
|
: LayoutBuilder(builder: (context, constraints) {
|
|
|
|
_widgetConstraints = constraints;
|
|
|
|
return NotificationListener<ScrollStartNotification>(
|
|
|
|
onNotification: (ScrollStartNotification notification) {
|
|
|
|
if (!_syncedLyrics) return false;
|
|
|
|
final extentDiff =
|
|
|
|
(notification.metrics.extentBefore -
|
|
|
|
notification.metrics.extentAfter)
|
|
|
|
.abs();
|
|
|
|
// avoid accidental clicks
|
|
|
|
const extentThreshold = 10.0;
|
|
|
|
if (extentDiff >= extentThreshold &&
|
|
|
|
!_animatedScroll &&
|
|
|
|
!_loading &&
|
|
|
|
!_freeScroll) {
|
|
|
|
setState(() => _freeScroll = true);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
child: ScrollConfiguration(
|
|
|
|
behavior: _scrollBehavior,
|
|
|
|
child: ListView.builder(
|
|
|
|
controller: _controller,
|
|
|
|
itemCount: _lyrics!.lyrics!.length,
|
|
|
|
itemBuilder: (BuildContext context, int i) {
|
|
|
|
return Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
horizontal: 8.0),
|
|
|
|
child: Container(
|
|
|
|
decoration: BoxDecoration(
|
2023-07-29 02:17:26 +00:00
|
|
|
borderRadius:
|
|
|
|
BorderRadius.circular(8.0),
|
2023-10-16 22:22:50 +00:00
|
|
|
color: _currentIndex == i
|
|
|
|
? Colors.grey.withOpacity(0.25)
|
|
|
|
: Colors.transparent,
|
|
|
|
),
|
|
|
|
height: _syncedLyrics ? height : null,
|
|
|
|
child: InkWell(
|
|
|
|
borderRadius:
|
|
|
|
BorderRadius.circular(8.0),
|
|
|
|
onTap: _syncedLyrics &&
|
|
|
|
_lyrics!.id != null
|
|
|
|
? () => audioHandler.seek(
|
|
|
|
_lyrics!.lyrics![i].offset!)
|
|
|
|
: null,
|
|
|
|
child: Center(
|
|
|
|
child: Padding(
|
|
|
|
padding: _currentIndex == i
|
|
|
|
? EdgeInsets.zero
|
|
|
|
: const EdgeInsets
|
|
|
|
.symmetric(
|
|
|
|
horizontal: 1.0),
|
|
|
|
child: Text(
|
|
|
|
_lyrics!.lyrics![i].text!,
|
|
|
|
textAlign: _syncedLyrics
|
|
|
|
? TextAlign.center
|
|
|
|
: TextAlign.start,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: _syncedLyrics
|
|
|
|
? 26.0
|
|
|
|
: 20.0,
|
|
|
|
fontWeight:
|
|
|
|
(_currentIndex == i)
|
|
|
|
? FontWeight.bold
|
|
|
|
: FontWeight
|
|
|
|
.normal),
|
|
|
|
),
|
2023-09-26 00:06:59 +00:00
|
|
|
),
|
2023-10-16 22:22:50 +00:00
|
|
|
))));
|
|
|
|
},
|
|
|
|
)));
|
|
|
|
}),
|
2023-07-29 02:17:26 +00:00
|
|
|
),
|
2023-10-16 22:22:50 +00:00
|
|
|
]);
|
2023-07-29 02:17:26 +00:00
|
|
|
}
|
|
|
|
}
|