-
Notifications
You must be signed in to change notification settings - Fork 497
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
/issues/5009 - Refactored away the NSExtensionContext dependency from…
… the ShareManager. Introduced different ShareItemProviders for the share extension and the main application. Improved item loading error handling.
- Loading branch information
1 parent
aa790b2
commit e3f1bd2
Showing
21 changed files
with
524 additions
and
273 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
RiotShareExtension/Shared/ShareItemProvider/ShareItemProviderProtocol.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// | ||
// Copyright 2021 New Vector Ltd | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
|
||
@objc public enum ShareItemType: UInt { | ||
case fileURL, text, URL, image, video, movie, voiceMessage, unknown | ||
} | ||
|
||
@objc public protocol ShareItemProtocol { | ||
var type: ShareItemType { get } | ||
} | ||
|
||
@objc public protocol ShareItemProviderProtocol { | ||
var items: [ShareItemProtocol] { get } | ||
|
||
func areAllItemsImages() -> Bool | ||
|
||
func areAllItemsLoaded() -> Bool | ||
|
||
func loadItem(_ item: ShareItemProtocol, completion: @escaping (Any?, Error?) -> Void) | ||
} |
95 changes: 95 additions & 0 deletions
95
RiotShareExtension/Shared/ShareItemProvider/SimpleShareItemProvider.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// | ||
// Copyright 2021 New Vector Ltd | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
|
||
private class SimpleShareItem: ShareItemProtocol { | ||
let attachment: MXKAttachment? | ||
let textMessage: String? | ||
|
||
init(withAttachment attachment: MXKAttachment) { | ||
self.attachment = attachment | ||
self.textMessage = nil | ||
} | ||
|
||
init(withTextMessage textMessage: String) { | ||
self.attachment = nil | ||
self.textMessage = textMessage | ||
} | ||
|
||
var type: ShareItemType { | ||
guard textMessage == nil else { | ||
return .text | ||
} | ||
|
||
guard let attachment = attachment else { | ||
return .unknown | ||
} | ||
|
||
if attachment.type == MXKAttachmentTypeImage { | ||
return .image | ||
} else if attachment.type == MXKAttachmentTypeVideo { | ||
return .video | ||
} else if attachment.type == MXKAttachmentTypeFile { | ||
return .fileURL | ||
} else if attachment.type == MXKAttachmentTypeVoiceMessage { | ||
return .voiceMessage | ||
} else { | ||
return .unknown | ||
} | ||
} | ||
} | ||
|
||
@objc class SimpleShareItemProvider: NSObject, ShareItemProviderProtocol { | ||
|
||
private let attachment: MXKAttachment? | ||
private let textMessage: String? | ||
|
||
let items: [ShareItemProtocol] | ||
|
||
@objc public init(withAttachment attachment: MXKAttachment) { | ||
self.attachment = attachment | ||
self.items = [SimpleShareItem(withAttachment: attachment)]; | ||
self.textMessage = nil | ||
} | ||
|
||
@objc public init(withTextMessage textMessage: String) { | ||
self.textMessage = textMessage | ||
self.items = [SimpleShareItem(withTextMessage: textMessage)]; | ||
self.attachment = nil | ||
} | ||
|
||
func loadItem(_ item: ShareItemProtocol, completion: @escaping (Any?, Error?) -> Void) { | ||
if let textMessage = self.textMessage { | ||
completion(textMessage, nil) | ||
return | ||
} | ||
|
||
attachment?.prepareShare({ url in | ||
completion(url, nil) | ||
}, failure: { error in | ||
completion(nil, error) | ||
}) | ||
} | ||
|
||
func areAllItemsLoaded() -> Bool { | ||
return true | ||
} | ||
|
||
func areAllItemsImages() -> Bool { | ||
return (attachment != nil && attachment?.type == MXKAttachmentTypeImage) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.