-
Notifications
You must be signed in to change notification settings - Fork 4
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
Showing
8 changed files
with
160 additions
and
207 deletions.
There are no files selected for viewing
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,64 @@ | ||
// | ||
// ContentView.swift | ||
// listapp | ||
// | ||
// Created by Daniel on 7/3/20. | ||
// Copyright © 2020 dk. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct ContentView: View { | ||
@ObservedObject private var tvmaze = Tvmaze() | ||
|
||
var body: some View { | ||
NavigationView { | ||
List(tvmaze.shows) { show in | ||
VStack(alignment: .leading) { | ||
Text(show.name) | ||
Text(show.subtitle) | ||
.foregroundColor(.secondary) | ||
.font(.caption) | ||
.lineLimit(4) | ||
} | ||
} | ||
.navigationBarTitle("SwiftUI - iOS 13", displayMode: .inline) | ||
.onAppear { | ||
self.tvmaze.loadData() | ||
} | ||
} | ||
} | ||
} | ||
|
||
struct Show: Codable, Identifiable { | ||
let id: Int | ||
let name, status, premiered, summary: String | ||
} | ||
|
||
extension Show { | ||
var subtitle: String { | ||
return premiered + "\n" + status + summary | ||
.replacingOccurrences(of: "<p>", with: "\n") | ||
.replacingOccurrences(of: "</p>", with: "\n") | ||
.replacingOccurrences(of: "<b>", with: "") | ||
.replacingOccurrences(of: "</b>", with: "") | ||
} | ||
} | ||
|
||
class Tvmaze: ObservableObject { | ||
@Published var shows: [Show] = [] | ||
|
||
func loadData() { | ||
let urlString = "https://api.tvmaze.com/shows" | ||
guard let url = URL(string: urlString) else { return } | ||
URLSession.shared.dataTask(with: url) { (data, response, error) in | ||
guard | ||
let data = data, | ||
let shows = try? JSONDecoder().decode([Show].self, from: data) else { return } | ||
|
||
DispatchQueue.main.async { | ||
self.shows = shows | ||
} | ||
}.resume() | ||
} | ||
} |
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
6 changes: 6 additions & 0 deletions
6
xcode/listapp/Preview Content/Preview Assets.xcassets/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,6 @@ | ||
{ | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
Oops, something went wrong.