commit 47ca50cda25c2a721810eba165f07d4670750e06
parent ce6d205076c0e832f35ca4d0c84424d26bbeac04
Author: Marc Stibane <marc@taler.net>
Date: Fri, 12 Jul 2024 23:50:08 +0200
View+Condition
Diffstat:
2 files changed, 57 insertions(+), 0 deletions(-)
diff --git a/TalerWallet.xcodeproj/project.pbxproj b/TalerWallet.xcodeproj/project.pbxproj
@@ -135,6 +135,8 @@
4E3EAEA82AA70157009F1BE8 /* Binding+onChange.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E3EAEA72AA70157009F1BE8 /* Binding+onChange.swift */; };
4E3EAEA92AA70157009F1BE8 /* Binding+onChange.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E3EAEA72AA70157009F1BE8 /* Binding+onChange.swift */; };
4E40E0BE29F25ABB00B85369 /* SendAmount.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E40E0BD29F25ABB00B85369 /* SendAmount.swift */; };
+ 4E4F60A82C3BBF9F003BB669 /* View+Condition.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E4F60A72C3BBF9F003BB669 /* View+Condition.swift */; };
+ 4E4F60A92C3BBF9F003BB669 /* View+Condition.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E4F60A72C3BBF9F003BB669 /* View+Condition.swift */; };
4E50B3502A1BEE8000F9F01C /* ManualWithdraw.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E50B34F2A1BEE8000F9F01C /* ManualWithdraw.swift */; };
4E53A33729F50B7B00830EC2 /* CurrencyField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E53A33629F50B7B00830EC2 /* CurrencyField.swift */; };
4E578E922A481D8600F21F1C /* Controller+playSound.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4E578E912A481D8600F21F1C /* Controller+playSound.swift */; };
@@ -357,6 +359,7 @@
4E3EAEA12AA12582009F1BE8 /* Nunito-BoldItalic.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "Nunito-BoldItalic.ttf"; sourceTree = "<group>"; };
4E3EAEA72AA70157009F1BE8 /* Binding+onChange.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Binding+onChange.swift"; sourceTree = "<group>"; };
4E40E0BD29F25ABB00B85369 /* SendAmount.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SendAmount.swift; sourceTree = "<group>"; };
+ 4E4F60A72C3BBF9F003BB669 /* View+Condition.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "View+Condition.swift"; sourceTree = "<group>"; };
4E50B34F2A1BEE8000F9F01C /* ManualWithdraw.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ManualWithdraw.swift; sourceTree = "<group>"; };
4E53A33629F50B7B00830EC2 /* CurrencyField.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CurrencyField.swift; sourceTree = "<group>"; };
4E578E912A481D8600F21F1C /* Controller+playSound.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Controller+playSound.swift"; sourceTree = "<group>"; };
@@ -660,6 +663,7 @@
4E3EAE8B2AA0933C009F1BE8 /* Font+Taler.swift */,
4EB095082989CB7C0043A8A1 /* View+dismissTop.swift */,
4E605DB62AB05E48002FB9A7 /* View+flippedDirection.swift */,
+ 4E4F60A72C3BBF9F003BB669 /* View+Condition.swift */,
4E3B4BC62A429F2A00CC88B8 /* View+Notification.swift */,
4E605DAE2AADDD13002FB9A7 /* UIScreen+screenSize.swift */,
4E363CBB2A237E0900D7E98C /* URL+id+iban.swift */,
@@ -1229,6 +1233,7 @@
4E3EAE592A990778009F1BE8 /* Model+Settings.swift in Sources */,
4EC4008C2AE5664100DF72C7 /* CharacterSet+contains.swift in Sources */,
4E3EAE5A2A990778009F1BE8 /* ErrorView.swift in Sources */,
+ 4E4F60A82C3BBF9F003BB669 /* View+Condition.swift in Sources */,
4E96583C2B79656E00404A68 /* DepositAmountV.swift in Sources */,
4E3EAE5B2A990778009F1BE8 /* View+Notification.swift in Sources */,
4E3EAE5C2A990778009F1BE8 /* Model+Pending.swift in Sources */,
@@ -1352,6 +1357,7 @@
4EB095152989CBB00043A8A1 /* Model+Settings.swift in Sources */,
4EC4008D2AE5664100DF72C7 /* CharacterSet+contains.swift in Sources */,
4EB095692989CBFE0043A8A1 /* ErrorView.swift in Sources */,
+ 4E4F60A92C3BBF9F003BB669 /* View+Condition.swift in Sources */,
4E96583D2B79656E00404A68 /* DepositAmountV.swift in Sources */,
4E3B4BC72A429F2A00CC88B8 /* View+Notification.swift in Sources */,
4EB0956E2989CBFE0043A8A1 /* Model+Pending.swift in Sources */,
diff --git a/TalerWallet1/Helper/View+Condition.swift b/TalerWallet1/Helper/View+Condition.swift
@@ -0,0 +1,51 @@
+// MIT License
+// Copyright © Antoine van der Lee
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+// and associated documentation files (the "Software"), to deal in the Software without restriction,
+// including without limitation the rights to use, copy, modify, merge, publish, distribute,
+// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in all copies or
+// substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+import SwiftUI
+
+extension View {
+ /// Applies the given transform if the given condition evaluates to `true`.
+ /// - Parameters:
+ /// - condition: The condition to evaluate.
+ /// - transform: The transform to apply to the source `View`.
+ /// - Returns: Either the original `View` or the modified `View` if the condition is `true`.
+ @ViewBuilder func `if`<Content: View>(_ condition: @autoclosure () -> Bool, transform: (Self) -> Content) -> some View {
+ if condition() {
+ transform(self)
+ } else {
+ self
+ }
+ }
+}
+
+extension Bool {
+ static var iOS15: Bool {
+ guard #available(iOS 16, *) else {
+ return true // iOS 15
+ }
+ return false // iOS 16+
+ }
+}
+
+// Use like this:
+//
+// Text("Hello, world!")
+// .padding()
+// .if(.iOS15) { view in
+// view.background(Color.red)
+// }