[ABANDONED] Mastodon iOS client.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
4.6 KiB

//
// TimelineViewController.swift
// elpha-ios
//
// Created by Dwayne Harris on 10/28/18.
// Copyright © 2018 Elpha. All rights reserved.
//
import CoreData
import UIKit
protocol TimelinesViewControllerDelegate: class {
func didSelect(_ sender: Any, timeline: TimelineMO)
}
class TimelinesViewController: UIViewController {
@IBOutlet var tableView: UITableView!
weak var delegate: TimelinesViewControllerDelegate?
var fetchedResultsController: NSFetchedResultsController<TimelineMO>?
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
tableView.layer.cornerRadius = 20
tableView.layer.masksToBounds = true
initializeFetchedResultsController()
}
func initializeFetchedResultsController() {
let request = NSFetchRequest<TimelineMO>(entityName: "Timeline")
request.sortDescriptors = [
NSSortDescriptor(key: "order", ascending: true),
]
fetchedResultsController = NSFetchedResultsController(
fetchRequest: request,
managedObjectContext: CoreDataManager.shared.context,
sectionNameKeyPath: nil,
cacheName: nil
)
fetchedResultsController!.delegate = self
do {
try fetchedResultsController!.performFetch()
} catch {
print("\(error)")
}
}
}
extension TimelinesViewController: NSFetchedResultsControllerDelegate {
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.beginUpdates()
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch type {
case NSFetchedResultsChangeType.insert:
tableView.insertRows(at: [newIndexPath!], with: UITableView.RowAnimation.fade)
case NSFetchedResultsChangeType.delete:
tableView.deleteRows(at: [indexPath!], with: UITableView.RowAnimation.fade)
case NSFetchedResultsChangeType.update:
return
case NSFetchedResultsChangeType.move:
tableView.deleteRows(at: [indexPath!], with: UITableView.RowAnimation.fade)
tableView.insertRows(at: [newIndexPath!], with: UITableView.RowAnimation.fade)
}
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.endUpdates()
}
}
extension TimelinesViewController: UITableViewDataSource, UITableViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let count = fetchedResultsController?.fetchedObjects?.count else {
return 0
}
return count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "TimelinesTableViewCell", for: indexPath) as? TimelinesTableViewCell else {
fatalError("Unable to find reusable cell")
}
guard let timeline = fetchedResultsController?.object(at: indexPath) else {
fatalError("CoreData error")
}
let selectedTimeline = AuthenticationManager.session?.timeline
cell.timelineLabel.text = timeline.name
if selectedTimeline == timeline {
cell.accessoryType = UITableViewCell.AccessoryType.checkmark
}
switch TimelineCategory(rawValue: timeline.category!)! {
case .home:
cell.timelineImageView.image = UIImage(named: "Home")
case .local:
cell.timelineImageView.image = UIImage(named: "Users")
case .federated:
cell.timelineImageView.image = UIImage(named: "Globe")
case .favorites:
cell.timelineImageView.image = UIImage(named: "Star Regular")
case .tag:
cell.timelineImageView.image = UIImage(named: "Tag")
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let timeline = fetchedResultsController?.object(at: indexPath) else {
fatalError("CoreData error")
}
delegate?.didSelect(self, timeline: timeline)
dismiss(animated: true)
}
}