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

351 lines
14 KiB

//
// AccountTableView.swift
// elpha-ios
//
// Created by Dwayne Harris on 10/9/18.
// Copyright © 2018 Elpha. All rights reserved.
//
import AlamofireImage
import CoreData
import UIKit
import SafariServices
class AccountTableViewController: AbstractStatusTableViewController {
@IBOutlet var headerView: UIView!
@IBOutlet var headerImageView: UIImageView!
@IBOutlet var avatarImageView: UIImageView!
@IBOutlet var displayNameLabel: UILabel!
@IBOutlet var usernameLabel: UILabel!
@IBOutlet var contentLabel: UILabel!
@IBOutlet var statusesLabel: UILabel!
@IBOutlet var followingLabel: UILabel!
@IBOutlet var followersLabel: UILabel!
@IBOutlet var statusTypeSegmentedControl: UISegmentedControl!
var fetchedResultsController: NSFetchedResultsController<StatusMO>? = nil
var account: AccountMO? = nil
override var currentPaginationContext: String {
get {
guard let account = self.account else {
return ""
}
switch statusTypeSegmentedControl.selectedSegmentIndex {
case 3:
return "timeline:favorites"
case let index:
return "account:\(account.acct!):\(index)"
}
}
set {
}
}
@IBAction func statusTypeChanged(_ sender: UISegmentedControl) {
initializeFetchedResultsController()
fetch()
}
private func updateHeader(withAccount account: AccountMO) {
navigationItem.title = account.displayName
let avatarFilter = AspectScaledToFillSizeWithRoundedCornersFilter(
size: CGSize(width: 70.0, height: 70.0),
radius: 20.0,
divideRadiusByImageScale: true
)
if let headerURL = account.headerURL {
headerImageView.af_setImage(withURL: headerURL)
}
if let avatarURL = account.avatarURL {
avatarImageView.af_setImage(withURL: avatarURL, filter: avatarFilter)
}
displayNameLabel.text = account.displayName
usernameLabel.text = "@\(account.acct!)"
if let note = account.note {
contentLabel.attributedText = note.htmlAttributed(size: 15, centered: true)
}
statusesLabel.text = NumberFormatter.localizedString(from: NSNumber(value: account.statusesCount), number: .decimal)
followingLabel.text = NumberFormatter.localizedString(from: NSNumber(value: account.followingCount), number: .decimal)
followersLabel.text = NumberFormatter.localizedString(from: NSNumber(value: account.followersCount), number: .decimal)
}
override func viewDidLoad() {
super.viewDidLoad()
refreshControl?.addTarget(self, action: #selector(self.fetch), for: .valueChanged)
if self.account == nil {
if let session = AuthenticationManager.session {
self.account = session.account
}
}
if let account = account {
updateHeader(withAccount: account)
initializeFetchedResultsController()
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if let account = self.account {
MastodonAPI.account(id: account.id!) { data, error in
guard let data = data, error == nil else {
return
}
self.account = MastodonDataManager.upsertAccount(data)
if let account = self.account {
DispatchQueue.main.async {
self.updateHeader(withAccount: account)
self.fetch()
}
}
}
}
}
override func loadMoreTapped(status: StatusMO, direction: PaginationDirection) {
if let markers = status.markers {
markers.forEach { marker in
if marker.context == self.currentPaginationContext && marker.item.direction == direction {
fetchStatuses(withPagination: marker.item) { error in
if error != nil {
AlertManager.shared.show(message: error!.localizedDescription, category: .error)
}
}
}
}
}
}
@objc func fetch() {
fetchStatuses { error in
if error != nil {
AlertManager.shared.show(message: error!.localizedDescription, category: .error)
}
}
}
func fetchStatuses(withPagination pagination: PaginationItem? = nil, completion: @escaping (Error?) -> Void) {
if let account = account {
func requestCompletion(data: JSONObjectArray?, pagination: [PaginationItem]?, error: Error?) {
guard let data = data, error == nil else {
self.loading = false
completion(error)
return
}
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 ?? []
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
}
}
}
MastodonAPI.pinnedStatuses(accountID: account.id!, limit: fetchLimit) { data, error in
guard let data = data, error == nil else {
self.loading = false
completion(error)
return
}
data.forEach { status in
_ = MastodonDataManager.upsertStatus(status.merging(["pinned": true]) { (_, new) in new })
}
}
CoreDataManager.shared.saveContext()
self.loading = false
completion(nil)
}
switch statusTypeSegmentedControl.selectedSegmentIndex {
case 1:
MastodonAPI.statuses(
accountID: account.id!,
onlyMedia: false,
excludeReplies: false,
limit: fetchLimit,
pagination: pagination,
completion: requestCompletion
)
case 2:
MastodonAPI.statuses(
accountID: account.id!,
onlyMedia: true,
excludeReplies: true,
limit: fetchLimit,
pagination: pagination,
completion: requestCompletion
)
case 3:
MastodonAPI.favorites(
limit: fetchLimit,
pagination: pagination,
completion: requestCompletion
)
default:
MastodonAPI.statuses(
accountID: account.id!,
onlyMedia: false,
excludeReplies: true,
limit: fetchLimit,
pagination: pagination,
completion: requestCompletion
)
}
}
}
}
extension AccountTableViewController: NSFetchedResultsControllerDelegate {
func initializeFetchedResultsController() {
if let account = self.account {
let request = NSFetchRequest<StatusMO>(entityName: "Status")
request.sortDescriptors = [
NSSortDescriptor(key: "pinned", ascending: false),
NSSortDescriptor(key: "createdAt", ascending: false),
]
switch statusTypeSegmentedControl.selectedSegmentIndex {
case 1:
request.predicate = NSPredicate(format: "account == %@", account)
case 2:
request.predicate = NSPredicate(format: "account == %@ AND attachments.@count > 0", account)
case 3:
request.predicate = NSPredicate(format: "favorited == YES")
default:
request.predicate = NSPredicate(format: "account == %@ AND inReplyToID == nil", account)
}
fetchedResultsController = NSFetchedResultsController(
fetchRequest: request,
managedObjectContext: CoreDataManager.shared.context,
sectionNameKeyPath: nil,
cacheName: nil
)
fetchedResultsController!.delegate = self
do {
try fetchedResultsController!.performFetch()
} catch {
print("\(error)")
}
}
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
self.tableView.reloadData()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if let cell = cell as? AccountTableViewCell {
CellHeightManager.set(status: cell.statusView.status, height: cell.frame.size.height)
}
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return CellHeightManager.get(status: fetchedResultsController?.object(at: indexPath))
}
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: "AccountTableViewCell", for: indexPath) as? AccountTableViewCell 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.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
}
}