Skip to content

Commit 0b72b20

Browse files
authored
Merge pull request #1 from sameersyd/func/network-call
Func/network call
2 parents 139341f + 2d65402 commit 0b72b20

11 files changed

Lines changed: 112 additions & 98 deletions

File tree

BoxFeed.xcodeproj/project.pbxproj

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10-
7306929126979531008482DE /* NewsData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7306929026979531008482DE /* NewsData.swift */; };
1110
732E1D59269620970085F136 /* Config.swift in Sources */ = {isa = PBXBuildFile; fileRef = 732E1D58269620970085F136 /* Config.swift */; };
1211
732E1D5F269621C10085F136 /* HomeViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 732E1D5E269621C10085F136 /* HomeViewModel.swift */; };
1312
732E1D612696222A0085F136 /* HomeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 732E1D602696222A0085F136 /* HomeView.swift */; };
1413
732E1D65269625DB0085F136 /* NewsSelectorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 732E1D64269625DB0085F136 /* NewsSelectorView.swift */; };
14+
736C8EE326F5B34A0053CC8F /* NewsService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 736C8EE226F5B34A0053CC8F /* NewsService.swift */; };
15+
736C8EE726F5B8ED0053CC8F /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 736C8EE626F5B8ED0053CC8F /* Extensions.swift */; };
1516
737FE3792697A7AB00A33EB7 /* NewsModelView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 737FE3782697A7AB00A33EB7 /* NewsModelView.swift */; };
16-
737FE37B2697A9B800A33EB7 /* DateExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 737FE37A2697A9B800A33EB7 /* DateExtension.swift */; };
1717
737FE37E2697B0BF00A33EB7 /* ActivityIndicator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 737FE37D2697B0BF00A33EB7 /* ActivityIndicator.swift */; };
1818
73D48CD7269387E50044A5CC /* BoxFeedApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73D48CD6269387E50044A5CC /* BoxFeedApp.swift */; };
1919
73D48CDB269387EA0044A5CC /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 73D48CDA269387EA0044A5CC /* Assets.xcassets */; };
@@ -31,13 +31,13 @@
3131
/* End PBXBuildFile section */
3232

3333
/* Begin PBXFileReference section */
34-
7306929026979531008482DE /* NewsData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewsData.swift; sourceTree = "<group>"; };
3534
732E1D58269620970085F136 /* Config.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Config.swift; sourceTree = "<group>"; };
3635
732E1D5E269621C10085F136 /* HomeViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeViewModel.swift; sourceTree = "<group>"; };
3736
732E1D602696222A0085F136 /* HomeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeView.swift; sourceTree = "<group>"; };
3837
732E1D64269625DB0085F136 /* NewsSelectorView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewsSelectorView.swift; sourceTree = "<group>"; };
38+
736C8EE226F5B34A0053CC8F /* NewsService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewsService.swift; sourceTree = "<group>"; };
39+
736C8EE626F5B8ED0053CC8F /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; };
3940
737FE3782697A7AB00A33EB7 /* NewsModelView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewsModelView.swift; sourceTree = "<group>"; };
40-
737FE37A2697A9B800A33EB7 /* DateExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateExtension.swift; sourceTree = "<group>"; };
4141
737FE37D2697B0BF00A33EB7 /* ActivityIndicator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActivityIndicator.swift; sourceTree = "<group>"; };
4242
73D48CD3269387E50044A5CC /* BoxFeed.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = BoxFeed.app; sourceTree = BUILT_PRODUCTS_DIR; };
4343
73D48CD6269387E50044A5CC /* BoxFeedApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BoxFeedApp.swift; sourceTree = "<group>"; };
@@ -86,6 +86,7 @@
8686
731DB92C26F43FCC004D9B27 /* Library */ = {
8787
isa = PBXGroup;
8888
children = (
89+
736C8EE126F5B3250053CC8F /* Services */,
8990
73D48CEB26938A8F0044A5CC /* Helpers */,
9091
730692922697A6BD008482DE /* CustomViews */,
9192
73D48CFC269396600044A5CC /* Models */,
@@ -112,6 +113,14 @@
112113
path = Home;
113114
sourceTree = "<group>";
114115
};
116+
736C8EE126F5B3250053CC8F /* Services */ = {
117+
isa = PBXGroup;
118+
children = (
119+
736C8EE226F5B34A0053CC8F /* NewsService.swift */,
120+
);
121+
path = Services;
122+
sourceTree = "<group>";
123+
};
115124
737FE37C2697B08C00A33EB7 /* CustomViews */ = {
116125
isa = PBXGroup;
117126
children = (
@@ -145,7 +154,6 @@
145154
73D48CF526938CA50044A5CC /* Info.plist */,
146155
73D48CD6269387E50044A5CC /* BoxFeedApp.swift */,
147156
732E1D58269620970085F136 /* Config.swift */,
148-
7306929026979531008482DE /* NewsData.swift */,
149157
73D48CDA269387EA0044A5CC /* Assets.xcassets */,
150158
73D48CE1269387EA0044A5CC /* BoxFeed.xcdatamodeld */,
151159
73D48CDC269387EA0044A5CC /* Preview Content */,
@@ -169,7 +177,7 @@
169177
73D48CF626938DE20044A5CC /* Models.swift */,
170178
73D48CF826938E980044A5CC /* ColorExtension.swift */,
171179
73D48CFA269394E00044A5CC /* ImageExtension.swift */,
172-
737FE37A2697A9B800A33EB7 /* DateExtension.swift */,
180+
736C8EE626F5B8ED0053CC8F /* Extensions.swift */,
173181
);
174182
path = Helpers;
175183
sourceTree = "<group>";
@@ -274,8 +282,9 @@
274282
buildActionMask = 2147483647;
275283
files = (
276284
73D48CFE269397C10044A5CC /* NewsModel.swift in Sources */,
285+
736C8EE726F5B8ED0053CC8F /* Extensions.swift in Sources */,
286+
736C8EE326F5B34A0053CC8F /* NewsService.swift in Sources */,
277287
737FE37E2697B0BF00A33EB7 /* ActivityIndicator.swift in Sources */,
278-
737FE37B2697A9B800A33EB7 /* DateExtension.swift in Sources */,
279288
73D48CE3269387EA0044A5CC /* BoxFeed.xcdatamodeld in Sources */,
280289
732E1D59269620970085F136 /* Config.swift in Sources */,
281290
737FE3792697A7AB00A33EB7 /* NewsModelView.swift in Sources */,
@@ -285,7 +294,6 @@
285294
73D48CFB269394E00044A5CC /* ImageExtension.swift in Sources */,
286295
73D48CF926938E980044A5CC /* ColorExtension.swift in Sources */,
287296
73FC2DBC269884BA00373EB3 /* ArticleView.swift in Sources */,
288-
7306929126979531008482DE /* NewsData.swift in Sources */,
289297
73D48CEE26938AE60044A5CC /* FontModifier.swift in Sources */,
290298
732E1D65269625DB0085F136 /* NewsSelectorView.swift in Sources */,
291299
73D48CF726938DE20044A5CC /* Models.swift in Sources */,

BoxFeed/Config.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ struct AppConfig {
1212
// App Globals
1313
static let APP_NAME = "Box Feed"
1414
static let APP_LINK = "https://github.com/sameersyd/BoxFeed"
15-
15+
// https://newsapi.org
16+
static let API_KEY = ""
1617
}

BoxFeed/Library/CustomViews/NewsModelView.swift

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ struct NewsModelView: View {
2323
}.frame(width: 100, height: 80).clipped().cornerRadius(4)
2424

2525
VStack(alignment: .leading, spacing: 8) {
26-
Text(model.title).lineLimit(2)
26+
Text(model.title ?? "").lineLimit(2)
2727
.multilineTextAlignment(.leading)
2828
.foregroundColor(.text_primary)
2929
.modifier(FontModifier(.bold, size: 14))
3030
HStack(alignment: .center, spacing: 12) {
31-
newsData(image: Image.calender, text: model.publishedAt.format("MMM. dd, yy"))
31+
newsData(image: Image.calender, text: model.publishedDate ?? "")
3232
if let time = model.readTime {
3333
newsData(image: Image.clock, text: "\(time < 10 ? "0" : "")\(time)m")
3434
}
@@ -52,9 +52,3 @@ struct NewsModelView: View {
5252
}
5353
}
5454
}
55-
56-
//struct NewsModelView_Previews: PreviewProvider {
57-
// static var previews: some View {
58-
// NewsModelView()
59-
// }
60-
//}

BoxFeed/Library/Helpers/DateExtension.swift

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// Extensions.swift
3+
// BoxFeed
4+
//
5+
// Created by Sameer Nawaz on 17/09/21.
6+
//
7+
8+
import Foundation
9+
10+
extension Date {
11+
func format(_ format: String = "MMM dd, yyyy - hh:mm:a") -> String {
12+
let dateFormatter = DateFormatter()
13+
dateFormatter.dateFormat = format
14+
return dateFormatter.string(from: self)
15+
}
16+
}
17+
18+
extension String {
19+
func convertIntoDate() -> Date? {
20+
let dateFormatter = DateFormatter()
21+
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
22+
dateFormatter.timeZone = TimeZone.current
23+
dateFormatter.locale = Locale.current
24+
return dateFormatter.date(from: self)
25+
}
26+
}

BoxFeed/Library/Models/NewsModel.swift

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,37 @@
77

88
import Foundation
99

10-
struct NewsSource: Codable {
11-
var id: Sources
12-
var name: String
10+
struct NewsDataModel: Decodable {
11+
var status: String?
12+
var totalResults: Int?
13+
var articles: [NewsModel]?
1314
}
1415

15-
struct NewsModel: Codable {
16+
struct NewsModel: Decodable {
1617

17-
var source: NewsSource
18+
var id: Sources?
1819

19-
var author: String
20-
var title: String
21-
var description: String
20+
var author: String?
21+
var title: String?
22+
var description: String?
2223

2324
var url: URL?
2425
var urlToImage: URL?
2526

26-
var publishedAt: Date
27-
var content: String
27+
var publishedAt: String?
28+
var content: String?
2829

2930
var readTime: Int?
31+
32+
var publishedDate: String? {
33+
guard let date = publishedAt?.convertIntoDate() else { return "" }
34+
return date.format("MMM. dd, yyyy")
35+
}
3036
}
3137

3238
// --------
3339

34-
enum Sources: String, Codable, CaseIterable {
40+
enum Sources: String, Decodable, CaseIterable {
3541

3642
case bbcNews = "bbc-news"
3743
case bbcSport = "bbc-sport"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// NewsService.swift
3+
// BoxFeed
4+
//
5+
// Created by Sameer Nawaz on 17/09/21.
6+
//
7+
8+
import Foundation
9+
10+
final class NewsService {
11+
12+
func fetchNews(_ id: Sources) async throws -> [NewsModel]? {
13+
14+
let urlString = "https://newsapi.org/v2/everything?q=\(id)&from=2021-08-18&sortBy=publishedAt&apiKey=\(AppConfig.API_KEY)"
15+
guard let url = URL(string: urlString) else { return nil }
16+
17+
let (data, _) = try await URLSession.shared.data(from: url)
18+
if var model = try? JSONDecoder().decode(NewsDataModel.self, from: data) {
19+
for i in 0..<(model.articles?.count ?? 0) {
20+
model.articles?[i].id = id
21+
#warning("remove random read time")
22+
model.articles?[i].readTime = (1...30).randomElement()
23+
}
24+
return model.articles
25+
}
26+
return []
27+
}
28+
}

BoxFeed/NewsData.swift

Lines changed: 0 additions & 43 deletions
This file was deleted.

BoxFeed/Screens/Article/ArticleView.swift

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

1010
struct ArticleView: View {
1111

12-
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
12+
@Environment(\.dismiss) var dismiss
1313
var model: NewsModel
1414

1515
var body: some View {
@@ -37,7 +37,7 @@ extension ArticleView {
3737
private var BodyView: some View {
3838
VStack(spacing: 16) {
3939
AuthorView
40-
Text(model.content)
40+
Text(model.content ?? "")
4141
.multilineTextAlignment(.leading)
4242
.foregroundColor(.text_primary.opacity(0.7))
4343
.modifier(FontModifier(.regular, size: 16))
@@ -47,7 +47,8 @@ extension ArticleView {
4747

4848
private var AuthorView: some View {
4949
HStack(alignment: .center) {
50-
Text(model.author).foregroundColor(.text_primary)
50+
Text(model.author ?? "")
51+
.foregroundColor(.text_primary)
5152
.modifier(FontModifier(.bold, size: 16))
5253
Spacer()
5354
Button(action: { }) {
@@ -81,7 +82,7 @@ extension ArticleView {
8182

8283
VStack(spacing: 0) {
8384
HStack(alignment: .center) {
84-
Button(action: { presentationMode.wrappedValue.dismiss() }) {
85+
Button(action: { dismiss() }) {
8586
Image.x.resizable()
8687
.renderingMode(.template)
8788
.foregroundColor(.white)
@@ -120,22 +121,22 @@ extension ArticleView {
120121

121122
private var Header_DataView: some View {
122123
VStack(spacing: 20) {
123-
Text(model.title).lineLimit(3)
124+
Text(model.title ?? "").lineLimit(3)
124125
.multilineTextAlignment(.leading)
125126
.foregroundColor(.white)
126127
.modifier(FontModifier(.bold, size: 32))
127128
.frame(maxWidth: .infinity)
128129

129130
HStack(alignment: .center) {
130-
Text(model.source.name).foregroundColor(.white)
131+
Text(model.id?.name ?? "Unknown").foregroundColor(.white)
131132
.modifier(FontModifier(.extraBold, size: 12))
132133
Spacer()
133134
HStack(alignment: .bottom, spacing: 8) {
134135
Image.calender.resizable()
135136
.renderingMode(.template)
136137
.foregroundColor(.white)
137138
.frame(width: 16, height: 16)
138-
Text(model.publishedAt.format("MMM. dd, yyyy"))
139+
Text(model.publishedDate ?? "")
139140
.foregroundColor(.white)
140141
.modifier(FontModifier(.bold, size: 12))
141142
.padding(.bottom, -2)

0 commit comments

Comments
 (0)