forked from LoopKit/LoopKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpandableSetting.swift
More file actions
69 lines (60 loc) · 2.01 KB
/
ExpandableSetting.swift
File metadata and controls
69 lines (60 loc) · 2.01 KB
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
65
66
67
68
69
//
// ExpandableSetting.swift
// LoopKitUI
//
// Created by Michael Pangburn on 5/15/20.
// Copyright © 2020 LoopKit Authors. All rights reserved.
//
import SwiftUI
public struct ExpandableSetting<
LeadingValueContent: View,
TrailingValueContent: View,
ExpandedContent: View
>: View {
@Binding var isEditing: Bool
var leadingValueContent: LeadingValueContent
var trailingValueContent: TrailingValueContent
var expandedContent: () -> ExpandedContent
public init(
isEditing: Binding<Bool>,
@ViewBuilder leadingValueContent: () -> LeadingValueContent,
@ViewBuilder trailingValueContent: () -> TrailingValueContent,
@ViewBuilder expandedContent: @escaping () -> ExpandedContent
) {
self._isEditing = isEditing
self.leadingValueContent = leadingValueContent()
self.trailingValueContent = trailingValueContent()
self.expandedContent = expandedContent
}
public var body: some View {
VStack(spacing: 0) {
HStack {
leadingValueContent
Spacer()
trailingValueContent
.fixedSize(horizontal: true, vertical: false)
}
.accessibilityElement(children: .combine)
.contentShape(Rectangle())
.onTapGesture {
withAnimation {
self.isEditing.toggle()
}
}
if isEditing {
expandedContent()
.padding(.horizontal, -8)
.transition(.fadeInFromTop)
}
}
}
}
extension ExpandableSetting where LeadingValueContent == EmptyView {
public init(
isEditing: Binding<Bool>,
@ViewBuilder valueContent: () -> TrailingValueContent,
@ViewBuilder expandedContent: @escaping () -> ExpandedContent
) {
self.init(isEditing: isEditing, leadingValueContent: EmptyView.init, trailingValueContent: valueContent, expandedContent: expandedContent)
}
}