Created
February 18, 2017 06:12
-
-
Save suprie/87f3e3745a48abecbc8f4077d5576c8a to your computer and use it in GitHub Desktop.
Slightly better
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ProductDataAdapter { | |
private var database: FMDatabase? | |
init() { | |
var paths: [Any] = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) | |
var docsPath: String = paths[0] | |
var path: String = URL(fileURLWithPath: docsPath).appendingPathComponent("abc.sqlite").absoluteString | |
database = FMDatabase(path: path) | |
} | |
func getProducts() -> Array { | |
openConnection() | |
var resultSet: FMResultSet? = database.executeQuery("SELECT * FROM products") | |
var listOfProducts = [Any]() | |
while resultSet?.next() { | |
var dictionary: [Any: Any]? = [ | |
"code" : resultSet?.string(forColumn: "code"), | |
"name" : resultSet?.string(forColumn: "name"), | |
"id" : Int(resultSet?.int(forColumn: "id")) | |
] | |
listOfProducts.append(dictionary) | |
} | |
var sortByFav = NSSortDescriptor(key: "favorite", ascending: false) | |
var sortByName = NSSortDescriptor(key: "name", ascending: true) | |
return (listOfProducts as NSArray).sortedArray(using: [sortByFav, sortByName]) | |
} | |
private func openConnection() { | |
guard let database = database else { return } | |
database.open() | |
} | |
} | |
class ViewController { | |
private let dataAdapter: ProductDataAdapter! | |
private var arrayOfProducts: [NSDictionary]! | |
override func viewDidLoad(_ animated: Bool) { | |
super.viewDidLoad(); | |
dataAdapter = ProductDataAdapter() | |
} | |
override func viewDidAppear(_ animated: Bool) { | |
arrayOfProducts = dataAdapter.getProducts(); | |
setupView(); | |
self.aTableView.reloadData() | |
} | |
private func setupView() { | |
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "splash_bg")) | |
self.aTableView.backgroundColor = UIColor.clear | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment