Skip to content

Commit

Permalink
Removed didFinishLoading. Used progress instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
anupamchugh committed Jun 14, 2020
1 parent 22b9365 commit e33c467
Showing 1 changed file with 5 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
//
// ContentView.swift
// SwiftUIWebViewsProgressBars
//
// Created by Anupam Chugh on 12/06/20.
// Copyright © 2020 iowncode. All rights reserved.
//

import SwiftUI
import WebKit


class WebViewModel: ObservableObject {
@Published var progress: Double = 0.0
@Published var didFinishLoading: Bool = false
@Published var link : String

init (progress: Double, link : String) {
Expand All @@ -24,9 +15,7 @@ class WebViewModel: ObservableObject {
struct SwiftUIProgressBar: View {

@Binding var progress: Double
@State var isShowing = false



var body: some View {
GeometryReader { geometry in
ZStack(alignment: .leading) {
Expand All @@ -36,14 +25,10 @@ struct SwiftUIProgressBar: View {
.frame(width: geometry.size.width, height: geometry.size.height)
Rectangle()
.foregroundColor(Color.blue)
.frame(width: self.isShowing ? geometry.size.width * CGFloat((self.progress)) : 0.0,
.frame(width: geometry.size.width * CGFloat((self.progress)),
height: geometry.size.height)
.animation(.linear(duration: 0.5))
}
.onAppear {
self.isShowing = true
}

}
}
}
Expand Down Expand Up @@ -85,14 +70,9 @@ class Coordinator: NSObject {
estimatedProgressObserver = self.parent.webView.observe(\.estimatedProgress, options: [.new]) { [weak self] webView, _ in
print(Float(webView.estimatedProgress))
guard let weakSelf = self else{return}

if webView.estimatedProgress == 1.0{
weakSelf.viewModel.didFinishLoading = true
}
else{
weakSelf.viewModel.progress = webView.estimatedProgress
}

weakSelf.viewModel.progress = webView.estimatedProgress

}
}

Expand All @@ -114,11 +94,10 @@ struct ContentView : View {
SwiftUIWebView(viewModel: model)
}

if !model.didFinishLoading {
if model.progress >= 0.0 && model.progress < 1.0 {
SwiftUIProgressBar(progress: .constant(model.progress))
.frame(height: 15.0)
.foregroundColor(.accentColor)

}

}
Expand Down

0 comments on commit e33c467

Please sign in to comment.