Skip to content

Commit

Permalink
refactor: optimize the code
Browse files Browse the repository at this point in the history
Co-authored-by: LucasXu0 <[email protected]>
  • Loading branch information
asjqkkkk and LucasXu0 committed Dec 23, 2024
1 parent d127965 commit 6c3a454
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@ import '../../shared/emoji.dart';
import '../../shared/util.dart';

void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
RecentIcons.enable = false;
setUpAll(() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
RecentIcons.enable = false;
});

tearDownAll(() {
RecentIcons.enable = true;
});

group('grid row detail page:', () {
testWidgets('opens', (tester) async {
await tester.initializeAppFlowy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ import '../../shared/emoji.dart';
import '../../shared/util.dart';

void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
RecentIcons.enable = false;
setUpAll(() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
RecentIcons.enable = false;
});

tearDownAll(() {
RecentIcons.enable = true;
});

group('cover image:', () {
testWidgets('document cover tests', (tester) async {
await tester.initializeAppFlowy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@ import '../../shared/common_operations.dart';
import '../../shared/expectation.dart';

void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
RecentIcons.enable = false;
final emoji = EmojiIconData.emoji('😁');

setUpAll(() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
RecentIcons.enable = false;
});

tearDownAll(() {
RecentIcons.enable = true;
});

Future<EmojiIconData> loadIcon() async {
await loadIconGroups();
final groups = kIconGroups!;
Expand Down
41 changes: 21 additions & 20 deletions frontend/appflowy_flutter/lib/plugins/base/emoji/emoji_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:flutter_emoji_mart/flutter_emoji_mart.dart';

// use a global value to store the selected emoji to prevent reloading every time.
EmojiData? kCachedEmojiData;
const _kRecentEmojiCategoryId = 'Recent';

class FlowyEmojiPicker extends StatefulWidget {
const FlowyEmojiPicker({
Expand All @@ -30,26 +31,6 @@ class _FlowyEmojiPickerState extends State<FlowyEmojiPicker> {
late EmojiData emojiData;
bool loaded = false;

void loadEmojis(EmojiData data) {
RecentIcons.getEmojiIds().then((v) {
if (v.isEmpty) {
emojiData = data;
setState(() => loaded = true);
return;
}
final categories = List.of(data.categories);
categories.insert(
0,
Category(
id: 'Recent',
emojiIds: v.sublist(0, min(widget.emojiPerLine, v.length)),
),
);
emojiData = EmojiData(categories: categories, emojis: data.emojis);
setState(() => loaded = true);
});
}

@override
void initState() {
super.initState();
Expand Down Expand Up @@ -132,4 +113,24 @@ class _FlowyEmojiPickerState extends State<FlowyEmojiPicker> {
},
);
}

void loadEmojis(EmojiData data) {
RecentIcons.getEmojiIds().then((v) {
if (v.isEmpty) {
emojiData = data;
setState(() => loaded = true);
return;
}
final categories = List.of(data.categories);
categories.insert(
0,
Category(
id: _kRecentEmojiCategoryId,
emojiIds: v.sublist(0, min(widget.emojiPerLine, v.length)),
),
);
emojiData = EmojiData(categories: categories, emojis: data.emojis);
setState(() => loaded = true);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import 'icon_color_picker.dart';

// cache the icon groups to avoid loading them multiple times
List<IconGroup>? kIconGroups;
const _kRecentIconGroupName = 'Recent';

extension IconGroupFilter on List<IconGroup> {
String? findSvgContent(String key) {
Expand Down Expand Up @@ -96,13 +97,13 @@ class _FlowyIconPickerState extends State<FlowyIconPicker> {
final ValueNotifier<String> keyword = ValueNotifier('');
final debounce = Debounce(duration: const Duration(milliseconds: 150));

Future loadIcons() async {
Future<void> loadIcons() async {
final localIcons = await loadIconGroups();
final recentIcons = await RecentIcons.getIcons();
if (recentIcons.isNotEmpty) {
iconGroups.add(
IconGroup(
name: 'Recent',
name: _kRecentIconGroupName,
icons: recentIcons.sublist(
0,
min(recentIcons.length, widget.iconPerLine),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,11 @@ class RecentIcons {
static final Map<String, List<String>> _dataMap = {};
static bool _loaded = false;
static const maxLength = 20;

///prevent the Recent Icon feature from affecting the unit tests of the Icon Selector.
@visibleForTesting
static bool enable = true;

static Future<void> _save() async {
await getIt<KeyValueStorage>().set(
KVKeys.kRecentIcons,
jsonEncode(_dataMap),
);
}

static Future _load() async {
if (_loaded || !enable) return;
final v = await getIt<KeyValueStorage>().get(KVKeys.kRecentIcons);
try {
final data = jsonDecode(v ?? '') as Map;
_dataMap.clear();
_dataMap.addAll(
data.map(
(k, v) => MapEntry<String, List<String>>(k, List<String>.from(v)),
),
);
} on FormatException catch (e) {
Log.error('RecentIcons load failed with :$v', e);
} on TypeError catch (e) {
Log.error('RecentIcons load failed with :$v', e);
}
_loaded = true;
}

static Future _put(FlowyIconType key, String value) async {
await _load();
if (!enable) return;
final list = _dataMap[key.name] ?? [];
list.remove(value);
list.insert(0, value);
if (list.length > maxLength) list.removeLast();
_dataMap[key.name] = list;
await _save();
}

static Future putEmoji(String id) async {
await _put(FlowyIconType.emoji, id);
}
Expand Down Expand Up @@ -86,4 +51,47 @@ class RecentIcons {
}
return [];
}

static Future<void> _save() async {
await getIt<KeyValueStorage>().set(
KVKeys.kRecentIcons,
jsonEncode(_dataMap),
);
}

static Future _load() async {
if (_loaded || !enable) {
return;
}
final storage = getIt<KeyValueStorage>();
final value = await storage.get(KVKeys.kRecentIcons);
if (value == null || value.isEmpty) {
_loaded = true;
return;
}
try {
final data = jsonDecode(value) as Map;
_dataMap
..clear()
..addAll(
Map<String, List<String>>.from(
data.map((k, v) => MapEntry(k, List<String>.from(v))),
),
);
} catch (e) {
Log.error('RecentIcons load failed with: $value', e);
}
_loaded = true;
}

static Future _put(FlowyIconType key, String value) async {
await _load();
if (!enable) return;
final list = _dataMap[key.name] ?? [];
list.remove(value);
list.insert(0, value);
if (list.length > maxLength) list.removeLast();
_dataMap[key.name] = list;
await _save();
}
}
93 changes: 0 additions & 93 deletions frontend/appflowy_flutter/test/unit_test/util/recent_icons.dart

This file was deleted.

Loading

0 comments on commit 6c3a454

Please sign in to comment.