|
| 1 | +// |
| 2 | +// RepositoryImageViewController.swift |
| 3 | +// Freetime |
| 4 | +// |
| 5 | +// Created by Ryan Nystrom on 11/17/18. |
| 6 | +// Copyright © 2018 Ryan Nystrom. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | +import SDWebImage |
| 11 | +import SnapKit |
| 12 | +import TUSafariActivity |
| 13 | + |
| 14 | +final class RepositoryImageViewController: UIViewController, |
| 15 | +EmptyViewDelegate, |
| 16 | +UIScrollViewDelegate { |
| 17 | + |
| 18 | + private let branch: String |
| 19 | + private let path: FilePath |
| 20 | + private let repo: RepositoryDetails |
| 21 | + private let client: GithubClient |
| 22 | + private let emptyView = EmptyView() |
| 23 | + private let scrollView = UIScrollView() |
| 24 | + private let imageView = FLAnimatedImageView() |
| 25 | + private let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray) |
| 26 | + private var imageUrl: URL? { |
| 27 | + let builder = URLBuilder.github() |
| 28 | + .add(path: repo.owner) |
| 29 | + .add(path: repo.name) |
| 30 | + .add(path: "raw") |
| 31 | + .add(path: branch) |
| 32 | + path.components.forEach { builder.add(path: $0) } |
| 33 | + return builder.url |
| 34 | + } |
| 35 | + |
| 36 | + private lazy var moreOptionsItem: UIBarButtonItem = { |
| 37 | + let barButtonItem = UIBarButtonItem( |
| 38 | + image: UIImage(named: "bullets-hollow"), |
| 39 | + target: self, |
| 40 | + action: #selector(RepositoryImageViewController.onShare(sender:))) |
| 41 | + barButtonItem.isEnabled = false |
| 42 | + return barButtonItem |
| 43 | + }() |
| 44 | + |
| 45 | + init(repo: RepositoryDetails, branch: String, path: FilePath, client: GithubClient) { |
| 46 | + self.branch = branch |
| 47 | + self.path = path |
| 48 | + self.repo = repo |
| 49 | + self.client = client |
| 50 | + super.init(nibName: nil, bundle: nil) |
| 51 | + } |
| 52 | + |
| 53 | + required init?(coder aDecoder: NSCoder) { |
| 54 | + fatalError("init(coder:) has not been implemented") |
| 55 | + } |
| 56 | + |
| 57 | + override func viewDidLoad() { |
| 58 | + super.viewDidLoad() |
| 59 | + |
| 60 | + view.backgroundColor = .white |
| 61 | + |
| 62 | + configureTitle(filePath: path, target: self, action: #selector(onFileNavigationTitle(sender:))) |
| 63 | + makeBackBarItemEmpty() |
| 64 | + navigationItem.rightBarButtonItem = moreOptionsItem |
| 65 | + |
| 66 | + emptyView.isHidden = true |
| 67 | + emptyView.label.text = NSLocalizedString("Error loading image", comment: "") |
| 68 | + view.addSubview(emptyView) |
| 69 | + emptyView.snp.makeConstraints { make in |
| 70 | + make.edges.equalToSuperview() |
| 71 | + } |
| 72 | + |
| 73 | + scrollView.contentInsetAdjustmentBehavior = .never |
| 74 | + scrollView.alwaysBounceVertical = true |
| 75 | + scrollView.alwaysBounceHorizontal = true |
| 76 | + scrollView.showsHorizontalScrollIndicator = false |
| 77 | + scrollView.showsVerticalScrollIndicator = false |
| 78 | + scrollView.maximumZoomScale = 4 |
| 79 | + scrollView.delegate = self |
| 80 | + view.addSubview(scrollView) |
| 81 | + scrollView.snp.makeConstraints { make in |
| 82 | + make.edges.equalToSuperview() |
| 83 | + } |
| 84 | + |
| 85 | + imageView.contentMode = .scaleAspectFit |
| 86 | + scrollView.addSubview(imageView) |
| 87 | + |
| 88 | + view.addSubview(activityIndicator) |
| 89 | + activityIndicator.snp.makeConstraints { make in |
| 90 | + make.center.equalToSuperview() |
| 91 | + } |
| 92 | + |
| 93 | + fetch() |
| 94 | + } |
| 95 | + |
| 96 | + // MARK: Private API |
| 97 | + |
| 98 | + @objc func onFileNavigationTitle(sender: UIView) { |
| 99 | + showAlert(filePath: path, sender: sender) |
| 100 | + } |
| 101 | + |
| 102 | + private func fetch() { |
| 103 | + emptyView.isHidden = true |
| 104 | + activityIndicator.startAnimating() |
| 105 | + imageView.sd_setImage(with: imageUrl) { [weak self] (_, error, _, _) in |
| 106 | + self?.handle(success: error == nil) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private func handle(success: Bool) { |
| 111 | + moreOptionsItem.isEnabled = success |
| 112 | + emptyView.isHidden = success |
| 113 | + |
| 114 | + activityIndicator.stopAnimating() |
| 115 | + |
| 116 | + if let size = imageView.image?.size { |
| 117 | + // centered later in UIScrollViewDelegate |
| 118 | + imageView.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height) |
| 119 | + scrollView.contentSize = size |
| 120 | + let bounds = view.bounds |
| 121 | + let scale = min(bounds.width / size.width, bounds.height / size.height) |
| 122 | + |
| 123 | + // min must be set before zoom |
| 124 | + scrollView.minimumZoomScale = scale |
| 125 | + scrollView.zoomScale = scale |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @objc func onShare(sender: UIButton) { |
| 130 | + let alertTitle = "\(repo.owner)/\(repo.name):\(branch)" |
| 131 | + let alert = UIAlertController.configured(title: alertTitle, preferredStyle: .actionSheet) |
| 132 | + |
| 133 | + let alertBuilder = AlertActionBuilder { [weak self] in $0.rootViewController = self } |
| 134 | + var actions = [ |
| 135 | + viewHistoryAction(owner: repo.owner, repo: repo.name, branch: branch, client: client, path: path), |
| 136 | + AlertAction(alertBuilder).share([path.path], activities: nil, type: .shareFilePath) { |
| 137 | + $0.popoverPresentationController?.setSourceView(sender) |
| 138 | + } |
| 139 | + ] |
| 140 | + |
| 141 | + if let image = imageView.image { |
| 142 | + actions.append(AlertAction(alertBuilder).share([image], activities: nil, type: .shareContent) { |
| 143 | + $0.popoverPresentationController?.setSourceView(sender) |
| 144 | + }) |
| 145 | + } |
| 146 | + |
| 147 | + if let url = imageUrl { |
| 148 | + actions.append(AlertAction(alertBuilder).share([url], activities: [TUSafariActivity()], type: .shareUrl) { |
| 149 | + $0.popoverPresentationController?.setSourceView(sender) |
| 150 | + }) |
| 151 | + } |
| 152 | + actions.append(AlertAction.cancel()) |
| 153 | + |
| 154 | + if let name = self.path.components.last { |
| 155 | + actions.append(AlertAction(alertBuilder).share([name], activities: nil, type: .shareFileName) { |
| 156 | + $0.popoverPresentationController?.setSourceView(sender) |
| 157 | + }) |
| 158 | + } |
| 159 | + |
| 160 | + alert.addActions(actions) |
| 161 | + alert.popoverPresentationController?.setSourceView(sender) |
| 162 | + present(alert, animated: trueUnlessReduceMotionEnabled) |
| 163 | + } |
| 164 | + |
| 165 | + // MARK: EmptyViewDelegate |
| 166 | + |
| 167 | + func didTapRetry(view: EmptyView) { |
| 168 | + fetch() |
| 169 | + } |
| 170 | + |
| 171 | + // MARK: UIScrollViewDelegate |
| 172 | + |
| 173 | + func viewForZooming(in scrollView: UIScrollView) -> UIView? { |
| 174 | + return imageView |
| 175 | + } |
| 176 | + |
| 177 | + func scrollViewDidZoom(_ scrollView: UIScrollView) { |
| 178 | + let offsetX = max((scrollView.bounds.width - scrollView.contentSize.width) / 2, 0) |
| 179 | + let offsetY = max((scrollView.bounds.height - scrollView.contentSize.height) / 2, 0) |
| 180 | + imageView.center = CGPoint( |
| 181 | + x: scrollView.contentSize.width / 2 + offsetX, |
| 182 | + y: scrollView.contentSize.height / 2 + offsetY |
| 183 | + ) |
| 184 | + } |
| 185 | + |
| 186 | +} |
0 commit comments