Caps-iOS/CapCollector/Data/NameFile.swift

146 lines
4.0 KiB
Swift
Raw Normal View History

2019-03-15 13:19:19 +01:00
//
// NameFile.swift
// CapFinder
//
// Created by User on 23.04.18.
// Copyright © 2018 User. All rights reserved.
//
import Foundation
import SwiftyDropbox
final class NameFile: Logger {
static let logToken = "[NameFile]"
/// The name of the file
private static let fileName = "names.txt"
private static let path = "/" + fileName
/// The url of the file on disk
private static let url = DiskManager.documentsDirectory.appendingPathComponent(fileName)
private static let fm = FileManager.default
// MARK: - Reading from disk
/// Indicates if the name list was written to disk
private static var nameFileExistsOnDisk: Bool {
return fm.fileExists(atPath: url.path)
}
// MARK: - Public API
@discardableResult static func save(names: String) -> Bool {
let data = names.data(using: .utf8)!
return save(names: data)
}
static func saveAndUpload(names: String, completion: @escaping (Bool) -> Void) {
let data = names.data(using: .utf8)!
guard save(names: data) else {
return
}
let client = DropboxController.client
client.files.upload(path: path, mode: .overwrite, input: data).response { _ , error in
if let error = error {
self.error("Error uploading name list: \(error)")
completion(false)
} else {
self.event("Uploaded name list")
completion(true)
}
}
}
// MARK: - Private
/// The content of the name file as a String
private static var content: String? {
do {
return try String(contentsOf: url, encoding: .utf8)
} catch {
self.error("Error reading \(url): \(error)")
return nil
}
}
/**
Save the name file to disk
- parameter names: The new name file content
- returns: True, if the data was written to disk
*/
@discardableResult private static func save(names: Data) -> Bool {
do {
try names.write(to: url, options: .atomic)
event("Name file saved to disk")
return true
} catch {
self.error("Could not save names to file: \(error)")
return false
}
}
static func makeAvailable(completion: ((String?) -> Void)? = nil) {
if nameFileExistsOnDisk {
completion?(self.content)
} else {
download() { success in
guard success else {
completion?(nil)
return
}
completion?(self.content)
}
}
}
/// The data of the name list
private static var data: Data? {
guard nameFileExistsOnDisk else { return nil }
do {
return try Data(contentsOf: url)
} catch {
self.error("Could not read data from \(url): \(error)")
return nil
}
}
/**
Delete the file on disk
- returns: True, if the file no longer exists on disk
*/
@discardableResult private static func delete() -> Bool {
guard nameFileExistsOnDisk else {
event("No name file to delete")
return true
}
do {
try fm.removeItem(at: url)
} catch {
self.error("Could not delete name file: \(error)")
return false
}
event("Deleted name file on disk")
return true
}
private static func download(completion: ((Bool) -> Void)? = nil) {
let client = DropboxController.client
event("Downloading names from Dropbox")
client.files.download(path: path).response { response, error in
guard let data = response?.1 else {
self.error("Error downloading file: \(error!)")
completion?(false)
return
}
self.event("Downloaded name file")
completion?(NameFile.save(names: data))
}
}
}