Skip to content

Commit

Permalink
Scroll position management
Browse files Browse the repository at this point in the history
  • Loading branch information
livid committed Dec 23, 2023
1 parent 786acdb commit 50b5f23
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Planet/Entities/DraftModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,9 @@ class DraftModel: Identifiable, Equatable, Hashable, Codable, ObservableObject {
else {
PlanetStore.shared.selectedArticle = article
}
Task(priority: .userInitiated) {
NotificationCenter.default.post(name: .scrollToArticle, object: article)
}
}
}
}
Expand All @@ -493,6 +496,9 @@ class DraftModel: Identifiable, Equatable, Hashable, Codable, ObservableObject {
else {
PlanetStore.shared.selectedArticle = article
}
Task(priority: .userInitiated) {
NotificationCenter.default.post(name: .scrollToArticle, object: article)
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions Planet/Helper/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ extension Notification.Name {
static let myArticleBuilt = Notification.Name("PlanetMyArticleBuiltNotification")

static let copiedIPNS = Notification.Name("PlanetCopiedIPNSNotification")

static let scrollToTopArticleList = Notification.Name("PlanetScrollToTopArticleListNotification")
static let scrollToArticle = Notification.Name("PlanetScrollToArticleNotification")
}

// Writer
Expand Down
3 changes: 3 additions & 0 deletions Planet/Search/SearchView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ struct SearchView: View {
planetStore.selectedView = .myPlanet(article.planet)
Task(priority: .userInitiated) { @MainActor in
planetStore.selectedArticle = article
Task(priority: .userInitiated) { @MainActor in
NotificationCenter.default.post(name: .scrollToArticle, object: article)
}
}
}
dismiss()
Expand Down
20 changes: 20 additions & 0 deletions Planet/Views/Articles/ArticleListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,39 @@ struct ArticleListView: View {
if let myArticle = article as? MyArticleModel {
if #available(macOS 13.0, *) {
MyArticleItemView(article: myArticle)
.id(myArticle.id.uuidString)
.listRowSeparator(.visible)
}
else {
MyArticleItemView(article: myArticle)
.id(myArticle.id.uuidString)
}
}
else if let followingArticle = article as? FollowingArticleModel {
if #available(macOS 13.0, *) {
FollowingArticleItemView(article: followingArticle)
.id(followingArticle.id.uuidString)
.listRowSeparator(.visible)
}
else {
FollowingArticleItemView(article: followingArticle)
.id(followingArticle.id.uuidString)
}
}
}
.onReceive(NotificationCenter.default.publisher(for: .scrollToTopArticleList)) { n in
if let article = articles.first {
debugPrint("Scrolling to top of Article List: \(article)")
withAnimation {
proxy.scrollTo(article.id.uuidString, anchor: .top)
}
}
}
.onReceive(NotificationCenter.default.publisher(for: .scrollToArticle)) { n in
if let article = n.object as? ArticleModel {
debugPrint("Scrolling to Article: \(article)")
withAnimation {
proxy.scrollTo(article.id.uuidString, anchor: .center)
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions Planet/Views/Articles/ArticleView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ struct ArticleView: View {
} label: {
Image(systemName: "square.and.pencil")
}

if let plausibleEnabled = planet.plausibleEnabled, plausibleEnabled {
Button {
isShowingAnalyticsPopover = true
Expand Down Expand Up @@ -562,6 +563,11 @@ struct ArticleView: View {
Image("custom.juicebox")
}.help("Visit Juicebox Project")
}
Button {
NotificationCenter.default.post(name: .scrollToTopArticleList, object: nil)
} label: {
Image(systemName: "arrow.up")
}
default:
Text("")
}
Expand Down

0 comments on commit 50b5f23

Please sign in to comment.