Action Share update

This commit is contained in:
Scott Pfeil 2023-10-19 12:30:13 -04:00
parent f2df0f52e6
commit d44235bd03
2 changed files with 78 additions and 20 deletions

View File

@ -14,15 +14,7 @@ open class ActionShareHandler: MVMCoreActionHandlerProtocol {
open func execute(with model: ActionModelProtocol, delegateObject: DelegateObject? = nil, additionalData: [AnyHashable: Any]? = nil) async throws { open func execute(with model: ActionModelProtocol, delegateObject: DelegateObject? = nil, additionalData: [AnyHashable: Any]? = nil) async throws {
guard let model = model as? ActionShareModel else { return } guard let model = model as? ActionShareModel else { return }
var shareData: [Any] try await shareWith(activityItems: model.items.map { $0.value }, model: model)
switch model.sharedType {
case .text:
shareData = [model.sharedText]
case .url:
let url = try URL.createURL(with: model.sharedText)
shareData = [url]
}
try await shareWith(activityItems: shareData, model: model)
} }
@MainActor @MainActor
@ -33,10 +25,6 @@ open class ActionShareHandler: MVMCoreActionHandlerProtocol {
controller.completionWithItemsHandler = {(activityType: UIActivity.ActivityType?, completed: Bool, returnedItems: [Any]?, error: Error?) in controller.completionWithItemsHandler = {(activityType: UIActivity.ActivityType?, completed: Bool, returnedItems: [Any]?, error: Error?) in
if completed { if completed {
// Activity was completed, considered finished. // Activity was completed, considered finished.
if activityType == .copyToPasteboard {
// Allow copy
MVMCoreSessionObject.sharedGlobal()?.copyString(toClipboard: model.sharedText)
}
continuation.resume() continuation.resume()
} else if let _ = activityType { } else if let _ = activityType {
// If a specific type of activity failed, the activity controller is still presented, cannot continue yet. // If a specific type of activity failed, the activity controller is still presented, cannot continue yet.

View File

@ -6,14 +6,51 @@
// Copyright © 2020 myverizon. All rights reserved. // Copyright © 2020 myverizon. All rights reserved.
// //
public struct ActionShareItemModel: Codable {
public struct ActionShareModel: ActionModelProtocol {
public enum SharedType: String, Codable { public enum SharedType: String, Codable {
case text case text
case url case url
} }
public var type: SharedType
public var value: Any
private enum CodingKeys: String, CodingKey {
case type
case value
}
public init(type: SharedType, value: Any) {
self.type = type
self.value = value
}
public init(from decoder: Decoder) throws {
let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
type = try typeContainer.decode(SharedType.self, forKey: .type)
switch type {
case .text:
value = try typeContainer.decode(String.self, forKey: .value)
case .url:
value = try typeContainer.decode(URL.self, forKey: .value)
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(type, forKey: .type)
switch type {
case .text:
try container.encode(value as! String, forKey: .value)
case .url:
try container.encode(value as! URL, forKey: .value)
}
}
}
public struct ActionShareModel: ActionModelProtocol {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Properties // MARK: - Properties
//-------------------------------------------------- //--------------------------------------------------
@ -21,8 +58,7 @@ public struct ActionShareModel: ActionModelProtocol {
public static var identifier: String = "share" public static var identifier: String = "share"
public var actionType: String = ActionShareModel.identifier public var actionType: String = ActionShareModel.identifier
public var sharedType: SharedType public var items: [ActionShareItemModel]
public var sharedText: String
public var extraParameters: JSONValueDictionary? public var extraParameters: JSONValueDictionary?
public var analyticsData: JSONValueDictionary? public var analyticsData: JSONValueDictionary?
@ -30,10 +66,44 @@ public struct ActionShareModel: ActionModelProtocol {
// MARK: - Initializer // MARK: - Initializer
//-------------------------------------------------- //--------------------------------------------------
public init(sharedText: String, sharedType: SharedType, _ extraParameters: JSONValueDictionary? = nil, _ analyticsData: JSONValueDictionary? = nil) { public init(sharedText: String, sharedType: ActionShareItemModel.SharedType, _ extraParameters: JSONValueDictionary? = nil, _ analyticsData: JSONValueDictionary? = nil) {
self.sharedType = sharedType items = [ActionShareItemModel(type: sharedType, value: sharedText)]
self.sharedText = sharedText
self.extraParameters = extraParameters self.extraParameters = extraParameters
self.analyticsData = analyticsData self.analyticsData = analyticsData
} }
//--------------------------------------------------
// MARK: - Codable
//--------------------------------------------------
private enum CodingKeys: String, CodingKey {
case actionType
case items
case sharedType
case sharedText
}
public init(from decoder: Decoder) throws {
let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
if let items = try typeContainer.decodeIfPresent([ActionShareItemModel].self, forKey: .items) {
self.items = items
} else {
// Legacy
let type = try typeContainer.decode(ActionShareItemModel.SharedType.self, forKey: .sharedType)
var value: Any
switch type {
case .url:
value = try typeContainer.decode(URL.self, forKey: .sharedText)
default:
value = try typeContainer.decode(String.self, forKey: .sharedText)
}
items = [ActionShareItemModel(type: type, value: value)]
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(actionType, forKey: .actionType)
try container.encode(items, forKey: .items)
}
} }