Skip to content

Instantly share code, notes, and snippets.

@pablocarmu
Created November 6, 2018 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pablocarmu/0c3f6967676affdb273b5b38af8c81af to your computer and use it in GitHub Desktop.
Save pablocarmu/0c3f6967676affdb273b5b38af8c81af to your computer and use it in GitHub Desktop.
Actions through extensions
import ownCloudSDK
enum ActionsExtensionActionType {
case normal
case destructive
case informal
case edit
case save
case unknown
}
typealias ActionsExtensionCompletionHandler = ((Bool) -> Void)?
typealias ActionsExtensionProgressHandler = ((Progress) -> Void)?
final class ActionsExtension: OCExtension {
// MARK: - Custom Instance Properties.
var actionName: String
var actionIcon: UIImage?
var actionType: ActionsExtensionActionType
// MARK: - Init & Deinit.
init!(actionName: String, actionIcon: UIImage? = nil, actionType: ActionsExtensionActionType = .unknown, identifier: OCExtensionIdentifier?, type: OCExtensionType!, locations locationIdentifiers: [OCExtensionLocationIdentifier]!, features: [String : Any]?, objectProvider: OCExtensionObjectProvider?, customMatcher: OCExtensionCustomContextMatcher?) {
self.actionName = actionName
self.actionIcon = actionIcon
self.actionType = actionType
super.init(identifier: identifier, type: type, locations: locationIdentifiers, features: features, objectProvider: objectProvider, customMatcher: customMatcher)
}
}
final class ActionsContext: OCExtensionContext {
// MARK: - Custom Instance Properties.
weak var viewController: UIViewController?
weak var core: OCCore?
weak var query: OCQuery?
var items: [OCItem]?
var completion: ActionsExtensionCompletionHandler
var progressHandler: ActionsExtensionProgressHandler
// MARK: - Init & Deinit.
init(viewController: UIViewController? = nil, core: OCCore? = nil, query: OCQuery? = nil, items: [OCItem]? = nil, completion: ActionsExtensionCompletionHandler, progressHandler: ActionsExtensionProgressHandler, location: OCExtensionLocation, requirements: [String : Any]? = nil, preferences: [String : Any]? = nil) {
super.init()
self.viewController = viewController
self.core = core
self.query = query
self.items = items
self.completion = completion
self.progressHandler = progressHandler
self.location = location
self.requirements = requirements
self.preferences = preferences
}
}
extension ActionsExtension {
static func moveExtension() -> ActionsExtension {
let extensionIdentifier: OCExtensionIdentifier = OCExtensionIdentifier(rawValue: "org.owncloud.extensions.actions.move")
let moveActionExtension = ActionsExtension(actionName: "Move".localized, actionIcon: nil, actionType: .normal, identifier: extensionIdentifier, type: .actions, locations: nil, features: nil, objectProvider: { (_ actionExtension, context, _ errorPointer) -> Any? in
guard let context = context as? ActionsContext else {
return nil
}
guard let core = context.core else {
return nil
}
guard let items = context.items, let viewController = context.viewController else {
return nil
}
let directoryPickerVC = ClientDirectoryPickerViewController(core: core, path: "/", completion: { (selectedDirectory) in
if let progress = core.move(items[0], to: selectedDirectory, withName: items[0].name, options: nil, resultHandler: { (error, _, _, _) in
if error != nil {
context.completion?(false)
} else {
context.completion?(true)
}
}) {
context.progressHandler?(progress)
}
})
let pickerNavigationController = ThemeNavigationController(rootViewController: directoryPickerVC)
viewController.navigationController?.present(pickerNavigationController, animated: true)
return true
}, customMatcher: nil)
return moveActionExtension!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment