-
-
Notifications
You must be signed in to change notification settings - Fork 721
/
ShareViewController.swift
141 lines (125 loc) · 5.61 KB
/
ShareViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import UIKit
@objc(WMFShareViewController)
class ShareViewController: UIViewController, Themeable {
@IBOutlet weak var cancelButton: UIButton!
let text: String
let articleTitle: String
let articleURL: URL
let articleImageURL: URL?
let articleDescription: String?
let loadGroup: WMFTaskGroup
var theme: Theme
var image: UIImage?
var imageLicense: MWKLicense?
let infoFetcher = MWKImageInfoFetcher()
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var busyView: UIView!
@IBOutlet weak var busyLabel: UILabel!
@IBOutlet weak var imageViewTopConstraint: NSLayoutConstraint!
@IBOutlet weak var imageViewVerticallyCenteredConstraint: NSLayoutConstraint!
@objc required public init?(text: String, article: WMFArticle, theme: Theme) {
guard let articleURL = article.url else {
return nil
}
self.text = text
self.articleTitle = article.displayTitle ?? ""
self.articleDescription = article.capitalizedWikidataDescription
self.articleURL = articleURL
self.articleImageURL = article.imageURL(forWidth: 640)
self.theme = theme
self.loadGroup = WMFTaskGroup()
super.init(nibName: "ShareViewController", bundle: nil)
modalPresentationStyle = .overCurrentContext
loadImage()
}
required convenience init?(coder aDecoder: NSCoder) {
self.init(text: "", article: WMFArticle(), theme: Theme.standard)
}
override func viewDidLoad() {
super.viewDidLoad()
busyLabel.text = WMFLocalizedString("share-building", value: "Building Share-a-fact card…", comment: "Shown while Share-a-fact card is being constructed")
cancelButton.setTitle(WMFLocalizedString("cancel", value: "Cancel", comment: "Cancel"), for: .normal)
apply(theme: theme)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.loadGroup.waitInBackground {
let image = self.createShareAFactCard()
self.showActivityViewController(with: image)
}
}
func loadImage() {
if let imageURL = self.articleImageURL, let imageName = WMFParseUnescapedNormalizedImageNameFromSourceURL(imageURL), let siteURL = articleURL.wmf_site {
loadGroup.enter()
let filename = "File:" + imageName
infoFetcher.fetchGalleryInfo(forImage: filename, fromSiteURL: siteURL, failure: { (error) in
self.loadGroup.leave()
}, success: { (info) in
defer {
self.loadGroup.leave()
}
guard let imageInfo = info as? MWKImageInfo else {
return
}
self.imageLicense = imageInfo.license
})
loadGroup.enter()
let _ = ImageController.shared.fetchImage(withURL: imageURL, failure: { (fail) in
self.loadGroup.leave()
}) { (download) in
self.image = download.image.staticImage
self.loadGroup.leave()
}
}
}
func createShareAFactCard() -> UIImage? {
let cardController = ShareAFactViewController(nibName: "ShareAFactViewController", bundle: nil)
let cardView = cardController.view
cardController.update(with: articleURL, articleTitle: articleTitle, text: text, image: image, imageLicense: imageLicense)
return cardView?.wmf_snapshotImage()
}
func showActivityViewController(with shareAFactImage: UIImage?) {
cancelButton.isEnabled = false
imageView.image = shareAFactImage
UIView.animate(withDuration: 0.3) {
self.imageView.alpha = 1
self.busyView.alpha = 0
self.cancelButton.alpha = 0
}
let itemProvider = ShareAFactActivityTextItemProvider(text: text, articleTitle: articleTitle, articleURL: articleURL)
var activityItems = [Any]()
if let image = shareAFactImage {
activityItems.append(ShareAFactActivityImageItemProvider(image: image))
}
activityItems.append(itemProvider)
let activityVC = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
activityVC.popoverPresentationController?.sourceView = view
activityVC.popoverPresentationController?.sourceRect = imageView.frame
activityVC.completionWithItemsHandler = { (activityType, completed, returnedItems, error) in
self.presentingViewController?.dismiss(animated: true, completion: nil)
}
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(750), execute: {
UIView.animate(withDuration: 0.3, animations: {
self.imageViewTopConstraint.isActive = true
self.imageViewVerticallyCenteredConstraint.isActive = false
self.view.layoutIfNeeded()
}, completion: nil)
self.present(activityVC, animated: true, completion: nil)
})
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
adjustTheme(previousTraitCollection)
}
@IBAction func cancel(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
func apply(theme: Theme) {
self.theme = theme
guard viewIfLoaded != nil else {
return
}
busyLabel.textColor = theme.colors.primaryText
view.backgroundColor = theme.colors.paperBackground.withAlphaComponent(0.9)
}
}