2017-06-10 23:40:47 +08:00
//
// A c t i o n V i e w C o n t r o l l e r . s w i f t
// p a s s f o r i o s e x t e n s i o n
//
// C r e a t e d b y Y i s h i L i n o n 9 / 6 / 1 7 .
// C o p y r i g h t © 2 0 1 7 Y i s h i L i n . A l l r i g h t s r e s e r v e d .
//
import UIKit
import MobileCoreServices
2017-06-13 11:42:49 +08:00
import passKit
2017-06-10 23:40:47 +08:00
class ActionViewController : UIViewController {
@IBOutlet weak var textView : UITextView !
2017-06-13 11:42:49 +08:00
let passwordStore = PasswordStore . shared
2017-06-10 23:40:47 +08:00
override func viewDidLoad ( ) {
super . viewDidLoad ( )
let item = extensionContext ? . inputItems . first as ! NSExtensionItem
let provider = item . attachments ? . first as ! NSItemProvider
let propertyList = String ( kUTTypePropertyList )
if provider . hasItemConformingToTypeIdentifier ( propertyList ) {
provider . loadItem ( forTypeIdentifier : propertyList , options : nil , completionHandler : { ( item , error ) -> Void in
let dictionary = item as ! NSDictionary
let results = dictionary [ NSExtensionJavaScriptPreprocessingResultsKey ] as ! NSDictionary
2017-06-13 11:42:49 +08:00
let url = URL ( string : ( results [ " url " ] as ? String ) ! ) ? . host
let numberFormatter = NumberFormatter ( )
numberFormatter . numberStyle = NumberFormatter . Style . decimal
let numberOfPasswordsString = " Number of password: " + numberFormatter . string ( from : NSNumber ( value : self . passwordStore . numberOfPasswords ) ) !
let sizeOfRepositoryString = " Size of repo: " + ByteCountFormatter . string ( fromByteCount : Int64 ( self . passwordStore . sizeOfRepositoryByteCount ) , countStyle : ByteCountFormatter . CountStyle . file )
var numberOfCommits : UInt = 0
do {
if let _ = try self . passwordStore . storeRepository ? . currentBranch ( ) . oid {
numberOfCommits = self . passwordStore . storeRepository ? . numberOfCommits ( inCurrentBranch : NSErrorPointer ( nilLiteral : ( ) ) ) ? ? 0
}
} catch {
print ( error )
}
let numberOfCommitsString = " Number of commits: " + numberFormatter . string ( from : NSNumber ( value : numberOfCommits ) ) !
let gitURL = SharedDefaults [ . gitURL ] !
2017-06-10 23:40:47 +08:00
DispatchQueue . main . async { [ weak self ] in
2017-06-13 11:42:49 +08:00
self ? . textView . text = url !
print ( numberOfPasswordsString )
print ( numberOfCommitsString )
print ( sizeOfRepositoryString )
print ( gitURL )
2017-06-10 23:40:47 +08:00
}
} )
} else {
print ( " error " )
}
}
override func didReceiveMemoryWarning ( ) {
super . didReceiveMemoryWarning ( )
// D i s p o s e o f a n y r e s o u r c e s t h a t c a n b e r e c r e a t e d .
}
@IBAction func done ( ) {
// R e t u r n a n y e d i t e d c o n t e n t t o t h e h o s t a p p .
// T h i s t e m p l a t e d o e s n ' t d o a n y t h i n g , s o w e j u s t e c h o t h e p a s s e d i n i t e m s .
self . extensionContext ! . completeRequest ( returningItems : self . extensionContext ! . inputItems , completionHandler : nil )
}
}