From d1e3e7c0a36ec3c72eb09ab912987648271c01d5 Mon Sep 17 00:00:00 2001 From: perol Date: Sun, 24 Nov 2024 20:18:45 +0800 Subject: [PATCH] import ban list init --- lib/models/ban_illust_id.dart | 11 ++++++++++ lib/models/ban_tag.dart | 17 +++++++++++++--- lib/models/ban_user_id.dart | 11 ++++++++++ lib/store/mute_store.dart | 38 +++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 3 deletions(-) diff --git a/lib/models/ban_illust_id.dart b/lib/models/ban_illust_id.dart index 6b6cb5e91..f106b4a96 100644 --- a/lib/models/ban_illust_id.dart +++ b/lib/models/ban_illust_id.dart @@ -62,6 +62,17 @@ create table $tableBanIllustId ( return todo; } + Future> insertAll( + List todos) async { + await db.transaction((txn) async { + for (var todo in todos) { + todo.id = await txn.insert(tableBanIllustId, todo.toJson(), + conflictAlgorithm: ConflictAlgorithm.replace); + } + }); + return todos; + } + Future getAccount(int id) async { List> maps = await db.query(tableBanIllustId, columns: [columnId, columnIllustId], diff --git a/lib/models/ban_tag.dart b/lib/models/ban_tag.dart index 3abee95f4..ce082f401 100644 --- a/lib/models/ban_tag.dart +++ b/lib/models/ban_tag.dart @@ -17,6 +17,7 @@ import 'package:json_annotation/json_annotation.dart'; import 'package:path/path.dart'; import 'package:sqflite_common_ffi/sqflite_ffi.dart'; part 'ban_tag.g.dart'; + @JsonSerializable() class BanTagPersist { int? id; @@ -24,10 +25,10 @@ class BanTagPersist { @JsonKey(name: 'translate_name') String translateName; - BanTagPersist( - { this.id, required this.name, required this.translateName}); + BanTagPersist({this.id, required this.name, required this.translateName}); - factory BanTagPersist.fromJson(Map json) => _$BanTagPersistFromJson(json); + factory BanTagPersist.fromJson(Map json) => + _$BanTagPersistFromJson(json); Map toJson() => _$BanTagPersistToJson(this); } @@ -99,4 +100,14 @@ create table $tableBanTag ( } Future close() async => db.close(); + + Future> insertAll(List list) async { + await db.transaction((txn) async { + for (var todo in list) { + todo.id = await txn.insert(tableBanTag, todo.toJson(), + conflictAlgorithm: ConflictAlgorithm.replace); + } + }); + return list; + } } diff --git a/lib/models/ban_user_id.dart b/lib/models/ban_user_id.dart index be2f6eb2f..49089e7f7 100644 --- a/lib/models/ban_user_id.dart +++ b/lib/models/ban_user_id.dart @@ -106,4 +106,15 @@ create table $tableBanUserId ( } Future close() async => db.close(); + + Future> insertAll( + List list) async { + await db.transaction((txn) async { + for (var todo in list) { + todo.id = await txn.insert(tableBanUserId, todo.toJson(), + conflictAlgorithm: ConflictAlgorithm.replace); + } + }); + return list; + } } diff --git a/lib/store/mute_store.dart b/lib/store/mute_store.dart index 22d09f999..c2d8cdd64 100644 --- a/lib/store/mute_store.dart +++ b/lib/store/mute_store.dart @@ -15,6 +15,7 @@ */ import 'dart:convert'; +import 'dart:typed_data'; import 'package:mobx/mobx.dart'; import 'package:pixez/er/lprinter.dart'; @@ -23,6 +24,7 @@ import 'package:pixez/models/ban_illust_id.dart'; import 'package:pixez/models/ban_tag.dart'; import 'package:pixez/models/ban_user_id.dart'; import 'package:pixez/models/comment_response.dart'; +import 'package:pixez/saf_plugin.dart'; import 'package:shared_preferences/shared_preferences.dart'; part 'mute_store.g.dart'; @@ -157,6 +159,42 @@ abstract class _MuteStoreBase with Store { "bantag": banTag }; final exportJson = jsonEncode(entity); + final uri = await SAFPlugin.createFile( + "pixez_mute_${DateTime.now().toIso8601String()}.json", + "application/json"); LPrinter.d("exportJson:$exportJson"); + if (uri != null) { + await SAFPlugin.writeUri( + uri, Uint8List.fromList(utf8.encode(exportJson))); + } + } + + importFile() async { + final uri = await SAFPlugin.openFile(); + if (uri != null) { + final data = utf8.decode(uri); + final entity = jsonDecode(data); + final banIllust = entity["banillustid"]; + final banUser = entity["banuserid"]; + final banTag = entity["bantag"]; + await banIllustIdProvider.open(); + await banUserIdProvider.open(); + await banTagProvider.open(); + if (banIllust is List) { + await banIllustIdProvider.insertAll( + banIllust.map((e) => BanIllustIdPersist.fromJson(e)).toList()); + } + if (banUser is List) { + await banUserIdProvider.insertAll( + banUser.map((e) => BanUserIdPersist.fromJson(e)).toList()); + } + if (banTag is List) { + await banTagProvider + .insertAll(banTag.map((e) => BanTagPersist.fromJson(e)).toList()); + } + await fetchBanIllusts(); + await fetchBanUserIds(); + await fetchBanTags(); + } } }