Skip to content

Commit

Permalink
feat(yt): comments sort (top, newest)
Browse files Browse the repository at this point in the history
this required redesign for comments header, which required Widget implementation for PullToRefresh

also smol fixes heh
  • Loading branch information
MSOB7YY committed Jun 25, 2024
1 parent 4d431db commit e82f32a
Show file tree
Hide file tree
Showing 12 changed files with 286 additions and 221 deletions.
3 changes: 2 additions & 1 deletion lib/base/audio_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1615,6 +1615,7 @@ extension TrackToAudioSourceMediaItem on Selectable {
MediaItem toMediaItem(int currentIndex, int queueLength) {
final tr = track.toTrackExt();
final artist = tr.originalArtist == '' ? UnknownTags.ARTIST : tr.originalArtist;
final imagePage = tr.pathToImage;
return MediaItem(
id: tr.path,
title: tr.title,
Expand All @@ -1625,7 +1626,7 @@ extension TrackToAudioSourceMediaItem on Selectable {
album: tr.hasUnknownAlbum ? '' : tr.album,
genre: tr.originalGenre,
duration: Duration(seconds: tr.duration),
artUri: Uri.file(File(tr.pathToImage).existsSync() ? tr.pathToImage : AppPaths.NAMIDA_LOGO),
artUri: Uri.file(File(imagePage).existsSync() ? imagePage : AppPaths.NAMIDA_LOGO),
);
}
}
Expand Down
48 changes: 46 additions & 2 deletions lib/base/pull_to_refresh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,48 @@ import 'package:flutter/material.dart';
import 'package:namida/core/icon_fonts/broken_icons.dart';
import 'package:namida/core/utils.dart';

typedef PullToRefreshCallback = Future<void> Function();
const double _defaultMaxDistance = 128.0;

class PullToRefresh extends StatefulWidget {
final Widget child;
final ScrollController controller;
final PullToRefreshCallback onRefresh;
final double maxDistance;

const PullToRefresh({
super.key,
required this.child,
required this.controller,
required this.onRefresh,
this.maxDistance = _defaultMaxDistance,
});

@override
State<PullToRefresh> createState() => _PullToRefreshState();
}

class _PullToRefreshState extends State<PullToRefresh> with TickerProviderStateMixin, PullToRefreshMixin {
@override
double get maxDistance => widget.maxDistance;

@override
Widget build(BuildContext context) {
return Listener(
onPointerMove: (event) => onPointerMove(widget.controller, event),
onPointerUp: (_) => onRefresh(widget.onRefresh),
onPointerCancel: (_) => onVerticalDragFinish(),
child: Stack(
alignment: Alignment.topCenter,
children: [
widget.child,
pullToRefreshWidget,
],
),
);
}
}

mixin PullToRefreshMixin<T extends StatefulWidget> on State<T> implements TickerProvider {
bool enablePullToRefresh = true;

Expand All @@ -16,6 +58,8 @@ mixin PullToRefreshMixin<T extends StatefulWidget> on State<T> implements Ticker
final _minTrigger = 20;
num get pullNormalizer => 100;

final double maxDistance = _defaultMaxDistance;

bool? _isDraggingVertically;
double _distanceDragged = 0;
bool _onVerticalDragUpdate(double dy) {
Expand Down Expand Up @@ -48,7 +92,7 @@ mixin PullToRefreshMixin<T extends StatefulWidget> on State<T> implements Ticker
}

bool _isRefreshing = false;
Future<void> onRefresh(Future<void> Function() execute) async {
Future<void> onRefresh(PullToRefreshCallback execute) async {
if (!enablePullToRefresh) return;
onVerticalDragFinish();
if (animation.value != 1 || _isRefreshing) return;
Expand Down Expand Up @@ -78,7 +122,7 @@ mixin PullToRefreshMixin<T extends StatefulWidget> on State<T> implements Ticker
const multiplier = 4.5;
const minus = multiplier / 3;
return Padding(
padding: EdgeInsets.only(top: 12.0 + p * 128.0),
padding: EdgeInsets.only(top: 12.0 + p * maxDistance),
child: Transform.rotate(
angle: (p * multiplier) - minus,
child: AnimatedBuilder(
Expand Down
9 changes: 9 additions & 0 deletions lib/core/namida_converter_ext.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:playlist_manager/module/playlist_id.dart';
import 'package:youtipie/class/result_wrapper/playlist_result.dart';
import 'package:youtipie/class/streams/audio_stream.dart';
import 'package:youtipie/class/streams/video_stream.dart';
import 'package:youtipie/core/enum.dart';
import 'package:youtipie/youtipie.dart';

import 'package:namida/class/faudiomodel.dart';
Expand Down Expand Up @@ -685,6 +686,10 @@ extension YTSeekActionModeUtils on YTSeekActionMode {
String toText() => _NamidaConverters.inst.getTitle(this);
}

extension CommentsSortTypeUtils on CommentsSortType {
String toText() => _NamidaConverters.inst.getTitle(this);
}

extension RouteUtils on NamidaRoute {
List<Selectable> tracksListInside() {
final iter = tracksInside();
Expand Down Expand Up @@ -1299,6 +1304,10 @@ class _NamidaConverters {
YTSeekActionMode.expandedMiniplayer: lang.EXPANDED_MINIPLAYER,
YTSeekActionMode.all: lang.ALL,
},
CommentsSortType: {
CommentsSortType.top: lang.TOP,
CommentsSortType.newest: lang.NEWEST,
},
};

// ====================================================
Expand Down
2 changes: 2 additions & 0 deletions lib/core/translations/static_strings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ part of 'language.dart';
mixin _StaticStrings {
final NO_NETWORK_AVAILABLE_TO_FETCH_VIDEO_PAGE = 'No Network Available to fetch video page';
final DID_YOU_MEAN = 'Did you mean';
final TOP = 'Top';
final NEWEST = 'Newest';
}
2 changes: 1 addition & 1 deletion lib/youtube/controller/youtube_info_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ part 'info_controllers/yt_video_info_controller.dart';
part 'youtube_current_info.dart';

class YoutubeInfoController {
YoutubeInfoController._();
const YoutubeInfoController._();

static const video = _VideoInfoController();
static const playlist = YoutiPie.playlist;
Expand Down
1 change: 1 addition & 0 deletions lib/youtube/pages/youtube_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class _YoutubePageState extends State<YoutubePage> with AutomaticKeepAliveClient
transparent: false,
shimmerEnabled: true,
child: ListView.builder(
padding: EdgeInsets.zero,
physics: const NeverScrollableScrollPhysics(),
itemCount: feed.length,
shrinkWrap: true,
Expand Down
1 change: 1 addition & 0 deletions lib/youtube/pages/yt_channel_subpage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ class _YTChannelSubpageState extends YoutubeChannelController<YTChannelSubpage>
? ShimmerWrapper(
shimmerEnabled: true,
child: ListView.builder(
padding: EdgeInsets.zero,
itemCount: 15,
itemBuilder: (context, index) {
return const YoutubeVideoCardDummy(
Expand Down
1 change: 1 addition & 0 deletions lib/youtube/pages/yt_channels_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ class _YoutubeChannelsPageState extends YoutubeChannelController<YoutubeChannels
? ShimmerWrapper(
shimmerEnabled: true,
child: ListView.builder(
padding: EdgeInsets.zero,
itemCount: 15,
itemBuilder: (context, index) {
return const YoutubeVideoCardDummy(
Expand Down
7 changes: 0 additions & 7 deletions lib/youtube/widgets/yt_thumbnail.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class YoutubeThumbnail extends StatefulWidget {
final String? smallBoxText;
final IconData? smallBoxIcon;
final bool displayFallbackIcon;
final String? localImagePath;
final bool extractColor;
final double blur;
final bool compressed;
Expand All @@ -57,7 +56,6 @@ class YoutubeThumbnail extends StatefulWidget {
this.smallBoxText,
this.smallBoxIcon,
this.displayFallbackIcon = true,
this.localImagePath,
this.extractColor = false,
this.blur = 1.5,
this.compressed = true,
Expand All @@ -79,9 +77,6 @@ class _YoutubeThumbnailState extends State<YoutubeThumbnail> with LoadingItemsDe
Color? smallBoxDynamicColor;
final _thumbnailNotFound = false.obs;

bool get canFetchYTImage => widget.videoId != null || widget.customUrl != null;
bool get canFetchImage => widget.localImagePath != null || canFetchYTImage;

Timer? _dontTouchMeImFetchingThumbnail;

@override
Expand All @@ -106,8 +101,6 @@ class _YoutubeThumbnailState extends State<YoutubeThumbnail> with LoadingItemsDe
_dontTouchMeImFetchingThumbnail = null;
_dontTouchMeImFetchingThumbnail = Timer(const Duration(seconds: 8), () {});

imagePath = widget.localImagePath;

void onThumbnailNotFound() => _thumbnailNotFound.value = true;

if (imagePath == null) {
Expand Down
62 changes: 9 additions & 53 deletions lib/youtube/youtube_miniplayer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -908,59 +908,14 @@ class YoutubeMiniPlayerState extends State<YoutubeMiniPlayer> {
rx: settings.ytTopComments,
builder: (ytTopComments) {
if (ytTopComments) return const SliverToBoxAdapter();
return ObxO(
rx: YoutubeInfoController.current.currentComments,
builder: (comments) {
final count = comments?.commentsCount;
return SliverToBoxAdapter(
child: Padding(
key: Key("${currentId}_comments_header"),
padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 12.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
const Icon(Broken.document),
const SizedBox(width: 8.0),
Text(
[
lang.COMMENTS,
if (count != null) count.formatDecimalShort(),
].join(' • '),
style: mainTextTheme.displayLarge,
textAlign: TextAlign.start,
),
const Spacer(),
ObxO(
rx: YoutubeInfoController.current.isCurrentCommentsFromCache,
builder: (commFromCache) {
commFromCache ??= false;
return NamidaIconButton(
// key: Key(currentId),
tooltip: commFromCache ? () => lang.CACHE : null,
icon: Broken.refresh,
iconSize: 22.0,
onPressed: () async => await YoutubeInfoController.current.updateCurrentComments(
currentId,
newSortType: YoutubeMiniplayerUiController.inst.currentCommentSort.value,
initial: true,
),
child: commFromCache
? const StackedIcon(
baseIcon: Broken.refresh,
secondaryIcon: Broken.global,
)
: Icon(
Broken.refresh,
color: defaultIconColor,
),
);
},
),
],
),
),
);
},
return SliverToBoxAdapter(
key: Key("${currentId}_comments_header"),
child: const Padding(
padding: EdgeInsets.symmetric(vertical: 8.0),
child: YoutubeCommentsHeader(
displayBackButton: false,
),
),
);
},
),
Expand All @@ -977,6 +932,7 @@ class YoutubeMiniPlayerState extends State<YoutubeMiniPlayer> {
transparent: false,
shimmerEnabled: true,
child: ListView.builder(
padding: EdgeInsets.zero,
// key: Key(currentId),
physics: const NeverScrollableScrollPhysics(),
itemCount: 10,
Expand Down
Loading

0 comments on commit e82f32a

Please sign in to comment.