-
Notifications
You must be signed in to change notification settings - Fork 7
/
TabTwoViewController.swift
196 lines (145 loc) · 6.9 KB
/
TabTwoViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//
// TabTwoViewController.swift
// DUPME
//
// Created by Hossein on 06/12/2016.
// Copyright © 2016 Dupify. All rights reserved.
//
//
// TAKE PICTURE SECTION
import UIKit
import ImagePicker
import Firebase
import LBTAComponents
class TabTwoViewController: DatasourceController , UINavigationBarDelegate, UserCellDelegate {
let databaseRef = FIRDatabase.database().reference()
let storageRef = FIRStorage.storage().reference()
var fullListOfOrders = [Order]()
var downloadedModels = [IndexPath: Model]()
override func viewDidLoad() {
super.viewDidLoad()
setupNavBar()
fetchOrders()
setupProfressView()
}
override func viewWillAppear(_ animated: Bool) {
print(downloadedModels)
}
override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: 100)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: view.frame.width, height: 50)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
return CGSize(width: view.frame.width, height: 50)
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// print(downloadedModels)
// let model = downloadedModels[indexPath]
// print(model)
// var orderCell = datasource?.item(indexPath) as! Order
// let texturePath = orderCell.textureURL
// //if there exists a file for texture url, then definitely there is file for module url.
// print(orderCell)
//
// if localFileExists(atPath: texturePath!) {
// let modelurl = URL(string: orderCell.modelURL!)
// let textureurl = URL(string: texturePath!)
// showModel(modelURL: modelurl!, textureURL: textureurl!)
// }
collectionView.deselectItem(at: indexPath, animated: true)
// code for how to remove item
// let userID = FIRAuth.auth()!.currentUser!.uid
// let ref = databaseRef.child("users/\(userID)/orders/\(key)")
// ref.removeValue()
}
func setupNavBar(){
navigationItem.title = "New Model"
navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "upload"), style: .plain, target: self, action: #selector(handleUpload))
navigationItem.rightBarButtonItem?.tintColor = UIColor.black
}
var progressView: UIProgressView!
func setupProfressView(){
progressView = UIProgressView(progressViewStyle: .default)
progressView.center = CGPoint(x: view.bounds.width / 2 , y: 1)
progressView.bounds = CGRect(x: 0, y: 0, width: view.bounds.width, height: 50)
progressView.backgroundColor = .red
progressView.progressTintColor = UIColor(r: 147, g: 62, b: 197)
progressView.trackTintColor = UIColor(r: 217, g: 217, b: 217)
view.addSubview(progressView)
}
func handleUpload(){
let imagePicker = ImagePickerController()
imagePicker.delegate = self
// imagePicker.imageLimit = 20
present(imagePicker, animated: true, completion: nil)
}
func localFileExists(atPath path: String) -> Bool {
var isDir : ObjCBool = false
return FileManager.default.fileExists(atPath: path, isDirectory: &isDir)
}
func localPath(forURL address: String) -> URL {
let ref = FIRStorage.storage().reference(forURL: address)
let fileManager = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
guard let localPath = fileManager?.appendingPathComponent(ref.name) else {
fatalError("cannot create local path for given URL address")
}
return localPath
}
func showModel(modelURL: URL, textureURL: URL){
let modelViewController = ModelViewerController()
modelViewController.modelURL = modelURL
modelViewController.textureURL = textureURL
navigationController?.pushViewController(modelViewController, animated: true)
}
func downloadModel(forCell cell: UserCell, modelURL: String, textureURL: String, completion: @escaping ((Model) ->())) {
// Download .obj file
let modelRef = FIRStorage.storage().reference(forURL: modelURL)
let modelPath = localPath(forURL: modelURL)
modelRef.write(toFile: modelPath) { (modelurl, error) in
if error != nil {
print(error.debugDescription)
return
}
// Download texture
let textureRef = FIRStorage.storage().reference(forURL: textureURL)
let texturePath = self.localPath(forURL: textureURL)
textureRef.write(toFile: texturePath) { (textureurl, err) in
if err != nil {
print(error.debugDescription)
return
}
let model = Model(modelURL: modelPath, textureURL: texturePath)
completion(model)
}
}
//attach observers for failure and download progress
}
// Mark - UserCell Delegate
func downloadClicked(_ cell: UserCell) {
let mycollectionView = cell.superview as! UICollectionView
let indexPath = mycollectionView.indexPath(for: cell)
let orderCell = cell.controller?.datasource?.item(indexPath!) as! Order
let key = orderCell.key
let userID = FIRAuth.auth()!.currentUser!.uid
let ref = FIRDatabase.database().reference().child("users/\(userID)/orders/\(key)")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as! NSDictionary
let modelURL = value["modelURL"] as! String
let textureURL = value["textureURL"] as! String
self.downloadModel(forCell: cell, modelURL: modelURL, textureURL: textureURL, completion: { (model) in
self.downloadedModels[indexPath!] = model
print(self.downloadedModels)
// print(orderCell)
// orderCell.modelURL = urls[0].path
// orderCell.textureURL = urls[1].path
// orderCell.imageCount = 100
})
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}