Skip to content

Commit

Permalink
history page implt. and limit set on home page
Browse files Browse the repository at this point in the history
  • Loading branch information
HemantKArya committed Apr 10, 2024
1 parent 90551b8 commit cc278a0
Show file tree
Hide file tree
Showing 9 changed files with 302 additions and 78 deletions.
30 changes: 21 additions & 9 deletions lib/blocs/explore/cubit/explore_cubits.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,51 @@ class TrendingCubit extends Cubit<TrendingCubitState> {
}

void getTrendingVideos() async {
final ytCharts = await fetchTrendingVideos();
emit(state.copyWith(ytCharts: ytCharts));
List<ChartModel> ytCharts = await fetchTrendingVideos();
ChartModel chart = ytCharts[0]
..chartItems = getFirstElements(ytCharts[0].chartItems!, 16);
emit(state.copyWith(ytCharts: [chart]));
isLatest = true;
}

List<ChartItemModel> getFirstElements(List<ChartItemModel> list, int count) {
return list.length > count ? list.sublist(0, count) : list;
}

void getTrendingVideosFromDB() async {
final ytChart = await BloomeeDBService.getChart("Trending Videos");
ChartModel? ytChart = await BloomeeDBService.getChart("Trending Videos");
if ((!isLatest) &&
ytChart != null &&
(ytChart.chartItems?.isNotEmpty ?? false)) {
emit(state.copyWith(ytCharts: [ytChart]));
ChartModel chart = ytChart
..chartItems = getFirstElements(ytChart.chartItems!, 16);
emit(state.copyWith(ytCharts: [chart]));
}
}
}

class RecentlyCubit extends Cubit<RecentlyCubitState> {
late Stream<void> watcher;
StreamSubscription<void>? watcher;
RecentlyCubit() : super(RecentlyCubitInitial()) {
BloomeeDBService.refreshRecentlyPlayed();
getRecentlyPlayed();
watchRecentlyPlayed();
}

Future<void> watchRecentlyPlayed() async {
watcher = await BloomeeDBService.watchRecentlyPlayed();
watcher.listen((event) {
(await BloomeeDBService.watchRecentlyPlayed()).listen((event) {
getRecentlyPlayed();
log("Recently Played Updated");
});
}

@override
Future<void> close() {
watcher?.cancel();
return super.close();
}

void getRecentlyPlayed() async {
final mediaPlaylist = await BloomeeDBService.getRecentlyPlayed();
final mediaPlaylist = await BloomeeDBService.getRecentlyPlayed(limit: 15);
emit(state.copyWith(mediaPlaylist: mediaPlaylist));
}
}
Expand Down
31 changes: 31 additions & 0 deletions lib/blocs/history/cubit/history_cubit.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'dart:async';
import 'dart:developer';
import 'package:Bloomee/services/db/bloomee_db_service.dart';
import 'package:bloc/bloc.dart';
import 'package:Bloomee/model/MediaPlaylistModel.dart';
part 'history_state.dart';

class HistoryCubit extends Cubit<HistoryState> {
StreamSubscription<void>? watcher;
HistoryCubit() : super(HistoryInitial()) {
getRecentlyPlayed();
watchRecentlyPlayed();
}
Future<void> watchRecentlyPlayed() async {
watcher = (await BloomeeDBService.watchRecentlyPlayed()).listen((event) {
getRecentlyPlayed();
log("History Updated");
});
}

void getRecentlyPlayed() async {
final mediaPlaylist = await BloomeeDBService.getRecentlyPlayed();
emit(state.copyWith(mediaPlaylist: mediaPlaylist));
}

@override
Future<void> close() {
watcher?.cancel();
return super.close();
}
}
21 changes: 21 additions & 0 deletions lib/blocs/history/cubit/history_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
part of 'history_cubit.dart';

class HistoryState {
MediaPlaylist mediaPlaylist;
HistoryState({
required this.mediaPlaylist,
});

HistoryState copyWith({
MediaPlaylist? mediaPlaylist,
}) {
return HistoryState(
mediaPlaylist: mediaPlaylist ?? this.mediaPlaylist,
);
}
}

class HistoryInitial extends HistoryState {
HistoryInitial()
: super(mediaPlaylist: MediaPlaylist(albumName: "", mediaItems: []));
}
73 changes: 41 additions & 32 deletions lib/screens/screen/explore_screen.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:Bloomee/blocs/downloader/cubit/downloader_cubit.dart';
import 'package:Bloomee/blocs/explore/cubit/explore_cubits.dart';
import 'package:Bloomee/blocs/mediaPlayer/bloomee_player_cubit.dart';
import 'package:Bloomee/routes_and_consts/global_str_consts.dart';
import 'package:Bloomee/screens/screen/home_views/recents_view.dart';
import 'package:Bloomee/screens/widgets/chart_list_tile.dart';
import 'package:Bloomee/screens/widgets/more_bottom_sheet.dart';
import 'package:Bloomee/screens/widgets/song_card_widget.dart';
Expand Down Expand Up @@ -80,28 +80,38 @@ class _ExploreScreenState extends State<ExploreScreen> {
)),
)
: ((state.mediaPlaylist.mediaItems.isNotEmpty)
? TabSongListWidget(
list: state.mediaPlaylist.mediaItems
.map((e) {
return SongCardWidget(
song: e,
onTap: () {
context
.read<BloomeePlayerCubit>()
.bloomeePlayer
.addQueueItem(
e,
);
// context
// .read<DownloaderCubit>()
// .downloadSong(e);
},
onOptionsTap: () =>
showMoreBottomSheet(context, e),
);
}).toList(),
category: "Recently",
columnSize: 3,
? InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
const HistoryView()));
},
child: TabSongListWidget(
list: state.mediaPlaylist.mediaItems
.map((e) {
return SongCardWidget(
song: e,
onTap: () {
context
.read<BloomeePlayerCubit>()
.bloomeePlayer
.addQueueItem(
e,
);
// context
// .read<DownloaderCubit>()
// .downloadSong(e);
},
onOptionsTap: () =>
showMoreBottomSheet(
context, e),
);
}).toList(),
category: "Recently",
columnSize: 3,
),
)
: const SizedBox()),
);
Expand All @@ -126,15 +136,14 @@ class _ExploreScreenState extends State<ExploreScreen> {
)),
)
: TabSongListWidget(
list:
state.ytCharts![0].chartItems!.map((e) {
return ChartListTile(
title: e.name ?? "",
subtitle: e.subtitle ?? "",
imgUrl: e.imageUrl ?? "",
rectangularImage: true,
);
}).toList(),
list: state.ytCharts![0].chartItems!
.map((e) => ChartListTile(
title: e.name ?? "",
subtitle: e.subtitle ?? "",
imgUrl: e.imageUrl ?? "",
rectangularImage: true,
))
.toList(),
category: "Trending",
columnSize: 4,
),
Expand Down
95 changes: 95 additions & 0 deletions lib/screens/screen/home_views/recents_view.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'package:Bloomee/blocs/history/cubit/history_cubit.dart';
import 'package:Bloomee/blocs/mediaPlayer/bloomee_player_cubit.dart';
import 'package:Bloomee/screens/widgets/more_bottom_sheet.dart';
import 'package:Bloomee/screens/widgets/song_card_widget.dart';
import 'package:flutter/material.dart';
import 'package:Bloomee/theme_data/default.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

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

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Default_Theme.themeColor,
appBar: AppBar(
centerTitle: true,
backgroundColor: Default_Theme.themeColor,
surfaceTintColor: Default_Theme.themeColor,
foregroundColor: Default_Theme.primaryColor1,
title: Text(
'History',
style: const TextStyle(
color: Default_Theme.primaryColor1,
fontSize: 20,
fontWeight: FontWeight.bold)
.merge(Default_Theme.secondoryTextStyle),
),
),
body: BlocProvider(
create: (context) => HistoryCubit(),
child: BlocBuilder<HistoryCubit, HistoryState>(
builder: (context, state) {
return (state is HistoryInitial)
? const Center(
child: CircularProgressIndicator(),
)
: ListView.builder(
itemCount: state.mediaPlaylist.mediaItems.length,
shrinkWrap: true,
physics: const BouncingScrollPhysics(),
itemBuilder: (context, index) {
return SongCardWidget(
song: state.mediaPlaylist.mediaItems[index],
onTap: () {
context
.read<BloomeePlayerCubit>()
.bloomeePlayer
.addQueueItem(
state.mediaPlaylist.mediaItems[index],
);
},
onOptionsTap: () => showMoreBottomSheet(
context, state.mediaPlaylist.mediaItems[index]),
);
},
);
},
),
),
);
}

ListTile settingListTile(
{required String title,
required String subtitle,
required IconData icon,
VoidCallback? onTap}) {
return ListTile(
leading: Icon(
icon,
size: 30,
color: Default_Theme.primaryColor1,
),
title: Text(
title,
style: const TextStyle(color: Default_Theme.primaryColor1, fontSize: 17)
.merge(Default_Theme.secondoryTextStyleMedium),
),
subtitle: Text(
subtitle,
style: TextStyle(
color: Default_Theme.primaryColor1.withOpacity(0.5),
fontSize: 12.5)
.merge(Default_Theme.secondoryTextStyleMedium),
),
onTap: () {
if (onTap != null) {
onTap();
}
},
);
}
}
12 changes: 1 addition & 11 deletions lib/screens/screen/home_views/setting_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,9 @@ import 'package:flutter/material.dart';
import 'package:Bloomee/theme_data/default.dart';
import 'package:icons_plus/icons_plus.dart';

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

@override
State<SettingsView> createState() => _SettingsViewState();
}

class _SettingsViewState extends State<SettingsView> {
@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down
58 changes: 58 additions & 0 deletions lib/screens/widgets/paging_scroll.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import 'package:flutter/material.dart';

//credit goes to: https://stackoverflow.com/a/72817531/21571707

class PagingScrollPhysics extends ScrollPhysics {
const PagingScrollPhysics(
{required this.itemCount, required this.viewSize, super.parent});

final double viewSize;

final int itemCount;

@override
PagingScrollPhysics applyTo(ScrollPhysics? ancestor) => PagingScrollPhysics(
itemCount: itemCount, viewSize: viewSize, parent: buildParent(ancestor));

double _getPage(double current, double itemDimension) =>
current / itemDimension;

double _getTargetPixels(
ScrollMetrics position, Tolerance tolerance, double velocity) {
// plus view size because the max scroll extent is about where the screen
// starts not where the screen ends.
final pixels = position.maxScrollExtent + viewSize;
final itemDimension = pixels / itemCount;
var page = _getPage(position.pixels, itemDimension);
if (velocity < -tolerance.velocity) {
page -= 0.5;
} else if (velocity > tolerance.velocity) {
page += 0.5;
}
final pageRound = page.round();
final itemsPerPage = viewSize ~/ itemDimension;
final showingLastItem = pageRound == itemCount - itemsPerPage;
if (showingLastItem) return pixels - viewSize;

return pageRound * itemDimension;
}

@override
Simulation? createBallisticSimulation(
ScrollMetrics position, double velocity) {
if ((velocity <= 0.0 && position.pixels <= position.minScrollExtent) ||
(velocity >= 0.0 && position.pixels >= position.maxScrollExtent)) {
return super.createBallisticSimulation(position, velocity);
}
final Tolerance tolerance = this.tolerance;
final double target = _getTargetPixels(position, tolerance, velocity);
if (target != position.pixels) {
return ScrollSpringSimulation(spring, position.pixels, target, velocity,
tolerance: tolerance);
}
return null;
}

@override
bool get allowImplicitScrolling => false;
}
Loading

0 comments on commit cc278a0

Please sign in to comment.