Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeya authored Oct 10, 2024
0 parents commit 9f974b9
Show file tree
Hide file tree
Showing 21 changed files with 1,031 additions and 0 deletions.
383 changes: 383 additions & 0 deletions News/News.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 not shown.
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>
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 News/News/Assets.xcassets/AccentColor.colorset/Contents.json
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 News/News/Assets.xcassets/AppIcon.appiconset/Contents.json
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
}
}
6 changes: 6 additions & 0 deletions News/News/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
55 changes: 55 additions & 0 deletions News/News/CategoriesView.swift
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()
}
33 changes: 33 additions & 0 deletions News/News/ContentView.swift
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()
}
59 changes: 59 additions & 0 deletions News/News/HighlightNewsView.swift
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)
}
17 changes: 17 additions & 0 deletions News/News/NewsApp.swift
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()
}
}
}
47 changes: 47 additions & 0 deletions News/News/NewsCategorySectionView.swift
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])
}
58 changes: 58 additions & 0 deletions News/News/NewsCategoryView.swift
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)
}
Loading

0 comments on commit 9f974b9

Please sign in to comment.