๐Ÿ“ฆ techouse / alfred-emoji

๐Ÿ“„ main_helpers.dart ยท 122 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122part of 'main.dart';

final AlfredWorkflow _workflow = AlfredWorkflow(
  cache: AlfredCache<AlfredItems>(
    fromEncodable: (Map<String, dynamic> json) => AlfredItems.fromJson(json),
    maxEntries: 10240,
    expiryPolicy: const CreatedExpiryPolicy(
      Duration(days: 7),
    ),
  ),
)..disableAlfredSmartResultOrdering = true;

final AlfredUpdater _updater = AlfredUpdater(
  githubRepositoryUrl: Env.githubRepositoryUrl,
  currentVersion: Env.appVersion,
  updateInterval: Duration(days: 7),
);

const updateItem = AlfredItem(
  title: 'Auto-Update available!',
  subtitle: 'Press <enter> to auto-update to a new version of this workflow.',
  arg: 'update:workflow',
  match:
      'Auto-Update available! Press <enter> to auto-update to a new version of this workflow.',
  icon: AlfredItemIcon(path: 'alfredhatcog.png'),
  valid: true,
);

void _showPlaceholder() {
  _workflow.addItem(
    const AlfredItem(
      title: 'Search for emojis ...',
      icon: AlfredItemIcon(path: 'icon.png'),
    ),
  );
}

Future<void> _performSearch(
  String query, {
  Fitzpatrick? tone,
}) async {
  try {
    final SearchResponse searchResponse = await AlgoliaSearch.query(
      query,
      tone: tone,
    );

    if (searchResponse.nbHits > 0) {
      _workflow.addItems(await Future.wait(searchResponse.hits
          .map((Hit hit) => SearchResult.fromJson(
              <String, dynamic>{...hit, 'objectID': hit.objectID}))
          .map(
        (SearchResult emoji) async {
          final File? image =
              await EmojiDownloader(emoji: emoji.char).downloadImage();

          return AlfredItem(
            uid: emoji.objectID,
            title: emoji.name,
            subtitle: '${emoji.char} :${emoji.shortName}:',
            arg: emoji.char,
            match:
                '${emoji.shortName} ${emoji.name} ${emoji.keywords.join(', ')}',
            text: AlfredItemText(
              copy: emoji.char,
              largeType: emoji.name,
            ),
            quickLookUrl:
                Uri.https('emojipedia.org', 'emoji/${emoji.char}/').toString(),
            icon: AlfredItemIcon(path: image?.absolute.path ?? 'question.png'),
            mods: {
              {AlfredItemModKey.alt}: AlfredItemMod(
                subtitle: 'Copy ":${emoji.shortName}:" to clipboard',
                arg: ':${emoji.shortName}:',
                icon: AlfredItemIcon(
                    path: image?.absolute.path ?? 'question.png'),
              ),
              {AlfredItemModKey.shift}: AlfredItemMod(
                subtitle: 'Copy Python source of "${emoji.char}" to clipboard',
                arg: 'u"\\U000'
                    '${emoji.char.runes.first.toRadixString(16).toUpperCase()}'
                    '${emoji.char.runes.toList().sublist(1).map(
                          (int i) => '\\u${i.toRadixString(16).toUpperCase()}',
                        ).join()}"',
                icon: AlfredItemIcon(
                    path: image?.absolute.path ?? 'question.png'),
              ),
              {AlfredItemModKey.ctrl}: AlfredItemMod(
                subtitle: 'Copy HTML Entity of "${emoji.char}" to clipboard',
                arg: emoji.char.runes
                    .map((int i) => '&#x${i.toRadixString(16)};')
                    .join(),
                icon: AlfredItemIcon(
                    path: image?.absolute.path ?? 'question.png'),
              ),
              {AlfredItemModKey.ctrl, AlfredItemModKey.shift}: AlfredItemMod(
                subtitle:
                    'Copy formal Unicode notation of "${emoji.char}" to clipboard',
                arg: emoji.char.runes
                    .map((int i) => 'U+${i.toRadixString(16).toUpperCase()}')
                    .join(', '),
                icon: AlfredItemIcon(
                    path: image?.absolute.path ?? 'question.png'),
              ),
            },
            valid: true,
          );
        },
      ).toList()));
    } else {
      _workflow.addItem(
        AlfredItem(
          title: 'No matching emoji found',
          icon: AlfredItemIcon(path: 'question.png'),
        ),
      );
    }
  } finally {
    AlgoliaSearch.dispose();
  }
}