Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
Sakurahihih committed Oct 14, 2024
1 parent fc2ece2 commit f926008
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 7 deletions.
4 changes: 4 additions & 0 deletions Assignment1_IOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
0F32953E2CBCF32900E86BA5 /* Assignment1_IOSTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0F32953D2CBCF32900E86BA5 /* Assignment1_IOSTests.swift */; };
0F3295482CBCF32900E86BA5 /* Assignment1_IOSUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0F3295472CBCF32900E86BA5 /* Assignment1_IOSUITests.swift */; };
0F32954A2CBCF32900E86BA5 /* Assignment1_IOSUITestsLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0F3295492CBCF32900E86BA5 /* Assignment1_IOSUITestsLaunchTests.swift */; };
0F3295572CBD049900E86BA5 /* HomeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0F3295562CBD049900E86BA5 /* HomeView.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -45,6 +46,7 @@
0F3295432CBCF32900E86BA5 /* Assignment1_IOSUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Assignment1_IOSUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
0F3295472CBCF32900E86BA5 /* Assignment1_IOSUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Assignment1_IOSUITests.swift; sourceTree = "<group>"; };
0F3295492CBCF32900E86BA5 /* Assignment1_IOSUITestsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Assignment1_IOSUITestsLaunchTests.swift; sourceTree = "<group>"; };
0F3295562CBD049900E86BA5 /* HomeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -97,6 +99,7 @@
children = (
0F32952B2CBCF32700E86BA5 /* Assignment1_IOSApp.swift */,
0F32952D2CBCF32700E86BA5 /* ContentView.swift */,
0F3295562CBD049900E86BA5 /* HomeView.swift */,
0F32952F2CBCF32800E86BA5 /* Assets.xcassets */,
0F3295312CBCF32800E86BA5 /* Assignment1_IOS.entitlements */,
0F3295322CBCF32800E86BA5 /* Preview Content */,
Expand Down Expand Up @@ -259,6 +262,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
0F3295572CBD049900E86BA5 /* HomeView.swift in Sources */,
0F32952E2CBCF32700E86BA5 /* ContentView.swift in Sources */,
0F32952C2CBCF32700E86BA5 /* Assignment1_IOSApp.swift in Sources */,
);
Expand Down
32 changes: 26 additions & 6 deletions Assignment1_IOS/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,34 @@
import SwiftUI

struct ContentView: View {

var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
TabView{
HomeView()
.tabItem {
Image(systemName: "house.circle.fill")
Text("Home")
}

HomeView()
.tabItem {
Image(systemName: "calendar.circle.fill")
Text("Events")
}

HomeView()
.tabItem {
Image(systemName: "magnifyingglass.circle.fill")
Text("Search")
}

HomeView()
.tabItem {
Image(systemName: "person.fill")
Text("Login")
}

}
.padding()
}
}

Expand Down
92 changes: 91 additions & 1 deletion Assignment1_IOS/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,94 @@
// Created by Sam Liu on 14/10/2024.
//

import Foundation
import SwiftUI

struct HomeView: View {

@State private var home: [Homes] = []

var body: some View {
List(home) { homeItem in
VStack(alignment: .leading) {
AsyncImage(url: URL(string: homeItem.image)) { image in
image.resizable()
} placeholder: {
ProgressView()
}
.scaledToFit()
.frame(height: 200)
Text(homeItem.title)
.font(.headline)
Text(homeItem.description)
.font(.subheadline)
.foregroundColor(.secondary)
}
.padding()
}
.onAppear(perform: startLoad)
.refreshable {
startLoad()
}
}
}

struct Homes: Identifiable, Decodable {
let id = UUID() // Automatically generate UUIDs
let title: String
let description: String
let image: String
}

extension HomeView {

func startLoad() {
let url = URL(string: "https://comp4097-event.azurewebsites.net/api/events?highlight=true")!
let task = URLSession.shared.dataTask(with: url) { data, response, error in
if let error = error {
DispatchQueue.main.async {
self.handleClientError(error)
}
return
}
guard let httpResponse = response as? HTTPURLResponse,
(200...299).contains(httpResponse.statusCode) else {
DispatchQueue.main.async {
self.handleServerError(response)
}
return
}
if let mimeType = httpResponse.mimeType, mimeType == "application/json",
let data = data {
do {
let homeItems = try JSONDecoder().decode([Homes].self, from: data)
DispatchQueue.main.async {
self.home = homeItems
}
} catch {
DispatchQueue.main.async {
self.handleClientError(error)
}
}
}
}
task.resume()
}

func handleClientError(_ error: Error) {
// Handle the error by showing a message or a fallback
self.home = [
Homes(title: "Error", description: "Failed to load data: \(error.localizedDescription)", image: "")
]
}

func handleServerError(_ response: URLResponse?) {
// Handle the server error
self.home = [
Homes(title: "Error", description: "Server error. Response: \(String(describing: response))", image: "")
]
}
}

#Preview {
HomeView()
}

0 comments on commit f926008

Please sign in to comment.