Skip to content

Commit cecc33f

Browse files
committed
Synchronize history by MySQL.
1 parent 5e5ae98 commit cecc33f

File tree

5 files changed

+295
-94
lines changed

5 files changed

+295
-94
lines changed

lib/common/controller/gallerycache_controller.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class GalleryCacheController extends GetxController {
8181
}
8282

8383
if (mysqlController.syncReadProgress) {
84-
final remoteMysql = await mysqlController.qryRead(gid);
84+
final remoteMysql = await mysqlController.downloadRead(gid);
8585
remotes.add(remoteMysql);
8686
}
8787

lib/common/controller/history_controller.dart

Lines changed: 132 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:convert';
22

3+
import 'package:fehviewer/common/controller/mysql_controller.dart';
34
import 'package:fehviewer/common/controller/webdav_controller.dart';
45
import 'package:fehviewer/common/service/ehsetting_service.dart';
56
import 'package:fehviewer/fehviewer.dart';
@@ -22,6 +23,7 @@ class HistoryController extends GetxController {
2223

2324
final EhSettingService _ehSettingService = Get.find();
2425
final WebdavController webdavController = Get.find();
26+
final MysqlController mysqlController = Get.find();
2527

2628
final thrSync = Throttling(duration: const Duration(seconds: 60));
2729
final debSync = Debouncing(duration: const Duration(seconds: 80));
@@ -85,17 +87,14 @@ class HistoryController extends GetxController {
8587
logger.t('add ${galleryProvider.gid} update1');
8688
update();
8789

88-
// TODO
89-
syncHistoryMySQL();
90-
9190
if (sync) {
9291
// 节流函数 最多每分钟一次同步
9392
thrSync.throttle(() {
9493
logger.t('throttle syncHistory');
95-
return syncHistoryWebDAV();
94+
return syncHistory();
9695
});
9796

98-
debSync.debounce(syncHistoryWebDAV);
97+
debSync.debounce(syncHistory);
9998
}
10099
}
101100

@@ -115,18 +114,17 @@ class HistoryController extends GetxController {
115114
update();
116115
}
117116

118-
// hiveHelper.removeHistory(gid);
119117
isarHelper.removeHistory(gid);
120118
_addHistoryDelFlg(gid);
121119

122120
if (sync) {
123121
// 节流函数 最多每分钟一次同步
124122
thrSync.throttle(() {
125123
logger.t('throttle syncHistory');
126-
return syncHistoryWebDAV();
124+
return syncHistory();
127125
});
128126

129-
debSync.debounce(syncHistoryWebDAV);
127+
debSync.debounce(syncHistory);
130128
}
131129
}
132130

@@ -181,86 +179,71 @@ class HistoryController extends GetxController {
181179
// webdavController.updateRemoveFlg(gid);
182180
// }
183181

184-
Future<void> syncHistoryMySQL() async {
185-
logger.d('syncHistoryMySQL');
186-
}
187-
188-
Future<void> syncHistoryWebDAV() async {
189-
if (!webdavController.syncHistory) {
190-
// logger.d('disable syncHistory');
191-
return;
192-
}
193-
182+
/// 同步历史记录
183+
Future<void> syncHistory() async {
194184
final List<HistoryIndexGid?> listLocal = histories
195185
.map((e) => HistoryIndexGid(t: e.lastViewTime, g: e.gid))
196186
.toList();
197187
logger.t('listLocal ${listLocal.length} \n${listLocal.map((e) => e?.g)}');
198188
logger.t('${jsonEncode(listLocal)} ');
199189

200-
// 下载远程列表
201-
final listRemote = await webdavController.getRemoteHistoryList();
202-
if (listRemote.isEmpty) {
203-
await _uploadHistories(listLocal.toList());
204-
return;
205-
}
206-
207-
logger.t('listRemote size ${listRemote.length}');
208-
// yield true;
209-
210-
// 比较远程和本地的差异
211-
final allGid = <HistoryIndexGid?>{...listRemote, ...listLocal};
212-
final diff = allGid
213-
.where((element) =>
214-
!listRemote.contains(element) || !listLocal.contains(element))
215-
.toList()
216-
.toSet();
217-
logger.t('diff ${diff.map((e) => e?.toJson())}');
218-
219-
// 本地时间更大的画廊
220-
final localNewer = listLocal.where(
221-
(eLocal) {
222-
if (eLocal == null) {
223-
return false;
224-
}
225-
final _eRemote =
226-
listRemote.firstWhereOrNull((eRemote) => eRemote.g == eLocal.g);
227-
if (_eRemote == null) {
228-
return true;
229-
}
190+
syncHistoryMySQL(listLocal);
191+
syncHistoryWebDAV(listLocal);
192+
}
230193

231-
return (eLocal.t ?? 0) > (_eRemote.t ?? 0);
232-
},
194+
/// 通过mysql同步历史记录
195+
Future<void> syncHistoryMySQL(List<HistoryIndexGid?> listLocal) async {
196+
await _syncHistoryCallback(
197+
enable: mysqlController.syncHistory,
198+
listLocal: listLocal,
199+
getRemoteList: mysqlController.getHistoryList,
200+
uploadLocalHistories: _uploadHistoriesMySQL,
201+
downloadRemoteHistories: _downloadHistoriesMySQL,
233202
);
234-
logger.t('localNewer count ${localNewer.length}');
235-
236-
// 远程时间更大的画廊
237-
final remoteNewer = listRemote.where(
238-
(eRemote) {
239-
final _eLocal = listLocal
240-
.firstWhereOrNull((eLocal) => (eLocal?.g ?? '') == eRemote.g);
241-
242-
final _eDelFlg = _delHistories
243-
.firstWhereOrNull((eDel) => (eDel.g ?? '') == eRemote.g);
203+
}
244204

245-
if (_eDelFlg != null) {
246-
return (eRemote.t ?? 0) > (_eDelFlg.t ?? 0);
247-
}
205+
/// 通过webdav同步历史记录
206+
Future<void> syncHistoryWebDAV(List<HistoryIndexGid?> listLocal) async {
207+
await _syncHistoryCallback(
208+
enable: webdavController.syncHistory,
209+
listLocal: listLocal,
210+
getRemoteList: webdavController.getRemoteHistoryList,
211+
uploadLocalHistories: _uploadHistoriesWebDAV,
212+
downloadRemoteHistories: _downloadHistoriesWebDAV,
213+
);
214+
}
248215

249-
if (_eLocal == null) {
250-
return true;
216+
Future<void> _downloadHistoriesMySQL(List<HistoryIndexGid> hisList) async {
217+
final _list = await mysqlController
218+
.downloadHistoryList(hisList.map((e) => e.g).toList());
219+
for (final _image in _list) {
220+
if (_image != null) {
221+
final ori =
222+
histories.firstWhereOrNull((element) => element.gid == _image.gid);
223+
if (ori != null &&
224+
(_image.lastViewTime ?? 0) <= (ori.lastViewTime ?? 0)) {
225+
continue;
251226
}
227+
addHistory(_image, updateTime: false, sync: false);
228+
}
229+
}
230+
}
252231

253-
return (eRemote.t ?? 0) > (_eLocal.t ?? 0);
254-
},
255-
);
256-
logger.t('remoteNewer ${remoteNewer.map((e) => e.g).toList()}');
257-
258-
await _downloadHistories(remoteNewer.toSet().toList());
232+
Future<void> _uploadHistoriesMySQL(
233+
List<HistoryIndexGid?> localHisList, {
234+
List<HistoryIndexGid?>? listRemote,
235+
}) async {
236+
for (final his in localHisList) {
237+
final GalleryProvider? _his =
238+
histories.firstWhereOrNull((element) => element.gid == his?.g);
259239

260-
await _uploadHistories(localNewer.toList(), listRemote: listRemote);
240+
if (_his != null) {
241+
await mysqlController.uploadHistory(_his);
242+
}
243+
}
261244
}
262245

263-
Future _downloadHistories(List<HistoryIndexGid> hisList) async {
246+
Future<void> _downloadHistoriesWebDAV(List<HistoryIndexGid> hisList) async {
264247
webdavController.initExecutor();
265248
for (final gid in hisList) {
266249
webdavController.webDAVExecutor.scheduleTask(() async {
@@ -282,7 +265,7 @@ class HistoryController extends GetxController {
282265
await webdavController.webDAVExecutor.join(withWaiting: true);
283266
}
284267

285-
Future _uploadHistories(
268+
Future<void> _uploadHistoriesWebDAV(
286269
List<HistoryIndexGid?> localHisList, {
287270
List<HistoryIndexGid?>? listRemote,
288271
}) async {
@@ -305,9 +288,79 @@ class HistoryController extends GetxController {
305288
await webdavController.webDAVExecutor.join(withWaiting: true);
306289
}
307290

308-
// Future<void> _uploadHistoryIndex(List<HistoryIndexGid?> gids) async {
309-
// final _time = DateTime.now().millisecondsSinceEpoch;
310-
// await webdavController.uploadHistoryIndex(
311-
// gids.where((element) => element != null).toList(), _time);
312-
// }
291+
Future<void> _syncHistoryCallback({
292+
bool enable = false,
293+
required List<HistoryIndexGid?> listLocal,
294+
required Future<List<HistoryIndexGid>> Function() getRemoteList,
295+
required Future<void> Function(
296+
List<HistoryIndexGid?>, {
297+
List<HistoryIndexGid?>? listRemote,
298+
}) uploadLocalHistories,
299+
required Future<void> Function(List<HistoryIndexGid>)
300+
downloadRemoteHistories,
301+
}) async {
302+
logger.d('syncHistoryCallback');
303+
if (!enable) {
304+
return;
305+
}
306+
307+
// 下载远程列表
308+
final listRemote = await getRemoteList();
309+
if (listRemote.isEmpty) {
310+
await uploadLocalHistories(listLocal);
311+
return;
312+
}
313+
314+
logger.d('listRemote ${listRemote.length}');
315+
316+
// 比较远程和本地的差异
317+
// final combinedList = <HistoryIndexGid?>{...listRemote, ...listLocal};
318+
// final diff = combinedList
319+
// .where((element) =>
320+
// !listRemote.contains(element) || !listLocal.contains(element))
321+
// .toList()
322+
// .toSet();
323+
324+
// 本地时间更大的画廊
325+
final localNewer = listLocal.where(
326+
(eLocal) {
327+
if (eLocal == null) {
328+
return false;
329+
}
330+
final _eRemote =
331+
listRemote.firstWhereOrNull((eRemote) => eRemote.g == eLocal.g);
332+
if (_eRemote == null) {
333+
return true;
334+
}
335+
336+
return (eLocal.t ?? 0) > (_eRemote.t ?? 0);
337+
},
338+
);
339+
logger.d('localNewer ${localNewer.length} ${localNewer.map((e) => e?.g)}');
340+
341+
// 远程时间更大的画廊
342+
final remoteNewer = listRemote.where(
343+
(eRemote) {
344+
final _eLocal = listLocal
345+
.firstWhereOrNull((eLocal) => (eLocal?.g ?? '') == eRemote.g);
346+
347+
final _eDelFlg = _delHistories
348+
.firstWhereOrNull((eDel) => (eDel.g ?? '') == eRemote.g);
349+
350+
if (_eDelFlg != null) {
351+
return (eRemote.t ?? 0) > (_eDelFlg.t ?? 0);
352+
}
353+
354+
if (_eLocal == null) {
355+
return true;
356+
}
357+
358+
return (eRemote.t ?? 0) > (_eLocal.t ?? 0);
359+
},
360+
);
361+
362+
await downloadRemoteHistories(remoteNewer.toSet().toList());
363+
364+
await uploadLocalHistories(localNewer.toList(), listRemote: listRemote);
365+
}
313366
}

0 commit comments

Comments
 (0)