-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatePickerBarView.swift
64 lines (57 loc) · 2.03 KB
/
DatePickerBarView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
// DatePickerBarView.swift
// Lucify (iOS)
//
// Created by Patrick Elfert on 25.06.22.
//
import SwiftUI
struct DatePickerBarView: View {
@Binding var selectedDay: Date
@ObservedObject var datePickerManager = DatePickerManager()
@State private var scrollViewContentOffset = CGFloat(0)
@State private var currentPage: Int = 1
var body: some View {
PagerView(pageCount: 3, currentIndex: $currentPage) {
ForEach(datePickerManager.shownWeeks, id: \.self) {
week in
HStack(spacing: 0) {
ForEach(week, id: \.self) {
day in
Spacer()
DatePickerElement(date: day, isSelected: Calendar.current.isDate(selectedDay, equalTo: day, toGranularity: .day)).onTapGesture {
selectedDay = day
}
}
Spacer()
}.onChange(of: currentPage) {
_ in
datePickerManager.onWeekChange(index: currentPage)
currentPage = 1
}
}
}.frame(height: 70)
}
}
struct DatePickerBarView_Previews: PreviewProvider {
static var previews: some View {
DatePickerBarView(selectedDay: .constant(Date.now))
}
}
struct DatePickerElement: View {
init(date: Date, isSelected: Bool) {
self.date = date
self.isSelected = isSelected
}
let isSelected: Bool
let date: Date
var body: some View {
VStack {
Text("\(date.toString(.custom("dd"))!)").font(Font.body).fontWeight(.bold)
if isSelected {
Text("\(date.toString(.custom("EEE"))!)").font(Font.caption).opacity(0.8)
} else {
Text("\(date.toString(.custom("EEE"))!)").font(Font.caption).opacity(0.8)
}
}.frame(width: 40, height: 40).background(isSelected ? PrimaryLight : Primary).cornerRadius(10).foregroundColor(isSelected ? .black : .white)
}
}