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.
376 lines
15 KiB
376 lines
15 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
|
|
|
|
class AccountTableViewController: UITableViewController {
|
|
@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!
|
|
|
|
let fetchLimit = 20
|
|
var fetchedResultsController: NSFetchedResultsController<StatusMO>? = nil
|
|
var account: AccountMO? = nil
|
|
|
|
var loading: Bool = false {
|
|
didSet {
|
|
DispatchQueue.main.async {
|
|
if self.loading {
|
|
self.refreshControl?.beginRefreshing()
|
|
} else {
|
|
self.refreshControl?.endRefreshing()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
var currentPaginationContext: String {
|
|
guard let account = self.account else {
|
|
return ""
|
|
}
|
|
|
|
switch statusTypeSegmentedControl.selectedSegmentIndex {
|
|
case 3:
|
|
return "timeline:favorites"
|
|
case let index:
|
|
return "account:\(account.acct!):\(index)"
|
|
}
|
|
}
|
|
|
|
@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()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@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, 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
|
|
}
|
|
}
|
|
|
|
extension AccountTableViewController: 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 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)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func attachmentTapped(status: StatusMO, index: Int) {
|
|
if let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AttachmentPageViewController") as? AttachmentPageViewController {
|
|
controller.status = status
|
|
controller.attachmentIndex = index
|
|
|
|
present(controller, animated: false)
|
|
}
|
|
}
|
|
}
|