From b212c51679ebd2a13a19af96803a8dca4741a7ca Mon Sep 17 00:00:00 2001 From: Valentin Perignon Date: Tue, 23 Apr 2024 15:35:09 +0200 Subject: [PATCH] feat: Use Environment macros --- .../IKButton/View+IKButtonEnvironment.swift | 43 +++---------------- Mail/Utils/Environment+Extension.swift | 13 ++---- .../Menu Drawer/Folders/FolderCell.swift | 11 ++--- .../MailboxManagement/MailboxCell.swift | 11 ++--- ...ettingsNotificationsInstructionsView.swift | 1 - Mail/Views/SheetViewModifier.swift | 15 ++----- Package.resolved | 18 ++++++++ Package.swift | 3 +- Project.swift | 6 ++- 9 files changed, 43 insertions(+), 78 deletions(-) diff --git a/Mail/Components/Buttons/IKButton/View+IKButtonEnvironment.swift b/Mail/Components/Buttons/IKButton/View+IKButtonEnvironment.swift index ee13a405fd..fbf4d22838 100644 --- a/Mail/Components/Buttons/IKButton/View+IKButtonEnvironment.swift +++ b/Mail/Components/Buttons/IKButton/View+IKButtonEnvironment.swift @@ -17,47 +17,16 @@ */ import SwiftUI - -// MARK: - EnvironmentKey - -struct IKButtonPrimaryStyleKey: EnvironmentKey { - static var defaultValue: any ShapeStyle = TintShapeStyle.tint -} - -struct IKButtonSecondaryStyleKey: EnvironmentKey { - static var defaultValue: any ShapeStyle = UserDefaults.shared.accentColor.onAccent.swiftUIColor -} - -struct IKButtonFullWidthKey: EnvironmentKey { - static var defaultValue = false -} - -struct IKButtonLoadingKey: EnvironmentKey { - static var defaultValue = false -} +import SwiftUIMacros // MARK: - EnvironmentValues +@EnvironmentStorage extension EnvironmentValues { - var ikButtonPrimaryStyle: any ShapeStyle { - get { self[IKButtonPrimaryStyleKey.self] } - set { self[IKButtonPrimaryStyleKey.self] = newValue } - } - - var ikButtonSecondaryStyle: any ShapeStyle { - get { self[IKButtonSecondaryStyleKey.self] } - set { self[IKButtonSecondaryStyleKey.self] = newValue } - } - - var ikButtonFullWidth: Bool { - get { self[IKButtonFullWidthKey.self] } - set { self[IKButtonFullWidthKey.self] = newValue } - } - - var ikButtonLoading: Bool { - get { self[IKButtonLoadingKey.self] } - set { self[IKButtonLoadingKey.self] = newValue } - } + var ikButtonPrimaryStyle: any ShapeStyle = TintShapeStyle.tint + var ikButtonSecondaryStyle: any ShapeStyle = UserDefaults.shared.accentColor.onAccent.swiftUIColor + var ikButtonFullWidth = false + var ikButtonLoading = false } // MARK: - View functions diff --git a/Mail/Utils/Environment+Extension.swift b/Mail/Utils/Environment+Extension.swift index ab1dc4904f..585ac5d8b8 100644 --- a/Mail/Utils/Environment+Extension.swift +++ b/Mail/Utils/Environment+Extension.swift @@ -18,14 +18,9 @@ import Foundation import SwiftUI +import SwiftUIMacros -public struct CompactWindowKey: EnvironmentKey { - public static let defaultValue = true -} - -public extension EnvironmentValues { - var isCompactWindow: CompactWindowKey.Value { - get { return self[CompactWindowKey.self] } - set { self[CompactWindowKey.self] = newValue } - } +extension EnvironmentValues { + @EnvironmentValue + var isCompactWindow = true } diff --git a/Mail/Views/Menu Drawer/Folders/FolderCell.swift b/Mail/Views/Menu Drawer/Folders/FolderCell.swift index 8156336b84..eba50a99af 100644 --- a/Mail/Views/Menu Drawer/Folders/FolderCell.swift +++ b/Mail/Views/Menu Drawer/Folders/FolderCell.swift @@ -22,16 +22,11 @@ import InfomaniakDI import MailCore import MailResources import SwiftUI - -struct FolderCellTypeEnvironment: EnvironmentKey { - static var defaultValue = FolderCell.CellType.menuDrawer -} +import SwiftUIMacros extension EnvironmentValues { - var folderCellType: FolderCell.CellType { - get { self[FolderCellTypeEnvironment.self] } - set { self[FolderCellTypeEnvironment.self] = newValue } - } + @EnvironmentValue + var folderCellType = FolderCell.CellType.menuDrawer } struct FolderCell: View { diff --git a/Mail/Views/Menu Drawer/MailboxManagement/MailboxCell.swift b/Mail/Views/Menu Drawer/MailboxManagement/MailboxCell.swift index a894680cb5..a260dece46 100644 --- a/Mail/Views/Menu Drawer/MailboxManagement/MailboxCell.swift +++ b/Mail/Views/Menu Drawer/MailboxManagement/MailboxCell.swift @@ -23,16 +23,11 @@ import MailCore import MailResources import SwiftModalPresentation import SwiftUI - -struct MailboxCellStyleKey: EnvironmentKey { - static var defaultValue = MailboxCell.Style.menuDrawer -} +import SwiftUIMacros extension EnvironmentValues { - var mailboxCellStyle: MailboxCell.Style { - get { self[MailboxCellStyleKey.self] } - set { self[MailboxCellStyleKey.self] = newValue } - } + @EnvironmentValue + var mailboxCellStyle = MailboxCell.Style.menuDrawer } extension View { diff --git a/Mail/Views/Settings/General/SettingsNotificationsInstructionsView.swift b/Mail/Views/Settings/General/SettingsNotificationsInstructionsView.swift index 8526979ea2..0516a28db7 100644 --- a/Mail/Views/Settings/General/SettingsNotificationsInstructionsView.swift +++ b/Mail/Views/Settings/General/SettingsNotificationsInstructionsView.swift @@ -33,7 +33,6 @@ struct SettingsNotificationsInstructionsView: View { ModalButtonsView( primaryButtonTitle: MailResourcesStrings.Localizable.alertNotificationsDisabledButton, primaryButtonAction: DeeplinkConstants.presentsNotificationSettings - ) } } diff --git a/Mail/Views/SheetViewModifier.swift b/Mail/Views/SheetViewModifier.swift index 73a1986a28..8ed3916561 100644 --- a/Mail/Views/SheetViewModifier.swift +++ b/Mail/Views/SheetViewModifier.swift @@ -19,22 +19,13 @@ import MailCore import MailResources import SwiftUI +import SwiftUIMacros typealias DismissModalAction = () -> Void -struct DismissModalKey: EnvironmentKey { - static let defaultValue: DismissModalAction = { /* dismiss nothing by default */ } -} - extension EnvironmentValues { - var dismissModal: DismissModalAction { - get { - return self[DismissModalKey.self] - } - set { - self[DismissModalKey.self] = newValue - } - } + @EnvironmentValue + var dismissModal: DismissModalAction = { /* dismiss nothing by default */ } } extension View { diff --git a/Package.resolved b/Package.resolved index 573dc5d815..4ba868a53f 100644 --- a/Package.resolved +++ b/Package.resolved @@ -269,6 +269,15 @@ "version" : "1.0.2" } }, + { + "identity" : "swift-syntax", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-syntax", + "state" : { + "revision" : "64889f0c732f210a935a0ad7cda38f77f876262d", + "version" : "509.1.1" + } + }, { "identity" : "swiftbackports", "kind" : "remoteSourceControl", @@ -314,6 +323,15 @@ "version" : "1.1.3" } }, + { + "identity" : "swiftui-macros", + "kind" : "remoteSourceControl", + "location" : "https://github.com/Wouter01/SwiftUI-Macros.git", + "state" : { + "revision" : "1c544d53727267a7a3e8b4a6728cf2dc88341e0e", + "version" : "1.1.0" + } + }, { "identity" : "swiftui-shimmer", "kind" : "remoteSourceControl", diff --git a/Package.swift b/Package.swift index 69544135ca..bf6031b77e 100644 --- a/Package.swift +++ b/Package.swift @@ -31,6 +31,7 @@ let package = Package( .package(url: "https://github.com/johnpatrickmorgan/NavigationBackport", .upToNextMajor(from: "0.8.1")), .package(url: "https://github.com/aheze/Popovers", .upToNextMajor(from: "1.3.2")), .package(url: "https://github.com/shaps80/SwiftUIBackports", .upToNextMajor(from: "1.15.1")), - .package(url: "https://github.com/httpswift/swifter", .upToNextMajor(from: "1.5.0")) + .package(url: "https://github.com/httpswift/swifter", .upToNextMajor(from: "1.5.0")), + .package(url: "https://github.com/Wouter01/SwiftUI-Macros.git", .upToNextMajor(from: "1.0.0")) ] ) diff --git a/Project.swift b/Project.swift index 2f3e52008e..1d46090b26 100644 --- a/Project.swift +++ b/Project.swift @@ -50,7 +50,8 @@ let project = Project(name: "Mail", .external(name: "Lottie"), .external(name: "NavigationBackport"), .external(name: "Popovers"), - .external(name: "SwiftUIBackports") + .external(name: "SwiftUIBackports"), + .external(name: "SwiftUIMacros") ], settings: .settings(base: Constants.baseSettings), environmentVariables: [ @@ -105,7 +106,8 @@ let project = Project(name: "Mail", .external(name: "Lottie"), .external(name: "NavigationBackport"), .external(name: "Popovers"), - .external(name: "SwiftUIBackports") + .external(name: "SwiftUIBackports"), + .external(name: "SwiftUIMacros") ], settings: .settings(base: Constants.baseSettings)), .target(name: "MailNotificationServiceExtension",