@@ -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}
0 commit comments