This file contains hidden or 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
@Environment(\.modelContext) private var context | |
// ... | |
VStack(spacing: .zero) { | |
Text("Does this card represent you?") | |
.multilineTextAlignment(.center) | |
.font(.headline) | |
Button { |
This file contains hidden or 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
struct CardGridView: View { | |
@Query(sort: \CardEntity.title) private var cardEntities: [CardEntity] | |
var body: some View { | |
NavigationStack { | |
ScrollView { | |
LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: 3), spacing: 12) { | |
ForEach(cardEntities.compactMap { $0.toCard() }) { card in | |
CardView(card: card, size: .small, showEffects: false) |
This file contains hidden or 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
@main | |
struct SummonSelfApp: App { | |
var body: some Scene { | |
WindowGroup { | |
CardGridView() | |
.modelContainer(for: [CardEntity.self, StatEntity.self]) | |
} | |
} | |
} |
This file contains hidden or 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
extension CardEntity { | |
static func from(_ card: Card) -> CardEntity { | |
CardEntity( | |
id: card.id, | |
title: card.stats.title, | |
emoji: card.stats.type.emoji, | |
hexColor: card.stats.type.hexClor, | |
rarity: String(describing: card.stats.rarity), | |
stats: card.stats.stats.map { StatEntity(name: $0.name, value: $0.value) }, | |
image: card.image |
This file contains hidden or 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
import SwiftData | |
@Model | |
final class CardEntity { | |
@Attribute(.unique) var id: UUID | |
var title: String | |
var emoji: String | |
var hexColor: String | |
var rarity: String | |
var stats: [StatEntity] |
This file contains hidden or 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
LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: 3), spacing: 12) { | |
ForEach(cardEntities.compactMap { $0.toCard() }) { card in | |
CardView(card: card, size: .small, showEffects: false) | |
.onTapGesture { | |
selectedCard = card | |
} | |
} | |
} |
This file contains hidden or 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
final class CardCollectionViewModel: ObservableObject { | |
@Published var cards: [Card] = [] | |
func addCard(_ card: Card) { | |
cards.insert(card, at: 0) | |
} | |
} | |
struct CardGridView: View { | |
@StateObject private var viewModel = CardCollectionViewModel() |
This file contains hidden or 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
private func predictType(from imageEmbedding: [Float]) -> ElementalType { | |
guard !typeEmbeddings.isEmpty else { return .init(emoji: "", hexClor: "") } | |
// 1 in 4 chance to return a random type | |
if Int.random(in: 0..<4) == 0 { | |
let randomType = typeEmbeddings.randomElement()! | |
return ElementalType(emoji: randomType.emoji, hexClor: randomType.hex) | |
} | |
let scoredTypes = typeEmbeddings.map { type -> (TypeEmbedding, Float) in |
This file contains hidden or 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
struct CardView: View { | |
let card: Card | |
@State private var cutoutImage: Image? = nil | |
@State private var isProcessing = true | |
var body: some View { | |
ZStack { | |
Image(uiImage: card.image) | |
.circularFoilEffect() |
This file contains hidden or 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
private func generateCutout(image: UIImage) throws -> UIImage? { | |
guard let cgImage = image.cgImage else { return nil } | |
let inputImage = CIImage(cgImage: cgImage, options: [.applyOrientationProperty: true]) | |
let request = VNGenerateForegroundInstanceMaskRequest() | |
let handler = VNImageRequestHandler(ciImage: inputImage) | |
try handler.perform([request]) | |
guard let observation = request.results?.first else { |