@Claude31, under asyncView there is the part "cities in " which cities is passed to the content function. I wish to set this to the citiesList variable, so i will pass it as content(citiesList)
import SwiftUI
struct CityListView: View {
private let MAX_DISPLAY_PER_FETCH = 20
private var cityUrl: String?
@State private var citiesList: [City]?
@State private var html: String?
@State private var index = 0
@State private var showFooter = true
init(cities: [City]? = nil) {
self.citiesList = cities
setupData()
}
var body: some View {
if let citiesList {
content(citiesList)
}
else {
AsyncView(
operation: { try await getCities() },
content: { cities in
content(cities)
}
)
}
}
func content(_ cities: [City]) -> some View {
return GeometryReader { geometryProxy in
ScrollView {
LazyVStack(alignment: .leading, spacing: 0) {
if cities.count > 0 {
ForEach(cities) { city in
HStack {
Text(city.header)
.bold()
.foregroundColor(.white)
.textCase(.uppercase)
.padding(.all, 8)
Spacer()
}
.background(Color(UIColor(named: PrefTool.instance.getBoolean(Keyword.DARK_MODE) ? "DarkerGray" : "DarkGray")!))
ForEach(city.businesses) { business in
Text(business.name)
Divider()
}
}
}
else {
Text("Empty List")
}
}
.safeAreaInset(edge: .bottom, spacing: 0) {
if showFooter {
VStack {
Text("Footer")
}
.padding()
.frame(maxWidth: .infinity)
// The background will extend automatically to the edge
.background(Color.green)
.onBecomingVisible {
Task {
do {
let moreList = try await getMoreCities(index)
showFooter = false
if !moreList.isEmpty {
citiesList?.append(contentsOf: moreList)
showFooter = true
}
} catch {
showFooter = false
}
}
}
}
}
}
}
}
func getCities() async throws -> [City] {
let cachedIndex = index
html = try await Web.getRequest(url)
index = index + MAX_DISPLAY_PER_FETCH
self.citiesList = CityParser.getCities(html ?? "", cachedIndex, MAX_DISPLAY_PER_FETCH)
return self.citiesList ?? []
}
func getMoreCities(_ from: Int) async throws -> [City] {
let cachedIndex = index
index = index + MAX_DISPLAY_PER_FETCH
return CityParser.getCities(html ?? "", cachedIndex, MAX_DISPLAY_PER_FETCH)
}
}
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: