Advertisement
Guest User

DayHourMinutePickerView (Bad UI Battle)

a guest
Feb 25th, 2024
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.53 KB | Source Code | 0 0
  1. //PSA: Don't.
  2. struct DayHourMinutePickerView: View {
  3.     @Binding public var seconds: TimeInterval
  4.  
  5.     var daysArray = [Int](0..<32)
  6.     var hoursArray = [Int](0..<24)
  7.     var minutesArray = [Int](0..<60)
  8.     var secondsArray = [Int](0..<60)
  9.  
  10.     private let hoursInDay = 24
  11.     private let secondsInMinute = 60
  12.     private let minutesInHour = 60
  13.     private let secondsInHour = 3600
  14.     private let secondsInDay = 86400
  15.  
  16.     @State private var daySelection = 0
  17.     @State private var hourSelection = 0
  18.     @State private var minuteSelection = 0
  19.     @State private var secondSelection = 0
  20.    
  21.     @State private var popoverPresent = false
  22.  
  23.     @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
  24.  
  25.     var body: some View {
  26.         Button("\(daySelection) d, \(hourSelection) h, \(minuteSelection) m") {
  27.             popoverPresent.toggle()
  28.         }.sheet(isPresented: $popoverPresent) {
  29.             NavigationView {
  30.                 ScrollView(.vertical) {
  31.                     Section {
  32.                         Text("Days")
  33.                         LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible())]) {
  34.                             ForEach((0...7), id: \.self) { i in
  35.                                 Button(action: {
  36.                                         daySelection = i
  37.                                         seconds = totalInSeconds
  38.                                     },
  39.                                     label:{
  40.                                        Text(String(i))
  41.                                         .frame(minWidth: 40, maxWidth:.infinity, minHeight: 40)
  42.                                         .contentShape(Rectangle())
  43.                                     }
  44.                                 ).foregroundColor((i == daySelection ? Color.black : Color.accentColor))
  45.                             }
  46.                         }
  47.                     }.padding()
  48.                    
  49.                     Section {
  50.                         Text("Hours")
  51.                         LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible())]) {
  52.                             ForEach((0...24), id: \.self) { i in
  53.                                 Button(action: {
  54.                                         hourSelection = i
  55.                                         seconds = totalInSeconds
  56.                                     },
  57.                                     label:{
  58.                                        Text(String(i))
  59.                                         .frame(minWidth: 40, maxWidth:.infinity, minHeight: 40)
  60.                                         .contentShape(Rectangle())
  61.                                     }
  62.                                 ).foregroundColor((i == hourSelection ? Color.black : Color.accentColor))
  63.                             }
  64.                         }
  65.                     }.padding()
  66.                    
  67.                     Section {
  68.                         Text("Minutes")
  69.                         LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible())]) {
  70.                             ForEach([0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60], id: \.self) { i in
  71.                                 Button(action: {
  72.                                         minuteSelection = i
  73.                                         seconds = totalInSeconds
  74.                                     },
  75.                                     label:{
  76.                                        Text(String(i))
  77.                                         .frame(minWidth: 40, maxWidth:.infinity, minHeight: 40)
  78.                                         .contentShape(Rectangle())
  79.                                     }
  80.                                 ).foregroundColor((i == minuteSelection ? Color.black : Color.accentColor))
  81.                             }
  82.                         }
  83.                     }.padding()
  84.                 }
  85.                 .navigationBarTitle("\(daySelection) d, \(hourSelection) h, \(minuteSelection) m", displayMode: .inline)
  86.                     .navigationBarItems(trailing:
  87.                         Button(action: {
  88.                             self.popoverPresent.toggle()
  89.                         }) {
  90.                             Text("Done").padding().contentShape(Rectangle())
  91.                         }
  92.                     )
  93.                
  94.             }
  95.         }.onAppear(perform: { updatePickers() })
  96.     }
  97.  
  98.     func updatePickers() {
  99.         let secs: Int = Int(seconds)
  100.         daySelection = secs / (60 * 60 * 24)
  101.         hourSelection = (secs % (60 * 60 * 24)) / (60 * 60)
  102.         minuteSelection = (secs % (60 * 60)) / 60
  103.         secondSelection = secs % 60
  104.     }
  105.  
  106.     var totalInSeconds: TimeInterval {
  107.         return TimeInterval(daySelection * self.secondsInDay + hourSelection * self.secondsInHour + minuteSelection * self.secondsInMinute + secondSelection)
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement