-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
NotificationView.swift
58 lines (50 loc) · 1.59 KB
/
NotificationView.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
import SwiftUI
struct NotificationView<Content: View>: View {
let alignment: Alignment
@ViewBuilder
let content: () -> Content
init(_ alignment: Alignment, @ViewBuilder content: @escaping () -> Content) {
self.alignment = alignment
self.content = content
}
var body: some View {
HStack {
AlignmentSpacer(alignment, validAlignments: .top, .bottom, .topTrailing, .trailing, .bottomTrailing)
VStack(alignment: horizontalAlignment) {
AlignmentSpacer(alignment, validAlignments: .leading, .trailing, .bottom, .bottomLeading, .bottomTrailing)
content()
.frame(alignment: alignment)
AlignmentSpacer(alignment, validAlignments: .leading, .trailing, .top, .topLeading, .topTrailing)
}
AlignmentSpacer(alignment, validAlignments: .top, .bottom, .topLeading, .leading, .bottomLeading)
}
}
private var horizontalAlignment: HorizontalAlignment {
switch alignment {
case .topLeading, .leading, .bottomLeading: .leading
case .topTrailing, .trailing, .bottomLeading: .trailing
default: .center
}
}
}
struct AlignmentSpacer: View {
let alignment: Alignment
let validAlignments: [Alignment]
init(_ alignment: Alignment, validAlignments: Alignment...) {
self.alignment = alignment
self.validAlignments = validAlignments
}
var body: some View {
if validAlignments.contains(alignment) {
Spacer()
}
}
}
struct NotificationView_Previews: PreviewProvider {
static var previews: some View {
NotificationView(.bottomTrailing) {
Text("ohai!")
}
.frame(width: 480, height: 320)
}
}