45 lines
1.1 KiB
Swift
45 lines
1.1 KiB
Swift
//
|
|
// StatusCache.swift
|
|
// Tusker
|
|
//
|
|
// Created by Shadowfacts on 9/17/18.
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import Pachyderm
|
|
|
|
class StatusCache {
|
|
|
|
static let cache = NSCache<NSString, Status>()
|
|
|
|
static func get(id: String) -> Status? {
|
|
return cache.object(forKey: id as NSString)
|
|
}
|
|
|
|
static func set(id: String, status: Status) {
|
|
cache.setObject(status, forKey: id as NSString)
|
|
}
|
|
|
|
static func get(id: String, completion: @escaping (Status?) -> Void) {
|
|
let request = MastodonController.shared.client.getStatus(id: id)
|
|
MastodonController.shared.client.run(request) { response in
|
|
guard case let .success(status, _) = response else {
|
|
completion(nil)
|
|
return
|
|
}
|
|
set(id: id, status: status)
|
|
completion(status)
|
|
}
|
|
}
|
|
|
|
static func add(_ status: Status) {
|
|
set(id: status.id, status: status)
|
|
}
|
|
|
|
static func addAll(_ statuses: [Status]) {
|
|
statuses.forEach { set(id: $0.id, status: $0) }
|
|
}
|
|
|
|
}
|