Skip to content

Commit

Permalink
corrected charts cubits and impt. db for caching
Browse files Browse the repository at this point in the history
  • Loading branch information
HemantKArya committed Mar 22, 2024
1 parent 7beb52d commit bc338fb
Show file tree
Hide file tree
Showing 33 changed files with 2,888 additions and 519 deletions.
16 changes: 0 additions & 16 deletions lib/blocs/explore/cubit/explore_cubit.dart

This file was deleted.

82 changes: 82 additions & 0 deletions lib/blocs/explore/cubit/explore_cubits.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'dart:developer';

import 'package:bloc/bloc.dart';

import 'package:Bloomee/model/MediaPlaylistModel.dart';
import 'package:Bloomee/model/chart_model.dart';
import 'package:Bloomee/plugins/chart_defines.dart';
import 'package:Bloomee/repository/Youtube/yt_charts_home.dart';
import 'package:Bloomee/screens/screen/chart/show_charts.dart';
import 'package:Bloomee/services/db/bloomee_db_service.dart';

part 'explore_states.dart';

class TrendingCubit extends Cubit<TrendingCubitState> {
bool isLatest = false;
TrendingCubit() : super(TrendingCubitInitial()) {
getTrendingVideosFromDB();
getTrendingVideos();
}

void getTrendingVideos() async {
final ytCharts = await fetchTrendingVideos();
emit(state.copyWith(ytCharts: ytCharts));
isLatest = true;
}

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

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

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

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

class ChartCubit extends Cubit<ChartState> {
ChartInfo chartInfo;
ChartCubit(
this.chartInfo,
) : super(ChartInitial()) {
getChartFromDB();
getChart();
}

void getChart() async {
final chart = await chartInfo.chartFunction(chartInfo.url);
emit(state.copyWith(
chart: chart, coverImg: chart.chartItems?.first.imageUrl));
}

void getChartFromDB() async {
final chart = await BloomeeDBService.getChart(chartInfo.title);
if (chart != null) {
emit(state.copyWith(
chart: chart, coverImg: chart.chartItems?.first.imageUrl));
}
}
}
21 changes: 0 additions & 21 deletions lib/blocs/explore/cubit/explore_state.dart

This file was deleted.

70 changes: 70 additions & 0 deletions lib/blocs/explore/cubit/explore_states.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// ignore_for_file: public_member_api_docs, sort_constructors_first
part of 'explore_cubits.dart';

class TrendingCubitState {
List<ChartModel>? ytCharts;
TrendingCubitState({
required this.ytCharts,
});

TrendingCubitState copyWith({
List<ChartModel>? ytCharts,
}) {
return TrendingCubitState(
ytCharts: ytCharts ?? this.ytCharts,
);
}
}

final class TrendingCubitInitial extends TrendingCubitState {
TrendingCubitInitial() : super(ytCharts: []);
}

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

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

class RecentlyCubitInitial extends RecentlyCubitState {
RecentlyCubitInitial()
: super(mediaPlaylist: MediaPlaylist(albumName: "", mediaItems: []));
}

class ChartState {
ChartModel chart;
String coverImg;
ChartState({
required this.chart,
required this.coverImg,
});

ChartState copyWith({
ChartModel? chart,
String? coverImg,
}) {
return ChartState(
chart: chart ?? this.chart,
coverImg: coverImg ?? this.coverImg,
);
}
}

class ChartInitial extends ChartState {
ChartInitial()
: super(
chart: ChartModel(
chartName: "",
chartItems: [],
),
coverImg: "");
}
6 changes: 4 additions & 2 deletions lib/blocs/settings_cubit/cubit/settings_cubit.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:Bloomee/routes_and_consts/global_str_consts.dart';
import 'package:Bloomee/services/db/bloomee_db_service.dart';
import 'package:bloc/bloc.dart';
import 'package:meta/meta.dart';
Expand All @@ -10,13 +11,14 @@ class SettingsCubit extends Cubit<SettingsState> {
}

void initSettings() {
BloomeeDBService.getSettingBool("auto_update_notify").then((value) {
BloomeeDBService.getSettingBool(GlobalStrConsts.autoUpdateNotify)
.then((value) {
emit(state.copyWith(autoUpdateNotify: value ?? false));
});
}

void updateAutoUpdateNotify(bool value) {
BloomeeDBService.putSettingBool("auto_update_notify", value);
BloomeeDBService.putSettingBool(GlobalStrConsts.autoUpdateNotify, value);
emit(state.copyWith(autoUpdateNotify: value));
}
}
12 changes: 12 additions & 0 deletions lib/model/MediaPlaylistModel.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'package:Bloomee/model/songModel.dart';
import 'package:Bloomee/services/db/GlobalDB.dart';

class MediaPlaylist {
late List<MediaItemModel> mediaItems;
Expand All @@ -11,3 +12,14 @@ class MediaPlaylist {
required this.albumName,
});
}

MediaPlaylist fromPlaylistDB2MediaPlaylist(MediaPlaylistDB mediaPlaylistDB) {
MediaPlaylist mediaPlaylist =
MediaPlaylist(mediaItems: [], albumName: mediaPlaylistDB.playlistName);
if (mediaPlaylistDB.mediaItems.isNotEmpty) {
mediaPlaylistDB.mediaItems.forEach((element) {
mediaPlaylist.mediaItems.add(MediaItemDB2MediaItem(element));
});
}
return mediaPlaylist;
}
58 changes: 58 additions & 0 deletions lib/model/chart_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import 'package:Bloomee/services/db/GlobalDB.dart';

class ChartModel {
final String chartName;
final String? url;
List<ChartItemModel>? chartItems = List.empty(growable: true);
DateTime? lastUpdated = DateTime.now();

ChartModel(
{required this.chartName, this.chartItems, this.lastUpdated, this.url});
}

class ChartItemModel {
final String? name;
final String? imageUrl;
final String? subtitle;

ChartItemModel({
required this.name,
required this.imageUrl,
required this.subtitle,
});
}

ChartsCacheDB chartModelToChartCacheDB(ChartModel chartModel) {
return ChartsCacheDB(
chartItems: chartModel.chartItems
?.map((e) => chartItemModelToChartItemDB(e))
.toList() ??
List.empty(growable: true),
chartName: chartModel.chartName,
lastUpdated: chartModel.lastUpdated ?? DateTime.now(),
permaURL: chartModel.url);
}

ChartModel chartCacheDBToChartModel(ChartsCacheDB chartsCacheDB) {
return ChartModel(
chartItems: chartsCacheDB.chartItems
.map((e) => chartItemDBToChartItemModel(e))
.toList(),
chartName: chartsCacheDB.chartName,
lastUpdated: chartsCacheDB.lastUpdated,
url: chartsCacheDB.permaURL);
}

ChartItemDB chartItemModelToChartItemDB(ChartItemModel chartItemModel) {
return ChartItemDB()
..artURL = chartItemModel.imageUrl
..artist = chartItemModel.subtitle
..title = chartItemModel.name;
}

ChartItemModel chartItemDBToChartItemModel(ChartItemDB chartItemDB) {
return ChartItemModel(
imageUrl: chartItemDB.artURL,
name: chartItemDB.title,
subtitle: chartItemDB.artist);
}
36 changes: 36 additions & 0 deletions lib/model/songModel.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'package:Bloomee/services/db/GlobalDB.dart';
import 'package:audio_service/audio_service.dart';
import 'package:flutter/foundation.dart';

Expand Down Expand Up @@ -55,3 +56,38 @@ class MediaItemModel extends MediaItem {
genre.hashCode;
}
}

MediaItemDB MediaItem2MediaItemDB(MediaItem mediaItem) {
return MediaItemDB(
title: mediaItem.title,
album: mediaItem.album ?? "Unknown",
artist: mediaItem.artist ?? "Unknown",
artURL: mediaItem.artUri.toString(),
genre: mediaItem.genre ?? "Unknown",
mediaID: mediaItem.id,
duration: mediaItem.duration?.inSeconds,
streamingURL: mediaItem.extras?["url"],
permaURL: mediaItem.extras?["perma_url"],
language: mediaItem.extras?["language"] ?? "Unknown",
isLiked: false,
source: mediaItem.extras?["source"] ?? "Saavn");
}

MediaItemModel MediaItemDB2MediaItem(MediaItemDB mediaItemDB) {
return MediaItemModel(
id: mediaItemDB.mediaID,
title: mediaItemDB.title,
album: mediaItemDB.album,
artist: mediaItemDB.artist,
duration: mediaItemDB.duration != null
? Duration(seconds: mediaItemDB.duration!)
: const Duration(seconds: 120),
artUri: Uri.parse(mediaItemDB.artURL),
genre: mediaItemDB.genre,
extras: {
"url": mediaItemDB.streamingURL,
"source": mediaItemDB.source ?? "None",
"perma_url": mediaItemDB.permaURL,
"language": mediaItemDB.language,
});
}
Loading

0 comments on commit bc338fb

Please sign in to comment.