NewsBlur/clients/ios/Classes/FeedDetailViewController.swift

158 lines
6.1 KiB
Swift
Raw Normal View History

//
// FeedDetailViewController.swift
// NewsBlur
//
// Created by David Sinclair on 2020-08-27.
// Copyright © 2020 NewsBlur. All rights reserved.
//
import UIKit
/// List of stories for a feed.
class FeedDetailViewController: FeedDetailObjCViewController {
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
}
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
}
var dataSource: UICollectionViewDiffableDataSource<SectionLayoutKind, Int>! = nil
override func viewDidLoad() {
super.viewDidLoad()
2022-09-03 21:24:36 -06:00
if appDelegate.detailViewController.layout == .grid {
feedCollectionView.collectionViewLayout = createGridLayout()
} else {
feedCollectionView.collectionViewLayout = createListLayout()
}
2022-09-02 20:39:00 -06:00
configureDataSource()
}
@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-09-03 21:24:36 -06:00
func createListLayout() -> UICollectionViewLayout {
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)
}
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
let columns = isStory ? 1 : self.feedColumns
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)
let groupHeight = isStory ?
NSCollectionLayoutDimension.absolute(1000) :
NSCollectionLayoutDimension.fractionalWidth(0.4)
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-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
//TODO: 🚧
cell.contentView.backgroundColor = UIColor.blue
self.prepareStoryCell(cell, indexPath: indexPath)
}
dataSource = UICollectionViewDiffableDataSource<SectionLayoutKind, Int>(collectionView: feedCollectionView) {
(collectionView: UICollectionView, indexPath: IndexPath, identifier: Int) -> UICollectionViewCell? in
return SectionLayoutKind(rawValue: indexPath.section)! == .selectedStory ?
collectionView.dequeueConfiguredReusableCell(using: storyCellRegistration, for: indexPath, item: identifier) : collectionView.dequeueConfiguredReusableCell(using: feedCellRegistration, for: indexPath, item: identifier)
}
var snapshot = NSDiffableDataSourceSnapshot<SectionLayoutKind, Int>()
2022-09-03 21:24:36 -06:00
let storyCount = Int(appDelegate.storiesCollection.storyLocationsCount)
2022-09-02 20:39:00 -06:00
snapshot.appendSections(SectionLayoutKind.allCases)
if self.messageView.isHidden {
2022-09-03 21:24:36 -06:00
if appDelegate.detailViewController.layout == .grid, storyCount > 0 {
2022-09-02 20:39:00 -06:00
let selectedIndex = max(appDelegate.storiesCollection.indexOfActiveStory(), 0)
2022-09-03 19:55:21 -06:00
if selectedIndex > 0 {
snapshot.appendItems(Array(0..<selectedIndex - 1), toSection: .feedBeforeStory)
}
2022-09-02 20:39:00 -06:00
snapshot.appendItems([selectedIndex], toSection: .selectedStory)
2022-09-03 19:55:21 -06:00
2022-09-03 21:24:36 -06:00
if selectedIndex < storyCount {
2022-09-03 19:55:21 -06:00
snapshot.appendItems(Array(selectedIndex + 1..<storyCount), toSection: .feedAfterStory)
}
2022-09-03 21:24:36 -06:00
} else {
snapshot.appendItems(Array(0..<storyCount), toSection: .feedBeforeStory)
2022-09-02 20:39:00 -06:00
}
snapshot.appendItems([0], toSection: .loading)
}
dataSource.apply(snapshot, animatingDifferences: false)
}
}