Skip to content

Commit 4a1bfed

Browse files
committed
Improve layout and data options
1 parent 6ffa60c commit 4a1bfed

File tree

7 files changed

+107
-73
lines changed

7 files changed

+107
-73
lines changed

CollectSomeMore/GamesAndThings.swift

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,25 @@ final class Collection {
1515
var releaseDate: Date
1616
var purchaseDate: Date
1717
var genre: String
18-
var gameConsole: String
18+
// var gameConsole: String
19+
var ratings: String
1920

20-
init(id: UUID = UUID(), title: String, releaseDate: Date, purchaseDate: Date, genre: String, gameConsole: String) {
21+
init(id: UUID = UUID(), title: String, releaseDate: Date, purchaseDate: Date, genre: String, ratings: String) {
2122
self.id = id
2223
self.title = title
2324
self.releaseDate = releaseDate
2425
self.purchaseDate = purchaseDate
2526
self.genre = genre
26-
self.gameConsole = gameConsole
27+
// self.gameConsole = gameConsole
28+
self.ratings = ratings
2729
}
2830

2931
@MainActor static let sampleData = [
3032
Collection(id: UUID(), title: "Deadpool",
31-
releaseDate: Date(timeIntervalSinceReferenceDate: -402_00_00),
32-
purchaseDate: Date(timeIntervalSinceNow: -5_000_000),
33-
genre: "Superhero",
34-
gameConsole: "Sega Genesis")
33+
releaseDate: Date(timeIntervalSinceReferenceDate: -402_00_00),
34+
purchaseDate: Date(timeIntervalSinceNow: -5_000_000),
35+
genre: "Superhero",
36+
ratings: "R"
37+
)
3538
]
3639
}

CollectSomeMore/Movie/MovieDetail.swift

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ struct MovieDetail: View {
2020

2121
let isNew: Bool
2222

23-
let genres = ["Action", "Animated", "Anime", "Comedy", "Documentary", "Drama", "Educational", "Horror", "Romance", "Sci-Fi", "Suspense","Superhero"]
24-
let gameConsole = ["Sega Genesis", "Nintendo Switch", "PlayStation 4", "PlayStation 5", "Xbox One", "Xbox Series X|S"]
23+
let genres = ["", "Action", "Animated", "Anime", "Comedy", "Documentary", "Drama", "Educational", "Horror", "Romance", "Sci-Fi", "Suspense","Superhero"]
24+
// let gameConsole = ["", "Sega Genesis", "Nintendo Switch", "PlayStation 4", "PlayStation 5", "Xbox One", "Xbox Series X|S"]
2525
// let figures = ["Board Game", "Book", "Movie", "Video Game"]
26-
// let ratings = ["G", "PG", "PG-13", "R", "NR"]
26+
let ratings = ["G", "PG", "PG-13", "R", "NR"]
2727
// let locations = ["Cabinet", "iTunes", "Network"]
2828
// let videoFormats = ["Digital", "DVD", "BluRay", "4k BluRay"]
2929

@@ -38,6 +38,11 @@ struct MovieDetail: View {
3838
TextField("Title", text: $collection.title)
3939
}
4040
Section(header: Text("Details")) {
41+
Picker("Rating", selection: $collection.ratings) {
42+
ForEach(ratings, id: \.self) { rating in
43+
Text(rating).tag(rating)
44+
}
45+
}
4146
Picker("Genre", selection: $collection.genre) {
4247
ForEach(genres, id: \.self) { genre in
4348
Text(genre).tag(genre)
@@ -49,14 +54,10 @@ struct MovieDetail: View {
4954
DatePicker("Purchase Date", selection: $collection.purchaseDate, displayedComponents: .date)
5055
}
5156
Section(header: Text("Other")) {
52-
Picker("Game Console", selection: $collection.gameConsole) {
53-
ForEach(gameConsole, id: \.self) { console in
54-
Text(console).tag(console)
55-
}
56-
}
57+
5758
}
5859
}
59-
.navigationTitle(isNew ? "New" : "\(collection.title)")
60+
.navigationBarTitle(isNew ? "New" : "\(collection.title)")
6061
.navigationBarTitleDisplayMode(.large)
6162
.toolbar {
6263
if isNew {
@@ -105,7 +106,7 @@ struct MovieDetail: View {
105106
#Preview("New Movie") {
106107
NavigationStack {
107108
MovieDetail(collection: CollectionData.shared.collection, isNew: false)
108-
.navigationTitle("New Movie")
109+
.navigationBarTitle("New Movie")
109110
.navigationBarTitleDisplayMode(.large)
110111
}
111112
.modelContainer(CollectionData.shared.modelContainer)

CollectSomeMore/Movie/MovieExport.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ struct Record: Identifiable {
1414
let releaseDate: Date
1515
let purchaseDate: Date
1616
let genre: String
17-
let gameConsole: String
17+
let ratings: String
18+
let favorite: Bool = false
1819

1920
func toCSV() -> String {
20-
return "\(title),\(releaseDate),\(purchaseDate), \(genre), \(gameConsole)\n"
21+
return "\(title),\(releaseDate),\(purchaseDate), \(genre), \(ratings), \(favorite)\n"
2122
}
2223
}
2324

@@ -28,7 +29,7 @@ struct ExportView: View {
2829

2930
// Sample data
3031
let records: [Record] = [
31-
Record(title: "Deadpool", releaseDate: Date(timeIntervalSinceReferenceDate: -402_00_00), purchaseDate: Date(timeIntervalSinceNow: -5_000_000), genre: "Superhero", gameConsole: "Sega Genesis")
32+
Record(title: "Deadpool", releaseDate: Date(timeIntervalSinceReferenceDate: -402_00_00), purchaseDate: Date(timeIntervalSinceNow: -5_000_000), genre: "Superhero", ratings: "R")
3233
]
3334

3435
var body: some View {
@@ -43,7 +44,7 @@ struct ExportView: View {
4344
.cornerRadius(8)
4445
}
4546
.sheet(isPresented: $showingExportSheet) {
46-
ShareSheet(activityItems: [createCSVFile()])
47+
ShareSheet(activityItems: [createCSVFile() ?? ""])
4748
}
4849
.alert("Export Error", isPresented: $showingAlert) {
4950
Button("OK", role: .cancel) { }
@@ -59,7 +60,7 @@ struct ExportView: View {
5960
}
6061

6162
private func createCSVFile() -> URL? {
62-
let headers = "Title,Release Date,PurchaseDate,Genere,Game Console\n"
63+
let headers = "Title,Release Date,PurchaseDate,Genere,Ratings,Favorite\n"
6364
let rows = records.map { $0.toCSV() }.joined(separator: "\n")
6465
let csvContent = headers + rows
6566

CollectSomeMore/Movie/MovieList.swift

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ struct MovieList: View {
1515
@Query(sort: \Collection.title) private var collections: [Collection]
1616

1717
enum SortOption {
18-
case title, genre
18+
case title, ratings
1919
}
2020

2121
@State private var newCollection: Collection?
2222
@State private var selectedItem: Int = 0
2323
@State private var sortOption: SortOption = .title
2424
@State private var showingExportSheet = false
2525
@State private var showingAlert = false
26-
@State private var alertMessage = ""
26+
@State private var alertMessage = ""
2727

2828
let records: [Record] = [
29-
Record(title: "Deadpool", releaseDate: Date(timeIntervalSinceReferenceDate: -402_00_00), purchaseDate: Date(timeIntervalSinceNow: -5_000_000), genre: "Superhero", gameConsole: "Sega Genesis")
29+
Record(title: "Deadpool", releaseDate: Date(timeIntervalSinceReferenceDate: -402_00_00), purchaseDate: Date(timeIntervalSinceNow: -5_000_000), genre: "Superhero", ratings: "R")
3030
]
3131

3232
let isNew: Bool
@@ -41,9 +41,9 @@ struct MovieList: View {
4141
case .title:
4242
// sort A -> Z
4343
return collections.sorted { $0.title < $1.title }
44-
case .genre:
44+
case .ratings:
4545
// sort A -> Z
46-
return collections.sorted { $0.genre < $1.genre }
46+
return collections.sorted { $0.ratings < $1.ratings }
4747
}
4848
}
4949

@@ -52,7 +52,7 @@ struct MovieList: View {
5252
Picker("Sort By", selection: $sortOption) {
5353
Text("Title").tag(SortOption.title)
5454
// Text("Release Date").tag(SortOption.releaseDate)
55-
Text("Genre").tag(SortOption.genre)
55+
Text("Rating").tag(SortOption.ratings)
5656
}.pickerStyle(SegmentedPickerStyle())
5757
Group {
5858
if !collections.isEmpty {
@@ -65,25 +65,38 @@ struct MovieList: View {
6565
.onDelete(perform: deleteItems)
6666
}
6767
.navigationTitle("Movies")
68-
.navigationBarTitleDisplayMode(.inline)
68+
.navigationBarTitleDisplayMode(.automatic)
6969
.toolbar {
70-
ToolbarItem(placement: .topBarTrailing) {
70+
// ToolbarItem(placement: .topBarLeading) {
71+
// Button("Export") {
72+
// if createCSVFile() != nil {
73+
// showingExportSheet = true
74+
// }
75+
// }
76+
// .sheet(isPresented: $showingExportSheet) {
77+
// ShareSheet(activityItems: [createCSVFile()].compactMap { $0 })
78+
// }
79+
// .alert("Export Error", isPresented: $showingAlert) {
80+
// Button("OK", role: .cancel) { }
81+
// } message: {
82+
// Text(alertMessage)
83+
// }
84+
// }
85+
ToolbarItemGroup(placement: .primaryAction) {
7186
EditButton();
72-
Button("Export") {
87+
Button("Export", systemImage: "square.and.arrow.up") {
7388
if createCSVFile() != nil {
74-
showingExportSheet = true
75-
}
76-
}
77-
.sheet(isPresented: $showingExportSheet) {
78-
ShareSheet(activityItems: [createCSVFile()].compactMap { $0 })
89+
showingExportSheet = true
7990
}
80-
.alert("Export Error", isPresented: $showingAlert) {
81-
Button("OK", role: .cancel) { }
82-
} message: {
83-
Text(alertMessage)
84-
}
85-
}
86-
ToolbarItemGroup(placement: .primaryAction) {
91+
}
92+
.sheet(isPresented: $showingExportSheet) {
93+
ShareSheet(activityItems: [createCSVFile()].compactMap { $0 })
94+
}
95+
.alert("Export Error", isPresented: $showingAlert) {
96+
Button("OK", role: .cancel) { }
97+
} message: {
98+
Text(alertMessage)
99+
}
87100
Button(action: addCollection) {
88101
Label("Add Movie", systemImage: "plus.app")
89102
}
@@ -109,7 +122,7 @@ struct MovieList: View {
109122

110123
private func addCollection() {
111124
withAnimation {
112-
let newItem = Collection(id: UUID(), title: "", releaseDate: .now, purchaseDate: Date(timeIntervalSinceNow: -5_000_000), genre: "Action", gameConsole: "Sega Genesis")
125+
let newItem = Collection(id: UUID(), title: "", releaseDate: .now, purchaseDate: Date(timeIntervalSinceNow: -5_000_000), genre: "Action", ratings: "R")
113126
modelContext.insert(newItem)
114127
newCollection = newItem
115128
}
@@ -122,7 +135,7 @@ struct MovieList: View {
122135
}
123136

124137
private func createCSVFile() -> URL? {
125-
let headers = "Title,Release Date,PurchaseDate,Genere,Game Console\n"
138+
let headers = "Title,Release Date,PurchaseDate,Genere,Ratings\n"
126139
let rows = records.map { $0.toCSV() }.joined(separator: "\n")
127140
let csvContent = headers + rows
128141

@@ -132,7 +145,7 @@ struct MovieList: View {
132145
return nil
133146
}
134147

135-
let fileName = "export_\(Date().timeIntervalSince1970).csv"
148+
let fileName = "Movie_List_Backup_\(Date().timeIntervalSince1970).csv"
136149
let fileURL = documentsPath.appendingPathComponent(fileName)
137150

138151
do {
@@ -155,16 +168,15 @@ struct MovieList: View {
155168
}
156169

157170
#Preview("Data List") {
158-
NavigationStack {
159-
MovieList(collection: CollectionData.shared.collection)
160-
}
161-
.navigationTitle("Movies")
162-
.navigationBarTitleDisplayMode(.inline)
171+
MovieList(collection: CollectionData.shared.collection)
172+
.navigationTitle("Data List")
173+
.navigationBarTitleDisplayMode(.inline)
174+
.modelContainer(for: Collection.self, inMemory: false)
163175
}
164176

165177
#Preview("Empty List") {
166-
NavigationStack {
167-
MovieList(collection: CollectionData.shared.collection)
168-
}
169-
.modelContainer(for: Collection.self, inMemory: true)
178+
MovieList(collection: CollectionData.shared.collection)
179+
.navigationTitle("Empty")
180+
.navigationBarTitleDisplayMode(.inline)
181+
.modelContainer(for: Collection.self, inMemory: true)
170182
}

CollectSomeMore/Movie/MovieRowView.swift

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import SwiftData
1212

1313
struct MovieRowView: View {
1414
@Bindable var collection: Collection
15-
15+
1616
var body: some View {
1717
HStack(spacing: 0) {
1818
HStack(spacing: 4) {
@@ -33,17 +33,31 @@ struct MovieRowView: View {
3333
Text(collection.title)
3434
.foregroundColor(.text)
3535
.font(.headline)
36-
HStack(spacing: 0) {
37-
Text(collection.genre)
36+
HStack(spacing: 1) {
37+
Label(collection.ratings, systemImage: "star")
38+
.labelStyle(.titleOnly)
39+
.foregroundColor(.secondary)
40+
.font(.caption)
3841
.padding(.horizontal)
39-
.foregroundStyle(.tertiaryText)
42+
Label(collection.genre, systemImage: "movieclapper")
43+
.labelStyle(.titleOnly)
44+
.foregroundColor(.secondary)
45+
.font(.caption)
46+
// Text(collection.genre)
47+
// .padding(.horizontal)
48+
// Text(collection.ratings)
49+
// .padding(.horizontal)
4050
}
4151
.foregroundColor(.secondary)
4252
.font(.subheadline)
4353
}
4454
}
4555
}
4656
}
57+
// Label(collection.ratings, systemImage: "star")
58+
// .labelStyle(.titleOnly)
59+
// .foregroundColor(.secondary)
60+
// .font(.caption)
4761
}
4862
}
4963

0 commit comments

Comments
 (0)