Files
mowesta/ios-app/app/View/StateView.swift
T
Marcus Handte 2c788e3329
LocationBasedServices/mowesta/pipeline/head This commit looks good
Add weather station configuration to ios app.
2022-11-03 16:47:26 +01:00

209 lines
6.9 KiB
Swift

//
// StateView.swift - The view to display a city list for a state.
//
// Created by Marcus Handte on 20.07.22.
//
import SwiftUI
import class MoWeStaApi.City
import class MoWeStaApi.Country
import class MoWeStaApi.DefaultAPI
import class MoWeStaApi.State
import LocosLabGeometry
struct StateView: View {
@Environment(\.managedObjectContext) private var viewContext
let stateId: Int64
@SwiftUI.State var state: MoWeStaApi.State? = nil
@SwiftUI.State var stateName: String? = nil
@SwiftUI.State var boundary: [[Coordinate]]? = nil
@SwiftUI.State var cities: [City] = []
@SwiftUI.State var loading = 0
@SwiftUI.State var failure = false
@SwiftUI.State var offset = 0
@SwiftUI.State var total = -1
@SwiftUI.State var favorite: Favorite?
@SwiftUI.State var showLoadingAnimation = false
func reload() {
if loading != 0 {
return
}
favorite = nil // core data is too clever, so we must force a reload to detect field changes
let request = Favorite.fetchRequest()
request.predicate = NSPredicate(format: "%K == %@ AND %K == %@", #keyPath(Favorite.identifier), stateId as NSNumber, #keyPath(Favorite.locationType), 1 as NSNumber)
if let result = try? viewContext.fetch(request) {
if let first = result.first {
favorite = first
let now = Int64(NSDate().timeIntervalSince1970)
if now - first.locationTime < RefreshTask.LOCATION_TIMEOUT {
if let loc = first.locationValue as? MoWeStaApi.State {
self.state = loc
stateName = TextBindings.getName(loc.names, false)
}
self.boundary = GeojsonConverter.coordinates(of: first.boundaryValue, simplify: 100)
}
}
} else {
favorite = nil
}
failure = false
if state == nil {
loading += 1
DefaultAPI.getState(id: stateId) {
response, error in
if let _ = error {
self.failure = true
} else if let r = response {
state = r
stateName = TextBindings.getName(r.names, false)
}
loading -= 1
}
}
if boundary == nil {
loading += 1
DefaultAPI.getStateBoundary(id: stateId, apiResponseQueue: DispatchQueue.global()) {
response, error in
var simplified: [[Coordinate]] = []
if let r = response {
simplified = GeojsonConverter.coordinates(of: r, simplify: 100)
}
DispatchQueue.main.async {
if let _ = error {
self.failure = true
} else {
boundary = simplified
}
loading -= 1
}
}
}
if total == -1 {
if cities.isEmpty {
loading += 1
DefaultAPI.getStateCities(id: stateId) {
response, error in
if let _ = error {
self.failure = true
} else if let r = response {
cities.append(contentsOf: r.elements)
total = r.total
offset = r.size
}
loading -= 1
}
}
}
}
func loadPage() {
if loading != 0 || failure || offset > total {
return
}
loading += 1
DefaultAPI.getStateCities(id: stateId, pageOffset: offset, pageSize: 50) {
response, error in
if let _ = error {
self.failure = true
} else if let r = response {
cities.append(contentsOf: r.elements)
total = r.total
offset += r.size
}
loading -= 1
}
}
var body: some View {
VStack {
if let s = state {
List() {
PlaceMapView(center: Coordinate(longitude: s.coordinate.longitude, latitude: s.coordinate.latitude), zoom: 7, boundary: boundary)
.frame(height: 200)
.frame(maxWidth: .infinity)
.clipShape(RoundedRectangle(cornerRadius: 6))
ForEach(cities, id: \City.id) { city in
CityEntryView(city: city)
}
if !showLoadingAnimation && !failure && total > offset {
Button(action: {
loadPage()
}) {
Text("action_load_more")
.frame(maxWidth: .infinity)
.frame(alignment: .center)
.foregroundColor(.accentColor)
}
}
}.listStyle(PlainListStyle())
Spacer()
ProgressBarView(animating: $showLoadingAnimation)
.frame(height: 3)
.frame(maxWidth: .infinity)
} else if showLoadingAnimation {
ProgressSpinnerView()
}
if failure && !showLoadingAnimation {
Spacer()
ErrorView() {
reload()
}
}
}
.if((stateName != nil)) { v in
v.navigationTitle(stateName!)
}
.onAppear() {
reload()
showLoadingAnimation = loading != 0
}
.onChange(of: loading) { value in
showLoadingAnimation = value != 0
}
.toolbar() {
ToolbarItemGroup(placement: .navigationBarTrailing) {
if loading == 0 {
if Favorites.canDelete(favorite) {
Button(action: {
Favorites.delete(viewContext, favorite)
reload()
}) {
Image(systemName: "heart.slash")
}
} else if !failure {
Button(action: {
Favorites.add(viewContext, favorite) { fav in
fav.setLocationType(type: .state)
fav.identifier = stateId
fav.locationValue = state
if let c = cities.first {
fav.cityValue = c
}
}
reload()
}) {
Image(systemName: "heart")
}
}
}
}
}
}
}
struct StateView_Previews: PreviewProvider {
static var previews: some View {
StateView(stateId: 1803)
}
}