Skip to content
This repository has been archived by the owner on Dec 23, 2023. It is now read-only.

Commit

Permalink
Merge pull request #3 from BenjaminHornbeck6/main
Browse files Browse the repository at this point in the history
Better way of fetching json
  • Loading branch information
haxi0 authored Nov 13, 2022
2 parents 732daf6 + 817971b commit 2df1123
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
5 changes: 2 additions & 3 deletions TrollApps/TrollApps/AppsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import SwiftUI

struct AppsView: View {
@Environment(\.openURL) var openURL
@ObservedObject var fetch = fetchtd()


var body: some View {
NavigationView {
List(fetch.jsonstuff) { json in
List(FetchApps()) { json in
Label {
HStack {
Text(json.title)
Expand Down
28 changes: 8 additions & 20 deletions TrollApps/TrollApps/JSONStuff.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,17 @@ struct stuff: Codable, Identifiable {
var id: Int
var title: String
var link: String
var prevlink: String
//No items in the json has this yet
//var prevlink: String
var urlimg: String
var completed: Bool
}

class fetchtd: ObservableObject {
@Published var jsonstuff = [stuff]()

init() {
let url = URL(string: "https://haxi0-dev.com/api/trollapps-0.1.json")!
URLSession.shared.dataTask(with: url) {(data, response, error) in
do {
if let jsondata = data {
let decodeddata = try JSONDecoder().decode([stuff].self, from: jsondata)
DispatchQueue.main.async {
self.jsonstuff = decodeddata
}
} else {
print("no data")
}
} catch {
print("oopsie")
}
}.resume()
func FetchApps() -> [stuff] {
do {
return try JSONDecoder().decode([stuff].self, from: try! Data(contentsOf: URL(string: "https://haxi0-dev.com/api/trollapps-0.1.json")!))
} catch {
print("oopsie")
return []
}
}

0 comments on commit 2df1123

Please sign in to comment.