ScheduleItemView.swift 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //
  2. // ScheduleItemView.swift
  3. // LoopKitUI
  4. //
  5. // Created by Michael Pangburn on 4/24/20.
  6. // Copyright © 2020 LoopKit Authors. All rights reserved.
  7. //
  8. import SwiftUI
  9. struct ScheduleItemView<ValueContent: View, ExpandedContent: View>: View {
  10. var time: TimeInterval
  11. @Binding var isEditing: Bool
  12. var valueContent: ValueContent
  13. var expandedContent: ExpandedContent
  14. private let fixedMidnight = Calendar.current.startOfDay(for: Date(timeIntervalSinceReferenceDate: 0))
  15. init(
  16. time: TimeInterval,
  17. isEditing: Binding<Bool>,
  18. @ViewBuilder valueContent: () -> ValueContent,
  19. @ViewBuilder expandedContent: () -> ExpandedContent
  20. ) {
  21. self.time = time
  22. self._isEditing = isEditing
  23. self.valueContent = valueContent()
  24. self.expandedContent = expandedContent()
  25. }
  26. var body: some View {
  27. ExpandableSetting(
  28. isEditing: $isEditing,
  29. leadingValueContent: { timeText },
  30. trailingValueContent: { valueContent },
  31. expandedContent: { self.expandedContent }
  32. )
  33. }
  34. private var timeText: Text {
  35. let dayAtTime = fixedMidnight.addingTimeInterval(time)
  36. return Text(DateFormatter.localizedString(from: dayAtTime, dateStyle: .none, timeStyle: .short))
  37. .foregroundColor(isEditing ? .accentColor : Color(.label))
  38. }
  39. }
  40. extension AnyTransition {
  41. static let fadeInFromTop = move(edge: .top).combined(with: .opacity)
  42. .delayingInsertion(by: 0.1)
  43. .speedingUpRemoval(by: 1.8)
  44. func delayingInsertion(by delay: TimeInterval) -> AnyTransition {
  45. .asymmetric(insertion: animation(Animation.default.delay(delay)), removal: self)
  46. }
  47. func speedingUpRemoval(by factor: Double) -> AnyTransition {
  48. .asymmetric(insertion: self, removal: animation(Animation.default.speed(factor)))
  49. }
  50. }