YearAndMonthPicker.swift 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //
  2. // YearAndMonthPicker.swift
  3. // ydnab
  4. //
  5. // Created by Andrea Franceschini on 03/10/2020.
  6. //
  7. import SwiftUI
  8. /// Allows to chose the month and year to view.
  9. struct YearAndMonthPicker: View {
  10. @Binding var month: Int
  11. @Binding var monthName: String
  12. @Binding var year: Int
  13. @State var locale: Locale
  14. private var monthNames: [String] {
  15. var c = Calendar(identifier: .gregorian)
  16. c.locale = locale
  17. return c.monthSymbols
  18. }
  19. var columns: [GridItem] = [
  20. GridItem(spacing: 8),
  21. GridItem(spacing: 8),
  22. GridItem(spacing: 8)
  23. ]
  24. var body: some View {
  25. VStack {
  26. HStack {
  27. Button(action: { year -= 1 }) {
  28. Image(systemName: "chevron.backward.square")
  29. .imageScale(.large)
  30. }
  31. Spacer()
  32. Text(String(year)).bold()
  33. Spacer()
  34. Button(action: { year += 1 }) {
  35. Image(systemName: "chevron.forward.square")
  36. .imageScale(.large)
  37. }
  38. }
  39. GeometryReader { geometry in
  40. LazyVGrid(columns: columns, spacing: 4) {
  41. ForEach(monthNames.indices) { i in
  42. Button(action: {
  43. month = i
  44. monthName = monthNames[i]
  45. }, label: {
  46. Text(monthNames[i])
  47. .frame(minWidth: geometry.size.width / 3,
  48. idealWidth: geometry.size.width / 3,
  49. maxWidth: geometry.size.width / 3,
  50. minHeight: geometry.size.height / 4,
  51. idealHeight: geometry.size.height / 4,
  52. maxHeight: geometry.size.height / 4,
  53. alignment: .center)
  54. })
  55. .background(month == i ? Color("bgActive") : Color("bgInactive"))
  56. .foregroundColor(month == i ? Color.white : Color("AccentColor"))
  57. }
  58. }
  59. }
  60. }
  61. .padding(.bottom, 22)
  62. }
  63. }