Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor 004 #509

Merged
merged 10 commits into from
Sep 8, 2024
8 changes: 7 additions & 1 deletion violet/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,25 @@ linter:
# producing the lint.
rules:
avoid_print: false
use_build_context_synchronously: false # TODO(ykh09242): Enable this lint.

sized_box_shrink_expand: true
prefer_single_quotes: true
always_use_package_imports: true
unrelated_type_equality_checks: true
use_build_context_synchronously: true
avoid_unnecessary_containers: true
sized_box_for_whitespace: true

analyzer:
errors:
always_use_package_imports: error
unrelated_type_equality_checks: error
exclude:
- "rust_builder/**"
- "lib/src/rust/**"
- "lib/pages/viewer/others/**"
- "lib/server/salt.dart"
- "lib/server/wsalt.dart"

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
4 changes: 2 additions & 2 deletions violet/assets/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
"useinnerstorage": "Use Inner Storage",
"default": "Default",
"read": "Read",
"artists": "Artists",
"groups": "Groups",
"artist": "Artists",
"group": "Groups",
"tags": "Tags",
"series": "Series",
"character": "Character",
Expand Down
4 changes: 2 additions & 2 deletions violet/assets/locale/eo.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
"useinnerstorage": "Use Inner Storage",
"default": "Default",
"read": "Leer",
"artists": "Artistas",
"groups": "Grupos",
"artist": "Artistas",
"group": "Grupos",
"tags": "Etiquetas",
"series": "Series",
"character": "Personajes",
Expand Down
4 changes: 2 additions & 2 deletions violet/assets/locale/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
"useinnerstorage": "Use Inner Storage",
"default": "Default",
"read": "Leggi",
"artists": "Artisti",
"groups": "Gruppi",
"artist": "Artisti",
"group": "Gruppi",
"tags": "Tags",
"series": "Serie",
"character": "Personaggi",
Expand Down
4 changes: 2 additions & 2 deletions violet/assets/locale/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
"useinnerstorage": "内部ストレージを使う",
"default": "Default",
"read": "読む",
"artists": "アーティスト",
"groups": "グループ",
"artist": "アーティスト",
"group": "グループ",
"tags": "タグ",
"series": "シリーズ",
"character": "キャラクター",
Expand Down
4 changes: 2 additions & 2 deletions violet/assets/locale/ko.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
"useinnerstorage": "내부 저장소 사용",
"default": "기본값",
"read": "읽기",
"artists": "작가",
"groups": "그룹",
"artist": "작가",
"group": "그룹",
"tags": "태그",
"series": "시리즈",
"character": "캐릭터",
Expand Down
4 changes: 2 additions & 2 deletions violet/assets/locale/pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
"useinnerstorage": "Usar armazenamento interno",
"default": "Padrão",
"read": "Ler",
"artists": "Artistas",
"groups": "Grupos",
"artist": "Artistas",
"group": "Grupos",
"tags": "Tags",
"series": "Series",
"character": "Personagem",
Expand Down
4 changes: 2 additions & 2 deletions violet/assets/locale/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
"useinnerstorage": "Use Inner Storage",
"default": "Default",
"read": "Read",
"artists": "Artists",
"groups": "Groups",
"artist": "Artists",
"group": "Groups",
"tags": "Tags",
"series": "Series",
"character": "Character",
Expand Down
4 changes: 2 additions & 2 deletions violet/assets/locale/zh_Hans.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
"useinnerstorage": "Use Inner Storage",
"default": "Default",
"read": "阅读",
"artists": "作者",
"groups": "团体",
"artist": "作者",
"group": "团体",
"tags": "标签",
"series": "系列",
"character": "角色",
Expand Down
4 changes: 2 additions & 2 deletions violet/assets/locale/zh_Hant.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
"useinnerstorage": "Use Inner Storage",
"default": "Default",
"read": "Read",
"artists": "Artists",
"groups": "Groups",
"artist": "Artists",
"group": "Groups",
"tags": "Tags",
"series": "Series",
"character": "Character",
Expand Down
70 changes: 52 additions & 18 deletions violet/lib/database/user/bookmark.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,42 @@ class BookmarkArticle {
}
}

enum ArtistType { artist, group, uploader, series, character }

extension ArtistTypeExtension on ArtistType {
bool get isGroup => this == ArtistType.group;
bool get isUploader => this == ArtistType.uploader;
bool get isCharacter => this == ArtistType.character;
bool get isSeries => this == ArtistType.series;
}

class ArtistTypeHelper {
static ArtistType? fromString(String name) {
switch (name.toLowerCase()) {
case 'artist':
case 'artists':
return ArtistType.artist;

case 'group':
case 'groups':
return ArtistType.group;

case 'uploader':
case 'uploaders':
return ArtistType.uploader;

case 'series':
return ArtistType.series;

case 'character':
case 'characters':
return ArtistType.character;
}

return null;
}
}

class BookmarkArtist {
Map<String, dynamic> result;
BookmarkArtist({required this.result});
Expand All @@ -72,7 +108,8 @@ class BookmarkArtist {
// 2: uploader
// 3: series
// 4: character
int type() => result['IsGroup']; // backward compatibility
ArtistType type() =>
ArtistType.values[result['IsGroup']]; // backward compatibility
String datetime() => result['DateTime'];
int group() => result['GroupId'];

Expand Down Expand Up @@ -233,17 +270,17 @@ class Bookmark {
bookmarkSet!.add(int.parse(article));
}

Future<void> insertArtist(String artist, int isgroup,
Future<void> insertArtist(String artist, ArtistType type,
[DateTime? datetime, int group = 1]) async {
datetime ??= DateTime.now();
var db = await CommonUserDatabase.getInstance();
await db.insert('BookmarkArtist', {
'Artist': artist,
'IsGroup': isgroup,
'IsGroup': type.index,
'DateTime': datetime.toString(),
'GroupId': group,
});
bookmarkArtistSet![isgroup]!.add(artist);
bookmarkArtistSet![type]!.add(artist);
}

Future<void> insertUser(String user,
Expand Down Expand Up @@ -454,17 +491,15 @@ class Bookmark {
bookmarkSet!.remove(id);
}

Map<int, HashSet<String>>? bookmarkArtistSet;
Future<bool> isBookmarkArtist(String name, int type) async {
Map<ArtistType, HashSet<String>>? bookmarkArtistSet;
Future<bool> isBookmarkArtist(String name, ArtistType 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) {
final artist = await getArtist();
bookmarkArtistSet = <ArtistType, HashSet<String>>{};
for (final type in ArtistType.values) {
bookmarkArtistSet![type] = HashSet<String>();
}
for (final element in artist) {
bookmarkArtistSet![element.type()]!.add(element.artist());
}
}
Expand Down Expand Up @@ -498,19 +533,18 @@ class Bookmark {
return historyUserSet!.contains(user);
}

Future<void> bookmarkArtist(String name, int type, [int group = 1]) async {
Future<void> bookmarkArtist(String name, ArtistType 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 {
Future<void> unbookmarkArtist(String name, ArtistType 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 {
Expand Down
14 changes: 7 additions & 7 deletions violet/lib/other/dialogs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Future<void> showOkDialog(BuildContext context, String message,
onPressed: () {
Navigator.pop(context);
},
child: Text(Translations.of(context).trans('ok')),
child: Text(Translations.instance!.trans('ok')),
),
],
),
Expand Down Expand Up @@ -61,7 +61,7 @@ Future<bool> showOkCancelDialog({
Navigator.pop(context, true);
}
},
child: Text(okText ?? Translations.of(context).trans('ok')),
child: Text(okText ?? Translations.instance!.trans('ok')),
),
TextButton(
style: TextButton.styleFrom(foregroundColor: Settings.majorColor),
Expand All @@ -72,7 +72,7 @@ Future<bool> showOkCancelDialog({
Navigator.pop(context, false);
}
},
child: Text(cancelText ?? Translations.of(context).trans('cancel')),
child: Text(cancelText ?? Translations.instance!.trans('cancel')),
),
],
),
Expand All @@ -92,14 +92,14 @@ Future<bool> showYesNoDialog(BuildContext context, String message,
onPressed: () {
Navigator.pop(context, true);
},
child: Text(Translations.of(context).trans('yes')),
child: Text(Translations.instance!.trans('yes')),
),
TextButton(
style: TextButton.styleFrom(foregroundColor: Settings.majorColor),
onPressed: () {
Navigator.pop(context, false);
},
child: Text(Translations.of(context).trans('no')),
child: Text(Translations.instance!.trans('no')),
),
],
),
Expand All @@ -120,14 +120,14 @@ Future<bool?> showYesNoCancelDialog(BuildContext context, String message,
onPressed: () {
Navigator.pop(context, true);
},
child: Text(Translations.of(context).trans('yes')),
child: Text(Translations.instance!.trans('yes')),
),
TextButton(
style: TextButton.styleFrom(foregroundColor: Settings.majorColor),
onPressed: () {
Navigator.pop(context, false);
},
child: Text(Translations.of(context).trans('no')),
child: Text(Translations.instance!.trans('no')),
),
],
),
Expand Down
8 changes: 5 additions & 3 deletions violet/lib/pages/after_loading/afterloading_page.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// This source code is a part of Project Violet.
// Copyright (C) 2020-2024. violet-team. Licensed under the Apache-2.0 License.

// ignore_for_file: use_build_context_synchronously

import 'dart:async';
import 'dart:io';

Expand Down Expand Up @@ -116,7 +118,7 @@ class AfterLoadingPageState extends State<AfterLoadingPage>
List.generate(7, (index) => GlobalKey());

Widget _buildBottomNavigationBar(BuildContext context) {
final translations = Translations.of(context);
final translations = Translations.instance!;

BottomNavigationBarItem buildItem(IconData iconData, String key) {
return BottomNavigationBarItem(
Expand Down Expand Up @@ -196,7 +198,7 @@ class AfterLoadingPageState extends State<AfterLoadingPage>

Widget _buildDrawer(BuildContext context) {
final mediaQuery = MediaQuery.of(context);
final translations = Translations.of(context);
final translations = Translations.instance!;

Widget buildButton(IconData iconData, int page, String key) {
final color = Settings.majorColor;
Expand Down Expand Up @@ -329,7 +331,7 @@ class AfterLoadingPageState extends State<AfterLoadingPage>
showToast(
icon: Icons.logout,
level: ToastLevel.warning,
message: Translations.of(context).trans('closedoubletap'),
message: Translations.instance!.trans('closedoubletap'),
);
}
},
Expand Down
Loading
Loading