I am using a ProgressView() in SwiftUI however I am unable to remove it from my screen once the data is loaded.
Below is how I am calling it.
VStack {
ProgressView(value: progressValue, total: 100)
}
.padding()
I have an if else loop which adds up the progress till 100
if progressValue < 100 {
progressValue += 10
}
I just want this ProgressView() to be removed from the screen so that my list view gets displayed.
List {
ForEach(fetch.users) { user in
NavigationLink(destination: DetailedView(item: user)) {
RowView(item: user)
}
}
}
Here is the complete code for reference:
import SwiftUI
//Defined fields to fetch from JSON
structUsers: Codable, Identifiable {
public var id: Int
public var title: String
public var heading: String
public var description: String
}
//Properties
//Calling API in below class
classFetchUsers: ObservableObject {
// 1.
@Published var users = [Users]()
init() {
let url = URL(string: "https://myapp.herokuapp.com/text.json")!
// 2.
URLSession.shared.dataTask(with: url) {(data, response, error) in
do {
if let userData = data {
// 3.
let decodedData = try JSONDecoder().decode([Users].self, from: userData)
DispatchQueue.main.async {
self.users = decodedData
}
} else {
print("No data")
}
} catch {
print("Error")
}
}.resume()
}
}
//Content View
structContentView: View {
// 1.
@ObservedObjectvarfetch = FetchUsers()
@StateprivatevarisShowingSettings: Bool = false
@State private var progressValue: Float = 0.0
let timer = Timer.publish(every: 0.2, on: .main, in: .common).autoconnect()
var body: some View {
NavigationView {
VStack {
ProgressView(value: progressValue, total: 100)
}
.padding()
.progressViewStyle(DarkBlueShadowProgressViewStyle())
.onReceive(timer) { _ in
if progressValue < 100 {
progressValue += 4
}
else {
print("Disable the progress here.")
}
}
List {
ForEach(fetch.users) { user in
NavigationLink(destination: DetailedView(item: user)) {
RowView(item: user)
}
}
}
.navigationTitle("My App")
.navigationBarItems(
trailing:
Button(action: {
isShowingSettings = true
}) {
Image(systemName: "slider.horizontal.3")
} // Button
.sheet(isPresented: $isShowingSettings) {
SettingsView()
}
)
.navigationViewStyle(StackNavigationViewStyle())
}
}
}
structDarkBlueShadowProgressViewStyle: ProgressViewStyle {
func makeBody(configuration: Configuration) -> some View {
ProgressView(configuration)
.shadow(color: Color(red: 0, green: 0, blue: 0.6),
radius: 4.0, x: 1.0, y: 2.0)
}
}