-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathKaomoji.swift
51 lines (43 loc) · 1.77 KB
/
Kaomoji.swift
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
import Foundation
struct Kaomoji: Decodable, Hashable, Identifiable {
var id: String { string }
let string: String
let tags: [String]
}
final class KaomojiStore {
@Published var allKaomojis: [Kaomoji] = []
@Published var kaomojisByTag: [String: [Kaomoji]] = [:]
static let shared = KaomojiStore()
private static let itemPerGroup: Int = 7
init() {
guard let kaomojiURL = Bundle.main.url(forResource: "kaomoji", withExtension: "json") else {
return
}
$allKaomojis
.map { kaomojis in
var kaomojiResult: [String: [Kaomoji]] = [:]
for tag in KaomojiTags.allCases {
kaomojiResult[tag.rawValue] = kaomojis.filter { $0.tags.contains(tag.rawValue) }
}
return kaomojiResult
}
.assign(to: &$kaomojisByTag)
FilePublisher(fileURL: kaomojiURL)
.subscribe(on: DispatchQueue.global(qos: .background))
.receive(on: DispatchQueue.main)
.decode(type: [Kaomoji].self, decoder: JSONDecoder())
.replaceError(with: [])
.assign(to: &$allKaomojis)
}
}
enum KaomojiTags: String, CaseIterable {
case animals, angry, bear, bird, cat, confused, crazy, cry, cheer,
dance, dead, dog, embarassed, evil, excited, fun, food,
happy, hide, hug, hungry, hurt, kiss, laugh, love, exercise,
monkey, music, pig, rabbit, run, sad, scared, smoke, punch,
sleep, sorry, smug, stare, surprised, surrender, sing, shrug,
think, troll, wave, whatever, wink, worried, write
case seaCreatures = "sea creatures"
case tableFlip = "table flip"
static let all = KaomojiTags.allCases.map { $0.rawValue }.sorted()
}