2019-12-20 03:41:23 +00:00
|
|
|
//
|
|
|
|
// InstanceTimelineViewController.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 12/19/19.
|
|
|
|
// Copyright © 2019 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
2021-05-22 17:42:53 +00:00
|
|
|
protocol InstanceTimelineViewControllerDelegate: AnyObject {
|
2019-12-20 03:41:23 +00:00
|
|
|
func didSaveInstance(url: URL)
|
|
|
|
func didUnsaveInstance(url: URL)
|
|
|
|
}
|
|
|
|
|
|
|
|
class InstanceTimelineViewController: TimelineTableViewController {
|
|
|
|
|
2020-01-20 04:02:07 +00:00
|
|
|
weak var delegate: InstanceTimelineViewControllerDelegate?
|
2019-12-20 03:41:23 +00:00
|
|
|
|
2020-01-20 16:48:47 +00:00
|
|
|
weak var parentMastodonController: MastodonController?
|
|
|
|
|
2019-12-20 03:41:23 +00:00
|
|
|
let instanceURL: URL
|
2020-01-20 16:18:55 +00:00
|
|
|
let instanceMastodonController: MastodonController
|
2019-12-20 03:41:23 +00:00
|
|
|
|
|
|
|
var toggleSaveButton: UIBarButtonItem!
|
|
|
|
var toggleSaveButtonTitle: String {
|
2020-01-20 16:48:47 +00:00
|
|
|
if SavedDataManager.shared.isSaved(instance: instanceURL, for: parentMastodonController!.accountInfo!) {
|
2019-12-20 03:41:23 +00:00
|
|
|
return NSLocalizedString("Unsave", comment: "unsave instance button")
|
|
|
|
} else {
|
|
|
|
return NSLocalizedString("Save", comment: "save instance button")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-13 19:51:06 +00:00
|
|
|
var browsingEnabled = true
|
|
|
|
|
2020-01-20 16:48:47 +00:00
|
|
|
init(for url: URL, parentMastodonController: MastodonController) {
|
|
|
|
self.parentMastodonController = parentMastodonController
|
2019-12-20 03:41:23 +00:00
|
|
|
|
2020-01-20 16:48:47 +00:00
|
|
|
self.instanceURL = url
|
|
|
|
|
2020-01-20 16:18:55 +00:00
|
|
|
// the timeline VC only stores a weak reference to it, so we need to store a strong reference to make sure it's not released immediately
|
2020-05-11 21:57:50 +00:00
|
|
|
instanceMastodonController = MastodonController(instanceURL: url, transient: true)
|
2020-01-08 02:29:15 +00:00
|
|
|
|
2020-01-25 15:37:22 +00:00
|
|
|
super.init(for: .public(local: true), mastodonController: instanceMastodonController)
|
|
|
|
|
|
|
|
title = url.host!
|
|
|
|
userActivity = nil // todo: activity for instance-specific timelines
|
2019-12-20 03:41:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
|
|
|
toggleSaveButton = UIBarButtonItem(title: toggleSaveButtonTitle, style: .plain, target: self, action: #selector(toggleSaveButtonPressed))
|
|
|
|
navigationItem.rightBarButtonItem = toggleSaveButton
|
|
|
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(savedInstancesChanged), name: .savedInstancesChanged, object: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func savedInstancesChanged() {
|
|
|
|
toggleSaveButton.title = toggleSaveButtonTitle
|
|
|
|
}
|
|
|
|
|
2020-01-06 00:39:37 +00:00
|
|
|
// MARK: - Table view data source
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
let cell = super.tableView(tableView, cellForRowAt: indexPath) as! TimelineStatusTableViewCell
|
2020-09-13 19:51:06 +00:00
|
|
|
cell.delegate = browsingEnabled ? self : nil
|
2020-01-06 00:39:37 +00:00
|
|
|
return cell
|
|
|
|
}
|
|
|
|
|
2019-12-20 03:41:23 +00:00
|
|
|
// MARK: - Table view delegate
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
2020-09-13 19:51:06 +00:00
|
|
|
guard browsingEnabled else { return }
|
|
|
|
super.tableView(tableView, didSelectRowAt: indexPath)
|
2020-01-25 15:44:31 +00:00
|
|
|
}
|
|
|
|
|
2019-12-20 03:41:23 +00:00
|
|
|
// MARK: - Interaction
|
|
|
|
@objc func toggleSaveButtonPressed() {
|
2020-01-20 16:48:47 +00:00
|
|
|
if SavedDataManager.shared.isSaved(instance: instanceURL, for: parentMastodonController!.accountInfo!) {
|
|
|
|
SavedDataManager.shared.remove(instance: instanceURL, for: parentMastodonController!.accountInfo!)
|
2019-12-20 03:41:23 +00:00
|
|
|
delegate?.didUnsaveInstance(url: instanceURL)
|
|
|
|
} else {
|
2020-01-20 16:48:47 +00:00
|
|
|
SavedDataManager.shared.add(instance: instanceURL, for: parentMastodonController!.accountInfo!)
|
2019-12-20 03:41:23 +00:00
|
|
|
delegate?.didSaveInstance(url: instanceURL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|