2020-08-27 21:26:12 -07:00
|
|
|
//
|
|
|
|
// FeedDetailViewController.swift
|
|
|
|
// NewsBlur
|
|
|
|
//
|
|
|
|
// Created by David Sinclair on 2020-08-27.
|
|
|
|
// Copyright © 2020 NewsBlur. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2023-01-21 16:20:53 -06:00
|
|
|
import SwiftUI
|
2020-08-27 21:26:12 -07:00
|
|
|
|
|
|
|
/// List of stories for a feed.
|
|
|
|
class FeedDetailViewController: FeedDetailObjCViewController {
|
2023-01-21 16:20:53 -06:00
|
|
|
lazy var gridViewController = makeGridViewController()
|
|
|
|
|
|
|
|
lazy var storyCache = StoryCache()
|
|
|
|
|
2022-09-02 20:39:00 -06:00
|
|
|
enum SectionLayoutKind: Int, CaseIterable {
|
|
|
|
/// Feed cells before the story.
|
|
|
|
case feedBeforeStory
|
|
|
|
|
|
|
|
/// The selected story.
|
|
|
|
case selectedStory
|
|
|
|
|
|
|
|
/// Feed cells after the story.
|
|
|
|
case feedAfterStory
|
|
|
|
|
|
|
|
/// Loading cell at the end.
|
|
|
|
case loading
|
|
|
|
}
|
2020-08-27 21:26:12 -07:00
|
|
|
|
2022-10-26 20:50:07 -06:00
|
|
|
var isGrid: Bool {
|
|
|
|
return appDelegate.detailViewController.layout == .grid
|
|
|
|
}
|
|
|
|
|
2022-09-02 20:39:00 -06:00
|
|
|
var feedColumns: Int {
|
|
|
|
guard let pref = UserDefaults.standard.string(forKey: "grid_columns"), let columns = Int(pref) else {
|
|
|
|
return 4
|
|
|
|
}
|
|
|
|
|
|
|
|
return columns
|
|
|
|
}
|
|
|
|
|
2022-10-27 20:53:47 -06:00
|
|
|
var gridHeight: CGFloat {
|
|
|
|
guard let pref = UserDefaults.standard.string(forKey: "grid_height") else {
|
|
|
|
return 400
|
|
|
|
}
|
|
|
|
|
|
|
|
switch pref {
|
|
|
|
case "xs":
|
|
|
|
return 250
|
|
|
|
case "short":
|
|
|
|
return 300
|
|
|
|
case "tall":
|
|
|
|
return 400
|
|
|
|
case "xl":
|
|
|
|
return 450
|
|
|
|
default:
|
|
|
|
return 350
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-21 16:20:53 -06:00
|
|
|
private func makeGridViewController() -> UIHostingController<FeedDetailGridView> {
|
|
|
|
let gridView = FeedDetailGridView(feedDetailInteraction: self, cache: storyCache)
|
|
|
|
let gridViewController = UIHostingController(rootView: gridView)
|
|
|
|
gridViewController.view.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
|
|
|
|
return gridViewController
|
|
|
|
}
|
|
|
|
|
2022-09-02 20:39:00 -06:00
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
2023-01-21 16:20:53 -06:00
|
|
|
|
|
|
|
addChild(gridViewController)
|
|
|
|
view.addSubview(gridViewController.view)
|
|
|
|
gridViewController.didMove(toParent: self)
|
|
|
|
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
gridViewController.view.topAnchor.constraint(equalTo: view.topAnchor),
|
|
|
|
gridViewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
2023-04-21 21:44:28 -07:00
|
|
|
gridViewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
|
|
|
gridViewController.view.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
|
2023-01-21 16:20:53 -06:00
|
|
|
])
|
2023-05-25 21:47:34 -07:00
|
|
|
|
|
|
|
changedLayout()
|
2022-10-27 15:19:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc override func changedLayout() {
|
2023-06-15 21:53:00 -07:00
|
|
|
// Make sure the view has loaded.
|
|
|
|
_ = view
|
|
|
|
|
2023-05-25 21:47:34 -07:00
|
|
|
storyTitlesTable.isHidden = !isLegacyTable
|
|
|
|
gridViewController.view.isHidden = isLegacyTable
|
|
|
|
|
2023-03-03 21:38:55 -07:00
|
|
|
deferredReload()
|
|
|
|
}
|
|
|
|
|
|
|
|
var reloadWorkItem: DispatchWorkItem?
|
|
|
|
|
2023-07-04 21:25:35 -07:00
|
|
|
func deferredReload(story: Story? = nil) {
|
2023-03-03 21:38:55 -07:00
|
|
|
reloadWorkItem?.cancel()
|
|
|
|
|
|
|
|
let workItem = DispatchWorkItem { [weak self] in
|
|
|
|
guard let self else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-20 21:07:56 -06:00
|
|
|
configureDataSource(story: story)
|
|
|
|
reloadWorkItem = nil
|
2022-09-03 21:24:36 -06:00
|
|
|
}
|
|
|
|
|
2023-03-03 21:38:55 -07:00
|
|
|
reloadWorkItem = workItem
|
2023-03-15 17:03:34 -07:00
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100), execute: workItem)
|
2022-09-02 20:39:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc override func reload() {
|
2023-03-15 17:03:34 -07:00
|
|
|
deferredReload()
|
2022-09-02 20:39:00 -06:00
|
|
|
}
|
2022-09-03 21:24:36 -06:00
|
|
|
|
2023-07-04 21:25:35 -07:00
|
|
|
func reload(story: Story) {
|
|
|
|
deferredReload(story: story)
|
|
|
|
}
|
2023-07-20 21:07:56 -06:00
|
|
|
|
|
|
|
@objc override func reload(_ indexPath: IndexPath, with rowAnimation: UITableView.RowAnimation = .none) {
|
|
|
|
if !isLegacyTable {
|
|
|
|
deferredReload()
|
|
|
|
} else if reloadWorkItem == nil {
|
|
|
|
// Only do this if a deferred reload isn't pending; otherwise no point in doing a partial reload, plus the table may be stale.
|
|
|
|
storyTitlesTable.reloadRows(at: [indexPath], with: rowAnimation)
|
|
|
|
}
|
|
|
|
}
|
2023-07-04 21:25:35 -07:00
|
|
|
}
|
2022-09-02 20:39:00 -06:00
|
|
|
|
|
|
|
extension FeedDetailViewController {
|
2023-07-04 21:25:35 -07:00
|
|
|
func configureDataSource(story: Story? = nil) {
|
|
|
|
if let story {
|
|
|
|
storyCache.reload(story: story)
|
|
|
|
} else {
|
|
|
|
storyCache.reload()
|
|
|
|
}
|
2022-09-02 20:39:00 -06:00
|
|
|
|
2023-05-25 21:47:34 -07:00
|
|
|
if isLegacyTable {
|
|
|
|
reloadTable()
|
|
|
|
}
|
2022-09-02 20:39:00 -06:00
|
|
|
}
|
2020-08-27 21:26:12 -07:00
|
|
|
}
|
2023-01-21 16:20:53 -06:00
|
|
|
|
|
|
|
extension FeedDetailViewController: FeedDetailInteraction {
|
2023-05-26 16:01:51 -07:00
|
|
|
var hasNoMoreStories: Bool {
|
|
|
|
return pageFinished
|
|
|
|
}
|
|
|
|
|
|
|
|
var isPremiumRestriction: Bool {
|
|
|
|
return !appDelegate.isPremium &&
|
|
|
|
storiesCollection.isRiverView &&
|
|
|
|
!storiesCollection.isReadView &&
|
|
|
|
!storiesCollection.isWidgetView &&
|
|
|
|
!storiesCollection.isSocialView &&
|
|
|
|
!storiesCollection.isSavedView
|
|
|
|
}
|
|
|
|
|
2023-04-21 21:44:28 -07:00
|
|
|
func pullToRefresh() {
|
|
|
|
instafetchFeed()
|
|
|
|
}
|
|
|
|
|
2023-03-03 21:38:55 -07:00
|
|
|
func visible(story: Story) {
|
2023-01-21 16:20:53 -06:00
|
|
|
print("\(story.title) appeared")
|
|
|
|
|
|
|
|
if story.index >= storyCache.before.count + storyCache.after.count - 5 {
|
|
|
|
if storiesCollection.isRiverView, storiesCollection.activeFolder != nil {
|
|
|
|
fetchRiverPage(storiesCollection.feedPage + 1, withCallback: nil)
|
|
|
|
} else {
|
|
|
|
fetchFeedDetail(storiesCollection.feedPage + 1, withCallback: nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-03 21:38:55 -07:00
|
|
|
func tapped(story: Story) {
|
2023-05-26 14:00:20 -07:00
|
|
|
if presentedViewController != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-21 16:20:53 -06:00
|
|
|
print("tapped \(story.title)")
|
|
|
|
|
|
|
|
let indexPath = IndexPath(row: story.index, section: 0)
|
|
|
|
|
2023-03-31 22:02:58 -07:00
|
|
|
didSelectItem(at: indexPath)
|
2023-01-21 16:20:53 -06:00
|
|
|
}
|
|
|
|
|
2023-03-03 21:38:55 -07:00
|
|
|
func reading(story: Story) {
|
|
|
|
print("reading \(story.title)")
|
|
|
|
}
|
|
|
|
|
2023-03-21 21:54:21 -07:00
|
|
|
func read(story: Story) {
|
|
|
|
let dict = story.dictionary
|
|
|
|
|
|
|
|
if storiesCollection.isStoryUnread(dict) {
|
|
|
|
print("marking as read '\(story.title)'")
|
|
|
|
|
|
|
|
storiesCollection.markStoryRead(dict)
|
|
|
|
storiesCollection.syncStory(asRead: dict)
|
|
|
|
|
|
|
|
story.load()
|
|
|
|
|
2023-07-04 21:25:35 -07:00
|
|
|
deferredReload(story: story)
|
2023-03-21 21:54:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-15 21:53:00 -07:00
|
|
|
func unread(story: Story) {
|
|
|
|
let dict = story.dictionary
|
|
|
|
|
|
|
|
if !storiesCollection.isStoryUnread(dict) {
|
|
|
|
print("marking as unread '\(story.title)'")
|
|
|
|
|
|
|
|
storiesCollection.markStoryUnread(dict)
|
|
|
|
storiesCollection.syncStory(asRead: dict)
|
|
|
|
|
|
|
|
story.load()
|
|
|
|
|
2023-07-04 21:25:35 -07:00
|
|
|
deferredReload(story: story)
|
2023-06-15 21:53:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-03 21:38:55 -07:00
|
|
|
func hid(story: Story) {
|
2023-02-02 21:41:10 -06:00
|
|
|
print("hiding \(story.title)")
|
|
|
|
|
|
|
|
appDelegate.activeStory = nil
|
|
|
|
reload()
|
|
|
|
}
|
2023-01-21 16:20:53 -06:00
|
|
|
}
|