-
Notifications
You must be signed in to change notification settings - Fork 41
/
bookmark.dart
458 lines (400 loc) · 13.8 KB
/
bookmark.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// This source code is a part of Project Violet.
// Copyright (C) 2020-2024. violet-team. Licensed under the Apache-2.0 License.
import 'dart:collection';
import 'package:flutter/material.dart';
import 'package:violet/database/user/user.dart';
import 'package:violet/log/log.dart';
////////////////////////////////////////////////////////////////////////
///
/// Bookmark
///
////////////////////////////////////////////////////////////////////////
class BookmarkGroup {
Map<String, dynamic> result;
BookmarkGroup({required this.result});
int id() => result['Id'];
String name() => result['Name'];
String datetime() => result['DateTime'];
String description() => result['Description'];
int color() => result['DateTime'];
int gorder() => result['Gorder'];
Future<void> update() async {
var db = await CommonUserDatabase.getInstance();
await db.update('BookmarkGroup', result, 'Id=?', [id()]);
}
Future<void> swap(BookmarkGroup target) async {
var db = await CommonUserDatabase.getInstance();
await db.swap('BookmarkGroup', 'Id', 'Gorder', id(), target.id(), gorder(),
target.gorder());
var tid = gorder();
var xx = Map<String, dynamic>.from(result);
var yy = Map<String, dynamic>.from(target.result);
xx['Gorder'] = target.gorder();
result = xx;
yy['Gorder'] = tid;
target.result = yy;
}
}
class BookmarkArticle {
Map<String, dynamic> result;
BookmarkArticle({required this.result});
int id() => result['Id'];
String article() => result['Article'];
String datetime() => result['DateTime'];
int group() => result['GroupId'];
Future<void> update() async {
var db = await CommonUserDatabase.getInstance();
await db.update('BookmarkArticle', result, 'Id=?', [id()]);
}
}
class BookmarkArtist {
Map<String, dynamic> result;
BookmarkArtist({required this.result});
int id() => result['Id'];
String artist() => result['Artist'];
// 0: artist
// 1: group
// 2: uploader
// 3: series
// 4: character
int type() => result['IsGroup']; // backward compatibility
String datetime() => result['DateTime'];
int group() => result['GroupId'];
Future<void> update() async {
var db = await CommonUserDatabase.getInstance();
await db.update('BookmarkArtist', result, 'Id=?', [id()]);
}
}
class BookmarkUser {
Map<String, dynamic> result;
BookmarkUser({required this.result});
int id() => result['Id'];
String user() => result['User'];
String datetime() => result['DateTime'];
int group() => result['GroupId'];
String? title() => result['Title'];
String? subtitle() => result['Subtitle'];
Future<void> update() async {
var db = await CommonUserDatabase.getInstance();
await db.update('BookmarkUser', result, 'Id=?', [id()]);
}
}
class HistoryUser {
Map<String, dynamic> result;
HistoryUser({required this.result});
int id() => result['Id'];
String user() => result['User'];
String datetime() => result['DateTime'];
Future<void> update() async {
var db = await CommonUserDatabase.getInstance();
await db.update('HistoryUser', result, 'Id=?', [id()]);
}
}
class Bookmark {
static Bookmark? _instance;
static Future<Bookmark> getInstance() async {
if (_instance == null) {
final db = await CommonUserDatabase.getInstance();
final ee = await db.query(
"SELECT name FROM sqlite_master WHERE type='table' AND name='BookmarkGroup';");
if (ee.isEmpty || ee[0].isEmpty) {
try {
await db.execute(
'CREATE TABLE BookmarkGroup (Id integer primary key autoincrement, Name text, DateTime text, Description text, Color integer, Gorder integer)');
await db.execute('''CREATE TABLE BookmarkArticle (
Id integer primary key autoincrement,
Article text,
DateTime text,
GroupId integer,
FOREIGN KEY(GroupId) REFERENCES BookmarkGroup(Id));
''');
await db.execute('''CREATE TABLE BookmarkArtist (
Id integer primary key autoincrement,
Artist text,
IsGroup integer,
DateTime text,
GroupId integer,
FOREIGN KEY(GroupId) REFERENCES BookmarkGroup(Id));
''');
// Insert default bookmark group.
await db.insert('BookmarkGroup', {
'Name': 'violet_default', // 미분류
'Description': 'Unclassified bookmarks.',
'DateTime': DateTime.now().toString(),
'Color': Colors.grey.value,
'Gorder': 1,
});
} catch (e, st) {
Logger.error('[Bookmark Instance] E: $e\n'
'$st');
}
}
final ex = await db.query(
"SELECT name FROM sqlite_master WHERE type='table' AND name='BookmarkUser';");
if (ex.isEmpty || ex[0].isEmpty) {
await db.execute('''CREATE TABLE BookmarkUser (
Id integer primary key autoincrement,
User text,
Title text,
Subtitle text,
DateTime text,
GroupId integer,
FOREIGN KEY(GroupId) REFERENCES BookmarkGroup(Id));
''');
}
final ex2 = await db.query(
"SELECT name FROM sqlite_master WHERE type='table' AND name='HistoryUser';");
if (ex2.isEmpty || ex2[0].isEmpty) {
await db.execute('''CREATE TABLE HistoryUser (
Id integer primary key autoincrement,
User text,
DateTime text);
''');
}
_instance = Bookmark();
}
return _instance!;
}
Future<void> insertArticle(String article,
[DateTime? datetime, int group = 1]) async {
datetime ??= DateTime.now();
var db = await CommonUserDatabase.getInstance();
await db.insert('BookmarkArticle', {
'Article': article,
'DateTime': datetime.toString(),
'GroupId': group,
});
if (bookmarkSet == null) {
bookmarkSet = HashSet<int>();
}
bookmarkSet!.add(int.parse(article));
}
Future<void> insertArtist(String artist, int isgroup,
[DateTime? datetime, int group = 1]) async {
datetime ??= DateTime.now();
var db = await CommonUserDatabase.getInstance();
await db.insert('BookmarkArtist', {
'Artist': artist,
'IsGroup': isgroup,
'DateTime': datetime.toString(),
'GroupId': group,
});
bookmarkArtistSet![isgroup]!.add(artist);
}
Future<void> insertUser(String user,
[DateTime? datetime, int group = 1]) async {
datetime ??= DateTime.now();
var db = await CommonUserDatabase.getInstance();
await db.insert('BookmarkUser', {
'User': user,
'DateTime': datetime.toString(),
'GroupId': group,
});
bookmarkUserSet!.add(user);
}
Future<void> historyUser(String user, [DateTime? datetime]) async {
datetime ??= DateTime.now();
var db = await CommonUserDatabase.getInstance();
await db.insert('HistoryUser', {
'User': user,
'DateTime': datetime.toString(),
});
historyUserSet!.add(user);
}
Future<int> createGroup(String name, String description, Color color,
[DateTime? datetime]) async {
datetime ??= DateTime.now();
var groups = await getGroup();
var db = await CommonUserDatabase.getInstance();
return await db.insert('BookmarkGroup', {
'Name': name,
'Description': description,
'DateTime': datetime.toString(),
'Color': color.value,
'Gorder': groups.last.gorder() + 1,
});
}
Future<void> deleteGroup(BookmarkGroup group) async {
var db = await CommonUserDatabase.getInstance();
await db.delete('BookmarkArticle', 'GroupId=?', [group.id().toString()]);
await db.delete('BookmarkArtist', 'GroupId=?', [group.id().toString()]);
await db.delete('BookmarkGroup', 'Id=?', [group.id().toString()]);
bookmarkSet = null;
//
}
Future<void> deleteUser(BookmarkUser user) async {
var db = await CommonUserDatabase.getInstance();
await db.delete('BookmarkUser', 'Id=?', [user.id()]);
bookmarkUserSet!.remove(user.user());
//
}
Future<void> fixGroup() async {
var groups = (await (await CommonUserDatabase.getInstance())
.query('SELECT * FROM BookmarkGroup'))
.map((x) => BookmarkGroup(result: x))
.toList();
if (groups.length > 1) {
if (groups[1].gorder() == 1) {
for (int i = 1; i < groups.length; i++) {
var rr = Map<String, dynamic>.from(groups[i].result);
rr['Gorder'] = i + 1;
groups[i].result = rr;
await groups[i].update();
}
}
}
}
Future<List<BookmarkGroup>> getGroup() async {
fixGroup();
return (await (await CommonUserDatabase.getInstance())
.query('SELECT * FROM BookmarkGroup ORDER BY Gorder ASC'))
.map((x) => BookmarkGroup(result: x))
.toList();
}
Future<void> positionSwap(int from, int to) async {
print('$from|$to');
var groups = await getGroup();
void swap(int x, int y) {
var tmp = groups[x];
groups[x] = groups[y];
groups[y] = tmp;
}
for (var element in groups) {
print(element.gorder());
}
if (from < to) {
for (; from < to; from++) {
await groups[from].swap(groups[from + 1]);
swap(from, from + 1);
}
} else if (from > to) {
for (; from > to; from--) {
await groups[from].swap(groups[from - 1]);
swap(from, from - 1);
}
}
for (var element in groups) {
print(element.gorder());
}
}
Future<List<BookmarkArticle>> getArticle() async {
return (await (await CommonUserDatabase.getInstance())
.query('SELECT * FROM BookmarkArticle'))
.map((x) => BookmarkArticle(result: x))
.toList();
}
Future<List<BookmarkArtist>> getArtist() async {
return (await (await CommonUserDatabase.getInstance())
.query('SELECT * FROM BookmarkArtist'))
.map((x) => BookmarkArtist(result: x))
.toList();
}
Future<List<BookmarkUser>> getUser() async {
return (await (await CommonUserDatabase.getInstance())
.query('SELECT * FROM BookmarkUser'))
.map((x) => BookmarkUser(result: x))
.toList();
}
Future<List<HistoryUser>> getHistoryUser() async {
return (await (await CommonUserDatabase.getInstance())
.query('SELECT * FROM HistoryUser'))
.map((x) => HistoryUser(result: x))
.toList();
}
Future<void> modfiyGroup(BookmarkGroup group) async {
await (await CommonUserDatabase.getInstance())
.update('BookmarkGroup', group.result, 'Id=?', [group.id()]);
}
Future<void> modfiyUser(BookmarkUser user) async {
await (await CommonUserDatabase.getInstance())
.update('BookmarkUser', user.result, 'Id=?', [user.id()]);
}
HashSet<int>? bookmarkSet;
Future<bool> isBookmark(int id) async {
if (bookmarkSet == null) {
var article = await getArticle();
bookmarkSet = HashSet<int>();
for (var element in article) {
bookmarkSet!.add(int.parse(element.article()));
}
}
return bookmarkSet!.contains(id);
}
Future<void> bookmark(int id) async {
if (await isBookmark(id)) return;
bookmarkSet!.add(id);
await insertArticle(id.toString());
}
Future<void> unbookmark(int id) async {
if (!await isBookmark(id)) return;
var db = await CommonUserDatabase.getInstance();
await db.delete('BookmarkArticle', 'Article=?', [id.toString()]);
bookmarkSet!.remove(id);
}
Map<int, HashSet<String>>? bookmarkArtistSet;
Future<bool> isBookmarkArtist(String name, int type) async {
if (bookmarkArtistSet == null) {
var artist = await getArtist();
bookmarkArtistSet = <int, HashSet<String>>{};
bookmarkArtistSet![0] = HashSet<String>();
bookmarkArtistSet![1] = HashSet<String>();
bookmarkArtistSet![2] = HashSet<String>();
bookmarkArtistSet![3] = HashSet<String>();
bookmarkArtistSet![4] = HashSet<String>();
for (var element in artist) {
bookmarkArtistSet![element.type()]!.add(element.artist());
}
}
return bookmarkArtistSet![type]!.contains(name);
}
HashSet<String>? bookmarkUserSet;
Future<bool> isBookmarkUser(String user) async {
if (bookmarkUserSet == null) {
var user = await getUser();
bookmarkUserSet = HashSet<String>();
for (var element in user) {
bookmarkUserSet!.add(element.user());
}
}
return bookmarkUserSet!.contains(user);
}
HashSet<String>? historyUserSet;
Future<bool> isHistoryUser(String user) async {
if (historyUserSet == null) {
var user = await getHistoryUser();
historyUserSet = HashSet<String>();
for (var element in user) {
historyUserSet!.add(element.user());
}
}
return historyUserSet!.contains(user);
}
Future<void> bookmarkArtist(String name, int type, [int group = 1]) async {
if (await isBookmarkArtist(name, type)) return;
bookmarkArtistSet![type]!.add(name);
await insertArtist(name, type, null, group);
}
Future<void> unbookmarkArtist(String name, int type) async {
if (!await isBookmarkArtist(name, type)) return;
var db = await CommonUserDatabase.getInstance();
await db.delete('BookmarkArtist', 'Artist=? AND IsGroup=?', [name, type]);
bookmarkArtistSet![type]!.remove(name);
print('delete $name, $type');
}
Future<void> bookmarkUser(String user, [int group = 1]) async {
if (await isBookmarkUser(user)) return;
bookmarkUserSet!.add(user);
await insertUser(user, null, group);
}
Future<void> unbookmarkUser(String user) async {
if (!await isBookmarkUser(user)) return;
var db = await CommonUserDatabase.getInstance();
await db.delete('BookmarkUser', 'User=?', [user]);
bookmarkUserSet!.remove(user);
}
Future<void> setHistoryUser(String user) async {
if (await isHistoryUser(user)) return;
historyUserSet!.add(user);
await historyUser(user, null);
}
}