-
Notifications
You must be signed in to change notification settings - Fork 0
/
playlist.dart
193 lines (168 loc) · 5.48 KB
/
playlist.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import 'package:flutter/cupertino.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:mystify/data/constants/config/mystify_config.dart';
import 'package:mystify/data/models/playlists/sync_event/playlist_sync_event.dart';
import 'package:mystify/data/models/playlists/sync_event/playlist_sync_status.dart';
import 'package:mystify/data/models/playlists/utilities/date_time_utility.dart';
import 'package:mystify/data/models/playlists/filters/date_filter/date_filter.dart';
import 'package:mystify/data/models/playlists/filters/filter.dart';
import 'package:mystify/data/models/playlists/filters/list_filter/list_filter.dart';
import 'package:mystify/data/models/playlists/filters/simple_filter/simple_filter.dart';
import 'package:mystify/data/models/playlists/filters/slider_filter/slider_filter.dart';
import 'package:mystify/data/models/playlists/filters/string_filter/string_filter.dart';
import 'package:mystify/generated/l10n.dart';
import 'package:timeago/timeago.dart' as timeago;
part 'playlist.g.dart';
@JsonSerializable()
class Playlist {
String id;
String spotifyId;
String userId;
String title;
String description;
String image;
int songCount;
bool isDeleted;
@JsonKey(fromJson: filtersFromJson, toJson: filtersToJson)
List<Filter> filters;
@JsonKey(
fromJson: DateTimeUtility.dateTimeFromTimestamp,
toJson: DateTimeUtility.timestampFromDateTime,
)
DateTime dateCreated;
@JsonKey(
fromJson: DateTimeUtility.dateTimeFromTimestamp,
toJson: DateTimeUtility.timestampFromDateTime,
)
DateTime dateModified;
@JsonKey(
fromJson: DateTimeUtility.dateTimeFromTimestamp,
toJson: DateTimeUtility.timestampFromDateTime,
)
DateTime dateDeleted;
PlaylistSyncEvent lastSyncEvent;
Playlist(
{this.id,
this.spotifyId,
this.userId,
this.title,
this.description,
this.image,
this.dateCreated,
this.dateModified,
this.dateDeleted,
this.isDeleted,
this.filters,
this.songCount,
this.lastSyncEvent});
factory Playlist.fromJson(Map<String, dynamic> json) =>
_$PlaylistFromJson(json);
Map<String, dynamic> toJson() => _$PlaylistToJson(this);
Playlist deepCopy() => Playlist.fromJson(toJson());
factory Playlist.getDefault(
String playlistId,
String userId,
String title,
String description,
List<Filter> filters,
DateTime currentDate,
) =>
Playlist(
id: playlistId,
spotifyId: null,
userId: userId,
title: title,
description: description,
image: MystifyConfig.iconPath,
filters: filters,
songCount: 0,
dateCreated: currentDate,
dateModified: currentDate,
dateDeleted: null,
isDeleted: false,
lastSyncEvent: PlaylistSyncEvent(
status: PlaylistSyncStatus.inProgress,
statusDate: currentDate,
retryCount: 0,
),
);
bool get isNew => (dateCreated != null && dateModified != null)
? dateCreated.compareTo(dateModified) == 0
: false;
bool get showSyncFailed =>
lastSyncEvent != null &&
lastSyncEvent.status == PlaylistSyncStatus.failure &&
lastSyncEvent.retryCount >= 2;
bool get showSyncInProgress =>
lastSyncEvent == null ||
lastSyncEvent.status == PlaylistSyncStatus.inProgress ||
(lastSyncEvent.status == PlaylistSyncStatus.failure &&
lastSyncEvent.retryCount < 2);
String getSyncText(BuildContext context) {
var text;
if (isDeleted) {
text = timeago.format(dateDeleted);
} else if (lastSyncEvent?.status == PlaylistSyncStatus.success) {
text = timeago.format(lastSyncEvent.statusDate);
} else if (showSyncFailed) {
text =
'${MystifyLocalizations.of(context).failed} ${timeago.format(lastSyncEvent.statusDate)}';
} else if (showSyncInProgress) {
text = MystifyLocalizations.of(context).inProgress;
} else {
text = '';
}
return text;
}
}
// Temporary workaround based off https://stackoverflow.com/questions/61042910/dart-jsonserializable-with-absract-class#
List<Filter> filtersFromJson(List<dynamic> json) {
if (json == null || json.isEmpty) {
return <Filter>[];
}
return json.map((filter) {
switch (filter['runtimeType'] ?? filter['\$']) {
case 'DateFilter':
return DateFilter.fromJson(filter);
case 'ListFilter':
return ListFilter.fromJson(filter);
case 'SimpleFilter':
return SimpleFilter.fromJson(filter);
case 'SliderFilter':
return SliderFilter.fromJson(filter);
case 'StringFilter':
return StringFilter.fromJson(filter);
default:
return null;
}
}).toList();
}
List<dynamic> filtersToJson(List<Filter> filters) {
if (filters == null || filters.isEmpty) {
return [];
}
return filters.map((filter) {
var filterJson;
switch (filter.runtimeType) {
case DateFilter:
filterJson = (filter as DateFilter).toJson();
break;
case ListFilter:
filterJson = (filter as ListFilter).toJson();
break;
case SimpleFilter:
filterJson = (filter as SimpleFilter).toJson();
break;
case SliderFilter:
filterJson = (filter as SliderFilter).toJson();
break;
case StringFilter:
filterJson = (filter as StringFilter).toJson();
break;
}
if (filterJson != null) {
filterJson.addAll({'runtimeType': filter.runtimeType.toString()});
}
return filterJson;
}).toList();
}