Skip to content

Commit

Permalink
fix: last track of queue keeps repeating #718
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Oct 29, 2023
1 parent 286ef83 commit 58e5698
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 38 deletions.
35 changes: 1 addition & 34 deletions lib/components/library/user_local_tracks.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';
Expand All @@ -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",
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion lib/components/shared/track_table/track_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
38 changes: 38 additions & 0 deletions lib/hooks/use_get_storage_perms.dart
Original file line number Diff line number Diff line change
@@ -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,
[],
);
}
2 changes: 2 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -181,6 +182,7 @@ class SpotubeState extends ConsumerState<Spotube> {
}, []);

useDisableBatteryOptimizations();
useGetStoragePermissions(ref);

final lightTheme = useMemoized(
() => theme(paletteColor ?? accentMaterialColor, Brightness.light, false),
Expand Down
3 changes: 0 additions & 3 deletions lib/services/audio_player/mk_state_player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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:
}
Expand Down

0 comments on commit 58e5698

Please sign in to comment.