Fix scrolling view on resource change

This commit is contained in:
Arkadiusz Fal 2021-09-30 01:29:18 +02:00
parent e270b7b213
commit 3a37ea656a
3 changed files with 13 additions and 18 deletions

View File

@ -33,7 +33,7 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "37D4B0D32671614900C925CA"
BuildableName = "Tests iOS.xctest"
BuildableName = "Tests (iOS).xctest"
BlueprintName = "Tests (iOS)"
ReferencedContainer = "container:Pearvidious.xcodeproj">
</BuildableReference>

View File

@ -9,6 +9,8 @@ struct VideosCellsHorizontal: View {
var videos = [Video]()
var body: some View {
EmptyView().id("cellsTop")
ScrollViewReader { scrollView in
ScrollView(.horizontal, showsIndicators: false) {
LazyHStack(spacing: 20) {
@ -32,19 +34,12 @@ struct VideosCellsHorizontal: View {
.padding(.vertical, 20)
#endif
}
.onAppear {
if let video = videos.first {
scrollView.scrollTo(video.id, anchor: .leading)
}
}
.onChange(of: videos) { [videos] newVideos in
#if !os(tvOS)
guard !videos.isEmpty, let video = newVideos.first else {
return
}
guard !videos.isEmpty else {
return
}
scrollView.scrollTo(video.id, anchor: .leading)
#endif
scrollView.scrollTo("cellsTop", anchor: .leading)
}
}
#if os(tvOS)

View File

@ -9,6 +9,8 @@ struct VideosCellsVertical: View {
var videos = [Video]()
var body: some View {
EmptyView().id("cellsTop")
ScrollViewReader { scrollView in
ScrollView(.vertical, showsIndicators: scrollViewShowsIndicators) {
LazyVGrid(columns: items, alignment: .center) {
@ -22,13 +24,11 @@ struct VideosCellsVertical: View {
.padding()
}
.onChange(of: videos) { [videos] newVideos in
#if !os(tvOS)
guard !videos.isEmpty, let video = newVideos.first else {
return
}
guard !videos.isEmpty else {
return
}
scrollView.scrollTo(video.id, anchor: .top)
#endif
scrollView.scrollTo("cellsTop", anchor: .top)
}
#if os(tvOS)
.padding(.horizontal, 10)