Skip to content

Commit

Permalink
chore: refactor & tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
MSOB7YY committed Mar 21, 2024
1 parent 92ba4cd commit af841de
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 97 deletions.
22 changes: 10 additions & 12 deletions lib/base/audio_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -857,11 +857,12 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
Duration? duration = playedFromCacheDetails.duration;

// race avoidance when playing multiple videos
bool checkInterrupted() {
bool checkInterrupted([Duration? dur]) {
if (item != currentVideo) {
return true;
} else {
if (duration != null) _currentItemDuration.value = duration;
dur ??= duration ?? playedFromCacheDetails.duration;
if (dur != null) _currentItemDuration.value = dur;
return false;
}
}
Expand All @@ -871,6 +872,8 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
currentCachedAudio.value = playedFromCacheDetails.audio;
currentCachedVideo.value = playedFromCacheDetails.video;

if (checkInterrupted(playedFromCacheDetails.duration)) return;

generateWaveform();

bool heyIhandledAudioPlaying = false;
Expand All @@ -881,7 +884,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
heyIhandledAudioPlaying = false;
}

if (checkInterrupted()) return;
if (checkInterrupted(playedFromCacheDetails.duration)) return;

if (ConnectivityController.inst.hasConnection) {
try {
Expand All @@ -898,6 +901,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
YoutubeController.inst.currentYTQualities.value = streams.videoOnlyStreams ?? [];
YoutubeController.inst.currentYTAudioStreams.value = streams.audioOnlyStreams ?? [];
currentVideoInfo.value = streams.videoInfo;
if (checkInterrupted(streams.videoInfo.duration)) return;
final vos = streams.videoOnlyStreams;
final allVideoStream = isAudioOnlyPlayback || vos == null || vos.isEmpty ? null : YoutubeController.inst.getPreferredStreamQuality(vos, preferIncludeWebm: false);
final prefferedVideoStream = allVideoStream;
Expand All @@ -924,7 +928,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
final cachedAudio = prefferedAudioStream?.getCachedFile(item.id);
final mediaItem = item.toMediaItem(currentVideoInfo.value, currentVideoThumbnail.value, index, currentQueue.length);
_isCurrentAudioFromCache = cachedAudio != null;
if (checkInterrupted()) return;
if (checkInterrupted(streams.videoInfo.duration)) return;
final isVideoCacheSameAsPrevSet = cachedVideo != null &&
playedFromCacheDetails.video != null &&
playedFromCacheDetails.video?.path == cachedVideo.path; // only if not the same cache path (i.e. diff resolution)
Expand All @@ -949,7 +953,7 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
);
}
await playerStoppingSeikoo.future;
if (checkInterrupted()) return;
if (checkInterrupted(streams.videoInfo.duration)) return;

if (cachedVideo?.path != null) {
File(cachedVideo!.path).setLastAccessedTry(DateTime.now());
Expand Down Expand Up @@ -1386,9 +1390,6 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
);
}

@override
Future<void> skipToNext([bool? andPlay]) async => await onSkipToNext(andPlay);

@override
Future<void> skipToPrevious() async {
if (previousButtonReplays) {
Expand All @@ -1406,12 +1407,9 @@ class NamidaAudioVideoHandler<Q extends Playable> extends BasicAudioHandler<Q> {
}
}

await onSkipToPrevious();
await skipToPrevious();
}

@override
Future<void> skipToQueueItem(int index, [bool? andPlay]) async => await onSkipToQueueItem(index, andPlay);

@override
Future<void> onDispose() async {
await [
Expand Down
8 changes: 6 additions & 2 deletions lib/base/ports_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ import 'package:namida/core/extensions.dart';

typedef PortsComm = ({ReceivePort items, Completer<SendPort> search});

abstract class _PortsProviderDisposeMessage {}

mixin PortsProvider {
PortsComm? port;
StreamSubscription? _streamSub;

static bool isDisposeMessage(dynamic message) => message == _PortsProviderDisposeMessage;

Future<void> disposePort() async {
final port = this.port;
if (port != null) {
port.items.close();
_streamSub?.cancel();
(await port.search.future).send('dispose');
(await port.search.future).send(_PortsProviderDisposeMessage);
this.port = null;
}
}
Expand Down Expand Up @@ -48,7 +52,7 @@ mixin PortsProviderBase {
Future<void> disposePort(PortsComm port) async {
port.items.close();
_streamSub?.cancel();
(await port.search.future).send('dispose');
(await port.search.future).send(_PortsProviderDisposeMessage);
}

Future<SendPort> preparePortBase({
Expand Down
7 changes: 4 additions & 3 deletions lib/controller/search_sort_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:playlist_manager/playlist_manager.dart';

import 'package:namida/base/ports_provider.dart';
import 'package:namida/class/split_config.dart';
import 'package:namida/class/track.dart';
import 'package:namida/controller/history_controller.dart';
Expand Down Expand Up @@ -346,7 +347,7 @@ class SearchSortController {

StreamSubscription? streamSub;
streamSub = receivePort.listen((p) {
if (p is String && p == 'dispose') {
if (PortsProvider.isDisposeMessage(p)) {
receivePort.close();
streamSub?.cancel();
return;
Expand Down Expand Up @@ -488,7 +489,7 @@ class SearchSortController {

StreamSubscription? streamSub;
streamSub = receivePort.listen((p) {
if (p is String && p == 'dispose') {
if (PortsProvider.isDisposeMessage(p)) {
receivePort.close();
streamSub?.cancel();
return;
Expand Down Expand Up @@ -892,7 +893,7 @@ class SearchSortController {
final cleanupFunction = _functionOfCleanup(cleanup);
StreamSubscription? streamSub;
streamSub = receivePort.listen((p) {
if (p is String && p == 'dispose') {
if (PortsProvider.isDisposeMessage(p)) {
receivePort.close();
streamSub?.cancel();
return;
Expand Down
7 changes: 3 additions & 4 deletions lib/controller/video_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,18 @@ class VideoController {

Future<void> toggleFullScreenVideoView({
required bool isLocal,
bool? setOrientations,
}) async {
final aspect = Player.inst.videoPlayerInfo?.aspectRatio;
await NamidaNavigator.inst.toggleFullScreen(
NamidaVideoControls(
key: VideoController.inst.videoControlsKeyFullScreen,
isLocal: isLocal,
onMinimizeTap: () {
NamidaNavigator.inst.exitFullScreen();
},
onMinimizeTap: NamidaNavigator.inst.exitFullScreen,
showControls: true,
isFullScreen: true,
),
setOrientations: aspect == null ? true : aspect > 1,
setOrientations: setOrientations ?? (aspect == null ? true : aspect > 1),
);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/ui/pages/albums_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class AlbumsPage extends StatelessWidget {
onCloseButtonPressed: () => ScrollSearchController.inst.clearSearchTextField(LibraryTab.albums),
sortByMenuWidget: SortByMenu(
title: settings.albumSort.value.toText(),
popupMenuChild: const SortByMenuAlbums(),
popupMenuChild: () => const SortByMenuAlbums(),
isCurrentlyReversed: settings.albumSortReversed.value,
onReverseIconTap: () => SearchSortController.inst.sortMedia(MediaType.album, reverse: !settings.albumSortReversed.value),
),
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/pages/artists_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class ArtistsPage extends StatelessWidget {
onCloseButtonPressed: () => ScrollSearchController.inst.clearSearchTextField(LibraryTab.artists),
sortByMenuWidget: SortByMenu(
title: settings.artistSort.value.toText(),
popupMenuChild: const SortByMenuArtists(),
popupMenuChild: () => const SortByMenuArtists(),
isCurrentlyReversed: settings.artistSortReversed.value,
onReverseIconTap: () => SearchSortController.inst.sortMedia(MediaType.artist, reverse: !settings.artistSortReversed.value),
),
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/pages/genres_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class GenresPage extends StatelessWidget {
onCloseButtonPressed: () => ScrollSearchController.inst.clearSearchTextField(LibraryTab.genres),
sortByMenuWidget: SortByMenu(
title: settings.genreSort.value.toText(),
popupMenuChild: const SortByMenuGenres(),
popupMenuChild: () => const SortByMenuGenres(),
isCurrentlyReversed: settings.genreSortReversed.value,
onReverseIconTap: () => SearchSortController.inst.sortMedia(MediaType.genre, reverse: !settings.genreSortReversed.value),
),
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/pages/playlists_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class _PlaylistsPageState extends State<PlaylistsPage> with TickerProviderStateM
onCloseButtonPressed: () => ScrollSearchController.inst.clearSearchTextField(LibraryTab.playlists),
sortByMenuWidget: SortByMenu(
title: settings.playlistSort.value.toText(),
popupMenuChild: const SortByMenuPlaylist(),
popupMenuChild: () => const SortByMenuPlaylist(),
isCurrentlyReversed: settings.playlistSortReversed.value,
onReverseIconTap: () => SearchSortController.inst.sortMedia(MediaType.playlist, reverse: !settings.playlistSortReversed.value),
),
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/pages/tracks_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class _TracksPageState extends State<TracksPage> with TickerProviderStateMixin,
},
sortByMenuWidget: SortByMenu(
title: settings.tracksSort.value.toText(),
popupMenuChild: const SortByMenuTracks(),
popupMenuChild: () => const SortByMenuTracks(),
isCurrentlyReversed: settings.tracksSortReversed.value,
onReverseIconTap: () {
SearchSortController.inst.sortMedia(MediaType.track, reverse: !settings.tracksSortReversed.value);
Expand Down
4 changes: 2 additions & 2 deletions lib/ui/widgets/expandable_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class CustomTextFiled extends StatelessWidget {
}

class SortByMenu extends StatelessWidget {
final Widget popupMenuChild;
final Widget Function() popupMenuChild;
final String title;
final bool isCurrentlyReversed;
final void Function()? onReverseIconTap;
Expand Down Expand Up @@ -177,7 +177,7 @@ class SortByMenu extends StatelessWidget {
items: [
PopupMenuItem(
padding: const EdgeInsets.symmetric(vertical: 0.0),
child: popupMenuChild,
child: popupMenuChild(),
),
],
),
Expand Down
124 changes: 62 additions & 62 deletions lib/ui/widgets/video_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -515,49 +515,69 @@ class NamidaVideoControlsState extends State<NamidaVideoControls> with TickerPro

@override
Widget build(BuildContext context) {
final fallbackChild = widget.isLocal && !widget.isFullScreen
? Container(
key: const Key('dummy_container'),
color: Colors.transparent,
)
: Obx(
() {
if (widget.isLocal) {
final track = Player.inst.nowPlayingTrack;
if (File(track.pathToImage).existsSync()) {
return ArtworkWidget(
key: Key(track.path),
track: track,
path: track.pathToImage,
thumbnailSize: context.width,
width: context.width,
height: context.width * 9 / 16,
borderRadius: 0,
blur: 0,
compressed: false,
);
}
}
final vidId = Player.inst.nowPlayingVideoID?.id ?? (YoutubeController.inst.currentYoutubeMetadataVideo.value ?? Player.inst.currentVideoInfo)?.id;
return YoutubeThumbnail(
key: Key(vidId ?? ''),
isImportantInCache: true,
width: context.width,
height: context.width * 9 / 16,
borderRadius: 0,
blur: 0,
videoId: vidId,
displayFallbackIcon: false,
compressed: false,
preferLowerRes: false,
);
},
);
final maxWidth = context.width;
final maxHeight = context.height;
final inLandscape = NamidaNavigator.inst.isInLanscape;

// -- in landscape, the size is calculated based on height, to fit in correctly.
final fallbackHeight = inLandscape ? maxHeight : maxWidth * 9 / 16;
final fallbackWidth = inLandscape ? maxHeight * 16 / 9 : maxWidth;

final finalVideoWidget = Obx(() {
final info = Player.inst.videoPlayerInfo;
if (info != null && info.isInitialized) {
return NamidaAspectRatio(
aspectRatio: info.aspectRatio,
child: Obx(
() => AnimatedScale(
duration: const Duration(milliseconds: 200),
scale: 1.0 + VideoController.inst.videoZoomAdditionalScale.value * 0.02,
child: Texture(textureId: info.textureId),
),
),
);
}
if (widget.isLocal && !widget.isFullScreen) {
return Container(
key: const Key('dummy_container'),
color: Colors.transparent,
);
}
// -- fallback images
if (widget.isLocal) {
final track = Player.inst.nowPlayingTrack;
if (File(track.pathToImage).existsSync()) {
return ArtworkWidget(
key: Key(track.path),
track: track,
path: track.pathToImage,
thumbnailSize: fallbackWidth,
width: fallbackWidth,
height: fallbackHeight,
borderRadius: 0,
blur: 0,
compressed: false,
);
}
}
final vidId = Player.inst.nowPlayingVideoID?.id ?? (YoutubeController.inst.currentYoutubeMetadataVideo.value ?? Player.inst.currentVideoInfo)?.id;
return YoutubeThumbnail(
key: Key(vidId ?? ''),
isImportantInCache: true,
width: fallbackWidth,
height: fallbackHeight,
borderRadius: 0,
blur: 0,
videoId: vidId,
displayFallbackIcon: false,
compressed: false,
preferLowerRes: false,
);
});
final horizontalControlsPadding = widget.isFullScreen
? inLandscape
? const EdgeInsets.symmetric(horizontal: 32.0, vertical: 12.0) // lanscape videos
: const EdgeInsets.symmetric(horizontal: 12.0, vertical: 24.0) // vertical videos
? const EdgeInsets.symmetric(horizontal: 32.0, vertical: 0.0) // lanscape videos
: const EdgeInsets.symmetric(horizontal: 12.0, vertical: 12.0) // vertical videos
: const EdgeInsets.symmetric(horizontal: 2.0, vertical: 2.0);
final itemsColor = Colors.white.withAlpha(200);
final shouldShowSliders = _canShowControls && widget.isFullScreen;
Expand Down Expand Up @@ -644,29 +664,9 @@ class NamidaVideoControlsState extends State<NamidaVideoControls> with TickerPro
fit: StackFit.passthrough,
alignment: Alignment.center,
children: [
Stack(
Align(
alignment: Alignment.center,
children: [
Obx(() {
final info = Player.inst.videoPlayerInfo;
return info != null && info.isInitialized
? NamidaAspectRatio(
aspectRatio: info.aspectRatio,
child: Obx(
() => AnimatedScale(
duration: const Duration(milliseconds: 200),
scale: 1.0 + VideoController.inst.videoZoomAdditionalScale.value * 0.02,
child: Texture(textureId: info.textureId),
),
),
)
: SizedBox(
height: context.height,
width: context.height * 16 / 9,
child: fallbackChild,
);
})
],
child: finalVideoWidget,
),
// ---- Brightness Mask -----
Positioned.fill(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class YTLocalSearchController with PortsProvider {
// -- start listening
StreamSubscription? streamSub;
streamSub = recievePort.listen((p) {
if (p is String && p == 'dispose') {
if (PortsProvider.isDisposeMessage(p)) {
recievePort.close();
lookupListStreamInfo.clear();
lookupListYTVH.clear();
Expand Down
2 changes: 1 addition & 1 deletion lib/youtube/controller/yt_generators_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class NamidaYTGenerator extends NamidaGeneratorBase<YoutubeID, String> with Port
// -- start listening
StreamSubscription? streamSub;
streamSub = recievePort.listen((p) async {
if (p is String && p == 'dispose') {
if (PortsProvider.isDisposeMessage(p)) {
recievePort.close();
releaseDateMap.clear();
allIds.clear();
Expand Down
Loading

0 comments on commit af841de

Please sign in to comment.