Skip to content

Commit

Permalink
Merge pull request #399 from lamarios/fix/pagination_when_there_are_h…
Browse files Browse the repository at this point in the history
…ide_filters

fix issue where removing filtered videos from source list was a probl…
  • Loading branch information
lamarios authored Dec 8, 2023
2 parents 621bfb7 + 54e428c commit b3d430a
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 22 deletions.
9 changes: 5 additions & 4 deletions lib/playlists/views/tv/screens/playlist.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import 'dart:ui';

import 'package:auto_route/annotations.dart';
import 'package:auto_route/auto_route.dart';
import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:invidious/app/views/tv/screens/tv_home.dart';
import 'package:invidious/globals.dart';
import 'package:invidious/player/views/tv/screens/tvPlayerView.dart';
import 'package:invidious/playlists/states/playlist.dart';
import 'package:invidious/playlists/views/screens/playlist.dart';
import 'package:invidious/router.dart';
Expand All @@ -27,7 +25,7 @@ class TvPlaylistScreen extends PlaylistViewScreen {
const TvPlaylistScreen({super.key, required super.playlist, required super.canDeleteVideos});

playPlaylist(BuildContext context, PlaylistState _) {
AutoRouter.of(context).push(TvPlayerRoute(videos: _.playlist.videos));
AutoRouter.of(context).push(TvPlayerRoute(videos: _.playlist.videos.where((element) => !element.filterHide).toList()));
}

@override
Expand All @@ -52,7 +50,9 @@ class TvPlaylistScreen extends PlaylistViewScreen {
return VideoThumbnailView(
videoId: video.videoId,
decoration: BoxDecoration(),
thumbnailUrl: video.deArrowThumbnailUrl ?? ImageObject.getBestThumbnail(video.videoThumbnails)?.url ?? '');
thumbnailUrl: video.deArrowThumbnailUrl ??
ImageObject.getBestThumbnail(video.videoThumbnails)?.url ??
'');
},
options: CarouselOptions(
autoPlayCurve: Curves.easeInOutQuad,
Expand Down Expand Up @@ -162,6 +162,7 @@ class TvPlaylistScreen extends PlaylistViewScreen {
crossAxisCount: 3,
children: [
..._.playlist.videos
.where((element) => !element.filterHide)
.map((e) => TvVideoItem(video: e, autoFocus: false))
.toList(),
if (_.loading)
Expand Down
4 changes: 3 additions & 1 deletion lib/settings/models/db/video_filter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ class VideoFilter {

videos = videos?.map((v) => _innerFilterVideo(v, filters)).toList() ?? [];

videos.removeWhere((element) => element.filtered && element.filterHide);
// leaving this for future self. CANNOT remove videos from list, otherwise it will break pagination
// TODO: write test cases about this.
// videos.removeWhere((element) => element.filtered && element.filterHide);

return videos;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ SystemUiOverlayStyle getUiOverlayStyle(BuildContext context) {
statusBarIconBrightness: colorScheme.brightness == Brightness.dark ? Brightness.light : Brightness.dark);
}

//List<T> filteredVideos<T extends BaseVideo>(List<T> videos) => videos.where((element) => !element.filterHide).toList();
List<T> filteredVideos<T extends BaseVideo>(List<T> videos) => videos.where((element) => !element.filterHide).toList();

String getWeekdayName(int weekday) {
final DateTime now = DateTime.now().toLocal();
Expand Down
26 changes: 18 additions & 8 deletions lib/utils/views/tv/components/tv_horizontal_item_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:invidious/utils/states/item_list.dart';
import 'package:invidious/utils/views/components/placeholders.dart';
import 'package:invidious/videos/models/base_video.dart';
import 'package:invidious/videos/views/tv/components/video_item.dart';

import '../../../../utils.dart';
import '../../../../videos/models/video_in_list.dart';
import '../../../models/paginatedList.dart';

Expand All @@ -14,39 +16,47 @@ class TvHorizontalItemList<T> extends StatelessWidget {
final Widget Function(BuildContext context, int index, T item) buildItem;

const TvHorizontalItemList(
{Key? key, this.tags, required this.paginatedList, required this.buildItem, required this.getPlaceholder})
: super(key: key);
{super.key, this.tags, required this.paginatedList, required this.buildItem, required this.getPlaceholder});

@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => ItemListCubit<T>(ItemListState<T>(itemList: paginatedList)),
child: BlocBuilder<ItemListCubit<T>, ItemListState<T>>(
builder: (context, _) => Stack(
builder: (context, _) {

// filter items if possible
List<T> items = _.items;
if (items.isNotEmpty && items[0] is BaseVideo) {
items = filteredVideos<BaseVideo>(_.items.cast()).cast();
}

return Stack(
children: [
if (_.loading)
const LinearProgressIndicator(
minHeight: 3,
),
!_.loading && _.items.isEmpty
!_.loading && items.isEmpty
? const SizedBox.shrink()
: SizedBox(
height: 250,
child: ListView.builder(
scrollDirection: Axis.horizontal,
controller: _.scrollController,
itemCount: _.items.length + (_.loading ? 10 : 0),
itemCount: items.length + (_.loading ? 10 : 0),
itemBuilder: (context, index) {
if (index >= _.items.length) {
if (index >= items.length) {
return getPlaceholder();
}
T e = _.items[index];
T e = items[index];
return buildItem(context, index, e);
},
),
),
],
),
);
},
),
);
}
Expand Down
18 changes: 14 additions & 4 deletions lib/utils/views/tv/components/tv_horizontal_paginated_list.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../../../utils.dart';
import '../../../../videos/models/base_video.dart';
import '../../../models/paginatedList.dart';
import '../../../states/paginated_list_view.dart';

Expand All @@ -20,7 +22,14 @@ class TvHorizontalPaginatedListView<T> extends StatelessWidget {
create: (context) => PaginatedListCubit(
PaginatedListViewController<T>(paginatedList: this.paginatedList, startItems: this.startItems)),
child: BlocBuilder<PaginatedListCubit<T>, PaginatedListViewController<T>>(
builder: (context, _) => Stack(
builder: (context, _) {

// filter items if possible
List<T> items = _.items;
if (items.isNotEmpty && items[0] is BaseVideo) {
items = filteredVideos<BaseVideo>(_.items.cast()).cast();
}
return Stack(
children: [
_.loading
? const LinearProgressIndicator(
Expand All @@ -30,12 +39,13 @@ class TvHorizontalPaginatedListView<T> extends StatelessWidget {
ListView.builder(
controller: _.scrollController,
scrollDirection: Axis.horizontal,
itemCount: _.items.length + (_.loading ? 10 : 0),
itemCount: items.length + (_.loading ? 10 : 0),
itemBuilder: (BuildContext context, int index) =>
index >= _.items.length ? getPlaceHolder() : itemBuilder(_.items[index]),
index >= items.length ? getPlaceHolder() : itemBuilder(items[index]),
),
],
)),
);
}),
);
}
}
4 changes: 3 additions & 1 deletion lib/videos/states/history.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ class HistoryItemCubit extends Cubit<HistoryItemState> {
state.cachedVid = await HistoryVideoCache.fromVideoIdToVideo(state.videoId);

state.loading = false;
emit(state);
if(!isClosed) {
emit(state);
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions lib/videos/views/components/video_in_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class VideoListItem extends StatelessWidget {

@override
Widget build(BuildContext context) {


ColorScheme colorScheme = Theme.of(context).colorScheme;
var locals = AppLocalizations.of(context)!;

Expand Down
2 changes: 1 addition & 1 deletion lib/videos/views/components/video_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class VideoList<T extends IdedVideo> extends StatelessWidget {

List<IdedVideo> items = _.items;
if (items.isNotEmpty && items[0] is VideoInList) {
// items = filteredVideos<VideoInList>(_.items.cast());
items = filteredVideos<VideoInList>(_.items.cast());
}

var gridCount = small ? 1 : getGridCount(context);
Expand Down
2 changes: 1 addition & 1 deletion lib/videos/views/tv/screens/video.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class TvVideoScreen extends StatelessWidget {
const TvVideoScreen({Key? key, required this.videoId}) : super(key: key);

playVideo(BuildContext context, Video video) {
AutoRouter.of(context).push(TvPlayerRoute(videos: [video, ...video.recommendedVideos]));
AutoRouter.of(context).push(TvPlayerRoute(videos: [video, ...video.recommendedVideos.where((element) => !element.filterHide)]));
}

showChannel(BuildContext context, String channelId) {
Expand Down
3 changes: 2 additions & 1 deletion lib/videos/views/tv/screens/video_grid_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class TvGridScreen extends StatelessWidget {
child: Scaffold(
body: TvOverscan(
child: BlocBuilder<ItemListCubit<VideoInList>, ItemListState<VideoInList>>(builder: (context, _) {

return Column(
children: [
Row(
Expand Down Expand Up @@ -52,7 +53,7 @@ class TvGridScreen extends StatelessWidget {
childAspectRatio: 16 / 13,
crossAxisCount: 3,
children: [
..._.items.map((e) => TvVideoItem(key: ValueKey(e.videoId), video: e, autoFocus: false)).toList(),
..._.items.where((element) => !element.filterHide).map((e) => TvVideoItem(key: ValueKey(e.videoId), video: e, autoFocus: false)).toList(),
if (_.loading) ...repeatWidget(() => const TvVideoItemPlaceHolder(), count: 10)
],
))
Expand Down

0 comments on commit b3d430a

Please sign in to comment.