Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
macOS ARM Support + HiberFile MagicShare + Removed ask for review due…
Browse files Browse the repository at this point in the history
… to depreciation
  • Loading branch information
PlugNPush committed Oct 24, 2020
1 parent 02f9447 commit f3bcb89
Show file tree
Hide file tree
Showing 21 changed files with 475 additions and 24 deletions.
27 changes: 27 additions & 0 deletions HiberFile Input/CustomShareNavigationController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// CustomShareNavigationController.swift
// HiberFile Input
//
// Created by PlugN on 24/10/2020.
// Copyright © 2020 Nathan FALLET. All rights reserved.
//

import Foundation
import UIKit

// 1: Set the `objc` annotation
@objc(CustomShareNavigationController)
class CustomShareNavigationController: UINavigationController {

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)

// 2: set the ViewControllers
self.setViewControllers([CustomShareViewController()], animated: false)
}

@available(*, unavailable)
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
143 changes: 143 additions & 0 deletions HiberFile Input/CustomShareViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
//
// ShareViewController.swift
// HiberFile Input
//
// Created by PlugN on 24/10/2020.
// Copyright © 2020 Nathan FALLET. All rights reserved.
//

import UIKit
import MobileCoreServices

@objc(ShareExtensionViewController)
class CustomShareViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return selectOptions.count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return selectOptions[row].localized()
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selected = selectOptions[row]
}


let scrollView = UIScrollView()
//let contentView = UIView()
let select = UIPickerView()
var bottomConstraint: NSLayoutConstraint!
let selectOptions = ["1 heure", "1 jour", "3 jours", "7 jours", "Jamais"]
var selected = "1 heure"

override func viewDidLoad() {
super.viewDidLoad()

view.backgroundColor = .background
setupNavBar()

// Add views
view.addSubview(scrollView)

scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
bottomConstraint = scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
bottomConstraint.isActive = true


scrollView.addSubview(select)

select.translatesAutoresizingMaskIntoConstraints = false
select.topAnchor.constraint(equalTo: scrollView.layoutMarginsGuide.topAnchor, constant: 15).isActive = true
//select.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor, constant: 15).isActive = true
//select.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor, constant: -15).isActive = true
select.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor).isActive = true
//select.widthAnchor.constraint(equalToConstant: 300).isActive = true
//select.heightAnchor.constraint(equalToConstant: 150).isActive = true
select.delegate = self
select.dataSource = self

}

// 2: Set the title and the navigation items
private func setupNavBar() {
self.navigationItem.title = "HiberFile"

let itemCancel = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancelAction))
self.navigationItem.setLeftBarButton(itemCancel, animated: false)

let itemDone = UIBarButtonItem(title: "upload_generate".localized(), style: .done, target: self, action: #selector(doneAction))
self.navigationItem.setRightBarButton(itemDone, animated: false)

}

// 3: Define the actions for the navigation items
@objc private func cancelAction () {
let alert = UIAlertController(title: "upload_error".localized(), message: nil, preferredStyle: .alert)
let error = NSError(domain: "me.nathanfallet.HiberFile", code: 0, userInfo: [NSLocalizedDescriptionKey: "Canceled operation"])
alert.addAction(UIAlertAction(title: "copied_close".localized(), style: .default) { action in
self.extensionContext?.cancelRequest(withError: error)
})


}

@objc private func doneAction() {
self.handleSharedFile()
}

private func handleSharedFile() {
// extracting the path to the URL that is being shared

let attachments = (self.extensionContext?.inputItems.first as? NSExtensionItem)?.attachments ?? []
let contentType = kUTTypeData as String
for provider in attachments {
// Check if the content type is the same as we expected
if provider.hasItemConformingToTypeIdentifier(contentType) {
provider.loadItem(forTypeIdentifier: contentType,
options: nil) { [unowned self] (data, error) in
// Handle the error here if you want
guard error == nil else { return }

if let url = data as? URL,
let imageData = try? Data(contentsOf: url) {
self.save(data: imageData, url: url)
} else {
// Handle this situation as you prefer
cancelAction()
}
}}
}
}

private func save(data: Data, url: URL) {
// Generate a link
APIRequest("POST", path: "/send.php").with(body: ["time": self.selected]).uploadFile(file: data, name: url.lastPathComponent) { string, status in
// Check if request was sent
if let string = string {
// Show generated link
print(string)

// Add it to database
Database.current.addFile((string, url.lastPathComponent, Date()))

UIPasteboard.general.string = string

let alert = UIAlertController(title: "copied_title".localized(), message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "copied_close".localized(), style: .default) { action in
self.extensionContext!.completeRequest(returningItems: [], completionHandler: nil)
})
self.present(alert, animated: true, completion: nil)

}
}
}
}
12 changes: 12 additions & 0 deletions HiberFile Input/HiberFile Input.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
</dict>
</plist>
45 changes: 45 additions & 0 deletions HiberFile Input/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
<string>HiberFile</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationRule</key>
<dict>
<key>NSExtensionActivationSupportsAttachmentsWithMaxCount</key>
<integer>1</integer>
<key>NSExtensionActivationSupportsFileWithMaxCount</key>
<integer>1</integer>
<key>NSExtensionActivationSupportsImageWithMaxCount</key>
<integer>1</integer>
<key>NSExtensionActivationSupportsMovieWithMaxCount</key>
<integer>1</integer>
</dict>
</dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.share-services</string>
<key>NSExtensionPrincipalClass</key>
<string>CustomShareNavigationController</string>
</dict>
</dict>
</plist>
Loading

0 comments on commit f3bcb89

Please sign in to comment.