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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// var storyHeight: CGFloat {
|
|
|
|
// if let pagesController = appDelegate.storyPagesViewController, let webView = pagesController.currentPage.webView {
|
|
|
|
// let frame = pagesController.view.frame
|
|
|
|
//
|
|
|
|
// print("Story pages frame: \(pagesController.view.frame), web height \(webView.scrollView.contentSize.height)")
|
|
|
|
//
|
|
|
|
// pagesController.view.frame = CGRect(x: frame.origin.x, y: frame.origin.y, width: frame.size.width, height: 500)
|
|
|
|
// pagesController.currentPage.view.frame = CGRect(x: frame.origin.x, y: frame.origin.y, width: frame.size.width, height: 500)
|
|
|
|
// pagesController.view.layoutIfNeeded()
|
|
|
|
//
|
|
|
|
// let height = webView.scrollView.contentSize.height + 50
|
|
|
|
//
|
|
|
|
// print("... frame now: \(pagesController.view.frame), height: \(height)")
|
|
|
|
//
|
|
|
|
// return height
|
|
|
|
// } else {
|
|
|
|
// return 1000
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2022-09-02 20:39:00 -06:00
|
|
|
var dataSource: UICollectionViewDiffableDataSource<SectionLayoutKind, Int>! = nil
|
|
|
|
|
2023-01-21 16:20:53 -06:00
|
|
|
private func makeGridViewController() -> UIHostingController<FeedDetailGridView> {
|
|
|
|
// let headerView = FeedDetailGridView(isGrid: isGrid, storyCache: storyCache)
|
|
|
|
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()
|
2022-10-27 15:19:31 -06:00
|
|
|
changedLayout()
|
|
|
|
configureDataSource()
|
2023-01-21 16:20:53 -06:00
|
|
|
|
|
|
|
feedCollectionView.isHidden = true
|
|
|
|
|
|
|
|
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),
|
|
|
|
gridViewController.view.widthAnchor.constraint(equalTo: view.widthAnchor),
|
|
|
|
gridViewController.view.heightAnchor.constraint(equalTo: view.heightAnchor)
|
|
|
|
])
|
2022-10-27 15:19:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc override func changedLayout() {
|
2022-10-26 20:50:07 -06:00
|
|
|
if isGrid {
|
2022-09-03 21:24:36 -06:00
|
|
|
feedCollectionView.collectionViewLayout = createGridLayout()
|
|
|
|
} else {
|
|
|
|
feedCollectionView.collectionViewLayout = createListLayout()
|
|
|
|
}
|
|
|
|
|
2022-10-27 15:19:31 -06:00
|
|
|
feedCollectionView.setNeedsLayout()
|
2022-09-02 20:39:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc override func reload() {
|
|
|
|
configureDataSource()
|
|
|
|
}
|
2022-09-03 21:24:36 -06:00
|
|
|
|
|
|
|
@objc override func reload(_ indexPath: IndexPath) {
|
|
|
|
configureDataSource()
|
|
|
|
}
|
2022-09-02 20:39:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
extension FeedDetailViewController {
|
2022-11-14 21:39:01 -06:00
|
|
|
func accessoriesForListCellItem(_ item: Int) -> [UICellAccessory] {
|
|
|
|
let isStarred = false //self.starredEmojis.contains(item)
|
|
|
|
var accessories = [UICellAccessory.disclosureIndicator()]
|
|
|
|
if isStarred {
|
|
|
|
let star = UIImageView(image: UIImage(systemName: "star.fill"))
|
|
|
|
accessories.append(.customView(configuration: .init(customView: star, placement: .trailing())))
|
|
|
|
}
|
|
|
|
return accessories
|
|
|
|
}
|
|
|
|
|
|
|
|
func leadingSwipeActionConfigurationForListCellItem(_ item: Int) -> UISwipeActionsConfiguration? {
|
|
|
|
let isStarred = false //self.starredEmojis.contains(item)
|
|
|
|
let starAction = UIContextualAction(style: .normal, title: nil) {
|
|
|
|
[weak self] (_, _, completion) in
|
|
|
|
guard let self = self else {
|
|
|
|
completion(false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't check again for the starred state. We promised in the UI what this action will do.
|
|
|
|
// If the starred state has changed by now, we do nothing, as the set will not change.
|
|
|
|
// if isStarred {
|
|
|
|
// self.starredEmojis.remove(item)
|
|
|
|
// } else {
|
|
|
|
// self.starredEmojis.insert(item)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Reconfigure the cell of this item
|
|
|
|
// Make sure we get the current index path of the item.
|
|
|
|
if let currentIndexPath = self.dataSource.indexPath(for: item) {
|
|
|
|
if let cell = self.feedCollectionView.cellForItem(at: currentIndexPath) as? UICollectionViewListCell {
|
|
|
|
UIView.animate(withDuration: 0.2) {
|
|
|
|
cell.accessories = self.accessoriesForListCellItem(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
completion(true)
|
|
|
|
}
|
|
|
|
starAction.image = UIImage(systemName: isStarred ? "star.slash" : "star.fill")
|
|
|
|
starAction.backgroundColor = .systemBlue
|
|
|
|
return UISwipeActionsConfiguration(actions: [starAction])
|
|
|
|
}
|
|
|
|
|
2022-09-03 21:24:36 -06:00
|
|
|
func createListLayout() -> UICollectionViewLayout {
|
2022-11-14 21:39:01 -06:00
|
|
|
// let size = NSCollectionLayoutSize(
|
|
|
|
// widthDimension: NSCollectionLayoutDimension.fractionalWidth(1),
|
|
|
|
// heightDimension: NSCollectionLayoutDimension.estimated(200)
|
|
|
|
// )
|
|
|
|
// let item = NSCollectionLayoutItem(layoutSize: size)
|
|
|
|
// let group = NSCollectionLayoutGroup.horizontal(layoutSize: size, subitem: item, count: 1)
|
|
|
|
//
|
|
|
|
// let section = NSCollectionLayoutSection(group: group)
|
|
|
|
// section.interGroupSpacing = 0
|
|
|
|
//
|
|
|
|
// return UICollectionViewCompositionalLayout(section: section)
|
|
|
|
|
2022-09-03 21:24:36 -06:00
|
|
|
|
|
|
|
|
2022-11-14 21:39:01 -06:00
|
|
|
var configuration = UICollectionLayoutListConfiguration(appearance: .plain)
|
2023-01-06 21:49:07 -06:00
|
|
|
|
2022-11-14 21:39:01 -06:00
|
|
|
configuration.leadingSwipeActionsConfigurationProvider = { [weak self] (indexPath) in
|
|
|
|
guard let self else { return nil }
|
|
|
|
guard let item = self.dataSource.itemIdentifier(for: indexPath) else { return nil }
|
|
|
|
return self.leadingSwipeActionConfigurationForListCellItem(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
return UICollectionViewCompositionalLayout.list(using: configuration)
|
2022-09-03 21:24:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func createGridLayout() -> UICollectionViewLayout {
|
2022-09-02 20:39:00 -06:00
|
|
|
let layout = UICollectionViewCompositionalLayout { (sectionIndex: Int,
|
|
|
|
layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? in
|
|
|
|
|
|
|
|
guard let sectionLayoutKind = SectionLayoutKind(rawValue: sectionIndex) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let isStory = sectionLayoutKind == .selectedStory
|
2022-10-27 20:53:47 -06:00
|
|
|
let isLoading = sectionLayoutKind == .loading
|
|
|
|
let columns = isStory || isLoading ? 1 : self.feedColumns
|
2022-09-02 20:39:00 -06:00
|
|
|
|
|
|
|
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
|
|
|
|
heightDimension: .fractionalHeight(1.0))
|
|
|
|
let item = NSCollectionLayoutItem(layoutSize: itemSize)
|
|
|
|
item.contentInsets = NSDirectionalEdgeInsets(top: 10, leading: 10, bottom: 10, trailing: 10)
|
|
|
|
|
2022-10-27 20:53:47 -06:00
|
|
|
let groupHeight = isStory ? NSCollectionLayoutDimension.absolute(self.storyHeight) : isLoading ? NSCollectionLayoutDimension.absolute(100) : NSCollectionLayoutDimension.absolute(self.gridHeight)
|
2022-09-02 20:39:00 -06:00
|
|
|
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
|
|
|
|
heightDimension: groupHeight)
|
|
|
|
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitem: item, count: columns)
|
|
|
|
|
|
|
|
let section = NSCollectionLayoutSection(group: group)
|
|
|
|
section.contentInsets = NSDirectionalEdgeInsets(top: 10, leading: 10, bottom: 0, trailing: 10)
|
|
|
|
|
|
|
|
return section
|
|
|
|
}
|
|
|
|
|
|
|
|
return layout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension FeedDetailViewController {
|
|
|
|
func configureDataSource() {
|
|
|
|
let feedCellRegistration = UICollectionView.CellRegistration<FeedDetailCollectionCell, Int> { (cell, indexPath, identifier) in
|
2022-09-03 21:24:36 -06:00
|
|
|
|
|
|
|
// cell.frame.size.height = self.heightForRow(at: indexPath)
|
2022-09-02 20:39:00 -06:00
|
|
|
|
|
|
|
self.prepareFeedCell(cell, indexPath: indexPath)
|
2022-12-12 22:04:04 -06:00
|
|
|
cell.setNeedsUpdateConfiguration()
|
2022-09-02 20:39:00 -06:00
|
|
|
}
|
|
|
|
|
2022-09-03 19:55:21 -06:00
|
|
|
let storyCellRegistration = UICollectionView.CellRegistration<StoryPagesCollectionCell, Int> { (cell, indexPath, identifier) in
|
2022-09-02 20:39:00 -06:00
|
|
|
self.prepareStoryCell(cell, indexPath: indexPath)
|
2022-12-12 22:04:04 -06:00
|
|
|
cell.setNeedsUpdateConfiguration()
|
2022-09-02 20:39:00 -06:00
|
|
|
}
|
|
|
|
|
2022-10-26 20:50:07 -06:00
|
|
|
let loadingCellRegistration = UICollectionView.CellRegistration<UICollectionViewCell, Int> { (cell, indexPath, identifier) in
|
|
|
|
self.prepareLoading(cell, indexPath: indexPath)
|
2022-12-12 22:04:04 -06:00
|
|
|
cell.setNeedsUpdateConfiguration()
|
2022-10-26 20:50:07 -06:00
|
|
|
}
|
|
|
|
|
2022-09-02 20:39:00 -06:00
|
|
|
dataSource = UICollectionViewDiffableDataSource<SectionLayoutKind, Int>(collectionView: feedCollectionView) {
|
|
|
|
(collectionView: UICollectionView, indexPath: IndexPath, identifier: Int) -> UICollectionViewCell? in
|
2022-10-26 20:50:07 -06:00
|
|
|
guard let sectionKind = SectionLayoutKind(rawValue: indexPath.section) else {
|
|
|
|
return nil
|
|
|
|
}
|
2022-09-02 20:39:00 -06:00
|
|
|
|
2022-10-26 20:50:07 -06:00
|
|
|
switch sectionKind {
|
|
|
|
case .feedBeforeStory, .feedAfterStory:
|
|
|
|
return collectionView.dequeueConfiguredReusableCell(using: feedCellRegistration, for: indexPath, item: identifier)
|
|
|
|
case .selectedStory:
|
|
|
|
if self.isGrid {
|
|
|
|
return collectionView.dequeueConfiguredReusableCell(using: storyCellRegistration, for: indexPath, item: identifier)
|
|
|
|
} else {
|
|
|
|
return collectionView.dequeueConfiguredReusableCell(using: feedCellRegistration, for: indexPath, item: identifier)
|
|
|
|
}
|
|
|
|
case .loading:
|
|
|
|
return collectionView.dequeueConfiguredReusableCell(using: loadingCellRegistration, for: indexPath, item: identifier)
|
|
|
|
}
|
2022-09-02 20:39:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var snapshot = NSDiffableDataSourceSnapshot<SectionLayoutKind, Int>()
|
|
|
|
|
2022-09-03 21:24:36 -06:00
|
|
|
let storyCount = Int(appDelegate.storiesCollection.storyLocationsCount)
|
2023-01-21 16:20:53 -06:00
|
|
|
var beforeSelection = [Int]()
|
|
|
|
var selectedIndex = -999
|
|
|
|
var afterSelection = [Int]()
|
2022-09-02 20:39:00 -06:00
|
|
|
|
|
|
|
snapshot.appendSections(SectionLayoutKind.allCases)
|
|
|
|
|
|
|
|
if self.messageView.isHidden {
|
2022-10-26 20:50:07 -06:00
|
|
|
if storyCount > 0 {
|
2023-01-21 16:20:53 -06:00
|
|
|
selectedIndex = appDelegate.storiesCollection.indexOfActiveStory()
|
2022-09-02 20:39:00 -06:00
|
|
|
|
2022-10-26 20:50:07 -06:00
|
|
|
if selectedIndex < 0 {
|
2023-01-21 16:20:53 -06:00
|
|
|
beforeSelection = Array(0..<storyCount)
|
|
|
|
snapshot.appendItems(beforeSelection, toSection: .feedBeforeStory)
|
2022-10-26 20:50:07 -06:00
|
|
|
} else {
|
2023-01-21 16:20:53 -06:00
|
|
|
beforeSelection = Array(0..<selectedIndex)
|
|
|
|
|
|
|
|
snapshot.appendItems(beforeSelection, toSection: .feedBeforeStory)
|
2022-10-26 20:50:07 -06:00
|
|
|
snapshot.appendItems([selectedIndex], toSection: .selectedStory)
|
|
|
|
|
|
|
|
if selectedIndex + 1 < storyCount {
|
2023-01-21 16:20:53 -06:00
|
|
|
afterSelection = Array(selectedIndex + 1..<storyCount)
|
|
|
|
snapshot.appendItems(afterSelection, toSection: .feedAfterStory)
|
2022-10-26 20:50:07 -06:00
|
|
|
}
|
2022-09-03 19:55:21 -06:00
|
|
|
}
|
2022-09-02 20:39:00 -06:00
|
|
|
}
|
|
|
|
|
2022-10-26 20:50:07 -06:00
|
|
|
snapshot.appendItems([-1], toSection: .loading)
|
2023-01-21 16:20:53 -06:00
|
|
|
|
|
|
|
//TODO: 🚧 move the above logic into StoryCache
|
|
|
|
storyCache.appendStories(beforeSelection: beforeSelection, selectedIndex: selectedIndex, afterSelection: afterSelection)
|
2022-09-02 20:39:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
dataSource.apply(snapshot, animatingDifferences: false)
|
|
|
|
}
|
2020-08-27 21:26:12 -07:00
|
|
|
}
|
2023-01-21 16:20:53 -06:00
|
|
|
|
|
|
|
extension FeedDetailViewController: FeedDetailInteraction {
|
|
|
|
func storyAppeared(_ story: Story) {
|
|
|
|
print("\(story.title) appeared")
|
|
|
|
|
|
|
|
//TODO: 🚧: this logic is from checkScroll; some more stuff there that may be needed
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func storyTapped(_ story: Story) {
|
|
|
|
print("tapped \(story.title)")
|
|
|
|
|
|
|
|
let indexPath = IndexPath(row: story.index, section: 0)
|
|
|
|
|
|
|
|
//TODO: 🚧 change this function to work better with the grid view
|
|
|
|
collectionView(feedCollectionView, didSelectItemAt: indexPath)
|
|
|
|
}
|
|
|
|
|
2023-02-02 21:41:10 -06:00
|
|
|
func storyHidden(_ story: Story) {
|
|
|
|
print("hiding \(story.title)")
|
|
|
|
|
|
|
|
let indexPath = IndexPath(row: story.index, section: 0)
|
|
|
|
|
|
|
|
appDelegate.activeStory = nil
|
|
|
|
reload()
|
|
|
|
}
|
2023-01-21 16:20:53 -06:00
|
|
|
}
|