Skip to content

Commit

Permalink
refactor: Create a generic ViewGeometry component
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinperignon committed Jun 6, 2023
1 parent 1c7178c commit 0e07415
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 32 deletions.
18 changes: 4 additions & 14 deletions Mail/Components/BottomBarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,10 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import MailCore
import MailResources
import SwiftUI

struct BottomSafeAreaKey: PreferenceKey {
static var defaultValue: CGFloat = .zero

static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
value = nextValue()
}
}

struct BottomBar<Items: View>: ViewModifier {
let isVisible: Bool
@ViewBuilder var items: () -> Items
Expand Down Expand Up @@ -59,19 +52,16 @@ struct BottomBarView<Items: View>: View {
items()
Spacer(minLength: 8)
}
.padding(.top, 8)
.padding(.bottom, hasBottomSafeArea ? 4 : 8)
.padding(.top, UIConstants.bottomBarVerticalPadding)
.padding(.bottom, hasBottomSafeArea ? UIConstants.bottomBarSmallVerticalPadding : UIConstants.bottomBarVerticalPadding)
.background(MailResourcesAsset.backgroundTabBarColor.swiftUIColor)
.overlay(alignment: .top) {
Divider()
.frame(height: 1)
.overlay(Color(uiColor: .systemGray3))
}
.overlay {
GeometryReader { proxy in
Color.clear
.preference(key: BottomSafeAreaKey.self, value: proxy.safeAreaInsets.bottom)
}
ViewGeometry(key: BottomSafeAreaKey.self, property: \.safeAreaInsets.bottom)
}
.onPreferenceChange(BottomSafeAreaKey.self) { value in
hasBottomSafeArea = value > 0
Expand Down
47 changes: 47 additions & 0 deletions Mail/Utils/GeometryReaderHelpers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Infomaniak Mail - iOS App
Copyright (C) 2022 Infomaniak Network SA
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import SwiftUI

struct ViewWidthKey: PreferenceKey {
static var defaultValue: CGFloat = .zero

static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
value = max(value, nextValue())
}
}

struct BottomSafeAreaKey: PreferenceKey {
static var defaultValue: CGFloat = .zero

static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
value = nextValue()
}
}

struct ViewGeometry<K>: View where K: PreferenceKey {
let key: K.Type
let property: KeyPath<GeometryProxy, K.Value>

var body: some View {
GeometryReader { proxy in
Color.clear
.preference(key: key, value: proxy[keyPath: property])
}
}
}
19 changes: 1 addition & 18 deletions Mail/Views/Thread/MessageHeaderDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,6 @@ import RealmSwift
import SwiftUI
import WrappingHStack

struct ViewWidthKey: PreferenceKey {
static var defaultValue: CGFloat = .zero

static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
value = max(value, nextValue())
}
}

struct ViewGeometry: View {
var body: some View {
GeometryReader { geometry in
Color.clear
.preference(key: ViewWidthKey.self, value: geometry.size.width)
}
}
}

struct MessageHeaderDetailView: View {
@ObservedRealmObject var message: Message

Expand Down Expand Up @@ -109,7 +92,7 @@ struct RecipientLabel: View {
HStack(alignment: .top) {
Text(title)
.textStyle(.bodySmallSecondary)
.background(ViewGeometry())
.background(ViewGeometry(key: ViewWidthKey.self, property: \.size.width))
.frame(width: labelWidth, alignment: .leading)
VStack(alignment: .leading, spacing: 4) {
ForEach(recipients, id: \.self) { recipient in
Expand Down
3 changes: 3 additions & 0 deletions MailCore/UI/UIConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ public enum UIConstants {
public static let buttonsRadius: CGFloat = 16
public static let buttonsIconSize: CGFloat = 16

public static let bottomBarVerticalPadding: CGFloat = 8
public static let bottomBarSmallVerticalPadding: CGFloat = 4

public static let bottomSheetHorizontalPadding: CGFloat = 24

public static let componentsMaxWidth: CGFloat = 496
Expand Down

0 comments on commit 0e07415

Please sign in to comment.