Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //PSA: Don't.
- struct DayHourMinutePickerView: View {
- @Binding public var seconds: TimeInterval
- var daysArray = [Int](0..<32)
- var hoursArray = [Int](0..<24)
- var minutesArray = [Int](0..<60)
- var secondsArray = [Int](0..<60)
- private let hoursInDay = 24
- private let secondsInMinute = 60
- private let minutesInHour = 60
- private let secondsInHour = 3600
- private let secondsInDay = 86400
- @State private var daySelection = 0
- @State private var hourSelection = 0
- @State private var minuteSelection = 0
- @State private var secondSelection = 0
- @State private var popoverPresent = false
- @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
- var body: some View {
- Button("\(daySelection) d, \(hourSelection) h, \(minuteSelection) m") {
- popoverPresent.toggle()
- }.sheet(isPresented: $popoverPresent) {
- NavigationView {
- ScrollView(.vertical) {
- Section {
- Text("Days")
- LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible())]) {
- ForEach((0...7), id: \.self) { i in
- Button(action: {
- daySelection = i
- seconds = totalInSeconds
- },
- label:{
- Text(String(i))
- .frame(minWidth: 40, maxWidth:.infinity, minHeight: 40)
- .contentShape(Rectangle())
- }
- ).foregroundColor((i == daySelection ? Color.black : Color.accentColor))
- }
- }
- }.padding()
- Section {
- Text("Hours")
- LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible())]) {
- ForEach((0...24), id: \.self) { i in
- Button(action: {
- hourSelection = i
- seconds = totalInSeconds
- },
- label:{
- Text(String(i))
- .frame(minWidth: 40, maxWidth:.infinity, minHeight: 40)
- .contentShape(Rectangle())
- }
- ).foregroundColor((i == hourSelection ? Color.black : Color.accentColor))
- }
- }
- }.padding()
- Section {
- Text("Minutes")
- LazyVGrid(columns: [GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible())]) {
- ForEach([0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60], id: \.self) { i in
- Button(action: {
- minuteSelection = i
- seconds = totalInSeconds
- },
- label:{
- Text(String(i))
- .frame(minWidth: 40, maxWidth:.infinity, minHeight: 40)
- .contentShape(Rectangle())
- }
- ).foregroundColor((i == minuteSelection ? Color.black : Color.accentColor))
- }
- }
- }.padding()
- }
- .navigationBarTitle("\(daySelection) d, \(hourSelection) h, \(minuteSelection) m", displayMode: .inline)
- .navigationBarItems(trailing:
- Button(action: {
- self.popoverPresent.toggle()
- }) {
- Text("Done").padding().contentShape(Rectangle())
- }
- )
- }
- }.onAppear(perform: { updatePickers() })
- }
- func updatePickers() {
- let secs: Int = Int(seconds)
- daySelection = secs / (60 * 60 * 24)
- hourSelection = (secs % (60 * 60 * 24)) / (60 * 60)
- minuteSelection = (secs % (60 * 60)) / 60
- secondSelection = secs % 60
- }
- var totalInSeconds: TimeInterval {
- return TimeInterval(daySelection * self.secondsInDay + hourSelection * self.secondsInHour + minuteSelection * self.secondsInMinute + secondSelection)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement