ソースを参照

Add BKPasscode

Marino Faggiana 6 年 前
コミット
41f2c51f92
1 ファイル変更120 行追加18 行削除
  1. 120 18
      iOSClient/Select/NCSelect.swift

+ 120 - 18
iOSClient/Select/NCSelect.swift

@@ -27,7 +27,7 @@ import Foundation
     @objc func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String)
 }
 
-class NCSelect: UIViewController ,UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIGestureRecognizerDelegate, NCListCellDelegate, NCGridCellDelegate, NCSectionHeaderMenuDelegate, DropdownMenuDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
+class NCSelect: UIViewController ,UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIGestureRecognizerDelegate, NCListCellDelegate, NCGridCellDelegate, NCSectionHeaderMenuDelegate, DropdownMenuDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate, BKPasscodeViewControllerDelegate {
     
     @IBOutlet fileprivate weak var collectionView: UICollectionView!
     @IBOutlet fileprivate weak var toolbar: UIToolbar!
@@ -54,6 +54,10 @@ class NCSelect: UIViewController ,UICollectionViewDataSource, UICollectionViewDe
     
     private let appDelegate = UIApplication.shared.delegate as! AppDelegate
     
+    private var serverUrlPush = ""
+    private var directoryIDPush = ""
+    private var metadataPush: tableMetadata?
+    
     private var isEditMode = false
     private var networkInProgress = false
     private var selectFileID = [String]()
@@ -77,6 +81,10 @@ class NCSelect: UIViewController ,UICollectionViewDataSource, UICollectionViewDe
     
     private let refreshControl = UIRefreshControl()
     
+    //BKPasscodeViewController
+    private var failedAttempts: Double = 0
+    private var lockUntilDate: NSDate?
+    
     override func viewDidLoad() {
         super.viewDidLoad()
         
@@ -189,6 +197,54 @@ class NCSelect: UIViewController ,UICollectionViewDataSource, UICollectionViewDe
         return true
     }
     
+    // MARK: BKPASSCODEVIEWCONTROLLER
+    
+    func passcodeViewController(_ aViewController: BKPasscodeViewController!, didFinishWithPasscode aPasscode: String!) {
+        aViewController.dismiss(animated: true, completion: nil)
+        performSegueDirectoryWithControlPasscode(controlPasscode: false)
+    }
+    
+    func passcodeViewController(_ aViewController: BKPasscodeViewController!, authenticatePasscode aPasscode: String!, resultHandler aResultHandler: ((Bool) -> Void)!) {
+        if aPasscode == CCUtility.getBlockCode() {
+            failedAttempts = 0
+            lockUntilDate = nil
+            aResultHandler(true)
+        } else {
+            aResultHandler(false)
+        }
+    }
+    
+    func passcodeViewControllerDidFailAttempt(_ aViewController: BKPasscodeViewController!) {
+        failedAttempts += 1
+        if failedAttempts > 5 {
+            var timeInterval: TimeInterval = 60
+            if failedAttempts > 6 {
+                let multiplier: Double = failedAttempts - 6
+                timeInterval = (5 * 60) * multiplier
+                if timeInterval > 3600 * 24 {
+                    timeInterval = 3600 * 24
+                }
+            }
+            lockUntilDate = NSDate.init(timeIntervalSinceNow: timeInterval)
+        }
+    }
+    
+    func passcodeViewControllerNumber(ofFailedAttempts aViewController: BKPasscodeViewController!) -> UInt {
+        return UInt(failedAttempts)
+    }
+    
+    func passcodeViewControllerLock(untilDate aViewController: BKPasscodeViewController!) -> Date! {
+        if lockUntilDate == nil {
+            return Date()
+        } else {
+            return lockUntilDate! as Date
+        }
+    }
+    
+    @objc func passcodeViewCloseButtonPressed(_ sender: Any) {
+        self.dismiss(animated: true, completion: nil)
+    }
+    
     // MARK: ACTION
     
     @IBAction func actionCancel(_ sender: Any) {
@@ -741,25 +797,12 @@ class NCSelect: UIViewController ,UICollectionViewDataSource, UICollectionViewDe
             guard let directoryIDPush = NCManageDatabase.sharedInstance.getDirectoryID(serverUrlPush) else {
                 return
             }
-            guard let visualController = UIStoryboard(name: "NCSelect", bundle: nil).instantiateViewController(withIdentifier: "NCSelect.storyboard") as? NCSelect else {
-                return
-            }
-            
-            visualController.delegate = delegate
             
-            visualController.hideButtonCreateFolder = hideButtonCreateFolder
-            visualController.selectFile = selectFile
-            visualController.includeDirectoryE2EEncryption = includeDirectoryE2EEncryption
-            visualController.includeImages = includeImages
-            visualController.type = type
-            visualController.titleButtonDone = titleButtonDone
-            visualController.layoutViewSelect = layoutViewSelect
-
-            visualController.titleCurrentFolder = metadata.fileNameView
-            visualController.serverUrl = serverUrlPush
-            visualController.directoryID = directoryIDPush
+            self.serverUrlPush = serverUrlPush
+            self.directoryIDPush = directoryIDPush
+            self.metadataPush = metadata
             
-            self.navigationController?.pushViewController(visualController, animated: true)
+            performSegueDirectoryWithControlPasscode(controlPasscode: true)
             
         } else {
             
@@ -768,6 +811,65 @@ class NCSelect: UIViewController ,UICollectionViewDataSource, UICollectionViewDe
         }
     }
     
+    // MARK: NAVIGATION
+    
+    private func performSegueDirectoryWithControlPasscode(controlPasscode: Bool) {
+        
+        guard let directoryPush = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "directoryID == %@", directoryIDPush))  else {
+            return
+        }
+        
+        if directoryPush.lock == true && CCUtility.getBlockCode() != nil && (CCUtility.getBlockCode()?.count)! > 0 && controlPasscode {
+            
+            let viewController = CCBKPasscode.init(nibName: nil, bundle: nil)
+            guard let touchIDManager = BKTouchIDManager.init(keychainServiceName: k_serviceShareKeyChain) else {
+                return
+            }
+            touchIDManager.promptText = NSLocalizedString("_scan_fingerprint_", comment: "")
+
+            viewController.delegate = self
+            viewController.type = BKPasscodeViewControllerCheckPasscodeType
+            viewController.inputViewTitlePassword = true
+            if CCUtility.getSimplyBlockCode() {
+                viewController.passcodeStyle = BKPasscodeInputViewNumericPasscodeStyle
+                viewController.passcodeInputView.maximumLength = 6
+            } else {
+                viewController.passcodeStyle = BKPasscodeInputViewNormalPasscodeStyle
+                viewController.passcodeInputView.maximumLength = 64
+            }
+            viewController.touchIDManager = touchIDManager
+            viewController.title = NSLocalizedString("_folder_blocked_", comment: "")
+            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonItem.SystemItem.cancel, target: self, action: #selector(passcodeViewCloseButtonPressed(_:)))
+            viewController.navigationItem.leftBarButtonItem?.tintColor = UIColor.black
+            
+            let navigationController = UINavigationController.init(rootViewController: viewController)
+            self.present(navigationController, animated: true, completion: nil)
+            
+            return
+        }
+        
+        guard let visualController = UIStoryboard(name: "NCSelect", bundle: nil).instantiateViewController(withIdentifier: "NCSelect.storyboard") as? NCSelect else {
+            return
+        }
+        
+        visualController.delegate = delegate
+        
+        visualController.hideButtonCreateFolder = hideButtonCreateFolder
+        visualController.selectFile = selectFile
+        visualController.includeDirectoryE2EEncryption = includeDirectoryE2EEncryption
+        visualController.includeImages = includeImages
+        visualController.type = type
+        visualController.titleButtonDone = titleButtonDone
+        visualController.layoutViewSelect = layoutViewSelect
+        
+        visualController.titleCurrentFolder = metadataPush!.fileNameView
+        visualController.serverUrl = serverUrlPush
+        visualController.directoryID = directoryIDPush
+        
+        self.navigationController?.pushViewController(visualController, animated: true)
+        
+    }
+    
     // MARK: UTILITY
     
     private func cellBlurEffect(with frame: CGRect) -> UIView {