-
Notifications
You must be signed in to change notification settings - Fork 274
/
Copy pathcontact_data.dart
377 lines (271 loc) · 10 KB
/
contact_data.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import 'dart:convert';
import 'dart:typed_data';
import 'package:flokk/_internal/utils/string_utils.dart';
import 'package:flokk/data/group_data.dart';
import 'package:flokk/data/social_activity_type.dart';
import 'package:json_annotation/json_annotation.dart';
part "contact_data.g.dart";
enum ContactOrderBy {
Email,
FirstName,
LastName,
}
@JsonSerializable(explicitToJson: true)
class ContactData {
bool get isNew => StringUtils.isEmpty(id);
String get id => googleId;
/// ////////////////////////////////////////
String get firstPhone {
if (phoneList?.isNotEmpty ?? false) {
return phoneList.first.number;
}
return "";
}
String get firstEmail {
if (emailList?.isNotEmpty ?? false) {
return emailList.first.value;
}
return "";
}
// Indicates that this contact came back in the the last refresh, UI can use this to animate new contacts
bool isRecentlyAdded = false;
bool isDeleted = false;
String googleId = "";
String etag = "";
//Social
String twitterHandle = "";
String gitUsername = "";
//Name info
String generatedTitle = "";
String nameFull = "";
String namePrefix = "";
String nameSuffix = "";
String nameFamily = "";
String nameGiven = "";
String nameGivenPhonetic = "";
String nameMiddle = "";
String nameMiddlePhonetic = "";
//Misc(
String notes = "";
BirthdayData birthday = BirthdayData();
String nickname = "";
String fileAs = "";
bool isStarred = false;
//Job
String jobTitle = "";
String jobDepartment = "";
String jobCompany = "";
String get formattedJob {
bool hasTitle = !StringUtils.isEmpty(jobTitle);
bool hasCompany = !StringUtils.isEmpty(jobCompany);
if (hasTitle && hasCompany) return "$jobTitle, $jobCompany";
return hasTitle ? jobTitle : jobCompany ?? "";
}
//Profile
String profilePic = "";
bool isDefaultPic = true;
@JsonKey(ignore: true)
String profilePicBase64 = ""; //base 64 encoded bytes of profile pic (from picker)
@JsonKey(ignore: true)
Uint8List profilePicBytes = null; //raw bytes of profile pic (from picker)
@JsonKey(ignore: true)
bool hasNewProfilePic = false;
//Phone
List<PhoneData> phoneList = [];
//Email
List<EmailData> emailList = [];
//Address
List<AddressData> addressList = [];
//Instant Messaging
List<InstantMessageData> imList = [];
//User fields
Map<String, String> customFields = {};
//Web Sites
List<WebsiteData> websiteList = [];
//Relations
List<RelationData> relationList = [];
//Labels
@JsonKey(ignore: true)
List<GroupData> groupList = [];
//Events
List<EventData> eventList = [];
ContactData();
factory ContactData.fromJson(Map<String, dynamic> json) => _$ContactDataFromJson(json);
bool get hasName => !StringUtils.isEmpty("$nameGiven$nameMiddle$nameFamily$nameSuffix$namePrefix");
bool get hasLabel => groupList?.isNotEmpty;
bool get hasEmail => emailList?.isNotEmpty;
bool get hasPhone => phoneList?.isNotEmpty;
bool get hasAddress => addressList?.isNotEmpty;
bool get hasLink => websiteList?.isNotEmpty;
bool get hasRelationship => relationList?.isNotEmpty;
bool get hasEvents => eventList?.isNotEmpty;
bool get hasJob => !StringUtils.isEmpty("$jobTitle$jobCompany$jobDepartment");
bool get hasNotes => notes?.isNotEmpty;
bool get hasBirthday => !StringUtils.isEmpty(birthday?.text);
bool get hasValidDateForBirthday => birthday.date != DateTime(0, 1, 1);
/// /////////////////////////////////////////////////////
/// SOCIAL
bool get hasTwitter => !StringUtils.isEmpty(twitterHandle);
bool get hasGit => !StringUtils.isEmpty(gitUsername);
bool get hasAllSocial => hasGit && hasTwitter;
bool get hasAnySocial => hasGit || hasTwitter;
bool hasSameSocial(ContactData other) => other.twitterHandle == twitterHandle && other.gitUsername == gitUsername;
bool hasSocialOfType(SocialActivityType type) {
if (type == SocialActivityType.Git) return hasGit;
if (type == SocialActivityType.Twitter) return hasTwitter;
return false;
}
//Not serialized, to be set/determined with each fetch because it's possible their account was deleted/changed in the meanwhile
@JsonKey(ignore: true)
bool hasValidGit = false;
@JsonKey(ignore: true)
bool hasValidTwitter = false;
/// ////////////////////////////////////////////////
/// SEARCHABLE
String _searchable;
String get searchable => _searchable ??= _getSearchableFields().toLowerCase();
String _getSearchableFields() => "$nameGiven $nameMiddle $nameFamily $nameMiddlePhonetic $nameGivenPhonetic "
"$namePrefix $nameSuffix $nameFull $twitterHandle $gitUsername $notes $birthday $nickname"
"$jobTitle $jobDepartment $jobCompany ${phoneList?.map((x) => x.number)?.join(",") ?? ""}"
"${addressList?.map((x) => x.getFullAddress())?.join(",") ?? ""}"
"${imList?.map((x) => x.username)?.join(",") ?? ""}"
"${customFields?.values?.map((x) => x)?.join(",") ?? ""}"
"${relationList?.map((x) => x.person)?.join(",") ?? ""}"
"${emailList?.map((x) => x.value)?.join(",") ?? ""} ${groupList?.map((x) => x.name)?.join(",") ?? ""}";
/// ////////////////////////////////////////////////
/// PUBLIC API
ContactData trimLists() {
emailList.removeWhere((email) => email.isEmpty);
phoneList.removeWhere((phone) => phone.isEmpty);
addressList.removeWhere((address) => address.isEmpty);
websiteList.removeWhere((email) => email.isEmpty);
relationList.removeWhere((rel) => rel.isEmpty);
eventList.removeWhere((evt) => evt.isEmpty);
return this;
}
List<DateMixin> get allDates {
//Need to explicitly cast x as DateMixin, otherwise will throw CastError when trying to add birthday
List<DateMixin> dates = hasEvents ? eventList.map((x) => x as DateMixin).toList() : [];
if (hasValidDateForBirthday) {
dates.add(birthday);
}
return dates;
}
ContactData copy() => ContactData.fromJson(toJson())..groupList = groupList;
bool equals(ContactData value) {
if (value == null) return false;
return jsonEncode(value.toJson()).hashCode == jsonEncode(toJson()).hashCode;
}
Map<String, dynamic> toJson() => _$ContactDataToJson(this);
}
@JsonSerializable()
class AddressData {
String formattedAddress = "";
String street = "";
String poBox = "";
String neighborhood = "";
String city = "";
String region = "";
String postcode = "";
String country = "";
String type = "";
AddressData();
get isEmpty => StringUtils.isEmpty("$street$poBox$neighborhood$city$region$postcode$country$type");
factory AddressData.fromJson(Map<String, dynamic> json) => _$AddressDataFromJson(json);
Map<String, dynamic> toJson() => _$AddressDataToJson(this);
String getFullAddress() {
String ss(String value, [String extra]) => StringUtils.safeGet(value, extra);
String streetAddress = "${ss(street, ", ")}${ss(formattedAddress)}";
String address = "${ss(streetAddress, " \n")}";
String cityRegion = ("${ss(city, ", ")}${ss(region)}");
address += ss(cityRegion, " \n");
String postCountry = "${ss(postcode, ", ")}${ss(country)}";
address += ss(postCountry, " \n");
/// Trim trailing line-break
if (address.length > 1) {
address = address.substring(0, address.length - 1);
}
return address;
}
String get singleLineStreet {
return street.replaceAll("\n", " "); //strip out \n chars for use in inputs
}
}
@JsonSerializable()
class InstantMessageData {
String username = "";
String type = "";
InstantMessageData();
get isEmpty => StringUtils.isEmpty("$username$type");
factory InstantMessageData.fromJson(Map<String, dynamic> json) => _$InstantMessageDataFromJson(json);
Map<String, dynamic> toJson() => _$InstantMessageDataToJson(this);
}
@JsonSerializable()
class PhoneData {
String uri = "";
String number = "";
String type = "";
PhoneData();
get isEmpty => StringUtils.isEmpty("$number$type");
factory PhoneData.fromJson(Map<String, dynamic> json) => _$PhoneDataFromJson(json);
Map<String, dynamic> toJson() => _$PhoneDataToJson(this);
}
@JsonSerializable()
class WebsiteData {
String href = "";
String type = "";
WebsiteData();
get isEmpty => StringUtils.isEmpty("$href$type");
factory WebsiteData.fromJson(Map<String, dynamic> json) => _$WebsiteDataFromJson(json);
Map<String, dynamic> toJson() => _$WebsiteDataToJson(this);
}
@JsonSerializable()
class EmailData {
String value = "";
String type = "";
EmailData();
get isEmpty => StringUtils.isEmpty("$value$type");
factory EmailData.fromJson(Map<String, dynamic> json) => _$EmailDataFromJson(json);
Map<String, dynamic> toJson() => _$EmailDataToJson(this);
}
@JsonSerializable()
class RelationData {
String person = "";
String type = "";
RelationData();
get isEmpty => StringUtils.isEmpty("$person$type");
factory RelationData.fromJson(Map<String, dynamic> json) => _$RelationDataFromJson(json);
Map<String, dynamic> toJson() => _$RelationDataToJson(this);
}
@JsonSerializable()
class EventData with DateMixin {
String type = "";
EventData();
@override
String getType() => type;
get isEmpty => date == DateTime(0, 1, 1) || date == null || date.toString().isEmpty;
factory EventData.fromJson(Map<String, dynamic> json) => _$EventDataFromJson(json);
Map<String, dynamic> toJson() => _$EventDataToJson(this);
}
@JsonSerializable()
class BirthdayData with DateMixin {
String text = "";
BirthdayData();
@override
String getType() => "Birthday";
get isEmpty => StringUtils.isEmpty("$text");
factory BirthdayData.fromJson(Map<String, dynamic> json) => _$BirthdayDataFromJson(json);
Map<String, dynamic> toJson() => _$BirthdayDataToJson(this);
}
abstract class DateMixin {
DateTime date = DateTime(0, 1, 1);
String getType() => "";
int get daysTilAnniversary {
final now = DateTime.now();
final currentYearXDate = DateTime(now.year, date.month, date.day);
final nextYearXDate = DateTime(now.year + 1, date.month, date.day);
return currentYearXDate.isAfter(now)
? currentYearXDate.difference(now).inDays
: nextYearXDate.difference(now).inDays;
}
}