mastodon-ios/Mastodon/Extension/CoreDataStack/Emojis.swift

41 lines
903 B
Swift
Raw Normal View History

//
// Emojis.swift
// Mastodon
//
// Created by MainasuK Cirno on 2021-5-7.
//
import Foundation
import MastodonSDK
import MastodonMeta
2021-06-14 23:09:50 +02:00
protocol EmojiContainer {
var emojisData: Data? { get }
}
2021-07-23 13:33:05 +02:00
// FIXME: `Mastodon.Entity.Account` extension
2021-06-14 23:09:50 +02:00
extension EmojiContainer {
static func encode(emojis: [Mastodon.Entity.Emoji]) -> Data? {
return try? JSONEncoder().encode(emojis)
}
var emojis: [Mastodon.Entity.Emoji]? {
let decoder = JSONDecoder()
return emojisData.flatMap { try? decoder.decode([Mastodon.Entity.Emoji].self, from: $0) }
}
var emojiMeta: MastodonContent.Emojis {
2021-07-23 13:33:05 +02:00
let isAnimated = !UserDefaults.shared.preferredStaticEmoji
var dict = MastodonContent.Emojis()
for emoji in emojis ?? [] {
2021-07-23 13:33:05 +02:00
dict[emoji.shortcode] = isAnimated ? emoji.url : emoji.staticURL
}
return dict
}
}