Skip to content

Commit

Permalink
更换接口的结构
Browse files Browse the repository at this point in the history
  • Loading branch information
thoamsy committed Nov 21, 2020
1 parent beb1f1c commit 340325f
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions Shared/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ import Combine

struct SFIcons: Codable {
let version: Float
let `public`: [String]
let icons: [String]
}


class SFIconsList: ObservableObject {
let requestURL = URL(string: "https://raw.githubusercontent.com/thoamsy/sf-icon-names/master/sf-name.json")!

@Published var icons: [String] = []
@Published var icons: [String] = [] {
didSet {
fetching = false
}
}
@Published var fetching = false

init() {
start()
Expand All @@ -22,11 +27,12 @@ class SFIconsList: ObservableObject {

func start() {
publisher?.cancel()
self.fetching = true
publisher = URLSession.shared
.dataTaskPublisher(for: requestURL)
.map(\.data)
.decode(type: SFIcons.self, decoder: JSONDecoder())
.map(\.public)
.map(\.icons)
.retry(2)
.replaceError(with: [])
.eraseToAnyPublisher()
Expand All @@ -46,7 +52,7 @@ struct ContentView: View {

var body: some View {
return VStack {
if sfIcons.icons.isEmpty {
if sfIcons.fetching {
ProgressView()
} else {
ScrollView {
Expand All @@ -64,6 +70,7 @@ struct ContentView: View {
.background(index == selectedIndex ? Color(UIColor.link) : Color(UIColor.secondarySystemGroupedBackground))
}
.font(.title2)
.padding()
}
}
}
Expand Down

0 comments on commit 340325f

Please sign in to comment.