Skip to content

Commit

Permalink
chore: some fixes n tweakies
Browse files Browse the repository at this point in the history
- hide "go to channel" button for playing video menu in yt miniplayer
- use network like button instead of local one for youtube videos in local style miniplayer
- more items in menu of youtube local style miniplayer
- fix cache related thingys
- perf: smol refactor for TrackTilePropertiesProvider
  • Loading branch information
MSOB7YY committed Aug 3, 2024
1 parent 2133df3 commit 666ee1c
Show file tree
Hide file tree
Showing 12 changed files with 309 additions and 159 deletions.
98 changes: 98 additions & 0 deletions lib/base/yt_video_like_manager.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import 'package:youtipie/class/videos/video_result.dart';
import 'package:youtipie/core/enum.dart';
import 'package:youtipie/youtipie.dart';

import 'package:namida/controller/navigator_controller.dart';
import 'package:namida/core/translations/language.dart';
import 'package:namida/core/utils.dart';
import 'package:namida/ui/widgets/custom_widgets.dart';
import 'package:namida/youtube/controller/youtube_info_controller.dart';

class YTVideoLikeParamters {
final YoutiPieVideoPageResult? page;
final bool isActive;
final LikeAction action;
final void Function() onStart;
final void Function() onEnd;

const YTVideoLikeParamters({
required this.page,
required this.isActive,
required this.action,
required this.onStart,
required this.onEnd,
});
}

class YtVideoLikeManager {
late final currentVideoLikeStatus = Rxn<LikeStatus>();

Future<bool> _confirmRemoveLike() async {
bool confirmed = false;
await NamidaNavigator.inst.navigateDialog(
dialog: CustomBlurryDialog(
isWarning: true,
normalTitleStyle: true,
bodyText: lang.CONFIRM,
actions: [
const CancelButton(),
NamidaButton(
text: lang.REMOVE.toUpperCase(),
onPressed: () async {
NamidaNavigator.inst.closeDialog();
confirmed = true;
},
),
],
),
);
return confirmed;
}

Future<bool> onLikeClicked(YTVideoLikeParamters parameters) async {
if (parameters.isActive) {
final confirmed = await _confirmRemoveLike();
if (!confirmed) return parameters.isActive;
}
return _onChangeLikeStatus(parameters);
}

Future<bool> onDisLikeClicked(YTVideoLikeParamters parameters) async {
return _onChangeLikeStatus(parameters);
}

Future<bool> _onChangeLikeStatus(YTVideoLikeParamters parameters) async {
final page = parameters.page;
if (page == null) return parameters.isActive;

parameters.onStart();
final res = await YoutiPie.videoAction.changeLikeStatus(
videoPage: page,
engagement: page.videoInfo?.engagement,
action: parameters.action,
);
parameters.onEnd();

if (res == true) {
currentVideoLikeStatus.value = currentVideoLikeStatus.value = parameters.action.toExpectedStatus();
return !parameters.isActive;
}

return parameters.isActive;
}

void _onPageChanged() {
final page = YoutubeInfoController.current.currentVideoPage.value;
currentVideoLikeStatus.value = page?.videoInfo?.engagement?.likeStatus;
}

void init() {
_onPageChanged(); // fill initial values
YoutubeInfoController.current.currentVideoPage.addListener(_onPageChanged);
}

void dispose() {
YoutubeInfoController.current.currentVideoPage.removeListener(_onPageChanged);
currentVideoLikeStatus.close();
}
}
2 changes: 1 addition & 1 deletion lib/controller/settings.youtube.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class _YoutubeSettings with SettingsFileWriter {

@override
Object get jsonToWrite => <String, dynamic>{
'showChannelWatermarkFullscreen': showChannelWatermarkFullscreen,
'showChannelWatermarkFullscreen': showChannelWatermarkFullscreen.value,
'ytVisibleShorts': ytVisibleShorts.map((key, value) => MapEntry(key.convertToString, value)),
'ytVisibleMixes': ytVisibleMixes.map((key, value) => MapEntry(key.convertToString, value)),
'addToPlaylistsTabIndex': addToPlaylistsTabIndex,
Expand Down
6 changes: 3 additions & 3 deletions lib/controller/storage_cache_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ class _VideoTrimmer {

Directory(tempDir).listSyncSafe().loop(checkFileAndAdd);
Directory(normalDir).listSyncSafe().loop((e) {
if (e.path.endsWith('.download')) {
if (e.path.endsWith('.part')) {
checkFileAndAdd(e);
}
});
Expand All @@ -483,7 +483,7 @@ class _VideoTrimmer {
size += e.statSync().size;
});
Directory(normalDir).listSyncSafe().loop((e) {
if (e.path.endsWith('.download')) {
if (e.path.endsWith('.part')) {
size += e.statSync().size;
}
});
Expand All @@ -501,7 +501,7 @@ class _VideoTrimmer {
}
});
Directory(normalDir).listSyncSafe().loop((e) {
if (e.path.endsWith('.download')) {
if (e.path.endsWith('.part')) {
if (e is File) {
try {
e.deleteSync();
Expand Down
5 changes: 3 additions & 2 deletions lib/controller/video_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ class VideoController {
/// - Detects: `new files`.
/// - DOES NOT handle: `deleted` & `needs-to-be-updated` files.
/// - Returns a map with **new videos only**.
/// - **New**: excludes files ending with `.download`
/// - **New**: excludes files ending with `.part`
Future<Map<String, List<NamidaVideo>>> _checkForNewVideosInCache(Map<String, List<NamidaVideo>> idsMap) async {
final newIds = await _checkForNewVideosInCacheIsolate.thready({
'dirPath': AppDirs.VIDEOS_CACHE,
Expand Down Expand Up @@ -759,7 +759,8 @@ class VideoController {
for (final df in dir.listSyncSafe()) {
if (df is File) {
final filename = df.path.getFilename;
if (filename.endsWith('.download')) continue; // first thing first
if (filename.endsWith('.part')) continue; // first thing first
if (filename.endsWith('.mime')) continue; // second thing second

final id = filename.substring(0, 11);
final videosInMap = idsMap[id];
Expand Down
63 changes: 52 additions & 11 deletions lib/packages/miniplayer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter/material.dart';

import 'package:animated_background/animated_background.dart';

import 'package:namida/base/yt_video_like_manager.dart';
import 'package:namida/class/route.dart';
import 'package:namida/class/track.dart';
import 'package:namida/class/video.dart';
Expand Down Expand Up @@ -300,23 +301,62 @@ class NamidaMiniPlayerTrack extends StatelessWidget {
}
}

class NamidaMiniPlayerYoutubeID extends StatelessWidget {
class NamidaMiniPlayerYoutubeID extends StatefulWidget {
const NamidaMiniPlayerYoutubeID({super.key});

@override
State<NamidaMiniPlayerYoutubeID> createState() => _NamidaMiniPlayerYoutubeIDState();
}

class _NamidaMiniPlayerYoutubeIDState extends State<NamidaMiniPlayerYoutubeID> {
final _videoLikeManager = YtVideoLikeManager();

@override
void initState() {
super.initState();
_videoLikeManager.init();
}

@override
void dispose() {
_videoLikeManager.dispose();
super.dispose();
}

void _openMenu(BuildContext context, YoutubeID video, TapUpDetails details) {
final vidpage = YoutubeInfoController.video.fetchVideoPageSync(video.id);
final vidstreams = YoutubeInfoController.video.fetchVideoStreamsSync(video.id);
final popUpItems = NamidaPopupWrapper(
childrenDefault: () => YTUtils.getVideoCardMenuItems(
videoId: video.id,
url: vidpage?.videoInfo?.buildUrl() ?? vidstreams?.info?.buildUrl(),
channelID: vidpage?.channelInfo?.id,
playlistID: null,
idsNamesLookup: {video.id: vidpage?.videoInfo?.title ?? vidstreams?.info?.title},
playlistName: '',
videoYTID: video,
copyUrl: true,
),
childrenDefault: () {
final defaultItems = YTUtils.getVideoCardMenuItems(
videoId: video.id,
url: vidpage?.videoInfo?.buildUrl() ?? vidstreams?.info?.buildUrl(),
channelID: vidpage?.channelInfo?.id,
playlistID: null,
idsNamesLookup: {video.id: vidpage?.videoInfo?.title ?? vidstreams?.info?.title},
playlistName: '',
videoYTID: video,
copyUrl: true,
);
final clearItem = NamidaPopupItem(
icon: Broken.trash,
title: lang.CLEAR,
onTap: () {
YTUtils().showVideoClearDialog(context, video.id, CurrentColor.inst.miniplayerColor);
},
);
final isFavourite = video.isFavourite;
final favouriteItem = NamidaPopupItem(
icon: isFavourite ? Broken.heart_tick : Broken.heart,
title: lang.FAVOURITES,
onTap: () => YoutubePlaylistController.inst.favouriteButtonOnPressed(video.id),
);
final items = <NamidaPopupItem>[];
items.add(favouriteItem);
items.addAll(defaultItems);
items.add(clearItem);
return items;
},
).convertItems(context);
NamidaNavigator.inst.showMenu(
context: context,
Expand Down Expand Up @@ -346,6 +386,7 @@ class NamidaMiniPlayerYoutubeID extends StatelessWidget {
onMenuOpen: (d) => _openMenu(context, video, d),
likedIcon: Broken.like_filled,
normalIcon: Broken.like_1,
ytLikeManager: _videoLikeManager,
);
}

Expand Down
53 changes: 43 additions & 10 deletions lib/packages/miniplayer_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import 'package:flutter/material.dart';
import 'package:playlist_manager/class/favourite_playlist.dart';
import 'package:youtipie/class/streams/video_stream.dart';
import 'package:youtipie/class/streams/video_streams_result.dart';
import 'package:youtipie/core/enum.dart';
import 'package:youtipie/core/extensions.dart';

import 'package:namida/base/yt_video_like_manager.dart';
import 'package:namida/class/track.dart';
import 'package:namida/class/video.dart';
import 'package:namida/controller/current_color.dart';
Expand Down Expand Up @@ -38,6 +40,7 @@ import 'package:namida/ui/widgets/custom_widgets.dart';
import 'package:namida/ui/widgets/library/track_tile.dart';
import 'package:namida/ui/widgets/settings/extra_settings.dart';
import 'package:namida/ui/widgets/waveform.dart';
import 'package:namida/youtube/controller/youtube_info_controller.dart';

class FocusedMenuOptions<E> {
final bool Function(E currentItem) onOpen;
Expand Down Expand Up @@ -75,6 +78,7 @@ class MiniplayerInfoData<T, E> {
final void Function(TapUpDetails details) onMenuOpen;
final IconData likedIcon;
final IconData normalIcon;
final YtVideoLikeManager? ytLikeManager;

late final bool firstLineGood;
late final bool secondLineGood;
Expand All @@ -89,6 +93,7 @@ class MiniplayerInfoData<T, E> {
required this.onMenuOpen,
required this.likedIcon,
required this.normalIcon,
this.ytLikeManager,
}) : firstLineGood = firstLine.isNotEmpty,
secondLineGood = secondLine.isNotEmpty;
}
Expand Down Expand Up @@ -1257,6 +1262,7 @@ class _TrackInfo<T, E> extends StatelessWidget {
@override
Widget build(BuildContext context) {
final double opacity = (inverseAboveOne(p) * 10 - 9).clamp(0, 1);
final ytLikeManager = textData.ytLikeManager;

return Transform.translate(
offset: Offset(0, bottomOffset + (-maxOffset / 4.0 * p.clamp(0, 2))),
Expand Down Expand Up @@ -1319,16 +1325,43 @@ class _TrackInfo<T, E> extends StatelessWidget {
offset: Offset(-100 * (1.0 - cp), 0.0),
child: LongPressDetector(
onLongPress: textData.onShowAddToPlaylistDialog,
child: ObxOClass(
rx: textData.favouritePlaylist,
builder: (favouritePlaylist) => NamidaRawLikeButton(
size: 32.0,
likedIcon: textData.likedIcon,
normalIcon: textData.normalIcon,
isLiked: favouritePlaylist.isSubItemFavourite(textData.itemToLike),
onTap: textData.onLikeTap,
),
),
child: ytLikeManager != null
? ObxO(
rx: ytLikeManager.currentVideoLikeStatus,
builder: (currentLikeStatus) {
final isUserLiked = currentLikeStatus == LikeStatus.liked;
return NamidaLoadingSwitcher(
size: 32.0,
builder: (startLoading, stopLoading, isLoading) => NamidaRawLikeButton(
isLiked: isUserLiked,
likedIcon: textData.likedIcon,
normalIcon: textData.normalIcon,
size: 32.0,
onTap: (isLiked) async {
return ytLikeManager.onLikeClicked(
YTVideoLikeParamters(
page: YoutubeInfoController.current.currentVideoPage.value,
isActive: isLiked,
action: isLiked ? LikeAction.removeLike : LikeAction.addLike,
onStart: startLoading,
onEnd: stopLoading,
),
);
},
),
);
},
)
: ObxOClass(
rx: textData.favouritePlaylist,
builder: (favouritePlaylist) => NamidaRawLikeButton(
size: 32.0,
likedIcon: textData.likedIcon,
normalIcon: textData.normalIcon,
isLiked: favouritePlaylist.isSubItemFavourite(textData.itemToLike),
onTap: textData.onLikeTap,
),
),
),
),
),
Expand Down
Loading

0 comments on commit 666ee1c

Please sign in to comment.