2023-01-21 16:20:53 -06:00
|
|
|
//
|
|
|
|
// FeedDetailGridView.swift
|
|
|
|
// NewsBlur
|
|
|
|
//
|
|
|
|
// Created by David Sinclair on 2023-01-19.
|
|
|
|
// Copyright © 2023 NewsBlur. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
2023-02-02 21:41:10 -06:00
|
|
|
/// A protocol of interaction between a card in the grid, and the enclosing feed detail view controller.
|
2023-01-21 16:20:53 -06:00
|
|
|
protocol FeedDetailInteraction {
|
2023-03-03 21:38:55 -07:00
|
|
|
var storyHeight: CGFloat { get }
|
|
|
|
|
|
|
|
func visible(story: Story)
|
|
|
|
func tapped(story: Story)
|
|
|
|
func reading(story: Story)
|
2023-03-21 21:54:21 -07:00
|
|
|
func read(story: Story)
|
2023-03-03 21:38:55 -07:00
|
|
|
func hid(story: Story)
|
2023-01-21 16:20:53 -06:00
|
|
|
}
|
|
|
|
|
2023-02-02 21:41:10 -06:00
|
|
|
/// A list or grid layout of story cards for the feed detail view.
|
2023-01-21 16:20:53 -06:00
|
|
|
struct FeedDetailGridView: View {
|
|
|
|
var feedDetailInteraction: FeedDetailInteraction
|
|
|
|
|
|
|
|
@ObservedObject var cache: StoryCache
|
|
|
|
|
2023-03-21 21:54:21 -07:00
|
|
|
@State private var scrollOffset = CGPoint()
|
|
|
|
|
|
|
|
let storyViewID = "storyViewID"
|
|
|
|
|
2023-01-21 16:20:53 -06:00
|
|
|
var columns: [GridItem] {
|
|
|
|
if cache.isGrid {
|
2023-02-02 21:41:10 -06:00
|
|
|
return Array(repeating: GridItem(.flexible(), spacing: 20), count: cache.settings.gridColumns)
|
2023-01-21 16:20:53 -06:00
|
|
|
} else {
|
2023-02-02 21:41:10 -06:00
|
|
|
return [GridItem(.flexible())]
|
2023-01-21 16:20:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var isOS15OrLater: Bool {
|
|
|
|
if #available(iOS 15.0, *) {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var cardHeight: CGFloat {
|
2023-02-02 21:41:10 -06:00
|
|
|
return cache.settings.gridHeight
|
2023-01-21 16:20:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var storyHeight: CGFloat {
|
2023-03-03 21:38:55 -07:00
|
|
|
print("Story height: \(feedDetailInteraction.storyHeight + 20)")
|
|
|
|
|
|
|
|
return feedDetailInteraction.storyHeight + 20
|
2023-01-21 16:20:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// let stories: [Story] = StoryCache.stories
|
|
|
|
|
|
|
|
var body: some View {
|
2023-03-21 21:54:21 -07:00
|
|
|
GeometryReader { reader in
|
|
|
|
// OffsetObservingScrollView(offset: $scrollOffset) {
|
|
|
|
ScrollView {
|
|
|
|
ScrollViewReader { scroller in
|
|
|
|
LazyVGrid(columns: columns, spacing: cache.isGrid ? 20 : 0) {
|
|
|
|
Section {
|
|
|
|
ForEach(cache.before, id: \.id) { story in
|
|
|
|
makeCardView(for: story, cache: cache, reader: reader)
|
|
|
|
}
|
2023-03-03 21:38:55 -07:00
|
|
|
}
|
2023-03-21 21:54:21 -07:00
|
|
|
|
|
|
|
if cache.isGrid {
|
|
|
|
EmptyView()
|
|
|
|
.id(storyViewID)
|
|
|
|
} else if let story = cache.selected {
|
|
|
|
makeCardView(for: story, cache: cache, reader: reader)
|
|
|
|
.id(story.id)
|
2023-03-03 21:38:55 -07:00
|
|
|
}
|
2023-03-21 21:54:21 -07:00
|
|
|
|
|
|
|
Section(header: makeStoryView(cache: cache)) {
|
|
|
|
ForEach(cache.after, id: \.id) { story in
|
|
|
|
makeCardView(for: story, cache: cache, reader: reader)
|
|
|
|
// .transformAnchorPreference(key: MyKey.self, value: .bounds) {
|
|
|
|
// $0.append(MyFrame(id: story.id.uuidString, frame: reader[$1]))
|
|
|
|
// }
|
|
|
|
// .onPreferenceChange(MyKey.self) {
|
|
|
|
// print("pref change for '\(story.title)': \($0)")
|
|
|
|
// // Handle content frame changes here
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// .coordinateSpace(name: "GridView")
|
2023-03-03 21:38:55 -07:00
|
|
|
}
|
2023-03-21 21:54:21 -07:00
|
|
|
.onChange(of: cache.selected) { [oldSelected = cache.selected] newSelected in
|
|
|
|
if oldSelected?.hash == newSelected?.hash {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
print("\(oldSelected?.title ?? "none") -> \(newSelected?.title ?? "none")")
|
|
|
|
|
|
|
|
Task {
|
|
|
|
// try await Task.sleep(nanoseconds: 3_000_000_000)
|
|
|
|
if cache.isGrid {
|
|
|
|
withAnimation(Animation.spring().delay(0.5)) {
|
|
|
|
scroller.scrollTo(storyViewID, anchor: .top)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
withAnimation(Animation.spring().delay(0.5)) {
|
|
|
|
scroller.scrollTo(newSelected?.id)
|
|
|
|
}
|
|
|
|
}
|
2023-03-03 21:38:55 -07:00
|
|
|
}
|
2023-01-21 16:20:53 -06:00
|
|
|
}
|
2023-03-21 21:54:21 -07:00
|
|
|
// .onChange(of: scrollOffset) { [oldOffset = scrollOffset] newOffset in
|
|
|
|
// print("scrolled \(oldOffset) -> \(newOffset)")
|
|
|
|
//
|
|
|
|
// scrolled(offset: newOffset.y)
|
|
|
|
// }
|
|
|
|
.if(cache.isGrid) { view in
|
|
|
|
view.padding()
|
|
|
|
}
|
2023-03-03 21:38:55 -07:00
|
|
|
}
|
2023-02-02 21:41:10 -06:00
|
|
|
}
|
2023-01-21 16:20:53 -06:00
|
|
|
}
|
2023-03-02 15:31:21 -07:00
|
|
|
.background(Color.themed([0xF4F4F4, 0xFFFDEF, 0x4F4F4F, 0x101010]))
|
2023-01-21 16:20:53 -06:00
|
|
|
}
|
|
|
|
|
2023-02-02 21:41:10 -06:00
|
|
|
@ViewBuilder
|
2023-03-21 21:54:21 -07:00
|
|
|
func makeCardView(for story: Story, cache: StoryCache, reader: GeometryProxy) -> some View {
|
2023-02-02 21:41:10 -06:00
|
|
|
CardView(cache: cache, story: loaded(story: story))
|
2023-03-21 21:54:21 -07:00
|
|
|
.transformAnchorPreference(key: CardKey.self, value: .bounds) {
|
|
|
|
$0.append(CardFrame(id: "\(story.id)", frame: reader[$1]))
|
|
|
|
}
|
|
|
|
.onPreferenceChange(CardKey.self) {
|
|
|
|
print("pref change for '\(story.title)': \($0)")
|
|
|
|
|
|
|
|
if let value = $0.first, value.frame.minY < 0 {
|
|
|
|
print("pref '\(story.title)': scrolled off the top")
|
|
|
|
|
|
|
|
feedDetailInteraction.read(story: story)
|
|
|
|
}
|
|
|
|
}
|
2023-01-21 16:20:53 -06:00
|
|
|
.onAppear {
|
2023-03-03 21:38:55 -07:00
|
|
|
feedDetailInteraction.visible(story: story)
|
2023-01-21 16:20:53 -06:00
|
|
|
}
|
|
|
|
.onTapGesture {
|
2023-03-03 21:38:55 -07:00
|
|
|
feedDetailInteraction.tapped(story: story)
|
2023-01-21 16:20:53 -06:00
|
|
|
}
|
|
|
|
.if(cache.isGrid) { view in
|
|
|
|
view.frame(height: cardHeight)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder
|
|
|
|
func makeStoryView(cache: StoryCache) -> some View {
|
|
|
|
if cache.isGrid, let story = cache.selected {
|
2023-02-02 21:41:10 -06:00
|
|
|
StoryView(cache: cache, story: loaded(story: story), interaction: feedDetailInteraction)
|
2023-03-03 21:38:55 -07:00
|
|
|
// .frame(height: storyHeight)
|
2023-01-21 16:20:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func loaded(story: Story) -> Story {
|
|
|
|
story.load()
|
2023-03-21 21:54:21 -07:00
|
|
|
|
|
|
|
print("Loaded story '\(story.title)")
|
|
|
|
|
2023-01-21 16:20:53 -06:00
|
|
|
return story
|
|
|
|
}
|
2023-03-21 21:54:21 -07:00
|
|
|
|
|
|
|
// func scrolled(offset: CGFloat) {
|
|
|
|
// guard let story = cache.all.first(where: { $0.frame.midY > offset }) else {
|
|
|
|
// print("scrolled to \(offset); didn't find story")
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// print("scrolled to \(offset); story: \(story.title) has frame: \(story.frame)")
|
|
|
|
//
|
|
|
|
// //TODO: 🚧
|
|
|
|
// }
|
2023-01-21 16:20:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
//struct FeedDetailGridView_Previews: PreviewProvider {
|
|
|
|
// static var previews: some View {
|
|
|
|
// FeedDetailGridView(feedDetailInteraction: FeedDetailViewController(), storyCache: StoryCache())
|
|
|
|
// }
|
|
|
|
//}
|
2023-03-21 21:54:21 -07:00
|
|
|
|
|
|
|
struct CardFrame : Equatable {
|
|
|
|
let id : String
|
|
|
|
let frame : CGRect
|
|
|
|
|
|
|
|
static func == (lhs: CardFrame, rhs: CardFrame) -> Bool {
|
|
|
|
lhs.id == rhs.id && lhs.frame == rhs.frame
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CardKey : PreferenceKey {
|
|
|
|
typealias Value = [CardFrame]
|
|
|
|
|
|
|
|
static var defaultValue: [CardFrame] = []
|
|
|
|
|
|
|
|
static func reduce(value: inout [CardFrame], nextValue: () -> [CardFrame]) {
|
|
|
|
value.append(contentsOf: nextValue())
|
|
|
|
}
|
|
|
|
}
|