taler-ios

iOS apps for GNU Taler (wallet)
Log | Files | Refs | README | LICENSE

View+Condition.swift (2256B)


      1 //  MIT License
      2 //  Copyright © Antoine van der Lee
      3 //
      4 //  Permission is hereby granted, free of charge, to any person obtaining a copy of this software
      5 //  and associated documentation files (the "Software"), to deal in the Software without restriction,
      6 //  including without limitation the rights to use, copy, modify, merge, publish, distribute,
      7 //  sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
      8 //  furnished to do so, subject to the following conditions:
      9 //
     10 //  The above copyright notice and this permission notice shall be included in all copies or
     11 //  substantial portions of the Software.
     12 //
     13 //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
     14 //  BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     15 //  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
     16 //  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     17 //  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     18 //
     19 import SwiftUI
     20 
     21 extension View {
     22     /// Applies the given transform if the given condition evaluates to `true`.
     23     /// - Parameters:
     24     ///   - condition: The condition to evaluate.
     25     ///   - transform: The transform to apply to the source `View`.
     26     /// - Returns: Either the original `View` or the modified `View` if the condition is `true`.
     27     @ViewBuilder func `if`<Content: View>(_ condition: @autoclosure () -> Bool, transform: (Self) -> Content) -> some View {
     28         if condition() {
     29             transform(self)
     30         } else {
     31             self
     32         }
     33     }
     34 }
     35 
     36 extension Bool {
     37     static var iOS15: Bool {
     38         guard #available(iOS 16, *) else {
     39             return true         // iOS 15
     40         }
     41         return false            // iOS 16+
     42     }
     43     static var iOS16: Bool {
     44         guard #available(iOS 16, *) else {
     45             return false        // iOS 15
     46         }
     47         return true             // iOS 16+
     48     }
     49 }
     50 
     51 // Use like this:
     52 //
     53 //        Text("Hello, world!")
     54 //            .padding()
     55 //            .if(.iOS15) { view in
     56 //                view.background(Color.red)
     57 //            }