Slight refactor paths (IOS-196)

This commit is contained in:
Nathan Mattes 2023-11-23 15:07:37 +01:00
parent 1e780481d1
commit 1514e5a2c2
2 changed files with 20 additions and 9 deletions

View File

@ -5,9 +5,11 @@ import MastodonCore
extension FileManager { extension FileManager {
func searchItems(forUser userID: String) throws -> [Persistence.SearchHistory.Item] { func searchItems(forUser userID: String) throws -> [Persistence.SearchHistory.Item] {
guard let path = documentsDirectory()?.appending(path: Persistence.searchHistory.filename).appendingPathExtension("json"), guard let documentsDirectory else { return [] }
let data = try? Data(contentsOf: path)
else { return [] } let searchHistoryPath = Persistence.searchHistory.filepath(baseURL: documentsDirectory)
guard let data = try? Data(contentsOf: searchHistoryPath) else { return [] }
let jsonDecoder = JSONDecoder() let jsonDecoder = JSONDecoder()
jsonDecoder.dateDecodingStrategy = .iso8601 jsonDecoder.dateDecodingStrategy = .iso8601
@ -24,7 +26,7 @@ extension FileManager {
} }
func addSearchItem(_ newSearchItem: Persistence.SearchHistory.Item) throws { func addSearchItem(_ newSearchItem: Persistence.SearchHistory.Item) throws {
guard let path = documentsDirectory()?.appending(path: Persistence.searchHistory.filename).appendingPathExtension("json") else { return } guard let documentsDirectory else { return }
var searchItems = (try? searchItems(forUser: newSearchItem.userID)) ?? [] var searchItems = (try? searchItems(forUser: newSearchItem.userID)) ?? []
@ -38,21 +40,24 @@ extension FileManager {
jsonEncoder.dateEncodingStrategy = .iso8601 jsonEncoder.dateEncodingStrategy = .iso8601
do { do {
let data = try jsonEncoder.encode(searchItems) let data = try jsonEncoder.encode(searchItems)
try data.write(to: path)
let searchHistoryPath = Persistence.searchHistory.filepath(baseURL: documentsDirectory)
try data.write(to: searchHistoryPath)
} catch { } catch {
debugPrint(error.localizedDescription) debugPrint(error.localizedDescription)
} }
} }
func removeSearchHistory() { func removeSearchHistory() {
guard let path = documentsDirectory()?.appending(path: Persistence.searchHistory.filename).appendingPathExtension("json") else { return } guard let documentsDirectory else { return }
try? removeItem(at: path) let searchHistoryPath = Persistence.searchHistory.filepath(baseURL: documentsDirectory)
try? removeItem(at: searchHistoryPath)
} }
} }
extension FileManager { extension FileManager {
func documentsDirectory() -> URL? { public var documentsDirectory: URL? {
return self.urls(for: .documentDirectory, in: .userDomainMask).first return self.urls(for: .documentDirectory, in: .userDomainMask).first
} }
} }

View File

@ -11,12 +11,18 @@ import Foundation
public enum Persistence { public enum Persistence {
case searchHistory case searchHistory
public var filename: String { private var filename: String {
switch self { switch self {
case .searchHistory: case .searchHistory:
return "search_history" return "search_history"
} }
} }
public func filepath(baseURL: URL) -> URL {
baseURL
.appending(path: filename)
.appendingPathExtension("json")
}
} }