5
5
// Created by Karima Thingvold on 01/12/2024.
6
6
//
7
7
8
- //import SwiftUI
9
- //
8
+ import SwiftUI
9
+ import SwiftData
10
+
10
11
//struct ArticlesView: View {
11
- // var body: some View {
12
- // VStack {
13
- // // HUSK å sette på "hvis artikler, vis de, hvis ikke vis dette
14
- // Image(systemName: "square.stack.3d.up.slash")
15
- // .resizable()
16
- // .aspectRatio(contentMode: .fit)
17
- // .frame(width: 100, height: 100)
18
- // .padding()
19
- //
20
- // Text("No articles are saved.")
21
- // .font(.title3)
22
- // .foregroundColor(.secondary)
23
- // .padding(.bottom, 2)
24
- //
25
- // Text("Please go to search and fetch articles and news from the internet.")
26
- // .font(.body)
27
- // .foregroundColor(.secondary)
28
- // .multilineTextAlignment(.center)
29
- // .padding(.horizontal, 30)
12
+ // @Environment(\.modelContext) var modelContext
13
+ // @Query(sort: \Article.savedDate, order: .reverse) var storedArticles: [Article]
14
+ // @State var selectedArticle: NewsArticle?
15
+ // @State var selectedCategory = "All"
16
+ // let defaultCategories = ["Technology", "Economy", "Politics", "Sports", "News"]
17
+ //
18
+ // @AppStorage("tickerPosition") var tickerPosition = "Top"
19
+ // @AppStorage("isTickerActive") var isTickerActive = false
20
+ //
21
+ // var filteredArticles: [Article] {
22
+ // if selectedCategory == "All" {
23
+ // return storedArticles
24
+ // }
25
+ // return storedArticles.filter { article in
26
+ // if let categoryName = article.category?.name {
27
+ // return categoryName == selectedCategory
28
+ // }
29
+ // return false
30
30
// }
31
- // .frame(maxWidth: .infinity, maxHeight: .infinity)
32
- // .background(Color(UIColor.systemBackground))
33
31
// }
34
- //}
35
-
36
- //#Preview {
37
- // ArticlesView()
38
- //}
39
-
40
- //
41
- //import SwiftUI
42
- //import SwiftData
43
- //
44
- //struct ArticlesView: View {
45
- // @Query(sort: \StoredArticle.savedDate, order: .reverse) private var storedArticles: [StoredArticle]
46
- //
32
+ //
47
33
// var body: some View {
34
+ //
48
35
// NavigationView {
49
- // List(storedArticles) { article in
50
- // VStack(alignment: .leading) {
51
- // Text(article.title)
52
- // .font(.headline)
53
- // if let description = article.articleDescription {
54
- // Text(description)
55
- // .font(.subheadline)
56
- // .foregroundColor(.secondary)
36
+ // if storedArticles.isEmpty {
37
+ // VStack {
38
+ // Image(systemName: "square.stack.3d.up.slash")
39
+ // .resizable()
40
+ // .frame(width: 70, height: 70)
41
+ // .padding()
42
+ //
43
+ // Text("No articles are saved.")
44
+ // .font(.title)
45
+ // .fontWeight(.bold)
46
+ // .foregroundStyle(.primary)
47
+ // .padding(.bottom, 2)
48
+ //
49
+ // Text("Please go to search and fetch articles and news from the internet.")
50
+ // .font(.body)
51
+ // .foregroundStyle(.secondary)
52
+ // .multilineTextAlignment(.center)
53
+ // .padding(.horizontal, 30)
54
+ // }
55
+ // .frame(maxWidth: .infinity, maxHeight: .infinity)
56
+ // .background(Color(UIColor.systemBackground))
57
+ // } else {
58
+ // List {
59
+ // ForEach(storedArticles) { article in
60
+ // NavigationLink(destination: ArticleDetailView(article: article.toNewsArticle())) {
61
+ // VStack(alignment: .leading) {
62
+ // Text(article.title)
63
+ // .font(.headline)
64
+ // if let description = article.articleDescription {
65
+ // Text(description)
66
+ // .font(.subheadline)
67
+ // .foregroundStyle(.secondary)
68
+ // }
69
+ // }
70
+ // }
71
+ // }
72
+ // .onDelete(perform: deleteArticle)
73
+ // }
74
+ // .navigationTitle("Your stored articles")
75
+ // .toolbar {
76
+ // ToolbarItem(placement: .navigationBarTrailing) {
77
+ // Menu {
78
+ // ForEach(defaultCategories, id: \.self) { category in
79
+ // Button(action: {
80
+ // selectedCategory = category
81
+ // })
82
+ // { Text(category)
83
+ // if selectedCategory == category {
84
+ // Image(systemName: "checkmark")
85
+ // }
86
+ // }
87
+ // }
88
+ // }
89
+ // label: {
90
+ // Image(systemName: "ellipsis.circle")
91
+ // .imageScale(.large)
92
+ // }
57
93
// }
58
94
// }
59
95
// }
60
- // .navigationTitle("Lagrede Artikler")
96
+ // }
97
+ // }
98
+ //
99
+ // func deleteArticle(at offsets: IndexSet) {
100
+ // for index in offsets {
101
+ // let article = storedArticles[index]
102
+ // article.deleteFromDatabase(context: modelContext)
61
103
// }
62
104
// }
63
105
//}
64
- //
65
106
66
- import SwiftUI
67
- import SwiftData
68
107
69
108
struct ArticlesView : View {
70
- @Environment ( \. modelContext) private var modelContext
71
- @Query ( sort: \Article . savedDate, order: . reverse)
72
- var storedArticles : [ Article ]
109
+ @Environment ( \. modelContext) var modelContext
110
+ @Query ( sort: \Article . savedDate, order: . reverse) var storedArticles : [ Article ]
111
+ @AppStorage ( " tickerPosition " ) private var tickerPosition : String = " Top "
112
+ @AppStorage ( " isNewsTickerActive " ) private var isNewsTickerActive : Bool = true
73
113
74
114
@State var selectedCategory = " All "
75
115
let defaultCategories = [ " Technology " , " Economy " , " Politics " , " Sports " , " News " ]
@@ -85,75 +125,80 @@ struct ArticlesView: View {
85
125
return false
86
126
}
87
127
}
88
-
128
+
89
129
var body : some View {
90
- // VStack {
91
- // NewsTickerView()
92
- // .frame(height: 50)
93
- // .background(Color.gray.opacity(0.1))
94
- // }
95
- NavigationView {
96
- if storedArticles. isEmpty {
97
- VStack {
98
- Image ( systemName: " square.stack.3d.up.slash " )
99
- . resizable ( )
100
- . aspectRatio ( contentMode: . fit)
101
- . frame ( width: 70 , height: 70 )
102
- . padding ( )
103
-
104
- Text ( " No articles are saved. " )
105
- . font ( . title)
106
- . fontWeight ( . bold)
107
- . foregroundStyle ( . primary)
108
- . padding ( . bottom, 2 )
109
-
110
- Text ( " Please go to search and fetch articles and news from the internet. " )
111
- . font ( . body)
112
- . foregroundStyle ( . secondary)
113
- . multilineTextAlignment ( . center)
114
- . padding ( . horizontal, 30 )
115
- }
116
- . frame ( maxWidth: . infinity, maxHeight: . infinity)
117
- . background ( Color ( UIColor . systemBackground) )
118
- } else {
119
- List {
120
- ForEach ( storedArticles) { article in
121
- NavigationLink ( destination: ArticleDetailView ( article: article. toNewsArticle ( ) ) ) {
122
- VStack ( alignment: . leading) {
123
- Text ( article. title)
124
- . font ( . headline)
125
- if let description = article. articleDescription {
126
- Text ( description)
127
- . font ( . subheadline)
128
- . foregroundStyle ( . secondary)
130
+ VStack {
131
+ if tickerPosition == " Top " && isNewsTickerActive {
132
+ NewsTickerView ( )
133
+ . frame ( height: 50 )
134
+ }
135
+
136
+ NavigationView {
137
+ if storedArticles. isEmpty {
138
+ VStack {
139
+ Image ( systemName: " square.stack.3d.up.slash " )
140
+ . resizable ( )
141
+ . frame ( width: 70 , height: 70 )
142
+ . padding ( )
143
+
144
+ Text ( " No articles are saved. " )
145
+ . font ( . title)
146
+ . fontWeight ( . bold)
147
+ . foregroundStyle ( . primary)
148
+ . padding ( . bottom, 2 )
149
+
150
+ Text ( " Please go to search and fetch articles and news from the internet. " )
151
+ . font ( . body)
152
+ . foregroundStyle ( . secondary)
153
+ . multilineTextAlignment ( . center)
154
+ . padding ( . horizontal, 30 )
155
+ }
156
+ . frame ( maxWidth: . infinity, maxHeight: . infinity)
157
+ . background ( Color ( UIColor . systemBackground) )
158
+ } else {
159
+ List {
160
+ ForEach ( filteredArticles) { article in
161
+ NavigationLink ( destination: ArticleDetailView ( article: article. toNewsArticle ( ) ) ) {
162
+ VStack ( alignment: . leading) {
163
+ Text ( article. title)
164
+ . font ( . headline)
165
+ if let description = article. articleDescription {
166
+ Text ( description)
167
+ . font ( . subheadline)
168
+ . foregroundStyle ( . secondary)
169
+ }
129
170
}
130
171
}
131
172
}
173
+ . onDelete ( perform: deleteArticle)
132
174
}
133
- . onDelete ( perform: deleteArticle)
134
- }
135
- . navigationTitle ( " Lagrede Artikler " )
136
- . toolbar {
137
- ToolbarItem ( placement: . navigationBarTrailing) {
138
- Menu {
139
- ForEach ( defaultCategories, id: \. self) { category in
140
- Button ( action: {
141
- selectedCategory = category
142
- } )
143
- { Text ( category)
144
- if selectedCategory == category {
145
- Image ( systemName: " checkmark " )
175
+ . navigationTitle ( " Your stored articles " )
176
+ . toolbar {
177
+ ToolbarItem ( placement: . navigationBarTrailing) {
178
+ Menu {
179
+ ForEach ( defaultCategories, id: \. self) { category in
180
+ Button ( action: {
181
+ selectedCategory = category
182
+ } ) {
183
+ Text ( category)
184
+ if selectedCategory == category {
185
+ Image ( systemName: " checkmark " )
186
+ }
146
187
}
147
188
}
189
+ } label: {
190
+ Image ( systemName: " ellipsis.circle " )
191
+ . imageScale ( . large)
148
192
}
149
193
}
150
- label: {
151
- Image ( systemName: " ellipsis.circle " )
152
- . imageScale ( . large)
153
- }
154
194
}
155
195
}
156
196
}
197
+
198
+ if tickerPosition == " Bottom " && isNewsTickerActive {
199
+ NewsTickerView ( )
200
+ . frame ( height: 50 )
201
+ }
157
202
}
158
203
}
159
204
0 commit comments