6.3 Nevication Menu
import SwiftUI
import WebKit
import Network
struct ContentView: View {
@State private var isConnected = true
@StateObject private var webViewStateModel = WebViewStateModel() // Use @StateObject for proper initialization
var body: some View {
ZStack {
// Header
Text("")
.padding(.vertical, -15) // Reduced vertical padding
.padding(.horizontal, -15) // Reduced horizontal padding
.background(Color(#colorLiteral(red: 0.662745098, green: 0.1215686275, blue: 0.137254902, alpha: 1))) // Background color
Theme.backgroundColor
.edgesIgnoringSafeArea(.all)
if isConnected {
TabBar(selectedTab: $webViewStateModel.selectedTab, webViewStateModel: webViewStateModel) // Pass webViewStateModel
.onAppear {
self.startMonitoringConnectivity()
}
} else {
// Your offline view code
}
}
}
private func startMonitoringConnectivity() {
// Your connectivity monitoring code
}
}
struct TabBar: View {
@Binding var selectedTab: Int
@ObservedObject var webViewStateModel: WebViewStateModel
private let tabItems = [
TabItem(icon: "house.fill", label: "Home"),
TabItem(icon: "square.grid.2x2.fill", label: "Categories"),
TabItem(icon: "person.fill", label: "Account"),
TabItem(icon: "heart.fill", label: "Wishlist"),
TabItem(icon: "dollarsign.circle.fill", label: "Wallet")
]
var body: some View {
VStack {
webViewContainerForTab(selectedTab)
Spacer()
HStack {
ForEach(tabItems.indices, id: \.self) { index in
Button(action: {
selectedTab = index
webViewStateModel.reloadWebView()
}) {
tabItemViewForIndex(index)
}
.foregroundColor(selectedTab == index ? .red : .gray)
}
}
.padding(.horizontal)
.padding(.bottom, 1)
.background(Color.white)
.clipShape(RoundedRectangle(cornerRadius: 20))
.shadow(radius: 5)
}
}
private func webViewContainerForTab(_ index: Int) -> some View {
WebViewContainer(urlString: webViewStateModel.originalURLs[index], webViewStateModel: webViewStateModel)
}
private func tabItemViewForIndex(_ index: Int) -> some View {
let item = tabItems[index]
return VStack(spacing: 4) {
Image(systemName: item.icon)
.font(Font.system(size: 22)) // Increase the icon and font size
Text(item.label)
.font(Font.caption.bold()) // Making the text bold
}
.padding(10)
}
}
struct TabItem {
let icon: String
let label: String
}
struct WebViewContainer: View {
let urlString: String
@ObservedObject var webViewStateModel: WebViewStateModel
var body: some View {
ZStack {
WebView(urlString: urlString, webViewStateModel: webViewStateModel)
if webViewStateModel.isLoading {
LoadingView()
}
}
.background(Color.red)
.cornerRadius(15)
.padding(.horizontal, 0)
.padding(.vertical, 0) // 2px top padding
.padding(.bottom, -22) // 5px bottom padding
}
}
struct WebView: UIViewRepresentable {
let urlString: String
@ObservedObject var webViewStateModel: WebViewStateModel
func makeUIView(context: Context) -> WKWebView {
guard let url = URL(string: urlString) else {
fatalError("Invalid URL")
}
let config = WKWebViewConfiguration()
config.allowsInlineMediaPlayback = true
let webView = WKWebView(frame: .zero, configuration: config)
webView.navigationDelegate = context.coordinator
webView.load(URLRequest(url: url))
webView.scrollView.isScrollEnabled = true
webView.allowsLinkPreview = false
// Hide scroll indicators
webView.scrollView.showsHorizontalScrollIndicator = false
webView.scrollView.showsVerticalScrollIndicator = false
setupRefreshControl(for: webView, context: context)
return webView
}
func updateUIView(_ uiView: WKWebView, context: Context) {
// Additional update code if needed
}
private func setupRefreshControl(for webView: WKWebView, context: Context) {
let refreshControl = UIRefreshControl()
webView.scrollView.refreshControl = refreshControl
refreshControl.addTarget(context.coordinator, action: #selector(WebViewCoordinator.refreshWebView(_:)), for: .valueChanged)
let additionalSensitivityHeight = 3.5 * refreshControl.frame.size.height
webView.scrollView.contentOffset = CGPoint(x: 0, y: -additionalSensitivityHeight)
}
func makeCoordinator() -> WebViewCoordinator {
WebViewCoordinator(webViewStateModel)
}
}
class WebViewCoordinator: NSObject, WKNavigationDelegate {
var webViewStateModel: WebViewStateModel
init(_ webViewStateModel: WebViewStateModel) {
self.webViewStateModel = webViewStateModel
}
@objc func refreshWebView(_ sender: UIRefreshControl) {
webViewStateModel.webView?.reload()
sender.endRefreshing()
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
webViewStateModel.isLoading = true
webViewStateModel.webView = webView
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webViewStateModel.isLoading = false
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
webViewStateModel.isLoading = false
webViewStateModel.didFailWithError = true
// Error handling can be expanded here
}
}
class WebViewStateModel: ObservableObject {
@Published var isLoading: Bool = false
@Published var didFailWithError: Bool = false
var webView: WKWebView?
@Published var selectedTab: Int = 0 // Track selected tab index
var originalURLs: [String] = [
"https://www.hlomart.com",
"https://www.hlomart.com/category",
"https://www.hlomart.com/my-account",
"https://www.hlomart.com/wishlist",
"https://www.hlomart.com/my-wallet"
]
func reloadWebView() {
if selectedTab < originalURLs.count {
let originalURL = originalURLs[selectedTab]
if let webView = webView, webView.url?.absoluteString != originalURL {
webView.load(URLRequest(url: URL(string: originalURL)!))
}
}
}
}
struct LoadingView: View {
var body: some View {
ProgressView()
.scaleEffect(1.5)
.progressViewStyle(CircularProgressViewStyle(tint: .blue))
}
}
struct Theme {
static let backgroundColor = Color(red: 169/255, green: 31/255, blue: 35/255)
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
6.2 Nevication Menu
import SwiftUI
import WebKit
import Network
struct ContentView: View {
@State private var isConnected = true
@StateObject private var webViewStateModel = WebViewStateModel() // Use @StateObject for proper initialization
var body: some View {
ZStack {
// Header
Text("")
.padding(.vertical, -15) // Reduced vertical padding
.padding(.horizontal, -15) // Reduced horizontal padding
.background(Color(#colorLiteral(red: 0.662745098, green: 0.1215686275, blue: 0.137254902, alpha: 1))) // Background color
Theme.backgroundColor
.edgesIgnoringSafeArea(.all)
if isConnected {
TabBar(selectedTab: $webViewStateModel.selectedTab, webViewStateModel: webViewStateModel) // Pass webViewStateModel
.onAppear {
self.startMonitoringConnectivity()
}
} else {
// Your offline view code
}
}
}
private func startMonitoringConnectivity() {
// Your connectivity monitoring code
}
}
struct TabBar: View {
@Binding var selectedTab: Int
@ObservedObject var webViewStateModel: WebViewStateModel
private let tabItems = [
TabItem(icon: "house.fill", label: "Home"),
TabItem(icon: "square.grid.2x2.fill", label: "Categories"),
TabItem(icon: "cart.fill", label: "Order"),
TabItem(icon: "person.fill", label: "Account"),
TabItem(icon: "heart.fill", label: "Wishlist")
]
var body: some View {
VStack {
webViewContainerForTab(selectedTab)
Spacer()
HStack {
ForEach(tabItems.indices, id: \.self) { index in
Button(action: {
selectedTab = index
webViewStateModel.reloadWebView()
}) {
tabItemViewForIndex(index)
}
.foregroundColor(selectedTab == index ? .red : .gray)
}
}
.padding(.horizontal)
.padding(.bottom, 1)
.background(Color.white)
.clipShape(RoundedRectangle(cornerRadius: 20))
.shadow(radius: 5)
}
}
private func webViewContainerForTab(_ index: Int) -> some View {
WebViewContainer(urlString: webViewStateModel.originalURLs[index], webViewStateModel: webViewStateModel)
}
private func tabItemViewForIndex(_ index: Int) -> some View {
let item = tabItems[index]
return VStack(spacing: 4) {
Image(systemName: item.icon)
.font(Font.system(size: 22)) // Increase the icon and font size
Text(item.label)
.font(.caption)
}
.padding(10)
}
}
struct TabItem {
let icon: String
let label: String
}
struct WebViewContainer: View {
let urlString: String
@ObservedObject var webViewStateModel: WebViewStateModel
var body: some View {
ZStack {
WebView(urlString: urlString, webViewStateModel: webViewStateModel)
if webViewStateModel.isLoading {
LoadingView()
}
}
.background(Color.red)
.cornerRadius(15)
.padding(.horizontal, 0)
.padding(.vertical, 0) // 2px top padding
.padding(.bottom, -22) // 5px bottom padding
}
}
struct WebView: UIViewRepresentable {
let urlString: String
@ObservedObject var webViewStateModel: WebViewStateModel
func makeUIView(context: Context) -> WKWebView {
guard let url = URL(string: urlString) else {
fatalError("Invalid URL")
}
let config = WKWebViewConfiguration()
config.allowsInlineMediaPlayback = true
let webView = WKWebView(frame: .zero, configuration: config)
webView.navigationDelegate = context.coordinator
webView.load(URLRequest(url: url))
webView.scrollView.isScrollEnabled = true
webView.allowsLinkPreview = false
// Hide scroll indicators
webView.scrollView.showsHorizontalScrollIndicator = false
webView.scrollView.showsVerticalScrollIndicator = false
setupRefreshControl(for: webView, context: context)
return webView
}
func updateUIView(_ uiView: WKWebView, context: Context) {
// Additional update code if needed
}
private func setupRefreshControl(for webView: WKWebView, context: Context) {
let refreshControl = UIRefreshControl()
webView.scrollView.refreshControl = refreshControl
refreshControl.addTarget(context.coordinator, action: #selector(WebViewCoordinator.refreshWebView(_:)), for: .valueChanged)
let additionalSensitivityHeight = 3.5 * refreshControl.frame.size.height
webView.scrollView.contentOffset = CGPoint(x: 0, y: -additionalSensitivityHeight)
}
func makeCoordinator() -> WebViewCoordinator {
WebViewCoordinator(webViewStateModel)
}
}
class WebViewCoordinator: NSObject, WKNavigationDelegate {
var webViewStateModel: WebViewStateModel
init(_ webViewStateModel: WebViewStateModel) {
self.webViewStateModel = webViewStateModel
}
@objc func refreshWebView(_ sender: UIRefreshControl) {
webViewStateModel.webView?.reload()
sender.endRefreshing()
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
webViewStateModel.isLoading = true
webViewStateModel.webView = webView
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webViewStateModel.isLoading = false
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
webViewStateModel.isLoading = false
webViewStateModel.didFailWithError = true
// Error handling can be expanded here
}
}
class WebViewStateModel: ObservableObject {
@Published var isLoading: Bool = false
@Published var didFailWithError: Bool = false
var webView: WKWebView?
@Published var selectedTab: Int = 0 // Track selected tab index
var originalURLs: [String] = [
"https://www.hlomart.com",
"https://www.hlomart.com/category",
"https://www.hlomart.com/order",
"https://www.hlomart.com/my-account",
"https://www.hlomart.com/wishlist"
]
func reloadWebView() {
if selectedTab < originalURLs.count {
let originalURL = originalURLs[selectedTab]
if let webView = webView, webView.url?.absoluteString != originalURL {
webView.load(URLRequest(url: URL(string: originalURL)!))
}
}
}
}
struct LoadingView: View {
var body: some View {
ProgressView()
.scaleEffect(1.5)
.progressViewStyle(CircularProgressViewStyle(tint: .blue))
}
}
struct Theme {
static let backgroundColor = Color(red: 169/255, green: 31/255, blue: 35/255)
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
6th Nevication Menu
import SwiftUI
import WebKit
import Network
struct ContentView: View {
@State private var isConnected = true
@StateObject private var webViewStateModel = WebViewStateModel() // Use @StateObject for proper initialization
var body: some View {
ZStack {
// Header
Text("")
.padding(.vertical, -15) // Reduced vertical padding
.padding(.horizontal, -15) // Reduced horizontal padding
.background(Color(#colorLiteral(red: 0.662745098, green: 0.1215686275, blue: 0.137254902, alpha: 1))) // Background color
Theme.backgroundColor
.edgesIgnoringSafeArea(.all)
if isConnected {
TabBar(selectedTab: $webViewStateModel.selectedTab, webViewStateModel: webViewStateModel) // Pass webViewStateModel
.onAppear {
self.startMonitoringConnectivity()
}
} else {
// Your offline view code
}
}
}
private func startMonitoringConnectivity() {
// Your connectivity monitoring code
}
}
struct TabBar: View {
@Binding var selectedTab: Int
@ObservedObject var webViewStateModel: WebViewStateModel
private let tabItems = [
TabItem(icon: "house.fill", label: "Home"),
TabItem(icon: "square.grid.2x2.fill", label: "Categories"),
TabItem(icon: "cart.fill", label: "Order"),
TabItem(icon: "person.fill", label: "Account"),
TabItem(icon: "heart.fill", label: "Wishlist")
]
var body: some View {
VStack {
webViewContainerForTab(selectedTab)
Spacer()
HStack {
ForEach(0..<tabItems.count) { index in
Button(action: {
selectedTab = index
webViewStateModel.reloadWebView()
}) {
tabItemViewForIndex(index)
}
.foregroundColor(selectedTab == index ? .red : .gray)
}
}
.padding(.horizontal)
.padding(.bottom, 1)
.background(Color.white)
.clipShape(RoundedRectangle(cornerRadius: 20))
.shadow(radius: 5)
}
}
private func webViewContainerForTab(_ index: Int) -> some View {
WebViewContainer(urlString: webViewStateModel.originalURLs[index], webViewStateModel: webViewStateModel)
}
private func tabItemViewForIndex(_ index: Int) -> some View {
let item = tabItems[index]
return VStack(spacing: 4) {
Image(systemName: item.icon)
.font(Font.system(size: 22)) // Increase the icon and font size
Text(item.label)
.font(.caption)
}
.padding(10)
}
}
struct TabItem {
let icon: String
let label: String
}
struct WebViewContainer: View {
let urlString: String
@ObservedObject var webViewStateModel: WebViewStateModel
var body: some View {
ZStack {
WebView(urlString: urlString, webViewStateModel: webViewStateModel)
if webViewStateModel.isLoading {
LoadingView()
}
}
.background(Color.red)
.cornerRadius(15)
.padding(.horizontal, 0)
.padding(.vertical, 0) // 2px top padding
.padding(.bottom, -22) // 5px bottom padding
}
}
struct WebView: UIViewRepresentable {
let urlString: String
@ObservedObject var webViewStateModel: WebViewStateModel
func makeUIView(context: Context) -> WKWebView {
guard let url = URL(string: urlString) else {
fatalError("Invalid URL")
}
let config = WKWebViewConfiguration()
config.allowsInlineMediaPlayback = true
let webView = WKWebView(frame: .zero, configuration: config)
webView.navigationDelegate = context.coordinator
webView.load(URLRequest(url: url))
webView.scrollView.isScrollEnabled = true
webView.allowsLinkPreview = false
// Hide scroll indicators
webView.scrollView.showsHorizontalScrollIndicator = false
webView.scrollView.showsVerticalScrollIndicator = false
setupRefreshControl(for: webView, context: context)
return webView
}
func updateUIView(_ uiView: WKWebView, context: Context) {
// Additional update code if needed
}
private func setupRefreshControl(for webView: WKWebView, context: Context) {
let refreshControl = UIRefreshControl()
webView.scrollView.refreshControl = refreshControl
refreshControl.addTarget(context.coordinator, action: #selector(WebViewCoordinator.refreshWebView(_:)), for: .valueChanged)
let additionalSensitivityHeight = 3.5 * refreshControl.frame.size.height
webView.scrollView.contentOffset = CGPoint(x: 0, y: -additionalSensitivityHeight)
}
func makeCoordinator() -> WebViewCoordinator {
WebViewCoordinator(webViewStateModel)
}
}
class WebViewCoordinator: NSObject, WKNavigationDelegate {
var webViewStateModel: WebViewStateModel
init(_ webViewStateModel: WebViewStateModel) {
self.webViewStateModel = webViewStateModel
}
@objc func refreshWebView(_ sender: UIRefreshControl) {
webViewStateModel.webView?.reload()
sender.endRefreshing()
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
webViewStateModel.isLoading = true
webViewStateModel.webView = webView
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webViewStateModel.isLoading = false
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
webViewStateModel.isLoading = false
webViewStateModel.didFailWithError = true
// Error handling can be expanded here
}
}
class WebViewStateModel: ObservableObject {
@Published var isLoading: Bool = false
@Published var didFailWithError: Bool = false
var webView: WKWebView?
@Published var selectedTab: Int = 0 // Track selected tab index
var originalURLs: [String] = [
"https://www.hlomart.com",
"https://www.hlomart.com/category",
"https://www.hlomart.com/order",
"https://www.hlomart.com/my-account",
"https://www.hlomart.com/wishlist"
]
func reloadWebView() {
if selectedTab < originalURLs.count {
let originalURL = originalURLs[selectedTab]
if let webView = webView, webView.url?.absoluteString != originalURL {
webView.load(URLRequest(url: URL(string: originalURL)!))
}
}
}
}
struct LoadingView: View {
var body: some View {
ProgressView()
.scaleEffect(1.5)
.progressViewStyle(CircularProgressViewStyle(tint: .blue))
}
}
struct Theme {
static let backgroundColor = Color(red: 169/255, green: 31/255, blue: 35/255)
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
5th including no-internet + 4
import SwiftUI
import WebKit
import Network
struct ContentView: View {
private let urlString: String = "https://www.hlomart.com"
@State private var isConnected = true
var body: some View {
ZStack {
Theme.backgroundColor
.edgesIgnoringSafeArea(.all)
if isConnected {
WebViewContainer(urlString: urlString)
.onAppear {
self.startMonitoringConnectivity()
}
} else {
VStack {
Image(systemName: "wifi.slash")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 50, height: 50)
.foregroundColor(.white)
Text("No Internet Connection")
.foregroundColor(.white)
Button(action: {
self.openWifiSettings()
}) {
Text("Connect Internet")
.foregroundColor(.white)
.padding()
.background(Color.blue)
.cornerRadius(6)
}
Button(action: {
// Attempt to reconnect action
self.startMonitoringConnectivity()
}) {
Text("Try Again")
.foregroundColor(.white)
.padding()
.background(Color.yellow)
.cornerRadius(6)
}
}
}
}
}
private func startMonitoringConnectivity() {
let monitor = NWPathMonitor()
let queue = DispatchQueue(label: "Monitor")
monitor.pathUpdateHandler = { path in
DispatchQueue.main.async {
self.isConnected = path.status == .satisfied
}
}
monitor.start(queue: queue)
}
private func openWifiSettings() {
guard let settingsURL = URL(string: UIApplication.openSettingsURLString) else { return }
UIApplication.shared.open(settingsURL)
}
}
// Other structs remain the same...
struct WebViewContainer: View {
let urlString: String
@ObservedObject var webViewStateModel: WebViewStateModel = WebViewStateModel()
var body: some View {
ZStack {
WebView(urlString: urlString, webViewStateModel: webViewStateModel)
if webViewStateModel.isLoading {
LoadingView()
}
}
}
}
struct WebView: UIViewRepresentable {
let urlString: String
@ObservedObject var webViewStateModel: WebViewStateModel
func makeUIView(context: Context) -> WKWebView {
guard let url = URL(string: urlString) else {
fatalError("Invalid URL")
}
let config = WKWebViewConfiguration()
config.allowsInlineMediaPlayback = true
let webView = WKWebView(frame: .zero, configuration: config)
webView.navigationDelegate = context.coordinator
webView.load(URLRequest(url: url))
webView.scrollView.isScrollEnabled = true
webView.allowsLinkPreview = false
setupRefreshControl(for: webView, context: context)
return webView
}
func updateUIView(_ uiView: WKWebView, context: Context) {
// Additional update code if needed
}
private func setupRefreshControl(for webView: WKWebView, context: Context) {
let refreshControl = UIRefreshControl()
webView.scrollView.refreshControl = refreshControl
refreshControl.addTarget(context.coordinator, action: #selector(WebViewCoordinator.refreshWebView(_:)), for: .valueChanged)
// Adjust content offset to make refresh control 50% more sensitive
let additionalSensitivityHeight = 3.5 * refreshControl.frame.size.height
webView.scrollView.contentOffset = CGPoint(x: 0, y: -additionalSensitivityHeight)
}
func makeCoordinator() -> WebViewCoordinator {
WebViewCoordinator(webViewStateModel)
}
}
class WebViewCoordinator: NSObject, WKNavigationDelegate {
var webViewStateModel: WebViewStateModel
init(_ webViewStateModel: WebViewStateModel) {
self.webViewStateModel = webViewStateModel
}
@objc func refreshWebView(_ sender: UIRefreshControl) {
webViewStateModel.webView?.reload() // Reload the current webpage
sender.endRefreshing() // End the refreshing animation
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
webViewStateModel.isLoading = true
webViewStateModel.webView = webView // Keep a reference to the webView
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webViewStateModel.isLoading = false
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
webViewStateModel.isLoading = false
webViewStateModel.didFailWithError = true
// Error handling can be expanded here
}
}
class WebViewStateModel: ObservableObject {
@Published var isLoading: Bool = false
@Published var didFailWithError: Bool = false
var webView: WKWebView?
}
struct LoadingView: View {
var body: some View {
ProgressView()
.scaleEffect(1.5)
.progressViewStyle(CircularProgressViewStyle(tint: .blue))
}
}
struct Theme {
static let backgroundColor = Color(red: 169/255, green: 31/255, blue: 35/255) //Changed the footer and header color
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
4th code (3ed + Final)
import SwiftUI
import WebKit
struct ContentView: View {
private let urlString: String = "https://www.hlomart.com"
var body: some View {
ZStack {
Theme.backgroundColor
.edgesIgnoringSafeArea(.all)
WebViewContainer(urlString: urlString)
}
}
}
struct WebViewContainer: View {
let urlString: String
@ObservedObject var webViewStateModel: WebViewStateModel = WebViewStateModel()
var body: some View {
ZStack {
WebView(urlString: urlString, webViewStateModel: webViewStateModel)
if webViewStateModel.isLoading {
LoadingView()
}
}
}
}
struct WebView: UIViewRepresentable {
let urlString: String
@ObservedObject var webViewStateModel: WebViewStateModel
func makeUIView(context: Context) -> WKWebView {
guard let url = URL(string: urlString) else {
fatalError("Invalid URL")
}
let config = WKWebViewConfiguration()
config.allowsInlineMediaPlayback = true
let webView = WKWebView(frame: .zero, configuration: config)
webView.navigationDelegate = context.coordinator
webView.load(URLRequest(url: url))
webView.scrollView.isScrollEnabled = true
webView.allowsLinkPreview = false
setupRefreshControl(for: webView, context: context)
return webView
}
func updateUIView(_ uiView: WKWebView, context: Context) {
// Additional update code if needed
}
private func setupRefreshControl(for webView: WKWebView, context: Context) {
let refreshControl = UIRefreshControl()
webView.scrollView.refreshControl = refreshControl
refreshControl.addTarget(context.coordinator, action: #selector(WebViewCoordinator.refreshWebView(_:)), for: .valueChanged)
// Adjust content offset to make refresh control 50% more sensitive
let additionalSensitivityHeight = 3.5 * refreshControl.frame.size.height
webView.scrollView.contentOffset = CGPoint(x: 0, y: -additionalSensitivityHeight)
}
func makeCoordinator() -> WebViewCoordinator {
WebViewCoordinator(webViewStateModel)
}
}
class WebViewCoordinator: NSObject, WKNavigationDelegate {
var webViewStateModel: WebViewStateModel
init(_ webViewStateModel: WebViewStateModel) {
self.webViewStateModel = webViewStateModel
}
@objc func refreshWebView(_ sender: UIRefreshControl) {
webViewStateModel.webView?.reload() // Reload the current webpage
sender.endRefreshing() // End the refreshing animation
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
webViewStateModel.isLoading = true
webViewStateModel.webView = webView // Keep a reference to the webView
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webViewStateModel.isLoading = false
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
webViewStateModel.isLoading = false
webViewStateModel.didFailWithError = true
// Error handling can be expanded here
}
}
class WebViewStateModel: ObservableObject {
@Published var isLoading: Bool = false
@Published var didFailWithError: Bool = false
var webView: WKWebView?
}
struct LoadingView: View {
var body: some View {
ProgressView()
.scaleEffect(1.5)
.progressViewStyle(CircularProgressViewStyle(tint: .blue))
}
}
struct Theme {
static let backgroundColor = Color(red: 169/255, green: 31/255, blue: 35/255) //Changed the footer and header color
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
3rd code Swiprefress Final
import SwiftUI
import WebKit
struct ContentView: View {
private let urlString: String = "https://www.hlomart.com"
var body: some View {
ZStack {
Theme.backgroundColor
.edgesIgnoringSafeArea(.all)
WebViewContainer(urlString: urlString)
}
}
}
struct WebViewContainer: View {
let urlString: String
@ObservedObject var webViewStateModel: WebViewStateModel = WebViewStateModel()
var body: some View {
ZStack {
WebView(urlString: urlString, webViewStateModel: webViewStateModel)
if webViewStateModel.isLoading {
LoadingView()
}
}
}
}
struct WebView: UIViewRepresentable {
let urlString: String
@ObservedObject var webViewStateModel: WebViewStateModel
func makeUIView(context: Context) -> WKWebView {
guard let url = URL(string: urlString) else {
fatalError("Invalid URL")
}
let config = WKWebViewConfiguration()
config.allowsInlineMediaPlayback = true
let webView = WKWebView(frame: .zero, configuration: config)
webView.navigationDelegate = context.coordinator
webView.load(URLRequest(url: url))
webView.scrollView.isScrollEnabled = true
webView.allowsLinkPreview = false
setupRefreshControl(for: webView, context: context)
return webView
}
func updateUIView(_ uiView: WKWebView, context: Context) {
// Additional update code if needed
}
private func setupRefreshControl(for webView: WKWebView, context: Context) {
let refreshControl = UIRefreshControl()
webView.scrollView.refreshControl = refreshControl
refreshControl.addTarget(context.coordinator, action: #selector(WebViewCoordinator.refreshWebView(_:)), for: .valueChanged)
// Adjust content offset to make refresh control 50% more sensitive
let additionalSensitivityHeight = 3.5 * refreshControl.frame.size.height
webView.scrollView.contentOffset = CGPoint(x: 0, y: -additionalSensitivityHeight)
}
func makeCoordinator() -> WebViewCoordinator {
WebViewCoordinator(webViewStateModel)
}
}
class WebViewCoordinator: NSObject, WKNavigationDelegate {
var webViewStateModel: WebViewStateModel
init(_ webViewStateModel: WebViewStateModel) {
self.webViewStateModel = webViewStateModel
}
@objc func refreshWebView(_ sender: UIRefreshControl) {
webViewStateModel.webView?.reload() // Reload the current webpage
sender.endRefreshing() // End the refreshing animation
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
webViewStateModel.isLoading = true
webViewStateModel.webView = webView // Keep a reference to the webView
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webViewStateModel.isLoading = false
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
webViewStateModel.isLoading = false
webViewStateModel.didFailWithError = true
// Error handling can be expanded here
}
}
class WebViewStateModel: ObservableObject {
@Published var isLoading: Bool = false
@Published var didFailWithError: Bool = false
var webView: WKWebView?
}
struct LoadingView: View {
var body: some View {
ProgressView()
.scaleEffect(1.5)
.progressViewStyle(CircularProgressViewStyle(tint: .blue))
}
}
struct Theme {
static let backgroundColor = Color(red: 0.662, green: 0.122, blue: 0.137)
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
3rd code Swiprefress
import SwiftUI
import WebKit
struct ContentView: View {
private let urlString: String = "https://www.hlomart.com"
var body: some View {
ZStack {
Theme.backgroundColor
.edgesIgnoringSafeArea(.all)
WebViewContainer(urlString: urlString)
}
}
}
struct WebViewContainer: View {
let urlString: String
@ObservedObject var webViewStateModel: WebViewStateModel = WebViewStateModel()
var body: some View {
ZStack {
WebView(urlString: urlString, webViewStateModel: webViewStateModel)
if webViewStateModel.isLoading {
LoadingView()
}
}
}
}
struct WebView: UIViewRepresentable {
let urlString: String
@ObservedObject var webViewStateModel: WebViewStateModel
func makeUIView(context: Context) -> WKWebView {
guard let url = URL(string: urlString) else {
fatalError("Invalid URL")
}
let config = WKWebViewConfiguration()
config.allowsInlineMediaPlayback = true
let webView = WKWebView(frame: .zero, configuration: config)
webView.navigationDelegate = context.coordinator
webView.load(URLRequest(url: url))
webView.scrollView.isScrollEnabled = true
webView.allowsLinkPreview = false
setupRefreshControl(for: webView, context: context)
return webView
}
func updateUIView(_ uiView: WKWebView, context: Context) {
// Additional update code if needed
}
private func setupRefreshControl(for webView: WKWebView, context: Context) {
let refreshControl = UIRefreshControl()
webView.scrollView.refreshControl = refreshControl
refreshControl.addTarget(context.coordinator, action: #selector(WebViewCoordinator.refreshWebView(_:)), for: .valueChanged)
}
func makeCoordinator() -> WebViewCoordinator {
WebViewCoordinator(webViewStateModel)
}
}
class WebViewCoordinator: NSObject, WKNavigationDelegate {
var webViewStateModel: WebViewStateModel
init(_ webViewStateModel: WebViewStateModel) {
self.webViewStateModel = webViewStateModel
}
@objc func refreshWebView(_ sender: UIRefreshControl) {
webViewStateModel.webView?.reload() // Reload the current webpage
sender.endRefreshing() // End the refreshing animation
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
webViewStateModel.isLoading = true
webViewStateModel.webView = webView // Keep a reference to the webView
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webViewStateModel.isLoading = false
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
webViewStateModel.isLoading = false
webViewStateModel.didFailWithError = true
// Error handling can be expanded here
}
}
class WebViewStateModel: ObservableObject {
@Published var isLoading: Bool = false
@Published var didFailWithError: Bool = false
var webView: WKWebView?
}
struct LoadingView: View {
var body: some View {
ProgressView()
.scaleEffect(1.5)
.progressViewStyle(CircularProgressViewStyle(tint: .blue))
}
}
struct Theme {
static let backgroundColor = Color(red: 0.662, green: 0.122, blue: 0.137)
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
2ed code
import SwiftUI
import WebKit
struct ContentView: View {
private let urlString: String = "https://www.hlomart.com"
var body: some View {
ZStack {
Theme.backgroundColor
.edgesIgnoringSafeArea(.all)
WebViewContainer(urlString: urlString)
}
}
}
struct WebViewContainer: View {
let urlString: String
@ObservedObject var webViewStateModel: WebViewStateModel = WebViewStateModel()
var body: some View {
ZStack {
WebView(urlString: urlString, webViewStateModel: webViewStateModel)
if webViewStateModel.isLoading {
LoadingView()
}
}
}
}
struct WebView: UIViewRepresentable {
let urlString: String
@ObservedObject var webViewStateModel: WebViewStateModel
func makeUIView(context: Context) -> WKWebView {
guard let url = URL(string: urlString) else {
fatalError("Invalid URL")
}
let config = WKWebViewConfiguration()
config.allowsInlineMediaPlayback = true // Example of enabling inline media playback
// Further configuration options can be set here
let webView = WKWebView(frame: .zero, configuration: config)
webView.navigationDelegate = context.coordinator
webView.load(URLRequest(url: url))
webView.scrollView.isScrollEnabled = true // Enable scrolling
webView.allowsLinkPreview = false // Disable link preview for better touch responsiveness
// Additional performance optimizations can be implemented here
return webView
}
func updateUIView(_ uiView: WKWebView, context: Context) {
// Additional update code if needed
}
func makeCoordinator() -> WebViewCoordinator {
WebViewCoordinator(webViewStateModel)
}
}
class WebViewCoordinator: NSObject, WKNavigationDelegate {
var webViewStateModel: WebViewStateModel
init(_ webViewStateModel: WebViewStateModel) {
self.webViewStateModel = webViewStateModel
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
webViewStateModel.isLoading = true
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webViewStateModel.isLoading = false
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
webViewStateModel.isLoading = false
webViewStateModel.didFailWithError = true
// Error handling can be expanded here
}
}
class WebViewStateModel: ObservableObject {
@Published var isLoading: Bool = false
@Published var didFailWithError: Bool = false
}
struct LoadingView: View {
var body: some View {
ProgressView()
.scaleEffect(1.5)
.progressViewStyle(CircularProgressViewStyle(tint: .blue))
}
}
struct Theme {
static let backgroundColor = Color(red: 0.662, green: 0.122, blue: 0.137)
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
1st code
import SwiftUI
import WebKit
struct ContentView: View {
private let urlString: String = "https://www.hlomart.com"
var body: some View {
ZStack(alignment: .topLeading) {
Color(#colorLiteral(red: 0.662745098, green: 0.1215686275, blue: 0.137254902, alpha: 1))
.ignoresSafeArea()
VStack(spacing: 0) {
// Fixed Header
HeaderView()
// Overlay the WebView
GeometryReader { geometry in
WebView(url: URL(string: urlString)!)
.frame(width: geometry.size.width, height: geometry.size.height)
}
}
}
.edgesIgnoringSafeArea(.all)
}
}
struct HeaderView: View {
var body: some View {
Text("Header")
.frame(height: 60)
.background(Color(#colorLiteral(red: 0.662745098, green: 0.1215686275, blue: 0.137254902, alpha: 1)))
.foregroundColor(Color(#colorLiteral(red: 0.662745098, green: 0.1215686275, blue: 0.137254902, alpha: 1)))
}
}
struct WebView: UIViewRepresentable {
var url: URL
@State private var isLoading = true
func makeUIView(context: Context) -> WKWebView {
let webView = WKWebView()
webView.navigationDelegate = context.coordinator
// Disable zooming
webView.scrollView.maximumZoomScale = 1.0
webView.scrollView.minimumZoomScale = 1.0
return webView
}
func updateUIView(_ uiView: WKWebView, context: Context) {
let request = URLRequest(url: url)
uiView.load(request)
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, WKNavigationDelegate {
var parent: WebView
init(_ webView: WebView) {
self.parent = webView
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
parent.isLoading = true
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
parent.isLoading = false
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
parent.isLoading = false
// Handle the error appropriately.
}
}
}
struct LoadingView: View {
var body: some View {
ProgressView()
.progressViewStyle(CircularProgressViewStyle(tint: .blue))
.scaleEffect(1.5)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}