-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9f974b9
Showing
21 changed files
with
1,031 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
News/News.xcodeproj/project.xcworkspace/contents.xcworkspacedata
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
News/News.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>IDEDidComputeMac32BitWarning</key> | ||
<true/> | ||
</dict> | ||
</plist> |
Binary file added
BIN
+54.4 KB
....xcodeproj/project.xcworkspace/xcuserdata/momo.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
6 changes: 6 additions & 0 deletions
6
News/News.xcodeproj/xcuserdata/momo.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Bucket | ||
uuid = "D24A5B50-FFD2-4866-803E-2C3D997BDA5B" | ||
type = "1" | ||
version = "2.0"> | ||
</Bucket> |
14 changes: 14 additions & 0 deletions
14
News/News.xcodeproj/xcuserdata/momo.xcuserdatad/xcschemes/xcschememanagement.plist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>SchemeUserState</key> | ||
<dict> | ||
<key>News.xcscheme_^#shared#^_</key> | ||
<dict> | ||
<key>orderHint</key> | ||
<integer>0</integer> | ||
</dict> | ||
</dict> | ||
</dict> | ||
</plist> |
11 changes: 11 additions & 0 deletions
11
News/News/Assets.xcassets/AccentColor.colorset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"colors" : [ | ||
{ | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
News/News/Assets.xcassets/AppIcon.appiconset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "universal", | ||
"platform" : "ios", | ||
"size" : "1024x1024" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// | ||
// CategoriesView.swift | ||
// News | ||
// | ||
// Created by Momo on 21/8/2567 BE. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct CategoriesView: View { | ||
|
||
var categories = ["Business", "Entertainment", "General", "Technology"] | ||
@State var newsCategories: [NewsCategory] = [] | ||
|
||
var body: some View { | ||
NavigationStack { | ||
ScrollView { | ||
VStack { | ||
ForEach(newsCategories, id: \.category) { category in | ||
NewsCategorySectionView(category: category.category, article: category.articles) | ||
} | ||
} | ||
} | ||
.navigationTitle("Category") | ||
.navigationBarTitleDisplayMode(.inline) | ||
.task { | ||
for category in categories { | ||
await loadNewsCategory(category: category) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func loadNewsCategory(category: String) async { | ||
guard let url = URL(string: "https://newsapi.org/v2/top-headlines?country=us&apiKey=4ab80a57dcd1405f8fdbd016bc0e2957&category=\(category)") else { return } | ||
|
||
do { | ||
let (data, _) = try await URLSession.shared.data(from: url) | ||
let decoder = JSONDecoder() | ||
decoder.dateDecodingStrategy = .iso8601 | ||
let response = try decoder.decode(News.self, from: data) | ||
|
||
if let articles = response.articles { | ||
newsCategories.append(NewsCategory(category: category, articles: articles)) | ||
} | ||
} catch { | ||
print("Error: \(error)") | ||
} | ||
} | ||
|
||
} | ||
|
||
#Preview { | ||
CategoriesView() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// ContentView.swift | ||
// News | ||
// | ||
// Created by Momo on 27/6/2567 BE. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct ContentView: View { | ||
|
||
var body: some View { | ||
TabView { | ||
TopStoriesView() | ||
.tabItem { | ||
Label("Today", systemImage: "house") | ||
} | ||
CategoriesView() | ||
.tabItem { | ||
Label("Category", systemImage: "square.grid.2x2") | ||
} | ||
SearchNewsView() | ||
.tabItem { | ||
Label("Search", systemImage: "magnifyingglass") | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
#Preview { | ||
ContentView() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// | ||
// HighlightNewsView.swift | ||
// News | ||
// | ||
// Created by Momo on 4/8/2567 BE. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct HighlightNewsView: View { | ||
|
||
var article: Article | ||
|
||
var body: some View { | ||
VStack { | ||
VStack(alignment: .leading, spacing: 12) { | ||
if let urlString = article.urlToImage, let url = URL(string: urlString) { | ||
AsyncImage(url: url) { image in | ||
image | ||
.resizable() | ||
.frame(width: UIScreen.main.bounds.width - 24, height: 250) | ||
.clipShape(RoundedRectangle(cornerRadius: 12)) | ||
} placeholder: { | ||
ProgressView() | ||
} | ||
} | ||
VStack(alignment: .leading, spacing: 8) { | ||
Text(article.source?.name ?? "-") | ||
.font(.system(size: 12, weight: .medium)) | ||
Text(article.title ?? "-") | ||
.font(.system(size: 18, weight: .bold)) | ||
if let date = article.publishedAt { | ||
let formatter = RelativeDateTimeFormatter() | ||
let formatText = formatter.localizedString(for: date, relativeTo: Date()) | ||
Text(formatText) | ||
.font(.system(size: 12)) | ||
} | ||
} | ||
} | ||
.padding(EdgeInsets(top: 12, leading: 12, bottom: 12, trailing: 12)) | ||
Divider() | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
let article = Article( | ||
author: "John Doe", | ||
title: "Understanding SwiftUI: A Comprehensive Guide", | ||
description: "This article provides an in-depth look at SwiftUI, exploring its components, advantages, and how to get started.", | ||
url: "https://www.swiftnewsdaily.com/articles/swiftui-comprehensive-guide", | ||
urlToImage: "https://picsum.photos/200", | ||
publishedAt: Date().addingTimeInterval(-50000), | ||
content: "", | ||
source: Source(id: "swift-news", name: "Swift News Daily") | ||
) | ||
|
||
return HighlightNewsView(article: article) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// NewsApp.swift | ||
// News | ||
// | ||
// Created by Momo on 27/6/2567 BE. | ||
// | ||
|
||
import SwiftUI | ||
|
||
@main | ||
struct NewsApp: App { | ||
var body: some Scene { | ||
WindowGroup { | ||
ContentView() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// NewsCategorySectionView.swift | ||
// News | ||
// | ||
// Created by Momo on 21/8/2567 BE. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct NewsCategorySectionView: View { | ||
|
||
var category: String | ||
var article: [Article] | ||
|
||
var body: some View { | ||
VStack { | ||
VStack(alignment: .leading, spacing: 12) { | ||
Text(category) | ||
.font(.system(size: 18, weight: .bold)) | ||
ScrollView(.horizontal) { | ||
HStack(alignment: .top, spacing: 12) { | ||
ForEach(article) { article in | ||
NewsCategoryView(article: article) | ||
} | ||
} | ||
} | ||
} | ||
.padding() | ||
Divider() | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
let article = Article( | ||
author: "John Doe", | ||
title: "Understanding SwiftUI: A Comprehensive Guide", | ||
description: "This article provides an in-depth look at SwiftUI, exploring its components, advantages, and how to get started.", | ||
url: "https://www.swiftnewsdaily.com/articles/swiftui-comprehensive-guide", | ||
urlToImage: "https://picsum.photos/200", | ||
publishedAt: Date().addingTimeInterval(-50000), | ||
content: "", | ||
source: Source(id: "swift-news", name: "Swift News Daily") | ||
) | ||
|
||
return NewsCategorySectionView(category: "Business", article: [article, article]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// | ||
// NewsCategoryView.swift | ||
// News | ||
// | ||
// Created by Momo on 21/8/2567 BE. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct NewsCategoryView: View { | ||
|
||
var article: Article | ||
|
||
var body: some View { | ||
VStack(alignment: .leading, spacing: 12) { | ||
if let urlString = article.urlToImage, let url = URL(string: urlString) { | ||
AsyncImage(url: url) { image in | ||
image | ||
.resizable() | ||
.scaledToFill() | ||
.frame(width: 120, height: 120) | ||
.clipShape(RoundedRectangle(cornerRadius: 12)) | ||
} placeholder: { | ||
ProgressView() | ||
} | ||
} else { | ||
Color.gray | ||
.frame(width: 120, height: 120) | ||
.clipShape(RoundedRectangle(cornerRadius: 12)) | ||
} | ||
Text(article.title ?? "") | ||
.font(.system(size: 12, weight: .bold)) | ||
.lineLimit(3) | ||
if let date = article.publishedAt { | ||
let formatter = RelativeDateTimeFormatter() | ||
Text(formatter.localizedString(for: date, relativeTo: Date())) | ||
.font(.system(size: 12)) | ||
} | ||
} | ||
.frame(width: 120) | ||
} | ||
|
||
} | ||
|
||
#Preview { | ||
let article = Article( | ||
author: "John Doe", | ||
title: "Understanding SwiftUI: A Comprehensive Guide", | ||
description: "This article provides an in-depth look at SwiftUI, exploring its components, advantages, and how to get started.", | ||
url: "https://www.swiftnewsdaily.com/articles/swiftui-comprehensive-guide", | ||
urlToImage: "https://picsum.photos/200", | ||
publishedAt: Date().addingTimeInterval(-50000), | ||
content: "", | ||
source: Source(id: "swift-news", name: "Swift News Daily") | ||
) | ||
|
||
return NewsCategoryView(article: article) | ||
} |
Oops, something went wrong.