[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.

441 lines
17 KiB

//
// TimelineTableViewController.swift
// elpha-ios
//
// Created by Dwayne Harris on 10/1/18.
// Copyright © 2018 Elpha. All rights reserved.
//
import AlamofireImage
import CoreData
import UIKit
class TimelineTableViewController: UITableViewController, TimelinesViewControllerDelegate {
var fetchedResultsController: NSFetchedResultsController<StatusMO>? = nil
let fetchLimit = 20
var loading: Bool = false {
didSet {
DispatchQueue.main.async {
if self.loading {
self.refreshControl?.beginRefreshing()
} else {
self.refreshControl?.endRefreshing()
}
}
}
}
var currentPaginationContext: String {
guard let timeline = AuthenticationManager.session?.timeline, let categoryString = timeline.category else {
return ""
}
switch TimelineCategory(rawValue: categoryString)! {
case .home:
return "timeline:home"
case .local:
return "timeline:local"
case .federated:
return "timeline:federated"
case .tag:
return "timeline:tag:\(timeline.subcategory!)"
case .favorites:
return "timeline:favorites"
}
}
override func viewDidLoad() {
super.viewDidLoad()
initializeFetchedResultsController()
let moreButtonItem = UIBarButtonItem(image: UIImage(named: "More"), style: .plain, target: self, action: #selector(more))
let composeButtonItem = UIBarButtonItem(image: UIImage(named: "Compose"), style: .plain, target: self, action: #selector(compose))
navigationItem.leftBarButtonItems = [moreButtonItem]
navigationItem.rightBarButtonItems = [composeButtonItem]
if let timeline = AuthenticationManager.session?.timeline {
navigationItem.title = timeline.name
} else {
navigationItem.title = "Home"
}
refreshControl?.addTarget(self, action: #selector(self.fetch), for: .valueChanged)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
fetchTimeline { error in
if error != nil {
print("\(String(describing: error))")
}
}
}
@objc func more() {
performSegue(withIdentifier: "TimelinesSegue", sender: self)
}
@objc func compose() {
// let alertController = UIAlertController(title: "Compose", message: "Toot", preferredStyle: .alert)
// alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
// present(alertController, animated: true)
AlertManager.shared.show(message: "Test message")
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "TimelinesSegue" {
if let destination = segue.destination as? TimelinesViewController {
destination.delegate = self
}
}
}
func didSelect(timeline: TimelineMO) {
navigationItem.title = timeline.name
AuthenticationManager.session?.timeline = timeline
CoreDataManager.shared.saveContext()
initializeFetchedResultsController()
tableView.reloadData()
}
func createDefaultTimelines(account: AccountMO) {
let homeTimeline = TimelineMO(context: CoreDataManager.shared.context)
homeTimeline.name = "Home"
homeTimeline.category = TimelineCategory.home.rawValue
homeTimeline.account = account
homeTimeline.order = 1
let localTimeline = TimelineMO(context: CoreDataManager.shared.context)
localTimeline.name = "Local"
localTimeline.category = TimelineCategory.local.rawValue
localTimeline.account = account
localTimeline.order = 2
let federatedTimeline = TimelineMO(context: CoreDataManager.shared.context)
federatedTimeline.name = "Federated"
federatedTimeline.category = TimelineCategory.federated.rawValue
federatedTimeline.account = account
federatedTimeline.order = 3
let favoritesTimeline = TimelineMO(context: CoreDataManager.shared.context)
favoritesTimeline.name = "Favorites"
favoritesTimeline.category = TimelineCategory.favorites.rawValue
favoritesTimeline.account = account
favoritesTimeline.order = 4
CoreDataManager.shared.saveContext()
}
@objc func fetch() {
fetchTimeline { error in
if error != nil {
print("\(String(describing: error))")
}
}
}
func fetchTimeline(withPagination pagination: PaginationItem? = nil, completion: @escaping (Error?) -> Void) {
guard let session = AuthenticationManager.session, let account = session.account else {
completion(nil)
return
}
if session.timeline == nil {
if let timelines = account.timelines, timelines.count == 0 {
createDefaultTimelines(account: account)
}
let request = NSFetchRequest<TimelineMO>(entityName: "Timeline")
request.predicate = NSPredicate(format: "category == %@", TimelineCategory.home.rawValue)
do {
let results = try CoreDataManager.shared.context.fetch(request)
session.timeline = results.first
CoreDataManager.shared.saveContext()
} catch {
print("\(error)")
}
}
guard let timeline = session.timeline else {
completion(nil)
return
}
func requestCompletion(data: [JSONObject]?, pagination: [PaginationItem]?, error: Error?) {
guard let data = data, error == nil else {
completion(error)
return
}
DispatchQueue.main.async {
for (index, status) in data.enumerated() {
if let upsertResult = MastodonDataManager.upsertStatus(status) {
let status = upsertResult.object
if let pagination = pagination {
var markers: [PaginationMarker] = status.markers ?? []
markers = Array(markers.drop { $0.context == self.currentPaginationContext })
if index == 0 {
pagination.forEach { item in
if item.direction == .prev {
markers.append(PaginationMarker(context: self.currentPaginationContext, item: item))
}
}
}
if index == data.count - 1 {
pagination.forEach { item in
if item.direction == .next {
markers.append(PaginationMarker(context: self.currentPaginationContext, item: item))
}
}
}
status.markers = markers
}
timeline.addToStatuses(status)
}
}
CoreDataManager.shared.saveContext()
self.loading = false
completion(nil)
}
}
loading = true
switch TimelineCategory(rawValue: timeline.category!)! {
case .home:
MastodonAPI.homeTimeline(
limit: fetchLimit,
pagination: pagination,
completion: requestCompletion
)
case .local:
MastodonAPI.publicTimeline(
local: true,
limit: fetchLimit,
pagination: pagination,
completion: requestCompletion
)
case .federated:
MastodonAPI.publicTimeline(
local: false,
limit: fetchLimit,
pagination: pagination,
completion: requestCompletion
)
case .tag:
MastodonAPI.tagTimeline(
tag: timeline.subcategory!,
local: false,
limit: fetchLimit,
pagination: pagination,
completion: requestCompletion
)
case .favorites:
MastodonAPI.favorites(
limit: fetchLimit,
pagination: pagination,
completion: requestCompletion
)
}
}
}
extension TimelineTableViewController: NSFetchedResultsControllerDelegate {
func initializeFetchedResultsController() {
guard let timeline = AuthenticationManager.session?.timeline else {
return
}
let request = NSFetchRequest<StatusMO>(entityName: "Status")
request.predicate = NSPredicate(format: "ANY timelines = %@", timeline)
request.sortDescriptors = [
NSSortDescriptor(key: "createdAt", ascending: false),
]
fetchedResultsController = NSFetchedResultsController(
fetchRequest: request,
managedObjectContext: CoreDataManager.shared.context,
sectionNameKeyPath: nil,
cacheName: nil
)
fetchedResultsController!.delegate = self
do {
try fetchedResultsController!.performFetch()
} catch {
print("\(error)")
}
}
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.none)
case NSFetchedResultsChangeType.delete:
tableView.deleteRows(at: [indexPath!], with: UITableView.RowAnimation.none)
case NSFetchedResultsChangeType.update:
if let cell = tableView.cellForRow(at: indexPath!) as? TimelineTableViewCell {
cell.statusView.update(withStatus: anObject as! StatusMO)
}
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 TimelineTableViewController {
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let count = fetchedResultsController?.fetchedObjects?.count else {
return 0
}
return count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "TimelineTableViewCell", for: indexPath) as? TimelineTableViewCell else {
fatalError("Unable to find reusable cell")
}
guard let status = fetchedResultsController?.object(at: indexPath) else {
fatalError("CoreData error")
}
cell.statusView.delegate = self
cell.statusView.update(withStatus: status)
cell.statusView.topDividerView.isHidden = indexPath.row == 0
cell.statusView.bottomDividerView.isHidden = false
cell.statusView.topLoadMoreView.isHidden = true
cell.statusView.bottomLoadMoreView.isHidden = true
let statusCount = fetchedResultsController?.fetchedObjects?.count ?? 0
if let markers = status.markers {
markers.forEach { marker in
if marker.context == self.currentPaginationContext {
switch marker.item.direction {
case .prev:
if indexPath.row > 0, let previousStatus = fetchedResultsController?.object(at: IndexPath(row: indexPath.row - 1, section: indexPath.section)) {
let previousMarkers = previousStatus.markers ?? []
let previousMarker = previousMarkers.first { $0.context == self.currentPaginationContext && $0.item.direction == .next }
if previousStatus.id! != marker.item.statusID && previousMarker != nil {
cell.statusView.topLoadMoreView.isHidden = false
cell.statusView.topDividerView.isHidden = true
}
}
case .next:
if indexPath.row < statusCount - 1, let nextStatus = fetchedResultsController?.object(at: IndexPath(row: indexPath.row + 1, section: indexPath.section)) {
let nextMarkers = nextStatus.markers ?? []
let nextMarker = nextMarkers.first { $0.context == self.currentPaginationContext && $0.item.direction == .prev }
if nextStatus.id! != marker.item.statusID && nextMarker != nil {
cell.statusView.bottomLoadMoreView.isHidden = false
cell.statusView.bottomDividerView.isHidden = true
}
}
}
}
}
}
if indexPath.row == statusCount - 1 {
cell.statusView.bottomLoadMoreView.isHidden = false
cell.statusView.bottomDividerView.isHidden = true
}
return cell
}
}
extension TimelineTableViewController: StatusViewDelegate {
func accountTapped(account: AccountMO) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let controller = storyboard.instantiateViewController(withIdentifier: "AccountTableViewController") as? AccountTableViewController {
controller.account = account
self.navigationController?.pushViewController(controller, animated: true)
}
}
func statusTapped(status: StatusMO) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let controller = storyboard.instantiateViewController(withIdentifier: "StatusTableViewController") as? StatusTableViewController {
controller.status = status
self.navigationController?.pushViewController(controller, animated: true)
}
}
func favoriteTapped(status: StatusMO) {
if status.favorited {
MastodonAPI.unfavorite(statusID: status.id!) { error in
if error != nil {
print("\(String(describing: error))")
}
}
} else {
MastodonAPI.favorite(statusID: status.id!) { error in
if error != nil {
print("\(String(describing: error))")
}
}
}
}
func reblogTapped(status: StatusMO) {
if status.reblogged {
MastodonAPI.unreblog(statusID: status.id!) { error in
if error != nil {
print("\(String(describing: error))")
}
}
} else {
MastodonAPI.reblog(statusID: status.id!) { error in
if error != nil {
print("\(String(describing: error))")
}
}
}
}
func loadMoreTapped(status: StatusMO, direction: PaginationDirection) {
if let markers = status.markers {
markers.forEach { marker in
if marker.context == self.currentPaginationContext && marker.item.direction == direction {
fetchTimeline(withPagination: marker.item) { error in
if error != nil {
print("\(String(describing: error))")
}
}
}
}
}
}
}