freezer/lib/api/cache.dart

166 lines
4.5 KiB
Dart
Raw Normal View History

import 'package:freezer/api/definitions.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as p;
import 'dart:io';
import 'dart:convert';
2021-07-02 16:28:59 +00:00
import 'dart:async';
part 'cache.g.dart';
2021-09-01 12:38:32 +00:00
late Cache cache;
//Cache for miscellaneous things
@JsonSerializable()
class Cache {
//ID's of tracks that are in library
2021-09-01 12:38:32 +00:00
List<String>? libraryTracks = [];
//Track ID of logged track, to prevent duplicates
@JsonKey(ignore: true)
2021-09-01 12:38:32 +00:00
String? loggedTrackId;
@JsonKey(defaultValue: [])
List<Track> history = [];
2020-11-28 21:32:17 +00:00
//All sorting cached
@JsonKey(defaultValue: [])
2021-09-01 12:38:32 +00:00
List<Sorting?> sorts = [];
2020-10-15 20:10:17 +00:00
//Sleep timer
@JsonKey(ignore: true)
2021-09-01 12:38:32 +00:00
DateTime? sleepTimerTime;
2020-10-15 20:10:17 +00:00
@JsonKey(ignore: true)
2021-09-01 12:38:32 +00:00
// ignore: cancel_subscriptions
StreamSubscription? sleepTimer;
2020-10-15 20:10:17 +00:00
2020-10-20 19:55:14 +00:00
//Search history
2021-09-01 12:38:32 +00:00
@JsonKey(name: 'searchHistory2')
List<SearchHistoryItem>? searchHistory;
2020-10-20 19:55:14 +00:00
//If download threads warning was shown
@JsonKey(defaultValue: false)
2021-09-01 12:38:32 +00:00
bool? threadsWarning;
//Last time update check
@JsonKey(defaultValue: 0)
2021-09-01 12:38:32 +00:00
int? lastUpdateCheck;
@JsonKey(ignore: true)
bool wakelock = false;
Cache({this.libraryTracks});
//Wrapper to test if track is favorite against cache
bool checkTrackFavorite(Track t) {
2021-09-01 12:38:32 +00:00
if (t.favorite != null && t.favorite!) return true;
if (libraryTracks == null || libraryTracks!.length == 0) return false;
return libraryTracks!.contains(t.id);
}
2020-10-20 19:55:14 +00:00
//Add to history
void addToSearchHistory(dynamic item) async {
if (searchHistory == null) searchHistory = [];
2020-10-20 19:55:14 +00:00
2021-07-02 16:28:59 +00:00
// Remove duplicate
2021-09-01 12:38:32 +00:00
int i = searchHistory!.indexWhere((e) => e.data.id == item.id);
2021-07-02 16:28:59 +00:00
if (i != -1) {
2021-09-01 12:38:32 +00:00
searchHistory!.removeAt(i);
2021-07-02 16:28:59 +00:00
}
2020-10-20 19:55:14 +00:00
if (item is Track)
2021-09-01 12:38:32 +00:00
searchHistory!.add(SearchHistoryItem(item, SearchHistoryItemType.TRACK));
2020-10-20 19:55:14 +00:00
if (item is Album)
2021-09-01 12:38:32 +00:00
searchHistory!.add(SearchHistoryItem(item, SearchHistoryItemType.ALBUM));
2020-10-20 19:55:14 +00:00
if (item is Artist)
2021-09-01 12:38:32 +00:00
searchHistory!.add(SearchHistoryItem(item, SearchHistoryItemType.ARTIST));
2020-10-20 19:55:14 +00:00
if (item is Playlist)
2021-09-01 12:38:32 +00:00
searchHistory!
.add(SearchHistoryItem(item, SearchHistoryItemType.PLAYLIST));
2020-10-20 19:55:14 +00:00
await save();
}
//Save, load
static Future<String> getPath() async {
return p.join(
(await getApplicationDocumentsDirectory()).path, 'metacache.json');
}
static Future wipe() async {
await File(await getPath()).delete();
}
static Future<Cache> load() async {
File file = File(await Cache.getPath());
//Doesn't exist, create new
if (!(await file.exists())) {
Cache c = Cache();
await c.save();
return c;
}
return Cache.fromJson(jsonDecode(await file.readAsString()));
}
Future save() async {
File file = File(await Cache.getPath());
file.writeAsString(jsonEncode(this.toJson()));
}
//JSON
factory Cache.fromJson(Map<String, dynamic> json) => _$CacheFromJson(json);
Map<String, dynamic> toJson() => _$CacheToJson(this);
2020-10-20 19:55:14 +00:00
//Search History JSON
2021-09-01 12:38:32 +00:00
// static List<SearchHistoryItem> _searchHistoryFromJson(List<dynamic>? json) {
// return (json ?? [])
// .map<SearchHistoryItem>((i) => _searchHistoryItemFromJson(i))
// .toList();
// }
// static SearchHistoryItem _searchHistoryItemFromJson(
// Map<String, dynamic> json) {
// SearchHistoryItemType type = SearchHistoryItemType.values[json['type']];
// dynamic data;
// switch (type) {
// case SearchHistoryItemType.TRACK:
// data = Track.fromJson(json['data']);
// break;
// case SearchHistoryItemType.ALBUM:
// data = Album.fromJson(json['data']);
// break;
// case SearchHistoryItemType.ARTIST:
// data = Artist.fromJson(json['data']);
// break;
// case SearchHistoryItemType.PLAYLIST:
// data = Playlist.fromJson(json['data']);
// break;
// }
// return SearchHistoryItem(data, type);
// }
2020-10-20 19:55:14 +00:00
}
@JsonSerializable()
class SearchHistoryItem {
dynamic data;
2021-09-01 12:38:32 +00:00
@JsonKey(
toJson: _searchHistoryItemTypeToJson,
fromJson: _searchHistoryItemTypeFromJson)
2020-10-20 19:55:14 +00:00
SearchHistoryItemType type;
SearchHistoryItem(this.data, this.type);
2021-09-01 12:38:32 +00:00
Map<String, dynamic> toJson() => _$SearchHistoryItemToJson(this);
factory SearchHistoryItem.fromJson(Map<String, dynamic> json) =>
_$SearchHistoryItemFromJson(json);
static int _searchHistoryItemTypeToJson(SearchHistoryItemType type) =>
type.index;
static SearchHistoryItemType _searchHistoryItemTypeFromJson(int index) =>
SearchHistoryItemType.values[index];
2020-10-20 19:55:14 +00:00
}
enum SearchHistoryItemType { TRACK, ALBUM, ARTIST, PLAYLIST }