-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSearch.swift
54 lines (41 loc) · 1.47 KB
/
Search.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
//
// Search.swift
// Fetch
//
// Created by Stephen Radford on 08/08/2015.
// Copyright (c) 2015 Cocoon Development Ltd. All rights reserved.
//
import UIKit
import RealmSwift
public class Search {
/// The search term
public let term: String
/// The search delegate
public var delegate: SearchDelegate?
/// Search results
public var results: [File] = []
/// Has this already been saved?
public var isSaved: Bool {
get {
let t = self.term.stringByReplacingOccurrencesOfString("'", withString: "\\'", options: [], range: nil)
return (Putio.realm.objects(SavedSearch.self).filter("term = '\(t)'").count) > 0
}
}
public var savedSearch: SavedSearch {
get {
let t = self.term.stringByReplacingOccurrencesOfString("'", withString: "\\'", options: [], range: nil)
return Putio.realm.objects(SavedSearch.self).filter("term = '\(t)'")[0]
}
}
public init(term: String) {
self.term = term
}
/// Search Put.io
public func search(sender: UIViewController) {
let t = term.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
Files.fetchWithURL("\(Putio.api)files/search/\(t!)/page/-1", params: ["oauth_token": "\(Putio.accessToken!)"], sender: sender) { files in
self.results = files
self.delegate?.searchCompleted(results: self.results)
}
}
}