use scroll view delegate to watch scroll changes and update nav bar visibility

This commit is contained in:
Satindar Dhillon
2022-02-15 22:16:01 -08:00
parent b0005e301c
commit 7575450121
4 changed files with 111 additions and 5 deletions

View File

@ -10,6 +10,7 @@ import WebKit
let rawAuthCookie: String?
let openLinkAction: (URL) -> Void
let webViewActionHandler: (WKScriptMessage) -> Void
let navBarVisibilityRatioUpdater: (Double) -> Void
@Binding var annotation: String
@Binding var annotationSaveTransactionID: UUID?
@Binding var sendIncreaseFontSignal: Bool
@ -33,6 +34,7 @@ import WebKit
webView.isOpaque = false
webView.backgroundColor = UIColor.clear
webView.configuration.userContentController = contentController
webView.scrollView.delegate = context.coordinator
for action in WebViewAction.allCases {
webView.configuration.userContentController.add(context.coordinator, name: action.rawValue)
@ -53,6 +55,7 @@ import WebKit
context.coordinator.linkHandler = openLinkAction
context.coordinator.webViewActionHandler = webViewActionHandler
context.coordinator.updateNavBarVisibilityRatio = navBarVisibilityRatioUpdater
return webView
}

View File

@ -1,6 +1,4 @@
// import Models
import SwiftUI
// import Utils
import WebKit
final class WebAppViewCoordinator: NSObject {
@ -8,6 +6,10 @@ final class WebAppViewCoordinator: NSObject {
var linkHandler: (URL) -> Void = { _ in }
var needsReload = true
var lastSavedAnnotationID: UUID?
var updateNavBarVisibilityRatio: (Double) -> Void = { _ in }
private var yOffsetAtStartOfDrag: Double?
private var lastYOffset: Double = 0
private var hasDragged = false
override init() {
super.init()
@ -34,6 +36,44 @@ extension WebAppViewCoordinator: WKNavigationDelegate {
}
}
extension WebAppViewCoordinator: UIScrollViewDelegate {
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
hasDragged = true
yOffsetAtStartOfDrag = scrollView.contentOffset.y
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
guard hasDragged else { return }
let yOffset = scrollView.contentOffset.y
if yOffset <= 0 {
updateNavBarVisibilityRatio(1)
return
}
if yOffset < 30 {
updateNavBarVisibilityRatio(1) // yOffset / 30)
return
}
guard let yOffsetAtStartOfDrag = yOffsetAtStartOfDrag else { return }
if yOffset > yOffsetAtStartOfDrag {
let translation = yOffset - yOffsetAtStartOfDrag
let ratio = 0.0 // translation < 30 ? translation / 30 : 0
updateNavBarVisibilityRatio(ratio)
}
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if decelerate, scrollView.contentOffset.y < (yOffsetAtStartOfDrag ?? 0) {
updateNavBarVisibilityRatio(1)
}
yOffsetAtStartOfDrag = nil
}
}
struct WebViewConfig {
let url: URL
let themeId: String

View File

@ -35,9 +35,11 @@ public struct WebAppWrapperView: View {
@State private var annotation = String()
@State var annotationSaveTransactionID: UUID?
@State var safariWebLink: SafariWebLink?
let navBarVisibilityRatioUpdater: (Double) -> Void
public init(viewModel: WebAppWrapperViewModel) {
public init(viewModel: WebAppWrapperViewModel, navBarVisibilityRatioUpdater: ((Double) -> Void)? = nil) {
self.viewModel = viewModel
self.navBarVisibilityRatioUpdater = navBarVisibilityRatioUpdater ?? { _ in }
}
public var body: some View {
@ -53,6 +55,7 @@ public struct WebAppWrapperView: View {
#endif
},
webViewActionHandler: webViewActionHandler,
navBarVisibilityRatioUpdater: navBarVisibilityRatioUpdater,
annotation: $annotation,
annotationSaveTransactionID: $annotationSaveTransactionID,
sendIncreaseFontSignal: $viewModel.sendIncreaseFontSignal,

View File

@ -25,8 +25,11 @@ public final class LinkItemDetailViewModel: ObservableObject {
}
public struct LinkItemDetailView: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
@ObservedObject private var viewModel: LinkItemDetailViewModel
@State private var showFontSizePopover = false
@State private var navBarVisibilityRatio = 1.0
public init(viewModel: LinkItemDetailViewModel) {
self.viewModel = viewModel
@ -51,12 +54,69 @@ public struct LinkItemDetailView: View {
}
public var body: some View {
innerBody
#if os(iOS)
.navigationBarTitleDisplayMode(.inline)
if UIDevice.isIPhone, !viewModel.item.isPDF {
compactInnerBody
} else {
innerBody
}
#else
innerBody
#endif
}
@ViewBuilder private var compactInnerBody: some View {
VStack {
withAnimation {
HStack(alignment: .center) {
Button(
action: { self.presentationMode.wrappedValue.dismiss() },
label: {
Image(systemName: "chevron.backward")
.font(.appTitleThree)
.foregroundColor(.appGrayTextContrast)
.padding(.horizontal)
.padding(.bottom, 5)
}
)
Spacer()
Button(
action: { showFontSizePopover = true },
label: {
Image(systemName: "textformat.size")
}
)
.padding(.horizontal)
#if os(iOS)
.fittedPopover(isPresented: $showFontSizePopover) {
FontSizeAdjustmentPopoverView(
increaseFontAction: { viewModel.webAppWrapperViewModel?.sendIncreaseFontSignal = true },
decreaseFontAction: { viewModel.webAppWrapperViewModel?.sendDecreaseFontSignal = true }
)
}
#endif
}
.scaleEffect(x: 1, y: navBarVisibilityRatio)
.frame(height: 30 * navBarVisibilityRatio)
}
if let webAppWrapperViewModel = viewModel.webAppWrapperViewModel {
WebAppWrapperView(
viewModel: webAppWrapperViewModel,
navBarVisibilityRatioUpdater: {
print($0)
navBarVisibilityRatio = $0
}
)
} else {
Spacer()
.onAppear {
viewModel.performActionSubject.send(.load)
}
}
}
.navigationBarHidden(true)
}
@ViewBuilder private var innerBody: some View {
if let pdfURL = viewModel.item.pdfURL {
#if os(iOS)