-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scroll to verse when orientation changes (#643)
- Loading branch information
1 parent
6a786a2
commit 775a63e
Showing
5 changed files
with
87 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// QuranScrollingViewModifier.swift | ||
// | ||
// | ||
// Created by Mohamed Afifi on 2024-08-03. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct QuranScrollingViewModifier<Value: Equatable, ID: Hashable>: ViewModifier { | ||
let scrollToValue: Value? | ||
let transform: (Value) -> ID? | ||
@State private var scrollToValueRequest: Value? | ||
|
||
func body(content: Content) -> some View { | ||
ScrollViewReader { scrollView in | ||
content | ||
.onChange(of: scrollToValue) { scrollToValueRequest = $0 } | ||
.onChange(of: scrollToValueRequest) { scrollToValue in | ||
if let scrollToValue, let id = transform(scrollToValue) { | ||
withAnimation { | ||
scrollView.scrollTo(id, anchor: UnitPoint(x: 0, y: 0.2)) | ||
} | ||
scrollToValueRequest = nil | ||
} | ||
} | ||
} | ||
.onSizeChange { _ in | ||
scrollToValueRequest = scrollToValue | ||
} | ||
} | ||
} | ||
|
||
extension View { | ||
public func quranScrolling<Value: Equatable>( | ||
scrollToValue: Value?, | ||
transform: @escaping (Value) -> (some Hashable)? | ||
) -> some View { | ||
modifier(QuranScrollingViewModifier(scrollToValue: scrollToValue, transform: transform)) | ||
} | ||
|
||
public func quranScrolling(scrollToValue: (some Hashable)?) -> some View { | ||
modifier(QuranScrollingViewModifier(scrollToValue: scrollToValue) { $0 }) | ||
} | ||
} |