Slight refactor paths (IOS-196)
This commit is contained in:
parent
1e780481d1
commit
1514e5a2c2
|
@ -5,9 +5,11 @@ import MastodonCore
|
|||
|
||||
extension FileManager {
|
||||
func searchItems(forUser userID: String) throws -> [Persistence.SearchHistory.Item] {
|
||||
guard let path = documentsDirectory()?.appending(path: Persistence.searchHistory.filename).appendingPathExtension("json"),
|
||||
let data = try? Data(contentsOf: path)
|
||||
else { return [] }
|
||||
guard let documentsDirectory else { return [] }
|
||||
|
||||
let searchHistoryPath = Persistence.searchHistory.filepath(baseURL: documentsDirectory)
|
||||
|
||||
guard let data = try? Data(contentsOf: searchHistoryPath) else { return [] }
|
||||
|
||||
let jsonDecoder = JSONDecoder()
|
||||
jsonDecoder.dateDecodingStrategy = .iso8601
|
||||
|
@ -24,7 +26,7 @@ extension FileManager {
|
|||
}
|
||||
|
||||
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)) ?? []
|
||||
|
||||
|
@ -38,21 +40,24 @@ extension FileManager {
|
|||
jsonEncoder.dateEncodingStrategy = .iso8601
|
||||
do {
|
||||
let data = try jsonEncoder.encode(searchItems)
|
||||
try data.write(to: path)
|
||||
|
||||
let searchHistoryPath = Persistence.searchHistory.filepath(baseURL: documentsDirectory)
|
||||
try data.write(to: searchHistoryPath)
|
||||
} catch {
|
||||
debugPrint(error.localizedDescription)
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
func documentsDirectory() -> URL? {
|
||||
public var documentsDirectory: URL? {
|
||||
return self.urls(for: .documentDirectory, in: .userDomainMask).first
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,12 +11,18 @@ import Foundation
|
|||
public enum Persistence {
|
||||
case searchHistory
|
||||
|
||||
public var filename: String {
|
||||
private var filename: String {
|
||||
switch self {
|
||||
case .searchHistory:
|
||||
return "search_history"
|
||||
}
|
||||
}
|
||||
|
||||
public func filepath(baseURL: URL) -> URL {
|
||||
baseURL
|
||||
.appending(path: filename)
|
||||
.appendingPathExtension("json")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue