From ddeda2ce2365ab0f69fbadf0b7b3b5c808b30972 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Mon, 2 Sep 2024 20:42:33 +0800 Subject: [PATCH] :lipstick: Player optimization --- ios/Podfile.lock | 6 ++++++ lib/screens/explore.dart | 28 +++++++++++++++++++++++----- lib/screens/player/lyrics.dart | 25 ++++++++++++++++++++++++- lib/screens/player/mini.dart | 18 ++++++++++++++++++ lib/screens/player/view.dart | 1 - lib/services/database/database.dart | 16 +++++++++++++++- 6 files changed, 86 insertions(+), 8 deletions(-) diff --git a/ios/Podfile.lock b/ios/Podfile.lock index 84e11fb..3a2a41e 100644 --- a/ios/Podfile.lock +++ b/ios/Podfile.lock @@ -57,6 +57,8 @@ PODS: - sqlite3/rtree - url_launcher_ios (0.0.1): - Flutter + - wakelock_plus (0.0.1): + - Flutter DEPENDENCIES: - audio_service (from `.symlinks/plugins/audio_service/ios`) @@ -76,6 +78,7 @@ DEPENDENCIES: - sqflite (from `.symlinks/plugins/sqflite/darwin`) - sqlite3_flutter_libs (from `.symlinks/plugins/sqlite3_flutter_libs/ios`) - url_launcher_ios (from `.symlinks/plugins/url_launcher_ios/ios`) + - wakelock_plus (from `.symlinks/plugins/wakelock_plus/ios`) SPEC REPOS: trunk: @@ -117,6 +120,8 @@ EXTERNAL SOURCES: :path: ".symlinks/plugins/sqlite3_flutter_libs/ios" url_launcher_ios: :path: ".symlinks/plugins/url_launcher_ios/ios" + wakelock_plus: + :path: ".symlinks/plugins/wakelock_plus/ios" SPEC CHECKSUMS: audio_service: f509d65da41b9521a61f1c404dd58651f265a567 @@ -138,6 +143,7 @@ SPEC CHECKSUMS: sqlite3: 0bb0e6389d824e40296f531b858a2a0b71c0d2fb sqlite3_flutter_libs: c00457ebd31e59fa6bb830380ddba24d44fbcd3b url_launcher_ios: 5334b05cef931de560670eeae103fd3e431ac3fe + wakelock_plus: 78ec7c5b202cab7761af8e2b2b3d0671be6c4ae1 PODFILE CHECKSUM: 819463e6a0290f5a72f145ba7cde16e8b6ef0796 diff --git a/lib/screens/explore.dart b/lib/screens/explore.dart index 52e60bf..83865b6 100644 --- a/lib/screens/explore.dart +++ b/lib/screens/explore.dart @@ -42,20 +42,34 @@ class _ExploreScreenState extends State { _featuredPlaylist = (await _spotify.api.playlists.featured.getPage(20)).items!.toList(); - setState(() => _isLoading['featured'] = false); + if (mounted) { + setState(() => _isLoading['featured'] = false); + } else { + return; + } + final idxList = Set(); _recentlyPlaylist = (await _history.fetch()) .where((x) => x.playlist != null) .map((x) => x.playlist!) - .toList(); - setState(() => _isLoading['recently'] = false); + .toList() + ..retainWhere((x) => idxList.add(x.id!)); + if (mounted) { + setState(() => _isLoading['recently'] = false); + } else { + return; + } _newReleasesPlaylist = (await _spotify.api.browse.newReleases(country: market).getPage(20)) .items ?.map((album) => album.toAlbum()) .toList(); - setState(() => _isLoading['newReleases'] = false); + if (mounted) { + setState(() => _isLoading['newReleases'] = false); + } else { + return; + } final customEndpoint = CustomSpotifyEndpoints(_auth.auth.value?.accessToken.value ?? ''); @@ -65,7 +79,11 @@ class _ExploreScreenState extends State { locale: Intl.canonicalizedLocale(locale.toString()), ); _forYouView = forYouView['content']?['items']; - setState(() => _isLoading['forYou'] = false); + if (mounted) { + setState(() => _isLoading['forYou'] = false); + } else { + return; + } } @override diff --git a/lib/screens/player/lyrics.dart b/lib/screens/player/lyrics.dart index b2cb54b..e08a574 100644 --- a/lib/screens/player/lyrics.dart +++ b/lib/screens/player/lyrics.dart @@ -2,12 +2,35 @@ import 'dart:math'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; +import 'package:rhythm_box/providers/user_preferences.dart'; import 'package:rhythm_box/widgets/lyrics/synced_lyrics.dart'; import 'package:rhythm_box/widgets/player/bottom_player.dart'; +import 'package:wakelock_plus/wakelock_plus.dart'; -class LyricsScreen extends StatelessWidget { +class LyricsScreen extends StatefulWidget { const LyricsScreen({super.key}); + @override + State createState() => _LyricsScreenState(); +} + +class _LyricsScreenState extends State { + late final UserPreferencesProvider _preferences = Get.find(); + + @override + void activate() { + super.activate(); + if (_preferences.state.value.playerWakelock) { + WakelockPlus.enable(); + } + } + + @override + void deactivate() { + super.deactivate(); + WakelockPlus.disable(); + } + @override Widget build(BuildContext context) { return Material( diff --git a/lib/screens/player/mini.dart b/lib/screens/player/mini.dart index 5fe142a..4e609c3 100644 --- a/lib/screens/player/mini.dart +++ b/lib/screens/player/mini.dart @@ -2,11 +2,13 @@ import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:go_router/go_router.dart'; import 'package:rhythm_box/platform.dart'; +import 'package:rhythm_box/providers/user_preferences.dart'; import 'package:rhythm_box/screens/player/queue.dart'; import 'package:rhythm_box/screens/player/siblings.dart'; import 'package:rhythm_box/widgets/lyrics/synced_lyrics.dart'; import 'package:rhythm_box/widgets/player/bottom_player.dart'; import 'package:rhythm_box/widgets/player/devices.dart'; +import 'package:wakelock_plus/wakelock_plus.dart'; import 'package:window_manager/window_manager.dart'; class MiniPlayerScreen extends StatefulWidget { @@ -19,6 +21,8 @@ class MiniPlayerScreen extends StatefulWidget { } class _MiniPlayerScreenState extends State { + late final UserPreferencesProvider _preferences = Get.find(); + bool _wasMaximized = false; bool _areaActive = false; @@ -51,6 +55,20 @@ class _MiniPlayerScreenState extends State { } } + @override + void activate() { + super.activate(); + if (_preferences.state.value.playerWakelock) { + WakelockPlus.enable(); + } + } + + @override + void deactivate() { + super.deactivate(); + WakelockPlus.disable(); + } + @override void initState() { super.initState(); diff --git a/lib/screens/player/view.dart b/lib/screens/player/view.dart index 0a610de..dd71bae 100644 --- a/lib/screens/player/view.dart +++ b/lib/screens/player/view.dart @@ -34,7 +34,6 @@ class _PlayerScreenState extends State { late final AudioPlayerProvider _playback = Get.find(); late final QueryingTrackInfoProvider _query = Get.find(); late final AuthenticationProvider _auth = Get.find(); - late final UserPreferencesProvider _preferences = Get.find(); String? get _albumArt => diff --git a/lib/services/database/database.dart b/lib/services/database/database.dart index 0672745..50ddc7d 100755 --- a/lib/services/database/database.dart +++ b/lib/services/database/database.dart @@ -55,7 +55,21 @@ class AppDatabase extends _$AppDatabase { AppDatabase() : super(_openConnection()); @override - int get schemaVersion => 1; + int get schemaVersion => 2; + + @override + MigrationStrategy get migration { + return MigrationStrategy( + onCreate: (Migrator m) async { + await m.createAll(); + }, + onUpgrade: (Migrator m, int from, int to) async { + if (from < 2) { + await m.addColumn(preferencesTable, preferencesTable.playerWakelock); + } + }, + ); + } } LazyDatabase _openConnection() {