2020-10-15 20:10:17 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2020-10-09 18:52:45 +00:00
|
|
|
import 'package:freezer/api/deezer.dart';
|
|
|
|
import 'package:freezer/api/definitions.dart';
|
|
|
|
import 'package:freezer/ui/details_screens.dart';
|
2020-10-12 20:49:13 +00:00
|
|
|
import 'package:freezer/ui/library.dart';
|
2020-10-09 18:52:45 +00:00
|
|
|
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';
|
|
|
|
|
|
|
|
part 'cache.g.dart';
|
|
|
|
|
|
|
|
Cache cache;
|
|
|
|
|
|
|
|
//Cache for miscellaneous things
|
|
|
|
@JsonSerializable()
|
|
|
|
class Cache {
|
|
|
|
|
|
|
|
//ID's of tracks that are in library
|
|
|
|
List<String> libraryTracks = [];
|
|
|
|
|
|
|
|
//Track ID of logged track, to prevent duplicates
|
|
|
|
@JsonKey(ignore: true)
|
|
|
|
String loggedTrackId;
|
|
|
|
|
|
|
|
@JsonKey(defaultValue: [])
|
|
|
|
List<Track> history = [];
|
|
|
|
|
2020-11-28 21:32:17 +00:00
|
|
|
//All sorting cached
|
|
|
|
@JsonKey(defaultValue: [])
|
|
|
|
List<Sorting> sorts = [];
|
2020-10-12 20:49:13 +00:00
|
|
|
|
2020-10-15 20:10:17 +00:00
|
|
|
//Sleep timer
|
|
|
|
@JsonKey(ignore: true)
|
|
|
|
DateTime sleepTimerTime;
|
|
|
|
@JsonKey(ignore: true)
|
|
|
|
StreamSubscription sleepTimer;
|
|
|
|
|
2020-10-20 19:55:14 +00:00
|
|
|
//Search history
|
|
|
|
@JsonKey(name: 'searchHistory2', toJson: _searchHistoryToJson, fromJson: _searchHistoryFromJson)
|
|
|
|
List<SearchHistoryItem> searchHistory;
|
|
|
|
|
2020-10-14 19:09:16 +00:00
|
|
|
//If download threads warning was shown
|
|
|
|
@JsonKey(defaultValue: false)
|
|
|
|
bool threadsWarning;
|
2020-10-09 18:52:45 +00:00
|
|
|
|
2020-11-15 19:25:28 +00:00
|
|
|
//Last time update check
|
|
|
|
@JsonKey(defaultValue: 0)
|
|
|
|
int lastUpdateCheck;
|
|
|
|
|
2020-10-09 18:52:45 +00:00
|
|
|
Cache({this.libraryTracks});
|
|
|
|
|
|
|
|
//Wrapper to test if track is favorite against cache
|
|
|
|
bool checkTrackFavorite(Track t) {
|
|
|
|
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 = [];
|
|
|
|
|
|
|
|
if (item is Track)
|
|
|
|
searchHistory.add(SearchHistoryItem(item, SearchHistoryItemType.TRACK));
|
|
|
|
if (item is Album)
|
|
|
|
searchHistory.add(SearchHistoryItem(item, SearchHistoryItemType.ALBUM));
|
|
|
|
if (item is Artist)
|
|
|
|
searchHistory.add(SearchHistoryItem(item, SearchHistoryItemType.ARTIST));
|
|
|
|
if (item is Playlist)
|
|
|
|
searchHistory.add(SearchHistoryItem(item, SearchHistoryItemType.PLAYLIST));
|
|
|
|
|
|
|
|
await save();
|
|
|
|
}
|
|
|
|
|
2020-10-09 18:52:45 +00:00
|
|
|
//Save, load
|
|
|
|
static Future<String> getPath() async {
|
|
|
|
return p.join((await getApplicationDocumentsDirectory()).path, 'metacache.json');
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
static List<Map<String, dynamic>> _searchHistoryToJson(List<SearchHistoryItem> data) => (data??[]).map<Map<String, dynamic>>((i) => {"type": i.type.index, "data": i.data.toJson()}).toList();
|
|
|
|
}
|
|
|
|
|
|
|
|
@JsonSerializable()
|
|
|
|
class SearchHistoryItem {
|
|
|
|
dynamic data;
|
|
|
|
SearchHistoryItemType type;
|
|
|
|
|
|
|
|
SearchHistoryItem(this.data, this.type);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
enum SearchHistoryItemType {
|
|
|
|
TRACK,
|
|
|
|
ALBUM,
|
|
|
|
ARTIST,
|
|
|
|
PLAYLIST
|
2020-10-09 18:52:45 +00:00
|
|
|
}
|