Skip to content

Commit

Permalink
chore: few tweaks
Browse files Browse the repository at this point in the history
- rely on "play when ready" instead of "is playing", results in more stability when skipping next/prev
- remove refresh button in video widget
-refactor snackbar duration & less duration for "added to queue" snackbar
- use package to show time difference in about page
  • Loading branch information
MSOB7YY committed Feb 1, 2025
1 parent 004dac3 commit b0c1d0e
Show file tree
Hide file tree
Showing 25 changed files with 188 additions and 192 deletions.
37 changes: 17 additions & 20 deletions lib/base/audio_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
late final equalizer = AndroidEqualizer();
late final loudnessEnhancer = AndroidLoudnessEnhancer();

bool get _willPlayWhenReady => playWhenReady.value;

RxBaseCore<Duration?> get currentItemDuration => _currentItemDuration;
final _currentItemDuration = Rxn<Duration>();

Expand Down Expand Up @@ -429,7 +431,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
);
},
onRapidDetected: () {
if (isPlaying.value) {
if (playWhenReady.value) {
_pausedTemporarily = true;
pause();
}
Expand Down Expand Up @@ -542,8 +544,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
final hadPermissionBefore = await Permission.manageExternalStorage.isGranted;
if (checkInterrupted()) return;
if (hadPermissionBefore) {
final wasPlayWhenReady = willPlayWhenReady;
onPauseRaw().then((_) => setPlayWhenReady(wasPlayWhenReady));
onPauseRaw();
cancelPlayErrorSkipTimer();
playErrorRemainingSecondsToSkip.value = 7;

Expand Down Expand Up @@ -600,7 +601,6 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
_nextSeekSetAudioCache = null;
_nextSeekSetVideoCache = null;

final wasPlayWhenReady = willPlayWhenReady;
setAudioOnlyPlayback(false);

currentVideoStream.value = stream;
Expand All @@ -612,7 +612,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
currentCachedVideo.value = videoItem;
await setVideoSource(source: AudioVideoSource.file(cachedFile.path), isFile: true);
} else if (stream != null) {
if (wasPlayWhenReady) await onPauseRaw();
if (!_willPlayWhenReady) await onPauseRaw();
final positionToRestore = currentPositionMS.value.milliseconds;

final bool expired = mainStreams?.hasExpired() ?? true;
Expand Down Expand Up @@ -679,7 +679,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
? await setSource(
activeAudioSource,
item: curritem,
startPlaying: () => willPlayWhenReady,
startPlaying: () => _willPlayWhenReady,
initialPosition: positionToRestore,
videoOptions: videoOptions,
keepOldVideoSource: false,
Expand Down Expand Up @@ -717,7 +717,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
}
}

if (wasPlayWhenReady) onPlayRaw();
if (_willPlayWhenReady) onPlayRaw();
}
}

Expand All @@ -731,8 +731,6 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
_nextSeekSetAudioCache = null;
_nextSeekSetVideoCache = null;

final wasPlayWhenReady = willPlayWhenReady;

currentAudioStream.value = stream;
mainStreams ??= YoutubeInfoController.video.fetchVideoStreamsSync(videoId) ?? YoutubeInfoController.current.currentYTStreams.value;

Expand All @@ -742,13 +740,13 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
await setSource(
AudioVideoSource.file(cachedAudio.path),
item: currentItem.value,
startPlaying: () => willPlayWhenReady,
startPlaying: () => _willPlayWhenReady,
keepOldVideoSource: true,
cachedAudioPath: cachedAudio.path,
);
refreshNotification();
} else if (stream != null) {
if (wasPlayWhenReady) await super.onPauseRaw();
if (!_willPlayWhenReady) await super.onPauseRaw();

final positionToRestore = currentPositionMS.value.milliseconds;

Expand All @@ -766,7 +764,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
),
initialPosition: positionToRestore,
item: currentItem.value,
startPlaying: () => willPlayWhenReady,
startPlaying: () => _willPlayWhenReady,
keepOldVideoSource: true,
);
refreshNotification();
Expand Down Expand Up @@ -799,7 +797,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
}
}

if (wasPlayWhenReady) onPlayRaw();
if (_willPlayWhenReady) onPlayRaw();
}
}

Expand Down Expand Up @@ -1212,7 +1210,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
final extraReasons = [playabilty.reason, ...?playabilty.messages].whereType<String>();
final extraReasonsText = extraReasons.isEmpty ? '' : ' | ${extraReasons.join(' | ')}';
snackyy(title: lang.ERROR, message: 'Empty audio streams. playabilty: `${playabilty.status.name}`$extraReasonsText', top: false, isError: true);
if (willPlayWhenReady) skipItem();
if (_willPlayWhenReady) skipItem();
}
return;
} else {
Expand Down Expand Up @@ -1740,7 +1738,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
// ------------------------------------------------------------

Future<void> togglePlayPause() {
if (isPlaying.value) {
if (playWhenReady.value) {
return pause();
} else {
return play();
Expand All @@ -1754,7 +1752,6 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
await currentItem.value?._execute(
selectable: (finalItem) => plsSeek(),
youtubeID: (finalItem) async {
final wasPlayWhenReady = willPlayWhenReady;
File? cachedAudioFile = _nextSeekSetAudioCache?.getFileIfPlaying(finalItem.id);
File? cachedVideoFile = _nextSeekSetVideoCache?.getFileIfPlaying(finalItem.id);
if (cachedAudioFile != null || cachedVideoFile != null) {
Expand All @@ -1781,7 +1778,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
item: currentItem.value,
keepOldVideoSource: true,
cachedAudioPath: cachedAudioFile.path,
startPlaying: () => willPlayWhenReady,
startPlaying: () => _willPlayWhenReady,
videoOptions: _isAudioOnlyPlayback
? null
: VideoSourceOptions(
Expand All @@ -1807,15 +1804,15 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
item: currentItem.value,
keepOldVideoSource: true,
cachedAudioPath: cachedAudioFile.path,
startPlaying: () => willPlayWhenReady,
startPlaying: () => _willPlayWhenReady,
);

_isCurrentAudioFromCache = true;
}
// <=======>

await plsSeek();
if (wasPlayWhenReady) onPlayRaw();
if (_willPlayWhenReady) onPlayRaw();
} else {
await plsSeek();
}
Expand Down Expand Up @@ -1880,7 +1877,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
_headsetButtonClickTimer?.cancel();

if (_headsetClicksCount == 1) {
_headsetButtonClickTimer = _createHeadsetClicksTimer(willPlayWhenReady ? pause : play);
_headsetButtonClickTimer = _createHeadsetClicksTimer(_willPlayWhenReady ? pause : play);
} else if (_headsetClicksCount == 2) {
_headsetButtonClickTimer = _createHeadsetClicksTimer(skipToNext);
} else if (_headsetClicksCount == 3) {
Expand Down
18 changes: 16 additions & 2 deletions lib/controller/navigator_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,20 @@ class NamidaNavigator {
}
}

enum SnackDisplayDuration {
flash(500),
short(1000),
mediumLow(1500),
medium(2000),
mediumHigh(2500),
long(3000),
veryLong(4000),
eternal(5000);

final int milliseconds;
const SnackDisplayDuration(this.milliseconds);
}

SnackbarController? snackyy({
IconData? icon,
String title = '',
Expand All @@ -499,7 +513,7 @@ SnackbarController? snackyy({
EdgeInsets margin = const EdgeInsets.symmetric(horizontal: 24.0, vertical: 8.0),
bool altDesign = false,
int animationDurationMS = 600,
int displaySeconds = 2,
SnackDisplayDuration displayDuration = SnackDisplayDuration.medium,
double borderRadius = 12.0,
Color? leftBarIndicatorColor,
Color? borderColor,
Expand Down Expand Up @@ -608,7 +622,7 @@ SnackbarController? snackyy({
);
final snackbar = NamSnackBar(
margin: margin,
duration: Duration(seconds: displaySeconds),
duration: Duration(milliseconds: displayDuration.milliseconds),
animationDuration: Duration(milliseconds: animationDurationMS),
alignment: Alignment.centerLeft,
top: top,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ class _NamidaChannelAndroid extends NamidaChannel {
@override
Future<void> showToast({
required String message,
int seconds = 5,
required SnackDisplayDuration duration,
}) async {
final seconds = (duration.milliseconds / 1000).ceil();
_channel.invokeMethod(
'showToast',
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ abstract class NamidaChannel {

Future<void> setCanEnterPip(bool canEnter);

Future<void> showToast({required String message, int seconds = 5});
Future<void> showToast({required String message, required SnackDisplayDuration duration});

Future<int> getPlatformSdk();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ class _NamidaChannelWindows extends NamidaChannel {
@override
Future<void> showToast({
required String message,
int seconds = 5,
required SnackDisplayDuration duration,
}) async {
// -- use in-app toast
snackyy(message: message, displaySeconds: seconds);
snackyy(message: message, displayDuration: duration);
}

@override
Expand Down
11 changes: 8 additions & 3 deletions lib/controller/player_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class Player {

Map<String, List<AudioCacheDetails>> get audioCacheMap => _audioHandler.audioCacheMap;

RxBaseCore<bool> get playWhenReady => _audioHandler.playWhenReady;

Selectable? get currentTrack {
final item = _audioHandler.currentItem.value;
return item is Selectable ? item : null;
Expand Down Expand Up @@ -112,7 +114,6 @@ class Player {
RxBaseCore<double> get currentSpeed => _audioHandler.currentSpeed;
RxBaseCore<Duration?> get currentItemDuration => _audioHandler.currentItemDuration;
RxBaseCore<bool> get isPlaying => _audioHandler.isPlaying;
bool get isPlayingR => _audioHandler.isPlaying.valueR;
bool get isBufferingR => _audioHandler.currentState.valueR == ProcessingState.buffering;
bool get isLoadingR => _audioHandler.currentState.valueR == ProcessingState.loading;
RxBaseCore<bool> get isFetchingInfo => _audioHandler.isFetchingInfo;
Expand Down Expand Up @@ -372,6 +373,8 @@ class Player {
icon: shouldInsertNext ? Broken.redo : Broken.add_circle,
message: '${addins.capitalizeFirst()} ${finalTracks.displayTrackKeyword}',
top: false,
displayDuration: SnackDisplayDuration.mediumLow,
animationDurationMS: 400,
);
}
return true;
Expand All @@ -395,6 +398,8 @@ class Player {
icon: shouldInsertNext ? Broken.redo : Broken.add_circle,
message: '${addins.capitalizeFirst()} ${finalVideos.length.displayVideoKeyword}',
top: false,
displayDuration: SnackDisplayDuration.mediumLow,
animationDurationMS: 400,
);
}
return true;
Expand Down Expand Up @@ -425,8 +430,8 @@ class Player {
}

Future<void> removeFromQueue(int index) async {
// why [isPlaying] ? imagine removing while paused
await _audioHandler.removeFromQueue(index, isPlaying.value && _audioHandler.defaultShouldStartPlayingOnNextPrev);
// why [playWhenReady] ? imagine removing while paused
await _audioHandler.removeFromQueue(index, playWhenReady.value && _audioHandler.defaultShouldStartPlayingOnNextPrev);
}

Future<void> replaceAllTracksInQueue(Playable oldTrack, Playable newTrack) async {
Expand Down
2 changes: 1 addition & 1 deletion lib/controller/video_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ class VideoController {
if (returnEarly) return erabaretaVideo;

if (erabaretaVideo == null) {
if ((vpsInSettings == VideoPlaybackSource.local) == false) {
if (vpsInSettings == VideoPlaybackSource.local) {
videoBlockedByType.value = VideoFetchBlockedBy.playbackSource;
} else if (!ConnectivityController.inst.hasConnection) {
videoBlockedByType.value = VideoFetchBlockedBy.noNetwork;
Expand Down
4 changes: 2 additions & 2 deletions lib/core/functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class NamidaOnTaps {
snackyy(
title: lang.UNDO_CHANGES,
message: lang.UNDO_CHANGES_DELETED_QUEUE,
displaySeconds: 3,
displayDuration: SnackDisplayDuration.long,
button: (
lang.UNDO,
() async => await QueueController.inst.reAddQueue(oldQueue),
Expand All @@ -183,7 +183,7 @@ class NamidaOnTaps {
snackyy(
title: lang.UNDO_CHANGES,
message: lang.UNDO_CHANGES_DELETED_TRACK,
displaySeconds: 3,
displayDuration: SnackDisplayDuration.long,
button: (
lang.UNDO,
whatDoYouWant,
Expand Down
4 changes: 2 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ void _initErrorInterpreters() {
void _initLifeCycle() {
NamidaChannel.inst.addOnDestroy('main', () async {
final mode = settings.player.killAfterDismissingApp.value;
if (mode == KillAppMode.always || (mode == KillAppMode.ifNotPlaying && !Player.inst.isPlaying.value)) {
if (mode == KillAppMode.always || (mode == KillAppMode.ifNotPlaying && !Player.inst.playWhenReady.value)) {
await Player.inst.pause();
await Player.inst.dispose();
}
Expand Down Expand Up @@ -424,7 +424,7 @@ Future<bool> requestIgnoreBatteryOptimizations() async {

snackyy(
message: lang.IGNORE_BATTERY_OPTIMIZATIONS_SUBTITLE,
displaySeconds: 5,
displayDuration: SnackDisplayDuration.eternal,
top: false,
isError: true,
button: (
Expand Down
8 changes: 4 additions & 4 deletions lib/packages/lyrics_lrc_parsed_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,10 @@ class LyricsLRCParsedViewState extends State<LyricsLRCParsedView> {
onPressed: Player.inst.previous,
),
ObxO(
rx: Player.inst.isPlaying,
builder: (context, isPlaying) => NamidaIconButton(
rx: Player.inst.playWhenReady,
builder: (context, playWhenReady) => NamidaIconButton(
horizontalPadding: 18.0,
icon: isPlaying ? Broken.pause : Broken.play,
icon: playWhenReady ? Broken.pause : Broken.play,
iconSize: 32.0,
onPressed: Player.inst.togglePlayPause,
),
Expand Down Expand Up @@ -393,7 +393,7 @@ class LyricsLRCParsedViewState extends State<LyricsLRCParsedView> {
onPointerUp: (event) {
_scrollTimer = Timer(const Duration(seconds: 3), () {
_canAnimateScroll = true;
if (Player.inst.isPlaying.value) {
if (Player.inst.playWhenReady.value) {
_updateHighlightedLine(Player.inst.nowPlayingPosition.value, forceAnimate: true);
}
if (_updateOpacityForEmptyLines && currentLRC != null && _checkIfTextEmpty(_currentLine)) {
Expand Down
6 changes: 3 additions & 3 deletions lib/packages/miniplayer_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -976,10 +976,10 @@ class _NamidaMiniPlayerBaseState extends State<NamidaMiniPlayerBase> {
icon: Padding(
padding: EdgeInsets.all(6.0 * cp * rcp),
child: ObxO(
rx: Player.inst.isPlaying,
builder: (context, isPlaying) => AnimatedSwitcher(
rx: Player.inst.playWhenReady,
builder: (context, playWhenReady) => AnimatedSwitcher(
duration: const Duration(milliseconds: 200),
child: isPlaying
child: playWhenReady
? Icon(
Broken.pause,
size: iconSize,
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/dialogs/general_popup_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ Future<void> showGeneralPopupDialog(
snackyy(
title: lang.UNDO_CHANGES,
message: lang.UNDO_CHANGES_DELETED_PLAYLIST,
displaySeconds: 3,
displayDuration: SnackDisplayDuration.long,
button: (
lang.UNDO,
() async => await PlaylistController.inst.reAddPlaylist(pl, pl.modifiedDate, artworkBytes: artworkBytes),
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/dialogs/track_info_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Future<void> showTrackInfoDialog(
bool shouldShowTheField(bool isUnknown) => !isUnknown || (settings.showUnknownFieldsInTrackInfoDialog.value && isUnknown);

void showPreviewTrackDialog() async {
final wasPlaying = Player.inst.isPlaying.value;
final wasPlaying = Player.inst.playWhenReady.value;
if (wasPlaying) {
Player.inst.pause();
}
Expand Down
Loading

0 comments on commit b0c1d0e

Please sign in to comment.