taler-ios

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

ListStyle.swift (3233B)


      1 /* MIT License
      2  * Copyright (c) 2022 young rtSwift
      3  *
      4  * Permission is hereby granted, free of charge, to any person obtaining a copy
      5  * of this software and associated documentation files (the "Software"), to deal
      6  * in the Software without restriction, including without limitation the rights
      7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      8  * copies of the Software, and to permit persons to whom the Software is
      9  * furnished to do so, subject to the following conditions:
     10  *
     11  * The above copyright notice and this permission notice shall be included in all
     12  * copies or substantial portions of the Software.
     13  *
     14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     20  * SOFTWARE.
     21  */
     22 import SwiftUI
     23 
     24 public extension View {
     25     var anyView: AnyView {
     26         AnyView(self)
     27     }
     28 }
     29 // MARK: -
     30 // Our ListStyle each case corresponds to a SwiftUI.ListStyle
     31 // Here we make it CaseIterable for SwiftUI.ForEach
     32 // and the UI Display name
     33 public enum MyListStyle: String, CaseIterable, Hashable {
     34     case automatic
     35     case grouped
     36     case inset
     37     case insetGrouped
     38     case plain
     39     case sidebar
     40 
     41     // map to SwiftUI ListStyle
     42     var style: any SwiftUI.ListStyle {
     43         switch self {
     44             case .automatic:    return .automatic
     45             case .grouped:      return .grouped
     46             case .inset:        return .inset
     47             case .insetGrouped: return .insetGrouped
     48             case .plain:        return .plain
     49             case .sidebar:      return .sidebar
     50         }
     51     }
     52 
     53     var displayName: String {
     54         String(self.rawValue)
     55     }
     56 }
     57 // MARK: -
     58 #if DEBUG
     59 struct AnyViewDemo: View {
     60     @State private var selectedStyle = MyListStyle.automatic
     61 
     62     let sections  = ["Breakfast" : ["pancakes", "bacon", "orange juice"],
     63                                     "Lunch"     : ["sandwich", "chips", "lemonade"],
     64                                     "Dinner"    : ["spaghetti", "bread", "water"]]
     65 
     66 
     67     var body: some View {
     68         VStack {
     69             Picker("List Style:", selection: $selectedStyle) {
     70                 ForEach(MyListStyle.allCases, id: \.self) {
     71                     Text($0.displayName.capitalized).tag($0)
     72                 }
     73             }
     74 
     75             let keys = Array(sections.keys)
     76             List(keys.indices, id: \.self) { index in
     77                 let key = keys[index]
     78                 if let section = sections[key] {
     79                     Section(key) {
     80                         ForEach(section, id: \.self) { item in
     81                             Text(item)
     82                         }
     83                     }
     84                 }
     85             }
     86             .listStyle(selectedStyle.style)
     87             .anyView
     88         }
     89     }
     90 }
     91 
     92 struct AnyViewDemo_Previews: PreviewProvider {
     93     static var previews: some View {
     94         AnyViewDemo()
     95     }
     96 }
     97 #endif