Skip to content

Commit 437837a

Browse files
committed
Adding a second collection for Games
1 parent b201adc commit 437837a

23 files changed

+870
-559
lines changed

CollectSomeMore.xctestplan

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"configurations" : [
3+
{
4+
"id" : "D3EAEF66-157F-4240-ACC9-D38E55C1CD83",
5+
"name" : "Test Scheme Action",
6+
"options" : {
7+
8+
}
9+
}
10+
],
11+
"defaultOptions" : {
12+
"codeCoverage" : false,
13+
"distributor" : "com.apple.TestFlight",
14+
"language" : "en",
15+
"region" : "US",
16+
"targetForVariableExpansion" : {
17+
"containerPath" : "container:GamesAndThings.xcodeproj",
18+
"identifier" : "8DEC3A8E2C13D0C0004D57B7",
19+
"name" : "CollectSomeMore"
20+
}
21+
},
22+
"testTargets" : [
23+
24+
],
25+
"version" : 1
26+
}

CollectSomeMore/CollectSomeMoreApp.swift

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,74 @@
88
import SwiftUI
99
import SwiftData
1010

11+
@MainActor
12+
class GameData {
13+
private(set) static var shared: GameData?
14+
let modelContainer: ModelContainer
15+
16+
var context: ModelContext {
17+
modelContainer.mainContext
18+
}
19+
20+
init(modelContainer: ModelContainer) {
21+
self.modelContainer = modelContainer
22+
insertCollectionData(modelContext: context)
23+
GameData.shared = self // Set the shared instance here
24+
}
25+
26+
private init() { // Make the default init unavailable
27+
fatalError("Use init(modelContainer:) instead")
28+
}
29+
30+
func insertCollectionData(modelContext: ModelContext) {
31+
// Check if any data already exists
32+
let existingCollections = try? modelContext.fetch(FetchDescriptor<GameCollection>())
33+
34+
if let existingCollections = existingCollections, existingCollections.isEmpty {
35+
// Insert sample data only if the database is empty
36+
for collection in GameCollection.sampleGameCollectionData {
37+
modelContext.insert(collection)
38+
}
39+
40+
do {
41+
try modelContext.save()
42+
print("Sample data inserted successfully.")
43+
} catch {
44+
print("Sample data context failed to save: \(error)")
45+
}
46+
} else {
47+
print("Sample data already exists. No need to insert.")
48+
}
49+
}
50+
51+
var collection: GameCollection {
52+
GameCollection.sampleGameCollectionData[0]
53+
}
54+
}
55+
1156
@main
1257
struct GamesAndThings: App {
13-
var sharedModelContainer: ModelContainer = {
58+
let container: ModelContainer = {
1459
let schema = Schema([
15-
Collection.self,
60+
MovieCollection.self,
61+
GameCollection.self
1662
])
1763
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
18-
1964
do {
2065
return try ModelContainer(for: schema, configurations: [modelConfiguration])
2166
} catch {
22-
print("Error creating ModelContainer: \(error.localizedDescription)")
2367
fatalError("Could not create ModelContainer: \(error)")
2468
}
2569
}()
2670

71+
init() {
72+
_ = GameData(modelContainer: container) // Initialize GameData and set shared
73+
}
74+
2775
var body: some Scene {
2876
WindowGroup {
2977
ContentView()
78+
.environment(\.modelContext, container.mainContext)
3079
}
31-
.modelContainer(sharedModelContainer)
3280
}
3381
}

CollectSomeMore/ContentView.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ struct Constants {
4040

4141
struct ContentView: View {
4242
@Environment(\.modelContext) private var modelContext
43-
@Query(sort: \Collection.movieTitle) private var collections: [Collection]
44-
45-
@State private var newCollection: Collection?
43+
4644
@State private var searchText = ""
4745

4846
var body: some View {
@@ -54,24 +52,26 @@ struct ContentView: View {
5452
.labelStyle(.iconOnly)
5553
}
5654
.background(Gradient(colors: darkBottom))
57-
MovieList(collection: CollectionData.shared.collection)
55+
MovieList() // Removed passing movieCollection
5856
.tabItem {
5957
Label("Movies", systemImage: "books.vertical")
6058
.labelStyle(.iconOnly)
6159
}
62-
// .background(Gradient(colors: transparentGradient))
60+
GameListView() // Removed passing gameCollection
61+
.tabItem {
62+
Label("Games", systemImage: "gamecontroller")
63+
.labelStyle(.iconOnly)
64+
}
6365
SearchView()
6466
.tabItem {
6567
Label("Search", systemImage: "magnifyingglass")
6668
.labelStyle(.iconOnly)
6769
}
68-
// .background(Gradient(colors: transparentGradient))
6970
AboutView()
7071
.tabItem {
71-
Label("About", systemImage: "info.circle")
72+
Label("About", systemImage: "info")
7273
.labelStyle(.iconOnly)
7374
}
74-
// .background(Gradient(colors: darkBottom))
7575
}
7676
.toolbarBackground(.tabBar, for: .tabBar)
7777
.toolbarBackground(.visible, for: .tabBar)
@@ -83,6 +83,6 @@ struct ContentView: View {
8383
#Preview("Content View") {
8484
ContentView()
8585
.navigationTitle("Welcome to Game and Things")
86-
.modelContainer(CollectionData.shared.modelContainer)
86+
.modelContainer(MovieData.shared.modelContainer)
8787
.frame(maxHeight: .infinity)
8888
}

CollectSomeMore/CustomFonts.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// Copyright © 2025 AdamJolicoeur. All rights reserved.
77
//
88

9-
import SwiftUICore
109
import SwiftUI
1110

1211
extension View {

CollectSomeMore/Data/GameData.swift

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//
2+
// GameData.swift
3+
// GamesAndThings
4+
//
5+
// Created by Adam Jolicoeur on 3/26/25.
6+
// Copyright © 2025 AdamJolicoeur. All rights reserved.
7+
//
8+
9+
//import Foundation
10+
//import SwiftData
11+
//
12+
//@MainActor
13+
//class GameData {
14+
// static let shared = GameData()
15+
//
16+
// let modelContainer: ModelContainer
17+
//
18+
// var context: ModelContext {
19+
// modelContainer.mainContext
20+
// }
21+
//
22+
// private init() {
23+
// let schema = Schema([
24+
// GameCollection.self,
25+
// MovieCollection.self,
26+
// ])
27+
// let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
28+
//
29+
// do {
30+
// modelContainer = try ModelContainer(for: schema, configurations: [modelConfiguration])
31+
//
32+
// insertCollectionData(modelContext: ModelContext.init(modelContainer))
33+
// } catch {
34+
// fatalError("Could not create ModelContainer: \(error)")
35+
// }
36+
// }
37+
//import Foundation
38+
//import SwiftData
39+
//
40+
//@MainActor
41+
//class GameData {
42+
// static let shared: GameData // Will be initialized in the App struct
43+
// let modelContainer: ModelContainer
44+
//
45+
// var context: ModelContext {
46+
// modelContainer.mainContext
47+
// }
48+
//
49+
// init(modelContainer: ModelContainer) {
50+
// self.modelContainer = modelContainer
51+
// insertCollectionData(modelContext: context)
52+
// }
53+
//
54+
// private init() { // Make the default init unavailable
55+
// fatalError("Use init(modelContainer:) instead")
56+
// }
57+
//
58+
// func insertCollectionData(modelContext: ModelContext) {
59+
// // Check if any data already exists
60+
// let existingCollections = try? modelContext.fetch(FetchDescriptor<GameCollection>())
61+
//
62+
// if let existingCollections = existingCollections, existingCollections.isEmpty {
63+
// // Insert sample data only if the database is empty
64+
// for collection in GameCollection.sampleGameCollectionData {
65+
// modelContext.insert(collection)
66+
// }
67+
//
68+
// do {
69+
// try modelContext.save()
70+
// print("Sample data inserted successfully.")
71+
// } catch {
72+
// print("Sample data context failed to save: \(error)")
73+
// }
74+
// } else {
75+
// print("Sample data already exists. No need to insert.")
76+
// }
77+
// }
78+
//
79+
// var collection: GameCollection {
80+
// GameCollection.sampleGameCollectionData[0]
81+
// }
82+
//}

CollectSomeMore/Movie/MovieData.swift renamed to CollectSomeMore/Data/MovieData.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import Foundation
99
import SwiftData
1010

1111
@MainActor
12-
class CollectionData {
13-
static let shared = CollectionData()
12+
class MovieData {
13+
static let shared = MovieData()
1414

1515
let modelContainer: ModelContainer
1616

@@ -20,7 +20,8 @@ class CollectionData {
2020

2121
private init() {
2222
let schema = Schema([
23-
Collection.self,
23+
MovieCollection.self,
24+
GameCollection.self,
2425
])
2526
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
2627

@@ -35,11 +36,11 @@ class CollectionData {
3536

3637
func insertCollectionData(modelContext: ModelContext) {
3738
// Check if any data already exists
38-
let existingCollections = try? modelContext.fetch(FetchDescriptor<Collection>())
39+
let existingCollections = try? modelContext.fetch(FetchDescriptor<MovieCollection>())
3940

4041
if let existingCollections = existingCollections, existingCollections.isEmpty {
4142
// Insert sample data only if the database is empty
42-
for collection in Collection.sampleData {
43+
for collection in MovieCollection.sampleCollectionData {
4344
modelContext.insert(collection)
4445
}
4546

@@ -54,7 +55,7 @@ class CollectionData {
5455
}
5556
}
5657

57-
var collection: Collection {
58-
Collection.sampleData[0]
58+
var collection: MovieCollection {
59+
MovieCollection.sampleCollectionData[0]
5960
}
6061
}

0 commit comments

Comments
 (0)