Skip to content

Instantly share code, notes, and snippets.

@edudnyk
Last active October 7, 2021 23:14
Show Gist options
  • Save edudnyk/bc1b83d1d8e6c32a75e4143f7f474322 to your computer and use it in GitHub Desktop.
Save edudnyk/bc1b83d1d8e6c32a75e4143f7f474322 to your computer and use it in GitHub Desktop.
The view for testing with _ViewTest.
import SwiftUI
@testable import SheeKit
import XCTest
enum ID: String {
case showPartDetailButton
case changePartDetailButton
}
struct ShowPartDetail: View {
var onDismiss: () -> Void
@State var sheetDetail: InventoryItem?
var body: some View {
ZStack {
HStack {
Button("Show Part Details") {
sheetDetail = InventoryItem.first
}
.frame(maxWidth: .infinity)
._identified(by: ID.showPartDetailButton)
Button("Change Part Details") {
if sheetDetail?.quantity ?? 0 < 100500 {
sheetDetail = InventoryItem.second
} else {
sheetDetail = nil
}
}
.frame(maxWidth: .infinity)
._identified(by: ID.changePartDetailButton)
}
.shee(item: $sheetDetail,
presentationStyle: presentationStyle,
onDismiss: onDismiss) { detail in
VStack(alignment: .leading, spacing: 20) {
Text("Part Number: \(detail.partNumber)")
Text("Name: \(detail.name)")
Text("Quantity On-Hand: \(detail.quantity)")
}
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color(.systemGreen.withAlphaComponent(0.2)))
}
var presentationStyle: ModalPresentationStyle {
var sheetProperties = SheetProperties()
sheetProperties.detents = sheetDetail?.quantity ?? 0 > 100500 ? [ .large() ] : [ .medium() ]
return .formSheet(properties: sheetProperties)
}
}
struct InventoryItem: Identifiable, Hashable {
var id: String
let partNumber: String
let quantity: Int
let name: String
static let first = InventoryItem(
id: "0123456789",
partNumber: "Z-1234A",
quantity: 100,
name: "Widget")
static let second = InventoryItem(
id: "9876543210",
partNumber: "A-4321Z",
quantity: 100500,
name: "Gadget")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment