Skip to content

Commit

Permalink
change implementation of caching homescreen data
Browse files Browse the repository at this point in the history
  • Loading branch information
anandnet committed Feb 13, 2024
1 parent e621dd6 commit d860123
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 47 deletions.
1 change: 0 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class MyApp extends StatelessWidget {
if (msg == "AppLifecycleState.resumed") {
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
} else if (msg == "AppLifecycleState.detached") {
await Get.find<HomeScreenController>().cachedHomeScreenData();
await Get.find<AudioHandler>().customAction("saveSession");
}
return null;
Expand Down
91 changes: 50 additions & 41 deletions lib/ui/screens/Home/home_screen_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ class HomeScreenController extends GetxController {
if (homeScreenData.keys.isNotEmpty) {
final String quickPicksType = homeScreenData.get("quickPicksType");
final List quickPicksData = homeScreenData.get("quickPicks");
final List middleContentData = homeScreenData.get("middleContent");
final List fixedContentData = homeScreenData.get("fixedContent");
final List middleContentData = homeScreenData.get("middleContent") ?? [];
final List fixedContentData = homeScreenData.get("fixedContent") ?? [];
quickPicks.value = QuickPicks(
quickPicksData.map((e) => MediaItemBuilder.fromJson(e)).toList(),
title: quickPicksType);
Expand All @@ -75,7 +75,6 @@ class HomeScreenController extends GetxController {
? AlbumContent.fromJson(e)
: PlaylistContent.fromJson(e))
.toList();
homeScreenData.close();
isContentFetched.value = true;
printINFO("Loaded from offline db");
return true;
Expand Down Expand Up @@ -143,11 +142,13 @@ class HomeScreenController extends GetxController {

middleContent.value = _setContentList(middleContentTemp);
fixedContent.value = _setContentList(homeContentListMap);

isContentFetched.value = true;

// set home content last update time
cachedHomeScreenData(updateAll: true);
await Hive.box("AppPrefs")
.put("homeScreenDataTime", DateTime.now().millisecondsSinceEpoch);

isContentFetched.value = true;
// ignore: unused_catch_stack
} on NetworkError catch (_, e) {
printERROR("Home Content not loaded due to ${_.message}");
Expand Down Expand Up @@ -187,8 +188,6 @@ class HomeScreenController extends GetxController {
quickPicks_ = QuickPicks(
List<MediaItem>.from(homeContentListMap[0]["contents"]),
title: homeContentListMap[0]["title"]);
await Hive.box("AppPrefs")
.put("homeScreenDataTime", DateTime.now().millisecondsSinceEpoch);
} else if (val == "TMV" || val == 'TR') {
try {
final charts = await _musicServices.getCharts();
Expand All @@ -200,8 +199,6 @@ class HomeScreenController extends GetxController {
quickPicks_ = QuickPicks(
List<MediaItem>.from(charts[index]["contents"]),
title: charts[index]["title"]);
await Hive.box("AppPrefs")
.put("homeScreenDataTime", DateTime.now().millisecondsSinceEpoch);
} catch (e) {
printERROR(
"Seems ${val == "TMV" ? "Top music videos" : "Trending songs"} currently not available!");
Expand All @@ -216,9 +213,6 @@ class HomeScreenController extends GetxController {
quickPicks_ =
QuickPicks(List<MediaItem>.from(value[0]["contents"]));
Hive.box("AppPrefs").put("recentSongId", songId);
// set home content last update time
await Hive.box("AppPrefs").put(
"homeScreenDataTime", DateTime.now().millisecondsSinceEpoch);
}
// ignore: empty_catches
} catch (e) {}
Expand All @@ -227,6 +221,11 @@ class HomeScreenController extends GetxController {
if (quickPicks_ == null) return;

quickPicks.value = quickPicks_;

// set home content last update time
cachedHomeScreenData(updateQuickPicksNMiddleContent: true);
await Hive.box("AppPrefs")
.put("homeScreenDataTime", DateTime.now().millisecondsSinceEpoch);
}

void onSideBarTabSelected(int index) {
Expand Down Expand Up @@ -286,39 +285,49 @@ class HomeScreenController extends GetxController {
}
}

Future<void> cachedHomeScreenData() async {
if (Get.find<SettingsScreenController>().cacheHomeScreenData.isFalse || quickPicks.value.songList.isEmpty) {
Future<void> cachedHomeScreenData({
bool updateAll = false,
bool updateQuickPicksNMiddleContent = false,
}) async {
if (Get.find<SettingsScreenController>().cacheHomeScreenData.isFalse ||
quickPicks.value.songList.isEmpty) {
return;
}
final homeScreenData = await Hive.openBox("homeScreenData");
await homeScreenData.clear();
final quickPicksSongs = quickPicks.value.songList
.toList()
.map((e) => MediaItemBuilder.toJson(e))
.toList();
final fixedContentData = fixedContent.toList().map((e) {
if (e.runtimeType == AlbumContent) {
return (e as AlbumContent).toJson();
} else {
return (e as PlaylistContent).toJson();
}
}).toList();
final middleContentData = middleContent.toList().map((e) {
if (e.runtimeType == AlbumContent) {
return (e as AlbumContent).toJson();
} else {
return (e as PlaylistContent).toJson();
}
}).toList();

await homeScreenData.putAll({
"quickPicksType": quickPicks.value.title,
"quickPicks": quickPicksSongs,
"middleContent": fixedContentData,
"fixedContent": middleContentData
});
final homeScreenData = Hive.box("homeScreenData");

if (updateQuickPicksNMiddleContent) {
await homeScreenData.putAll({
"quickPicksType": quickPicks.value.title,
"quickPicks": _getContentDataInJson(quickPicks.value.songList,
isQuickPicks: true),
"middleContent": _getContentDataInJson(middleContent.toList()),
});
} else if (updateAll) {
await homeScreenData.putAll({
"quickPicksType": quickPicks.value.title,
"quickPicks": _getContentDataInJson(quickPicks.value.songList,
isQuickPicks: true),
"middleContent": _getContentDataInJson(middleContent.toList()),
"fixedContent": _getContentDataInJson(fixedContent.toList())
});
}

await homeScreenData.close();
printINFO("Saved Homescreen data data");
}

List<Map<String, dynamic>> _getContentDataInJson(List content,
{bool isQuickPicks = false}) {
if (isQuickPicks) {
return content.toList().map((e) => MediaItemBuilder.toJson(e)).toList();
} else {
return content.map((e) {
if (e.runtimeType == AlbumContent) {
return (e as AlbumContent).toJson();
} else {
return (e as PlaylistContent).toJson();
}
}).toList();
}
}
}
7 changes: 5 additions & 2 deletions lib/ui/screens/Settings/settings_screen_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,17 @@ class SettingsScreenController extends GetxController {
restorePlaybackSession.value = val;
}

void toggleCacheHomeScreenData(bool val) {
Future<void> toggleCacheHomeScreenData(bool val) async {
setBox.put("cacheHomeScreenData", val);
cacheHomeScreenData.value = val;
if (!val) {
Hive.openBox("homeScreenData").then((box) async {
await box.clear();
box.close();
await box.close();
});
}else{
await Hive.openBox("homeScreenData");
Get.find<HomeScreenController>().cachedHomeScreenData(updateAll: true);
}
}

Expand Down
3 changes: 0 additions & 3 deletions lib/utils/system_tray.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'package:harmonymusic/ui/screens/Settings/settings_screen_controller.dart
import 'package:system_tray/system_tray.dart';
import 'package:window_manager/window_manager.dart';

import '../ui/screens/Home/home_screen_controller.dart';

class DesktopSystemTray extends GetxService {
late WindowListener listener;
Expand Down Expand Up @@ -68,7 +67,6 @@ class DesktopSystemTray extends GetxService {
label: 'Quit',
onClicked: (menuItem) async {
await Get.find<AudioHandler>().customAction("saveSession");
await Get.find<HomeScreenController>().cachedHomeScreenData();
exit(0);
}),
]);
Expand Down Expand Up @@ -110,7 +108,6 @@ class CloseWindowListener extends WindowListener {
await windowManager.hide();
} else {
await Get.find<AudioHandler>().customAction("saveSession");
await Get.find<HomeScreenController>().cachedHomeScreenData();
exit(0);
}
}
Expand Down

0 comments on commit d860123

Please sign in to comment.