-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
551 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// Avatar.swift | ||
// Vault | ||
// | ||
// Created by Charles Lanier on 04/06/2024. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct Avatar: View { | ||
var imageData: Data? | ||
var name: String | ||
var size: CGFloat = 42 | ||
|
||
var body: some View { | ||
if | ||
let imageData = self.imageData, | ||
let uiImage = UIImage(data: imageData) { | ||
Image(uiImage: uiImage) | ||
.resizable() | ||
.aspectRatio(contentMode: .fill) | ||
.frame(width: size, height: size) | ||
.scaledToFit() | ||
.clipShape(Circle()) | ||
} else { | ||
NoAvatar(name: self.name) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// | ||
// Popover.swift | ||
// Vault | ||
// | ||
// Created by Charles Lanier on 04/06/2024. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct InnerHeightPreferenceKey: PreferenceKey { | ||
|
||
static let defaultValue: CGFloat = .zero | ||
|
||
static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) { | ||
value = nextValue() | ||
} | ||
} | ||
|
||
struct PopoverModifier<PopoverContent>: ViewModifier where PopoverContent : View { | ||
|
||
@State private var sheetHeight: CGFloat = .zero | ||
|
||
@Binding var isPresented: Bool | ||
|
||
var onDismiss: (() -> Void)? | ||
var popoverContent: () -> PopoverContent | ||
|
||
func body(content: Content) -> some View { | ||
content | ||
.sheet(isPresented: self.$isPresented) { | ||
VStack { | ||
VStack { | ||
self.popoverContent() | ||
} | ||
.padding(EdgeInsets(top: 44, leading: 20, bottom: 32, trailing: 20)) | ||
.frame(maxWidth: .infinity) | ||
.background(.background2) | ||
.clipShape(RoundedRectangle(cornerRadius: 32)) | ||
} | ||
.padding(EdgeInsets(top: 0, leading: 16, bottom: 16, trailing: 16)) | ||
.overlay { | ||
GeometryReader { geometry in | ||
Color.clear.preference(key: InnerHeightPreferenceKey.self, value: geometry.size.height) | ||
} | ||
} | ||
.onPreferenceChange(InnerHeightPreferenceKey.self) { newHeight in | ||
self.sheetHeight = newHeight | ||
} | ||
.presentationDetents([.height(self.sheetHeight)]) | ||
.presentationDragIndicator(.visible) | ||
.presentationBackground(.clear) | ||
} | ||
} | ||
} | ||
|
||
extension View { | ||
public func sheetPopover<Content>( | ||
isPresented: Binding<Bool>, | ||
onDismiss: (() -> Void)? = nil, | ||
@ViewBuilder content: @escaping () -> Content | ||
) -> some View where Content : View { | ||
self.modifier( | ||
PopoverModifier( | ||
isPresented: isPresented, | ||
onDismiss: onDismiss, | ||
popoverContent: content | ||
) | ||
) | ||
} | ||
} | ||
|
||
#if DEBUG | ||
struct PopoverViewPreviews : PreviewProvider { | ||
|
||
@State static var isPresented = true | ||
|
||
static var previews: some View { | ||
VStack { | ||
Button("Open popover") { | ||
self.isPresented = true | ||
} | ||
} | ||
.frame(maxWidth: .infinity, maxHeight: .infinity) | ||
.defaultBackground() | ||
.sheetPopover(isPresented: self.$isPresented) { | ||
Text("Holà, I'm the popover") | ||
.textTheme(.headlineMedium) | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.