@@ -12,17 +12,96 @@ import VisionKit
1212
1313class ScanVC : UIViewController , UIImagePickerControllerDelegate & UINavigationControllerDelegate , UICollectionViewDelegate , UICollectionViewDataSource {
1414
15+ var workingDirectory : URL ?
16+ var allFiles : [ File ] = [ ]
17+
18+ @IBOutlet var InstructionView : UIView !
19+ @IBOutlet weak var collectionView : UICollectionView !
1520 @IBOutlet var emptyView : UIView !
1621 @IBOutlet weak var addBttn : UIBarButtonItem !
1722
18- let imgArr = [ " FolderIconPurple " , " FolderIconPink " , " FolderIconOrange " ]
19- let nameArr = [ " Document 1 " , " Document 2 " , " Document 3 " ]
20- let dateArr = [ " 17.10.2020 " , " 10.10.2020 " , " 11.10.2020 " ]
23+ //MARK: - Create directory
24+ func gotoDirectory( directory: URL ) {
25+ if ( directory. lastPathComponent == " DocScanner " ) {
26+ self . navigationItem. leftBarButtonItem = nil
27+ self . title = " HOME "
28+ } else {
29+ //self.navigationItem.leftBarButtonItem = self.barBackButton
30+ self . title = directory. lastPathComponent. uppercased ( )
31+ }
32+ self . workingDirectory = directory
33+ reloadTableData ( )
34+ }
35+
36+ //MARK: -
2137
2238 @IBAction func showaddTapped( _ sender: Any ) {
23- showCamera ( )
39+
40+ AlertUtil . shared. alertWithTextField ( parent: self , title: " Add Folder " , message: " Enter folder name to add new folder " , placeholder: " Folder Name " , value: " " , proceedTitle: " Add " , cancelTitle: " Later " , didProceed: { ( folderName) in
41+ let errorMessage = FileUtility . shared. createFolder ( directory: self . workingDirectory!, name: folderName)
42+ if ( errorMessage != " " ) {
43+ self . presentAlert ( title: " Failed " , message: errorMessage)
44+ } else {
45+ // self.presentAlert(title: "Success", message: "Folder added successfully!")
46+ self . reloadTableData ( )
47+ }
48+ } ) {
49+ print ( " canceled " )
50+ }
51+ }
52+ override func viewDidAppear( _ animated: Bool ) {
53+ if let directory = self . workingDirectory{
54+ self . allFiles = FileUtility . shared. scanDirectory ( directory: directory)
55+ } else {
56+ self . workingDirectory = FileUtility . shared. defaultPath
57+ self . allFiles = FileUtility . shared. scanDirectory ( directory: self . workingDirectory!)
58+ }
59+ self . collectionView. reloadData ( )
60+ }
61+
62+ func reloadTableData( ) {
63+ self . allFiles. removeAll ( )
64+ self . allFiles = FileUtility . shared. scanDirectory ( directory: self . workingDirectory!)
65+ self . collectionView. reloadData ( )
66+ }
67+
68+ //MARK:- To create the collectionView on the page
69+ func numberOfSections( in collectionView: UICollectionView ) -> Int {
70+ return 1
71+ }
72+ func collectionView( _ collectionView: UICollectionView , numberOfItemsInSection section: Int ) -> Int {
73+ return self . allFiles. count
74+ }
75+
76+ func collectionView( _ collectionView: UICollectionView , cellForItemAt indexPath: IndexPath ) -> UICollectionViewCell {
77+ let cell : FoldersCVCell = collectionView. dequeueReusableCell ( withReuseIdentifier: " FoldersCVCell " , for: indexPath) as! FoldersCVCell
78+ //cell.imageView.image = UIImage(named: imgArr[indexPath.row])
79+ //cell.dateLabel.text = dateArr[indexPath.row]
80+ //cell.documentName.text = nameArr[indexPath.row]
81+
82+ //cell.imageView?.image = self.allFiles[indexPath.row].image
83+ cell. documentName? . text = self . allFiles [ indexPath. row] . name
84+ cell. dateLabel? . text = self . allFiles [ indexPath. row] . size
85+
86+ //cell.menuBttn.addTarget(self, action: #selector(showCamera), for: .touchUpInside)
87+ cell. layer. cornerRadius = 20
88+ return cell
2489 }
2590
91+ func collectionView( _ collectionView: UICollectionView , didSelectItemAt indexPath: IndexPath ) {
92+ let selectedFile = self . allFiles [ indexPath. row]
93+ switch selectedFile. type{
94+ case . pdf:
95+ self . presentFile ( path: selectedFile. path)
96+ break
97+ default :
98+ self . gotoDirectory ( directory: selectedFile. url)
99+ break
100+ }
101+ }
102+
103+ // Deprecated
104+ /*
26105 @objc func showCamera(){
27106 let actionSheet = UIAlertController(title: "Select Photo", message: "Where do you want to select a photo?", preferredStyle: .actionSheet)
28107
@@ -48,7 +127,7 @@ class ScanVC: UIViewController, UIImagePickerControllerDelegate & UINavigationCo
48127
49128 self.present(actionSheet, animated: true, completion: nil)
50129 }
51-
130+ */
52131 override func viewDidLoad( ) {
53132 super. viewDidLoad ( )
54133 // To hide the top line
@@ -59,20 +138,7 @@ class ScanVC: UIViewController, UIImagePickerControllerDelegate & UINavigationCo
59138 setUpMenu ( )
60139 }
61140
62- //MARK:- To create the collectionView on the page
63- func collectionView( _ collectionView: UICollectionView , numberOfItemsInSection section: Int ) -> Int {
64- return 3
65- }
66-
67- func collectionView( _ collectionView: UICollectionView , cellForItemAt indexPath: IndexPath ) -> UICollectionViewCell {
68- let cell : FoldersCVCell = collectionView. dequeueReusableCell ( withReuseIdentifier: " FoldersCVCell " , for: indexPath) as! FoldersCVCell
69- cell. imageView. image = UIImage ( named: imgArr [ indexPath. row] )
70- cell. dateLabel. text = dateArr [ indexPath. row]
71- cell. documentName. text = nameArr [ indexPath. row]
72- cell. menuBttn. addTarget ( self , action: #selector( showCamera) , for: . touchUpInside)
73- cell. layer. cornerRadius = 20
74- return cell
75- }
141+
76142 //MARK:- Set up uimenu Button
77143
78144 @objc func setUpMenu( ) {
@@ -103,32 +169,17 @@ class ScanVC: UIViewController, UIImagePickerControllerDelegate & UINavigationCo
103169 }
104170 }
105171
106- //MARK:- Set up scanner
107- private func configureDocumentView( ) {
108- let scanningDocumentVC = VNDocumentCameraViewController ( )
109- scanningDocumentVC. delegate = self
110- self . present ( scanningDocumentVC, animated: true , completion: nil )
111- }
112-
113172}
114173
115- extension ScanVC : VNDocumentCameraViewControllerDelegate {
116-
117- func documentCameraViewController( _ controller: VNDocumentCameraViewController , didFinishWith scan: VNDocumentCameraScan ) {
118- for pageNumber in 0 ..< scan. pageCount {
119- let image = scan. imageOfPage ( at: pageNumber)
120- print ( image)
121- }
122- controller. dismiss ( animated: true , completion: nil )
123- }
124-
125- func documentCameraViewControllerDidCancel( _ controller: VNDocumentCameraViewController ) {
126- controller. dismiss ( animated: true )
174+
175+ extension ScanVC : UIDocumentInteractionControllerDelegate {
176+ func presentFile( path: String ) {
177+ let documentInteractionController = UIDocumentInteractionController ( url: URL ( fileURLWithPath: path) )
178+ documentInteractionController. delegate = self
179+ documentInteractionController. presentPreview ( animated: true )
127180 }
128-
129- func documentCameraViewController( _ controller: VNDocumentCameraViewController , didFailWithError error: Error ) {
130- print ( error)
131-
132- controller. dismiss ( animated: true )
181+ //MARK: UIDocumentInteractionController delegates
182+ func documentInteractionControllerViewControllerForPreview( _ controller: UIDocumentInteractionController ) -> UIViewController {
183+ return self
133184 }
134185}
0 commit comments