Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix the size of floating panel just for iPad #45

Merged
merged 7 commits into from
Nov 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions Sources/InfomaniakCoreSwiftUI/Modifiers/FloatingPanelHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public struct SelfSizingPanelBackportViewModifier: ViewModifier {
@LazyInjectService private var platformDetector: PlatformDetectable

@Environment(\.dismiss) private var dismiss
@Environment(\.isCompactWindow) private var isCompactWindow

@State private var currentDetents: Set<Backport.PresentationDetent> = [.medium]

Expand Down Expand Up @@ -96,12 +97,12 @@ public struct SelfSizingPanelBackportViewModifier: ViewModifier {
return topPadding + titleSpacing + UIFont.preferredFont(forTextStyle: .headline).pointSize
}

private var shouldShowCloseButton: Bool {
return platformDetector.isMac || (UIDevice.current.orientation.isLandscape && UIDevice.current.userInterfaceIdiom != .pad)
private var isCompactMode: Bool {
return !isCompactWindow || (UIDevice.current.orientation.isLandscape && UIDevice.current.userInterfaceIdiom != .pad)
}

private var shouldShowHeader: Bool {
return title != nil || shouldShowCloseButton
return title != nil || isCompactMode
}

public init(dragIndicator: Visibility = Visibility.visible, title: String? = nil) {
Expand All @@ -118,10 +119,10 @@ public struct SelfSizingPanelBackportViewModifier: ViewModifier {
.font(Font(UIFont.preferredFont(forTextStyle: .headline)))
}

if shouldShowCloseButton {
if isCompactMode {
FloatingPanelCloseButton(size: .medium, dismissAction: dismiss)
.frame(maxWidth: .infinity, alignment: .trailing)
.padding(.trailing, value: .medium)
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.leading, value: .medium)
}
}
}
Expand All @@ -131,7 +132,8 @@ public struct SelfSizingPanelBackportViewModifier: ViewModifier {
.padding(.bottom, value: .medium)
}
.introspect(.scrollView, on: .iOS(.v15)) { scrollView in
guard !currentDetents.contains(.large) else { return }
guard isCompactMode, !currentDetents.contains(.large) else { return }

let totalPanelContentHeight = scrollView.contentSize.height + headerSize

scrollView.isScrollEnabled = totalPanelContentHeight > (scrollView.window?.bounds.height ?? 0)
Expand All @@ -141,8 +143,8 @@ public struct SelfSizingPanelBackportViewModifier: ViewModifier {
}
}
.padding(.top, topPadding)
.backport.presentationDragIndicator(backportDragIndicator)
.backport.presentationDetents(currentDetents)
.backport.presentationDragIndicator(isCompactWindow ? backportDragIndicator : .hidden)
.backport.presentationDetents(isCompactWindow ? currentDetents : [.large])
.ikPresentationCornerRadius(20)
}
}
Expand All @@ -152,6 +154,7 @@ public struct SelfSizingPanelViewModifier: ViewModifier {
@LazyInjectService private var platformDetector: PlatformDetectable

@Environment(\.dismiss) private var dismiss
@Environment(\.isCompactWindow) private var isCompactWindow

@State private var currentDetents: Set<PresentationDetent> = [.height(0)]
@State private var selection: PresentationDetent = .height(0)
Expand All @@ -169,12 +172,12 @@ public struct SelfSizingPanelViewModifier: ViewModifier {
return topPadding + titleSpacing + UIFont.preferredFont(forTextStyle: .headline).pointSize
}

private var shouldShowCloseButton: Bool {
return platformDetector.isMac || (UIDevice.current.orientation.isLandscape && UIDevice.current.userInterfaceIdiom != .pad)
private var isCompactMode: Bool {
return !isCompactWindow || (UIDevice.current.orientation.isLandscape && UIDevice.current.userInterfaceIdiom != .pad)
}

private var shouldShowHeader: Bool {
return title != nil || shouldShowCloseButton
return title != nil || isCompactMode
}

public init(dragIndicator: Visibility = Visibility.visible, title: String? = nil) {
Expand All @@ -191,10 +194,10 @@ public struct SelfSizingPanelViewModifier: ViewModifier {
.font(Font(UIFont.preferredFont(forTextStyle: .headline)))
}

if shouldShowCloseButton {
if isCompactMode {
FloatingPanelCloseButton(size: .medium, dismissAction: dismiss)
.frame(maxWidth: .infinity, alignment: .trailing)
.padding(.trailing, value: .medium)
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.leading, value: .medium)
}
}
}
Expand All @@ -204,6 +207,7 @@ public struct SelfSizingPanelViewModifier: ViewModifier {
.padding(.bottom, value: .medium)
}
.introspect(.scrollView, on: .iOS(.v16, .v17, .v18)) { scrollView in
guard isCompactMode else { return }
let totalPanelContentHeight = scrollView.contentSize.height + headerSize
guard selection != .height(totalPanelContentHeight) else { return }

Expand All @@ -221,8 +225,8 @@ public struct SelfSizingPanelViewModifier: ViewModifier {
}
}
.padding(.top, topPadding)
.presentationDetents(currentDetents, selection: $selection)
.presentationDragIndicator(dragIndicator)
.presentationDetents(isCompactWindow ? currentDetents : [.large], selection: $selection)
.presentationDragIndicator(isCompactWindow ? dragIndicator : .hidden)
.ikPresentationCornerRadius(20)
}
}
Loading