-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableView.swift
More file actions
369 lines (228 loc) · 11.3 KB
/
TableView.swift
File metadata and controls
369 lines (228 loc) · 11.3 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
//
// TableView.swift
// GitApp
//
// Created by Fernando on 4/28/15.
// Copyright (c) 2015 Fernando. All rights reserved.
//
import UIKit
import CoreData
class TableView: UIViewController, UITableViewDataSource, UITableViewDelegate {
let managedObjectContext =
(UIApplication.sharedApplication().delegate
as! AppDelegate).managedObjectContext
var newReposFromJSON = NSMutableArray()
@IBOutlet weak var table: UITableView!
var reposit = NSMutableArray()
var reposit2 = NSArray()
var reposit3 = NSArray()
var interReposit = NSArray()
override func viewDidLoad() {
super.viewDidLoad()
let defaults = NSUserDefaults.standardUserDefaults()
var nome:NSString = defaults.stringForKey("nome")! as NSString!
table.delegate = self
table.dataSource = self
if(hasConnectivity()){
println("acceso a internet")
addUser(nome as String)
reposit3 = Manager.sharedInstance.getAllUserRepos(nome)
reposit2 = Manager.sharedInstance.getAllUserRepos("mackmobile")
for repo1 in reposit3{
for repo2 in reposit2{
if((repo1 as! NSString) as NSString == (repo2 as! NSString) as NSString){
reposit.addObject(repo1)
}
}
}
var reposFromBD = getReposBD(nome as String)
for a in reposFromBD {
if let repo: AnyObject = a.valueForKey("repoName"){
println(repo)
}
}
for reposJson in reposit{
var existInBd = Bool()
existInBd = false
for reposBD in reposFromBD{
if let repo: AnyObject = reposBD.valueForKey("repoName"){
if((repo as! NSString) as NSString == (reposJson as! NSString) as NSString){
existInBd = true
}
}
}
if(existInBd == false){
println("O repos \(reposJson) não existia no banco")
newReposFromJSON.addObject(reposJson)
}
}
addRepoBD(nome as String, repos: newReposFromJSON)
}else{
println("sem acesso a internet ")
var reposBD = getReposBD(nome as String)
for repo in reposBD{
if let repoName: AnyObject = repo.valueForKey("repoName"){
println("\(repoName) nome do repo")
reposit.addObject(repoName)
}
}
}
}
func getReposBD(username: String) -> NSSet {
var arrayOfRepos = NSMutableArray()
var error: NSError? = nil
var fReq = NSFetchRequest(entityName: "User")
fReq.predicate = NSPredicate(format:"username ==%@", username)
var results = managedObjectContext!.executeFetchRequest(fReq, error:&error)
if let result: AnyObject = results{
var UserResult = result[0] as! User
if let user: AnyObject = result.valueForKey("username") {
println(user)
return UserResult.repos
}
}
return NSSet()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func addRepoBD(username: String, repos: NSMutableArray) {
var error: NSError? = nil
var fReq = NSFetchRequest(entityName: "User")
fReq.predicate = NSPredicate(format:"username ==%@", username)
var result = managedObjectContext!.executeFetchRequest(fReq, error:&error)
if(result?.count > 0){
for resultItem in result! {
var UserResult = resultItem as! User
if(username == UserResult.username){
var newii000 = repos
for ni in repos{
var newRepo: Repo = NSEntityDescription.insertNewObjectForEntityForName("Repo", inManagedObjectContext: managedObjectContext!) as! Repo
newRepo.repoName = ni as! String
newRepo.user = UserResult
var error: NSError?
managedObjectContext!.save(&error)
}
}
}
}else{
var user: User = NSEntityDescription.insertNewObjectForEntityForName("User", inManagedObjectContext: managedObjectContext!) as! User
user.username = username
var newii000 = repos
for ni in repos{
var newRepo: Repo = NSEntityDescription.insertNewObjectForEntityForName("Repo", inManagedObjectContext: managedObjectContext!) as! Repo
newRepo.repoName = ni as! String
newRepo.user = user
}
var error: NSError?
managedObjectContext!.save(&error)
}
}
func hasConnectivity() -> Bool {
var Status:Bool = false
let url = NSURL(string: "http://google.com/")
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "HEAD"
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData
request.timeoutInterval = 10.0
var response: NSURLResponse?
var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: nil) as NSData?
if let httpResponse = response as? NSHTTPURLResponse {
if httpResponse.statusCode == 200 {
Status = true
}
}
return Status
}
// MARK: - Table view data source
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return reposit.count
}
func addUser(username: String) {
var error: NSError? = nil
var fReq = NSFetchRequest(entityName: "User")
fReq.predicate = NSPredicate(format:"username ==%@", username)
var result = managedObjectContext!.executeFetchRequest(fReq, error:&error)
if ((result?.count == 0) || (result?.count == nil)){
let entityDescription =
NSEntityDescription.entityForName("User",
inManagedObjectContext: managedObjectContext!)
let newUser = User(entity: entityDescription!,
insertIntoManagedObjectContext: managedObjectContext)
newUser.username = username
var error: NSError?
managedObjectContext?.save(&error)
if let err = error {
println("erro")
} else {
println("Usuário \(username) salvo no coredata")
}
}
else{
println("usuario \(username) existe")
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel!.text = reposit.objectAtIndex((indexPath.row)) as? String
return cell
}
override func viewDidAppear(animated: Bool) {
table.reloadData()
}
/*
// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the specified item to be editable.
return true
}
*/
/*
// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// Delete the row from the data source
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the item to be re-orderable.
return true
}
*/
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if (segue.identifier == "descri"){
if let cel = sender as? UITableViewCell{
let indexPath = table.indexPathForCell(cel)?.row
if let destination = segue.destinationViewController as? DescView {
destination.activateNotificationCenter()
let notification: NSNotificationCenter = NSNotificationCenter.defaultCenter()
var mensagem : NSDictionary = NSDictionary(object: (self.reposit.objectAtIndex(indexPath!) as? String)!, forKey: "Mensagem")
notification.postNotificationName("novoNome", object: self, userInfo: mensagem as [NSObject : AnyObject])
}
}
}
}
}