Switches to Core Data model objects

This commit is contained in:
Riley Testut
2019-05-20 21:24:53 +02:00
parent 65a8414727
commit c3a8abf8dc
13 changed files with 272 additions and 121 deletions

View File

@@ -32,7 +32,6 @@ class AppDetailViewController: UITableViewController
private lazy var screenshotsDataSource = self.makeScreenshotsDataSource()
@IBOutlet private var nameLabel: UILabel!
@IBOutlet private var subtitleLabel: UILabel!
@IBOutlet private var developerButton: UIButton!
@IBOutlet private var appIconImageView: UIImageView!
@@ -73,8 +72,7 @@ private extension AppDetailViewController
func update()
{
self.nameLabel.text = self.app.name
self.subtitleLabel.text = self.app.subtitle
self.developerButton.setTitle(self.app.developer, for: .normal)
self.developerButton.setTitle(self.app.developerName, for: .normal)
self.appIconImageView.image = UIImage(named: self.app.iconName)
let text = String(format: NSLocalizedString("Download %@", comment: ""), self.app.name)

View File

@@ -11,7 +11,7 @@ import UIKit
@objc class AppTableViewCell: UITableViewCell
{
@IBOutlet var nameLabel: UILabel!
@IBOutlet var subtitleLabel: UILabel!
@IBOutlet var developerLabel: UILabel!
@IBOutlet var appIconImageView: UIImageView!
@IBOutlet var button: UIButton!

View File

@@ -23,6 +23,13 @@ class AppsViewController: UITableViewController
self.tableView.tableFooterView = UIView()
}
override func viewWillAppear(_ animated: Bool)
{
super.viewWillAppear(animated)
self.fetchApps()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
guard segue.identifier == "showAppDetail" else { return }
@@ -38,27 +45,45 @@ class AppsViewController: UITableViewController
private extension AppsViewController
{
func makeDataSource() -> RSTArrayTableViewDataSource<App>
func makeDataSource() -> RSTFetchedResultsTableViewDataSource<App>
{
let appsFileURL = Bundle.main.url(forResource: "Apps", withExtension: "plist")!
let fetchRequest = App.fetchRequest() as NSFetchRequest<App>
fetchRequest.relationshipKeyPathsForPrefetching = [#keyPath(InstalledApp.app)]
fetchRequest.sortDescriptors = [NSSortDescriptor(keyPath: \App.name, ascending: true)]
fetchRequest.returnsObjectsAsFaults = false
do
{
let data = try Data(contentsOf: appsFileURL)
let apps = try PropertyListDecoder().decode([App].self, from: data)
let dataSource = RSTFetchedResultsTableViewDataSource(fetchRequest: fetchRequest, managedObjectContext: DatabaseManager.shared.viewContext)
dataSource.cellConfigurationHandler = { (cell, app, indexPath) in
let cell = cell as! AppTableViewCell
cell.nameLabel.text = app.name
cell.developerLabel.text = app.developerName
cell.appIconImageView.image = UIImage(named: app.iconName)
let dataSource = RSTArrayTableViewDataSource(items: apps)
dataSource.cellConfigurationHandler = { (cell, app, indexPath) in
let cell = cell as! AppTableViewCell
cell.nameLabel.text = app.name
cell.subtitleLabel.text = app.subtitle
cell.appIconImageView.image = UIImage(named: app.iconName)
}
return dataSource
}
catch
{
fatalError("Failed to load apps. \(error)")
return dataSource
}
func fetchApps()
{
DatabaseManager.shared.persistentContainer.performBackgroundTask { (context) in
let appsFileURL = Bundle.main.url(forResource: "Apps", withExtension: "json")!
do
{
let data = try Data(contentsOf: appsFileURL)
let decoder = JSONDecoder()
decoder.managedObjectContext = context
_ = try decoder.decode([App].self, from: data)
try context.save()
}
catch
{
fatalError("Failed to save fetched apps. \(error)")
}
}
}
}