Skip to content

Commit

Permalink
chart fetching moved to isolates
Browse files Browse the repository at this point in the history
  • Loading branch information
HemantKArya committed Mar 23, 2024
1 parent 75fab2e commit e6972c4
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 117 deletions.
85 changes: 76 additions & 9 deletions lib/blocs/explore/cubit/explore_cubits.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'dart:async';
import 'dart:developer';

import 'dart:isolate';
import 'package:Bloomee/services/db/GlobalDB.dart';
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';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:isar/isar.dart';
import 'package:path_provider/path_provider.dart';

part 'explore_states.dart';

Expand Down Expand Up @@ -58,24 +64,85 @@ class RecentlyCubit extends Cubit<RecentlyCubitState> {

class ChartCubit extends Cubit<ChartState> {
ChartInfo chartInfo;
StreamSubscription? strm;
FetchChartCubit fetchChartCubit;
ChartCubit(
this.chartInfo,
this.fetchChartCubit,
) : super(ChartInitial()) {
getChartFromDB();
getChart();
initListener();
}

void getChart() async {
final chart = await chartInfo.chartFunction(chartInfo.url);
emit(state.copyWith(
chart: chart, coverImg: chart.chartItems?.first.imageUrl));
void initListener() {
strm = fetchChartCubit.stream.listen((state) {
if (state.isFetched) {
log("Chart Fetched from Isolate - ${chartInfo.title}",
name: "Isolate Fetched");
getChartFromDB();
}
});
}

void getChartFromDB() async {
Future<void> getChartFromDB() async {
final chart = await BloomeeDBService.getChart(chartInfo.title);
if (chart != null) {
emit(state.copyWith(
chart: chart, coverImg: chart.chartItems?.first.imageUrl));
}
}

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

class FetchChartCubit extends Cubit<FetchChartState> {
FetchChartCubit() : super(FetchChartInitial()) {
fetchCharts();
}

Future<void> fetchCharts() async {
String _path = (await getApplicationDocumentsDirectory()).path;
BackgroundIsolateBinaryMessenger.ensureInitialized(
ServicesBinding.rootIsolateToken!);

final chartList = await Isolate.run<List<ChartModel>>(() async {
log(_path, name: "Isolate Path");
List<ChartModel> _chartList = List.empty(growable: true);
ChartModel chart;
final db = await Isar.open(
[
ChartsCacheDBSchema,
],
directory: _path,
);
for (var i in chartInfoList) {
final chartCacheDB = db.chartsCacheDBs
.where()
.filter()
.chartNameEqualTo(i.title)
.findFirstSync();
if ((chartCacheDB?.lastUpdated.difference(DateTime.now()).inHours ??
0) >
12) {
chart = await i.chartFunction(i.url);
if ((chart.chartItems?.isNotEmpty) ?? false) {
db.writeTxnSync(() =>
db.chartsCacheDBs.putSync(chartModelToChartCacheDB(chart)));
}
log("Chart Fetched - ${chart.chartName}", name: "Isolate");
_chartList.add(chart);
}
}
db.close();
return _chartList;
});

if (chartList.isNotEmpty) {
emit(state.copyWith(isFetched: true));
}
}
}
19 changes: 19 additions & 0 deletions lib/blocs/explore/cubit/explore_states.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,22 @@ class ChartInitial extends ChartState {
),
coverImg: "");
}

class FetchChartState {
bool isFetched;
FetchChartState({
required this.isFetched,
});

FetchChartState copyWith({
bool? isFetched,
}) {
return FetchChartState(
isFetched: isFetched ?? this.isFetched,
);
}
}

class FetchChartInitial extends FetchChartState {
FetchChartInitial() : super(isFetched: false);
}
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class _MyAppState extends State<MyApp> {
),
BlocProvider(
create: (context) => FetchSearchResultsCubit(),
)
),
],
child: MultiRepositoryProvider(
providers: [
Expand Down
28 changes: 14 additions & 14 deletions lib/plugins/billboard_charts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:developer';

import 'package:Bloomee/model/chart_model.dart';
import 'package:Bloomee/plugins/chart_defines.dart';
import 'package:Bloomee/services/db/bloomee_db_service.dart';
// import 'package:Bloomee/services/db/bloomee_db_service.dart';
import 'package:http/http.dart' as http;
import 'package:html/parser.dart' show parse;
import 'package:html/dom.dart';
Expand Down Expand Up @@ -173,26 +173,26 @@ Future<ChartModel> getBillboardChart(ChartURL url) async {
chartItems: chartItems,
url: url.url,
lastUpdated: DateTime.now());
BloomeeDBService.putChart(chart);
// BloomeeDBService.putChart(chart);
log('Billboard Charts: ${chart.chartItems!.length} tracks',
name: "Billboard");
return chart;
} else {
final chart = await BloomeeDBService.getChart(url.title);
if (chart != null) {
log('Billboard Charts: ${chart.chartItems!.length} tracks loaded from cache',
name: "Billboard");
return chart;
}
// final chart = await BloomeeDBService.getChart(url.title);
// if (chart != null) {
// log('Billboard Charts: ${chart.chartItems!.length} tracks loaded from cache',
// name: "Billboard");
// return chart;
// }
throw Exception("Failed to load page");
}
} catch (e) {
final chart = await BloomeeDBService.getChart(url.title);
if (chart != null) {
log('Billboard Charts: ${chart.chartItems!.length} tracks loaded from cache',
name: "Billboard");
return chart;
}
// final chart = await BloomeeDBService.getChart(url.title);
// if (chart != null) {
// log('Billboard Charts: ${chart.chartItems!.length} tracks loaded from cache',
// name: "Billboard");
// return chart;
// }
throw Exception("Error: $e");
}
}
28 changes: 14 additions & 14 deletions lib/plugins/last_dot_fm_charts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:developer';

import 'package:Bloomee/model/chart_model.dart';
import 'package:Bloomee/plugins/chart_defines.dart';
import 'package:Bloomee/services/db/bloomee_db_service.dart';
// import 'package:Bloomee/services/db/bloomee_db_service.dart';
import 'package:http/http.dart' as http;
import 'package:html/parser.dart' as parser;

Expand Down Expand Up @@ -63,28 +63,28 @@ Future<ChartModel> getLastFmCharts(ChartURL url) async {
chartItems: chartItems,
url: url.url,
lastUpdated: DateTime.now());
BloomeeDBService.putChart(lastfmModel);
// BloomeeDBService.putChart(lastfmModel);
log('Last.fm Charts: ${lastfmModel.chartItems!.length} tracks',
name: "LastFM");

return lastfmModel;
} else {
final chart = await BloomeeDBService.getChart(url.title);
if (chart != null) {
log('LastFM Charts: ${chart.chartItems!.length} tracks loaded from cache',
name: "LastFM");
return chart;
}
// final chart = await BloomeeDBService.getChart(url.title);
// if (chart != null) {
// log('LastFM Charts: ${chart.chartItems!.length} tracks loaded from cache',
// name: "LastFM");
// return chart;
// }
throw Exception(
'Failed to load page with status code: ${response.statusCode}');
}
} on Exception catch (e) {
final chart = await BloomeeDBService.getChart(url.title);
if (chart != null) {
log('LastFM Charts: ${chart.chartItems!.length} tracks loaded from cache',
name: "LastFM");
return chart;
}
// final chart = await BloomeeDBService.getChart(url.title);
// if (chart != null) {
// log('LastFM Charts: ${chart.chartItems!.length} tracks loaded from cache',
// name: "LastFM");
// return chart;
// }
throw Exception('Failed to parse page: $e');
} finally {
client.close();
Expand Down
28 changes: 14 additions & 14 deletions lib/plugins/melon_charts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:developer';

import 'package:Bloomee/model/chart_model.dart';
import 'package:Bloomee/plugins/chart_defines.dart';
import 'package:Bloomee/services/db/bloomee_db_service.dart';
// import 'package:Bloomee/services/db/bloomee_db_service.dart';
import 'package:http/http.dart' as http;
import 'package:html/parser.dart' as parser;

Expand Down Expand Up @@ -103,27 +103,27 @@ Future<ChartModel> getMelonChart(ChartURL url) async {
chartItems: chartItems,
url: url.url,
lastUpdated: DateTime.now());
BloomeeDBService.putChart(melonChart);
// BloomeeDBService.putChart(melonChart);
log('Melon Charts: ${melonChart.chartItems!.length} tracks',
name: "Melon");
return melonChart;
} else {
final chart = await BloomeeDBService.getChart(url.title);
if (chart != null) {
log('Melon Charts: ${chart.chartItems!.length} tracks loaded from cache',
name: "Melon");
return chart;
}
// final chart = await BloomeeDBService.getChart(url.title);
// if (chart != null) {
// log('Melon Charts: ${chart.chartItems!.length} tracks loaded from cache',
// name: "Melon");
// return chart;
// }
throw Exception(
'Parsing failed with status code: ${response.statusCode}');
}
} catch (e) {
final chart = await BloomeeDBService.getChart(url.title);
if (chart != null) {
log('Melon Charts: ${chart.chartItems!.length} tracks loaded from cache',
name: "Melon");
return chart;
}
// final chart = await BloomeeDBService.getChart(url.title);
// if (chart != null) {
// log('Melon Charts: ${chart.chartItems!.length} tracks loaded from cache',
// name: "Melon");
// return chart;
// }
throw Exception('Failed to parse page');
}
}
28 changes: 14 additions & 14 deletions lib/plugins/spotify_top50_chart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:convert';
import 'dart:developer';
import 'package:Bloomee/model/chart_model.dart';
import 'package:Bloomee/plugins/chart_defines.dart';
import 'package:Bloomee/services/db/bloomee_db_service.dart';
// import 'package:Bloomee/services/db/bloomee_db_service.dart';
import 'package:http/http.dart' as http;

const List<String> spotifyIMGs = [
Expand Down Expand Up @@ -42,26 +42,26 @@ Future<ChartModel> getSpotifyTop50Chart(ChartURL url) async {
url: url.url,
lastUpdated: DateTime.now(),
);
BloomeeDBService.putChart(chart);
// BloomeeDBService.putChart(chart);
log('Spotify Charts: ${chart.chartItems!.length} tracks',
name: "Spotify");
return chart;
} else {
final chart = await BloomeeDBService.getChart(url.title);
if (chart != null) {
log('Spotify Charts: ${chart.chartItems!.length} tracks loaded from cache',
name: "Spotify");
return chart;
}
// final chart = await BloomeeDBService.getChart(url.title);
// if (chart != null) {
// log('Spotify Charts: ${chart.chartItems!.length} tracks loaded from cache',
// name: "Spotify");
// return chart;
// }
throw Exception('Failed to load chart');
}
} catch (e) {
final chart = await BloomeeDBService.getChart(url.title);
if (chart != null) {
log('Spotify Charts: ${chart.chartItems!.length} tracks loaded from cache',
name: "Spotify");
return chart;
}
// final chart = await BloomeeDBService.getChart(url.title);
// if (chart != null) {
// log('Spotify Charts: ${chart.chartItems!.length} tracks loaded from cache',
// name: "Spotify");
// return chart;
// }
throw Exception('Something went wrong while parsing the page');
}
}
Loading

0 comments on commit e6972c4

Please sign in to comment.