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.
402 lines
16 KiB
402 lines
16 KiB
//
|
|
// AccountTableView.swift
|
|
// elpha-ios
|
|
//
|
|
// Created by Dwayne Harris on 10/9/18.
|
|
// Copyright © 2018 Elpha. All rights reserved.
|
|
//
|
|
|
|
import AlamofireImage
|
|
import CoreData
|
|
import Kingfisher
|
|
import UIKit
|
|
import SafariServices
|
|
|
|
class FieldTableViewController: NSObject, UITableViewDelegate, UITableViewDataSource {
|
|
var fields: [AccountField]? = nil
|
|
|
|
func numberOfSections(in tableView: UITableView) -> Int {
|
|
return 1
|
|
}
|
|
|
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
return fields?.count ?? 0
|
|
}
|
|
|
|
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
guard let cell = tableView.dequeueReusableCell(withIdentifier: "FieldTableViewCell", for: indexPath) as? FieldTableViewCell else {
|
|
fatalError("Unable to find reusable cell")
|
|
}
|
|
|
|
if let fields = fields {
|
|
let field = fields[indexPath.row]
|
|
|
|
cell.nameLabel.text = field.name
|
|
|
|
do {
|
|
let htmlString = """
|
|
<style>
|
|
html * {
|
|
font-family: -apple-system !important;
|
|
font-size: 12px !important;
|
|
font-weight: bold;
|
|
color: \(UIColor(named: "Text")!.hexString()) !important;
|
|
text-align: right !important;
|
|
}
|
|
</style>
|
|
\(field.value)
|
|
"""
|
|
|
|
if let data = htmlString.data(using: String.Encoding.utf16) {
|
|
cell.valueLabel.attributedText = try NSAttributedString(
|
|
data: data,
|
|
options: [.documentType: NSAttributedString.DocumentType.html],
|
|
documentAttributes: nil
|
|
)
|
|
}
|
|
} catch {
|
|
print("\(error)")
|
|
}
|
|
}
|
|
|
|
return cell
|
|
}
|
|
}
|
|
|
|
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 statusesLabel: UILabel!
|
|
@IBOutlet var followingLabel: UILabel!
|
|
@IBOutlet var followersLabel: UILabel!
|
|
@IBOutlet var statusTypeSegmentedControl: UISegmentedControl!
|
|
@IBOutlet var contentTextView: UITextViewFixed!
|
|
@IBOutlet var fieldTableView: UITableView!
|
|
|
|
var fetchedResultsController: NSFetchedResultsController<StatusMO>? = nil
|
|
var account: AccountMO? = nil
|
|
var fieldTableViewController: FieldTableViewController? = nil
|
|
|
|
override var currentPaginationContext: String {
|
|
get {
|
|
guard let account = self.account else {
|
|
return ""
|
|
}
|
|
|
|
return "account:\(account.acct!):\(statusTypeSegmentedControl.selectedSegmentIndex)"
|
|
}
|
|
|
|
set {
|
|
|
|
}
|
|
}
|
|
|
|
@IBAction func statusTypeChanged(_ sender: UISegmentedControl) {
|
|
initializeFetchedResultsController()
|
|
fetch()
|
|
}
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
refreshControl?.addTarget(self, action: #selector(self.fetch), for: .valueChanged)
|
|
fieldTableViewController = FieldTableViewController()
|
|
|
|
avatarImageView.setRoundedCorners()
|
|
avatarImageView.setShadow()
|
|
|
|
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()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private func updateHeader(withAccount account: AccountMO) {
|
|
navigationItem.title = account.displayName
|
|
|
|
if let headerURL = account.headerURL {
|
|
headerImageView.kf.setImage(with: headerURL)
|
|
}
|
|
|
|
if let avatarURL = account.avatarURL {
|
|
let options: KingfisherOptionsInfo = SettingsManager.automaticallyPlayGIFs ? [] : [.onlyLoadFirstFrame]
|
|
avatarImageView.kf.setImage(with: avatarURL, options: options)
|
|
}
|
|
|
|
displayNameLabel.text = account.displayName
|
|
usernameLabel.text = "@\(account.acct!)"
|
|
|
|
if let note = account.note {
|
|
contentTextView.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)
|
|
|
|
if let fields = account.fields {
|
|
fieldTableViewController!.fields = fields
|
|
|
|
fieldTableView.dataSource = fieldTableViewController
|
|
fieldTableView.delegate = fieldTableViewController
|
|
|
|
fieldTableView.isHidden = false
|
|
} else {
|
|
fieldTableView.isHidden = true
|
|
}
|
|
}
|
|
|
|
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
|
|
)
|
|
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)
|
|
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, estimatedHeightForRowAt 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
|
|
}
|
|
}
|