123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- //
- // YearAndMonthPicker.swift
- // ydnab
- //
- // Created by Andrea Franceschini on 03/10/2020.
- //
- import SwiftUI
- /// Allows to chose the month and year to view.
- struct YearAndMonthPicker: View {
- @Binding var month: Int
- @Binding var monthName: String
- @Binding var year: Int
- @State var locale: Locale
- private var monthNames: [String] {
- var c = Calendar(identifier: .gregorian)
- c.locale = locale
- return c.monthSymbols
- }
- var columns: [GridItem] = [
- GridItem(spacing: 8),
- GridItem(spacing: 8),
- GridItem(spacing: 8)
- ]
- var body: some View {
- VStack {
- HStack {
- Button(action: { year -= 1 }) {
- Image(systemName: "chevron.backward.square")
- .imageScale(.large)
- }
- Spacer()
- Text(String(year)).bold()
- Spacer()
- Button(action: { year += 1 }) {
- Image(systemName: "chevron.forward.square")
- .imageScale(.large)
- }
- }
- GeometryReader { geometry in
- LazyVGrid(columns: columns, spacing: 4) {
- ForEach(monthNames.indices) { i in
- Button(action: {
- month = i
- monthName = monthNames[i]
- }, label: {
- Text(monthNames[i])
- .frame(minWidth: geometry.size.width / 3,
- idealWidth: geometry.size.width / 3,
- maxWidth: geometry.size.width / 3,
- minHeight: geometry.size.height / 4,
- idealHeight: geometry.size.height / 4,
- maxHeight: geometry.size.height / 4,
- alignment: .center)
- })
- .background(month == i ? Color("bgActive") : Color("bgInactive"))
- .foregroundColor(month == i ? Color.white : Color("AccentColor"))
- }
- }
- }
- }
- .padding(.bottom, 22)
- }
- }
|