Skip to content

Commit

Permalink
refactor: optimize the code
Browse files Browse the repository at this point in the history
Co-authored-by: Mathias Mogensen <[email protected]>
Co-authored-by: LucasXu0 <[email protected]>
  • Loading branch information
3 people committed Dec 24, 2024
1 parent 570b7fd commit 0cf25f1
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 174 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
4 changes: 2 additions & 2 deletions frontend/appflowy_flutter/lib/core/config/kv_keys.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ class KVKeys {
/// The value is a boolean string
static const String hasUpgradedSpace = 'hasUpgradedSpace060';

/// The key for saving the rencent icons
/// The key for saving the recent icons
///
/// The value is a json string of [RecentIcons]
static const String kRecentIcons = 'kRecentIcons';
static const String recentIcons = 'kRecentIcons';
}
51 changes: 22 additions & 29 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,30 +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 @@ -95,11 +72,7 @@ class _FlowyEmojiPickerState extends State<FlowyEmojiPicker> {
RecentIcons.putEmoji(id);
},
padding: const EdgeInsets.symmetric(horizontal: 16.0),
headerBuilder: (context, category) {
return FlowyEmojiHeader(
category: category,
);
},
headerBuilder: (_, category) => FlowyEmojiHeader(category: category),
itemBuilder: (context, emojiId, emoji, callback) {
final name = emojiData.emojis[emojiId]?.name ?? '';
return SizedBox.square(
Expand Down Expand Up @@ -136,4 +109,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,51 +13,16 @@ class RecentIcons {
static final Map<String, List<String>> _dataMap = {};
static bool _loaded = false;
static const maxLength = 20;

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

static Future _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 with :$v', e);
} on TypeError catch (e) {
Log.error('RecentIcons load 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 {
static Future<void> putEmoji(String id) async {
await _put(FlowyIconType.emoji, id);
}

static Future putIcon(Icon icon) async {
static Future<void> putIcon(Icon icon) async {
await _put(
FlowyIconType.icon,
jsonEncode(
Expand All @@ -79,11 +44,52 @@ class RecentIcons {
return iconList
.map((e) => Icon.fromJson(jsonDecode(e) as Map<String, dynamic>))
.toList();
} on FormatException catch (e) {
Log.error('RecentIcons getIcons with :$iconList', e);
} on TypeError catch (e) {
} catch (e) {
Log.error('RecentIcons getIcons with :$iconList', e);
}
return [];
}

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

static Future<void> _load() async {
if (_loaded || !enable) {
return;
}
final storage = getIt<KeyValueStorage>();
final value = await storage.get(KVKeys.recentIcons);
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<void> _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 0cf25f1

Please sign in to comment.