From ea93f3926e8cb1cc45b84b31939c2fccd7f0cf5f Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Thu, 17 Feb 2022 15:36:41 -0800 Subject: [PATCH] WiP --- .../Sources/Views/Article/WebAppView.swift | 2 +- .../Views/Article/WebAppViewCoordinator.swift | 30 ++-- .../LinkedItemDetail/LinkItemDetailView.swift | 131 ++++++++++-------- 3 files changed, 91 insertions(+), 72 deletions(-) diff --git a/apple/OmnivoreKit/Sources/Views/Article/WebAppView.swift b/apple/OmnivoreKit/Sources/Views/Article/WebAppView.swift index 6d7286f2c..0176a7dd7 100644 --- a/apple/OmnivoreKit/Sources/Views/Article/WebAppView.swift +++ b/apple/OmnivoreKit/Sources/Views/Article/WebAppView.swift @@ -29,7 +29,7 @@ import WebKit let webView = WebView(frame: CGRect.zero) let contentController = WKUserContentController() - webView.scrollView.isScrollEnabled = true + webView.scrollView.contentInset.top = LinkItemDetailView.navBarHeight webView.navigationDelegate = context.coordinator webView.isOpaque = false webView.backgroundColor = UIColor.clear diff --git a/apple/OmnivoreKit/Sources/Views/Article/WebAppViewCoordinator.swift b/apple/OmnivoreKit/Sources/Views/Article/WebAppViewCoordinator.swift index e741a70f6..5cd8010f5 100644 --- a/apple/OmnivoreKit/Sources/Views/Article/WebAppViewCoordinator.swift +++ b/apple/OmnivoreKit/Sources/Views/Article/WebAppViewCoordinator.swift @@ -20,6 +20,7 @@ final class WebAppViewCoordinator: NSObject { var navBarVisibilityRatio: Double = 1.0 { didSet { isNavBarHidden = navBarVisibilityRatio == 0 + print(navBarVisibilityRatio) updateNavBarVisibilityRatio(navBarVisibilityRatio) } } @@ -48,24 +49,31 @@ extension WebAppViewCoordinator: WKNavigationDelegate { extension WebAppViewCoordinator: UIScrollViewDelegate { func scrollViewWillBeginDragging(_ scrollView: UIScrollView) { hasDragged = true - yOffsetAtStartOfDrag = scrollView.contentOffset.y + yOffsetAtStartOfDrag = scrollView.contentOffset.y + scrollView.contentInset.top } func scrollViewDidScroll(_ scrollView: UIScrollView) { guard hasDragged else { return } - let yOffset = scrollView.contentOffset.y + let yOffset = scrollView.contentOffset.y + scrollView.contentInset.top if yOffset == 0 { - let additionalOffset = (1 - navBarVisibilityRatio) * navBarHeight - scrollView.contentOffset.y += additionalOffset + scrollView.contentInset.top = navBarHeight navBarVisibilityRatio = 1 return } + if yOffset < 0 { + navBarVisibilityRatio = 1 + scrollView.contentInset.top = navBarHeight + return + } + if yOffset < navBarHeight { let isScrollingUp = yOffsetAtStartOfDrag ?? 0 > yOffset - navBarVisibilityRatio = isScrollingUp ? 1 : 1 - (yOffset / navBarHeight) + navBarVisibilityRatio = isScrollingUp || yOffset < 0 ? 1 : min(1, 1 - (yOffset / navBarHeight)) + print("parkour!", navBarVisibilityRatio, isScrollingUp, yOffsetAtStartOfDrag, yOffset) + scrollView.contentInset.top = navBarVisibilityRatio * navBarHeight return } @@ -74,22 +82,22 @@ extension WebAppViewCoordinator: UIScrollViewDelegate { if yOffset > yOffsetAtStartOfDrag, !isNavBarHidden { let translation = yOffset - yOffsetAtStartOfDrag let ratio = translation < navBarHeight ? 1 - (translation / navBarHeight) : 0 - navBarVisibilityRatio = ratio + navBarVisibilityRatio = min(ratio, 1) +// print("bike!", navBarVisibilityRatio) + scrollView.contentInset.top = navBarVisibilityRatio * navBarHeight } } func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) { - if decelerate, scrollView.contentOffset.y < (yOffsetAtStartOfDrag ?? 0) { - let additionalOffset = (1 - navBarVisibilityRatio) * navBarHeight - scrollView.contentOffset.y += additionalOffset + if decelerate, scrollView.contentOffset.y + scrollView.contentInset.top < (yOffsetAtStartOfDrag ?? 0) { + scrollView.contentInset.top = navBarHeight navBarVisibilityRatio = 1 } yOffsetAtStartOfDrag = nil } func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool { - let additionalOffset = (1 - navBarVisibilityRatio) * navBarHeight - scrollView.contentOffset.y += additionalOffset + scrollView.contentInset.top = navBarHeight navBarVisibilityRatio = 1 return false } diff --git a/apple/OmnivoreKit/Sources/Views/LinkedItemDetail/LinkItemDetailView.swift b/apple/OmnivoreKit/Sources/Views/LinkedItemDetail/LinkItemDetailView.swift index b8c4654e3..c107daf51 100644 --- a/apple/OmnivoreKit/Sources/Views/LinkedItemDetail/LinkItemDetailView.swift +++ b/apple/OmnivoreKit/Sources/Views/LinkedItemDetail/LinkItemDetailView.swift @@ -73,73 +73,84 @@ public struct LinkItemDetailView: View { #endif } - @ViewBuilder private var compactInnerBody: some View { - VStack(spacing: 0) { - withAnimation { - HStack(alignment: .center) { - Button( - action: { self.presentationMode.wrappedValue.dismiss() }, - label: { - Image(systemName: "chevron.backward") - .font(.appTitleTwo) - .foregroundColor(.appGrayTextContrast) - .padding(.horizontal) - } - ) - .scaleEffect(navBarVisibilityRatio) - Spacer() - Button( - action: { showFontSizePopover.toggle() }, - label: { - Image(systemName: "textformat.size") - .font(.appTitleTwo) - } - ) - .padding(.horizontal) - .scaleEffect(navBarVisibilityRatio) + var navBar: some View { + HStack(alignment: .center) { + Button( + action: { self.presentationMode.wrappedValue.dismiss() }, + label: { + Image(systemName: "chevron.backward") + .font(.appTitleTwo) + .foregroundColor(.appGrayTextContrast) + .padding(.horizontal) } - .frame(height: LinkItemDetailView.navBarHeight * navBarVisibilityRatio) - .opacity(navBarVisibilityRatio) - } - if let webAppWrapperViewModel = viewModel.webAppWrapperViewModel { - ZStack { - WebAppWrapperView( - viewModel: webAppWrapperViewModel, - navBarVisibilityRatioUpdater: { - if $0 < 1 { + ) + .scaleEffect(navBarVisibilityRatio) + Spacer() + Button( + action: { showFontSizePopover.toggle() }, + label: { + Image(systemName: "textformat.size") + .font(.appTitleTwo) + } + ) + .padding(.horizontal) + .scaleEffect(navBarVisibilityRatio) + } + .frame(height: LinkItemDetailView.navBarHeight * navBarVisibilityRatio) + .opacity(navBarVisibilityRatio) + .background(Color.systemBackground) + } + + @ViewBuilder private var compactInnerBody: some View { + if let webAppWrapperViewModel = viewModel.webAppWrapperViewModel { + ZStack { + WebAppWrapperView( + viewModel: webAppWrapperViewModel, + navBarVisibilityRatioUpdater: { + if $0 < 1 { + showFontSizePopover = false + } + navBarVisibilityRatio = $0 + } + ) + if showFontSizePopover { + VStack { + Color.clear + .contentShape(Rectangle()) + .frame(height: LinkItemDetailView.navBarHeight) + HStack { + Spacer() + fontAdjustmentPopoverView + .background(Color.appButtonBackground) + .cornerRadius(8) + .padding(.trailing, 5) + } + Spacer() + } + .background( + Color.clear + .contentShape(Rectangle()) + .onTapGesture { showFontSizePopover = false } - navBarVisibilityRatio = $0 - } ) - if showFontSizePopover { - VStack { - HStack { - Spacer() - fontAdjustmentPopoverView - .background(Color.appButtonBackground) - .cornerRadius(8) - .padding(.trailing, 5) - } - Spacer() - } - .background( - Color.clear - .contentShape(Rectangle()) - .onTapGesture { - showFontSizePopover = false - } - ) - } } - } else { - Spacer() - .onAppear { - viewModel.performActionSubject.send(.load) - } + VStack(spacing: 0) { + navBar + Spacer() + } } + .navigationBarHidden(true) + } else { + VStack(spacing: 0) { + navBar + Spacer() + } + .onAppear { + viewModel.performActionSubject.send(.load) + } + .navigationBarHidden(true) } - .navigationBarHidden(true) } @ViewBuilder private var innerBody: some View {