0

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)

        }

    }
IAmNoob
  • 576
  • 4
  • 14
  • In SwiftUI, a View is not on or off the screen. It is a description of the interface. If there are conditions under which the progress view should not be part of the interface, then do not unconditionally describe the progress view as part of the interface. – matt Jul 31 '21 at 14:12
  • @matt What I am trying to do is I am loading my data from a JSON file and I want to show the loading progress in my app. When I am doing so, I am unable to hide the progress view even after the JSON is loaded. – IAmNoob Jul 31 '21 at 14:16
  • use an `if else` – lorem ipsum Jul 31 '21 at 14:19
  • Do you understand the notions unconditionally and condition? I'm telling you to use a condition when you describe the progress view in the interface. – matt Jul 31 '21 at 14:21
  • Please see the complete code which I just added. – IAmNoob Jul 31 '21 at 14:27

0 Answers0