Skip to content

Commit

Permalink
added download button on file preview screen (#511)
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitgoyalgloballogic authored Sep 2, 2022
1 parent 3bee875 commit 6a96dd3
Showing 1 changed file with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class FilePreviewViewController: SystemThemableViewController {
} else {
startPreviewingNode()
}

addDownloadContentButton()
}

override func viewWillAppear(_ animated: Bool) {
Expand Down Expand Up @@ -93,6 +95,84 @@ class FilePreviewViewController: SystemThemableViewController {
func allowInterfaceRotation() {
ControllerRotation.lockOrientation(.all)
}

private func addDownloadContentButton() {
let isContentAlreadyDownloaded = filePreviewViewModel?.isContentAlreadyDownloaded ?? false
if isContentAlreadyDownloaded {
let downloadButton = UIButton(type: .custom)
downloadButton.accessibilityIdentifier = "downloadButton"
downloadButton.frame = CGRect(x: 0.0, y: 0.0,
width: 30.0,
height: 30.0)
downloadButton.imageView?.contentMode = .scaleAspectFill
downloadButton.layer.masksToBounds = true
downloadButton.addTarget(self,
action: #selector(downloadButtonTapped),
for: UIControl.Event.touchUpInside)
downloadButton.setImage(UIImage(named: "ic-action-download"),
for: .normal)

let searchBarButtonItem = UIBarButtonItem(customView: downloadButton)
searchBarButtonItem.accessibilityIdentifier = "downloadBarButton"
let currWidth = searchBarButtonItem.customView?.widthAnchor.constraint(equalToConstant: 30.0)
currWidth?.isActive = true
let currHeight = searchBarButtonItem.customView?.heightAnchor.constraint(equalToConstant: 30.0)
currHeight?.isActive = true
self.navigationItem.rightBarButtonItem = searchBarButtonItem
}
}

@objc func downloadButtonTapped() {
let path = filePreviewViewModel?.listNode?.path ?? ""
let fileUrl = URL(fileURLWithPath: path)
displayActivityViewController(for: fileUrl)
}

private func displayActivityViewController(for url: URL) {
guard let presentationContext = UIViewController.applicationTopMostPresented else { return }

AlfrescoLog.debug("____ URL _____ \(url)")

let activityViewController =
UIActivityViewController(activityItems: [url],
applicationActivities: nil)
activityViewController.modalPresentationStyle = .popover

let clearController = UIViewController()
clearController.view.backgroundColor = .clear
clearController.modalPresentationStyle = .overCurrentContext

activityViewController.completionWithItemsHandler = { [weak self] (activity, success, _, _) in
guard let sSelf = self else { return }

activityViewController.dismiss(animated: true)
clearController.dismiss(animated: false) {
// Will not base check on error code as used constants have been deprecated
if activity?.rawValue == KeyConstants.Save.toCameraRoll && !success {
let privacyVC = PrivacyNoticeViewController.instantiateViewController()
privacyVC.viewModel = PrivacyNoticePhotosModel()
privacyVC.coordinatorServices = sSelf.coordinatorServices
presentationContext.present(privacyVC,
animated: true,
completion: nil)
}
}
}

if let popoverController = activityViewController.popoverPresentationController {
popoverController.sourceRect = presentationContext.view.bounds
popoverController.sourceView = presentationContext.view

popoverController.permittedArrowDirections = []
}

presentationContext.present(clearController,
animated: false) {
clearController.present(activityViewController,
animated: true,
completion: nil)
}
}

// MARK: - IBActions

Expand Down

0 comments on commit 6a96dd3

Please sign in to comment.