From 58e569864dddd74c3064624998dfc184046e97eb Mon Sep 17 00:00:00 2001 From: Kingkor Roy Tirtho Date: Sun, 29 Oct 2023 19:51:53 +0600 Subject: [PATCH] fix: last track of queue keeps repeating #718 --- lib/components/library/user_local_tracks.dart | 35 +---------------- .../shared/track_table/track_tile.dart | 4 +- lib/hooks/use_get_storage_perms.dart | 38 +++++++++++++++++++ lib/main.dart | 2 + .../audio_player/mk_state_player.dart | 3 -- 5 files changed, 44 insertions(+), 38 deletions(-) create mode 100644 lib/hooks/use_get_storage_perms.dart diff --git a/lib/components/library/user_local_tracks.dart b/lib/components/library/user_local_tracks.dart index 50ae64be7..c7cd06825 100644 --- a/lib/components/library/user_local_tracks.dart +++ b/lib/components/library/user_local_tracks.dart @@ -1,7 +1,6 @@ import 'dart:io'; import 'package:catcher_2/catcher_2.dart'; -import 'package:device_info_plus/device_info_plus.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; @@ -12,7 +11,6 @@ import 'package:metadata_god/metadata_god.dart'; import 'package:mime/mime.dart'; import 'package:path/path.dart'; import 'package:path_provider/path_provider.dart'; -import 'package:permission_handler/permission_handler.dart'; import 'package:spotify/spotify.dart'; import 'package:spotube/collections/spotube_icons.dart'; @@ -22,15 +20,12 @@ import 'package:spotube/components/shared/shimmers/shimmer_track_tile.dart'; import 'package:spotube/components/shared/sort_tracks_dropdown.dart'; import 'package:spotube/components/shared/track_table/track_tile.dart'; import 'package:spotube/extensions/context.dart'; -import 'package:spotube/hooks/use_async_effect.dart'; import 'package:spotube/models/local_track.dart'; import 'package:spotube/provider/proxy_playlist/proxy_playlist_provider.dart'; import 'package:spotube/provider/user_preferences_provider.dart'; -import 'package:spotube/utils/platform.dart'; import 'package:spotube/utils/service_utils.dart'; import 'package:spotube/utils/type_conversion_utils.dart'; -import 'package:flutter_rust_bridge/flutter_rust_bridge.dart' - show FfiException; +import 'package:flutter_rust_bridge/flutter_rust_bridge.dart' show FfiException; const supportedAudioTypes = [ "audio/webm", @@ -162,40 +157,12 @@ class UserLocalTracks extends HookConsumerWidget { final trackSnapshot = ref.watch(localTracksProvider); final isPlaylistPlaying = playlist.containsTracks(trackSnapshot.value ?? []); - final isMounted = useIsMounted(); final searchController = useTextEditingController(); useValueListenable(searchController); final searchFocus = useFocusNode(); final isFiltering = useState(false); - useAsyncEffect( - () async { - if (!kIsMobile) return; - - final androidInfo = await DeviceInfoPlugin().androidInfo; - - final hasNoStoragePerm = androidInfo.version.sdkInt < 33 && - !await Permission.storage.isGranted && - !await Permission.storage.isLimited; - - final hasNoAudioPerm = androidInfo.version.sdkInt >= 33 && - !await Permission.audio.isGranted && - !await Permission.audio.isLimited; - - if (hasNoStoragePerm) { - await Permission.storage.request(); - if (isMounted()) ref.refresh(localTracksProvider); - } - if (hasNoAudioPerm) { - await Permission.audio.request(); - if (isMounted()) ref.refresh(localTracksProvider); - } - }, - null, - [], - ); - return Column( children: [ Padding( diff --git a/lib/components/shared/track_table/track_tile.dart b/lib/components/shared/track_table/track_tile.dart index 0666b7f9f..ff1b314b0 100644 --- a/lib/components/shared/track_table/track_tile.dart +++ b/lib/components/shared/track_table/track_tile.dart @@ -91,7 +91,9 @@ class TrackTile extends HookConsumerWidget { isLoading.value = true; await onTap?.call(); } finally { - isLoading.value = false; + if (context.mounted) { + isLoading.value = false; + } } }, onLongPress: onLongPress, diff --git a/lib/hooks/use_get_storage_perms.dart b/lib/hooks/use_get_storage_perms.dart new file mode 100644 index 000000000..d83c60f64 --- /dev/null +++ b/lib/hooks/use_get_storage_perms.dart @@ -0,0 +1,38 @@ +import 'package:device_info_plus/device_info_plus.dart'; +import 'package:flutter_desktop_tools/flutter_desktop_tools.dart'; +import 'package:flutter_hooks/flutter_hooks.dart'; +import 'package:hooks_riverpod/hooks_riverpod.dart'; +import 'package:permission_handler/permission_handler.dart'; +import 'package:spotube/components/library/user_local_tracks.dart'; +import 'package:spotube/hooks/use_async_effect.dart'; + +void useGetStoragePermissions(WidgetRef ref) { + final isMounted = useIsMounted(); + + useAsyncEffect( + () async { + if (!DesktopTools.platform.isMobile) return; + + final androidInfo = await DeviceInfoPlugin().androidInfo; + + final hasNoStoragePerm = androidInfo.version.sdkInt < 33 && + !await Permission.storage.isGranted && + !await Permission.storage.isLimited; + + final hasNoAudioPerm = androidInfo.version.sdkInt >= 33 && + !await Permission.audio.isGranted && + !await Permission.audio.isLimited; + + if (hasNoStoragePerm) { + await Permission.storage.request(); + if (isMounted()) ref.refresh(localTracksProvider); + } + if (hasNoAudioPerm) { + await Permission.audio.request(); + if (isMounted()) ref.refresh(localTracksProvider); + } + }, + null, + [], + ); +} diff --git a/lib/main.dart b/lib/main.dart index f8c3aa8c7..b92dfaf19 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -15,6 +15,7 @@ import 'package:shared_preferences/shared_preferences.dart'; import 'package:spotube/collections/routes.dart'; import 'package:spotube/collections/intents.dart'; import 'package:spotube/hooks/use_disable_battery_optimizations.dart'; +import 'package:spotube/hooks/use_get_storage_perms.dart'; import 'package:spotube/l10n/l10n.dart'; import 'package:spotube/models/logger.dart'; import 'package:spotube/models/matched_track.dart'; @@ -181,6 +182,7 @@ class SpotubeState extends ConsumerState { }, []); useDisableBatteryOptimizations(); + useGetStoragePermissions(ref); final lightTheme = useMemoized( () => theme(paletteColor ?? accentMaterialColor, Brightness.light, false), diff --git a/lib/services/audio_player/mk_state_player.dart b/lib/services/audio_player/mk_state_player.dart index 386b0a0e2..af94a0e8f 100644 --- a/lib/services/audio_player/mk_state_player.dart +++ b/lib/services/audio_player/mk_state_player.dart @@ -174,9 +174,6 @@ class MkPlayerWithState extends Player { case PlaylistMode.none: // Fixes auto-repeating the last track await super.stop(); - await Future.delayed(const Duration(seconds: 2), () { - super.open(_playlist!.medias[_playlist!.index], play: false); - }); break; default: }