90 lines
2.2 KiB
Swift
90 lines
2.2 KiB
Swift
//
|
|
// DropboxController.swift
|
|
// CapFinder
|
|
//
|
|
// Created by User on 08.04.18.
|
|
// Copyright © 2018 User. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import SwiftyDropbox
|
|
import UIKit
|
|
|
|
class DropboxController: Logger {
|
|
|
|
static var logToken = "[Dropbox]"
|
|
|
|
static var shared = DropboxController()
|
|
|
|
// MARK: Dropbox API
|
|
|
|
static var client: DropboxClient {
|
|
return DropboxClientsManager.authorizedClient!
|
|
}
|
|
|
|
var isEnabled: Bool {
|
|
return DropboxClientsManager.authorizedClient != nil
|
|
}
|
|
|
|
// MARK: - Setup
|
|
|
|
init() {
|
|
|
|
}
|
|
|
|
/** Register dropbox, load names and images.
|
|
- parameter viewController: The controller launching the request
|
|
*/
|
|
func setup(in viewController: UIViewController) {
|
|
guard isEnabled == false else {
|
|
event("Enabled")
|
|
return
|
|
}
|
|
event("Requesting access")
|
|
DropboxClientsManager.authorizeFromController(
|
|
UIApplication.shared,
|
|
controller: viewController) {
|
|
UIApplication.shared.open($0, options: [:])
|
|
}
|
|
}
|
|
|
|
func signOut() {
|
|
DropboxClientsManager.unlinkClients()
|
|
}
|
|
|
|
/// Process the response of the dropbox registration handler
|
|
func handle(url: URL) {
|
|
guard let authResult = DropboxClientsManager.handleRedirectURL(url) else {
|
|
return
|
|
}
|
|
switch authResult {
|
|
case .success:
|
|
event("enabled")
|
|
case .cancel:
|
|
error("Authorization flow canceled")
|
|
case .error(_, let description):
|
|
error("Error on authentification: \(description)")
|
|
}
|
|
}
|
|
|
|
/// Download name list and cap images
|
|
func initializeDatabase() {
|
|
guard isEnabled else {
|
|
event("Dropbox not enabled")
|
|
return
|
|
}
|
|
Cap.load()
|
|
}
|
|
|
|
func move(file: String, to: String, completion: @escaping (Bool) -> Void) {
|
|
DropboxController.client.files.moveV2(fromPath: file, toPath: to).response { _, error in
|
|
if let err = error {
|
|
self.error("Failed to move file: \(err)")
|
|
completion(false)
|
|
return
|
|
}
|
|
completion(true)
|
|
}
|
|
}
|
|
}
|