PhotoRank/PhotoRank/Final Rankings/RankingsTableViewController...

112 lines
3.5 KiB
Swift

//
// RankingsTableViewController.swift
// PhotoRank
//
// Created by Shadowfacts on 8/5/19.
// Copyright © 2019 Shadowfacts. All rights reserved.
//
import UIKit
import Photos
private let reuseIdentifier = "AssetScoreCell"
class RankingsTableViewController: UITableViewController {
var rankings: [(PHAsset, Int)]
init(rankings: [(PHAsset, Int)]) {
self.rankings = rankings
super.init(nibName: "RankingsTableViewController", bundle: .main)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: "RankingTableViewCell", bundle: .main), forCellReuseIdentifier: reuseIdentifier)
tableView.rowHeight = 96
tableView.alwaysBounceVertical = true
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return rankings.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) as! RankingTableViewCell
let (asset, score) = rankings[indexPath.row]
cell.updateUI(asset: asset, score: score)
return cell
}
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false 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, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
guard editingStyle == .delete else { return }
let (asset, _) = rankings[indexPath.row]
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.deleteAssets(NSArray(object: asset))
}) { (success, error) in
let generator = UINotificationFeedbackGenerator()
if success {
generator.notificationOccurred(.success)
DispatchQueue.main.async {
self.rankings.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: .fade)
}
} else {
generator.notificationOccurred(.error)
print("Error occurred deleting asset: \(String(describing: error))")
}
}
}
/*
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
// Return false 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 prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}