//
//  NetworkStatusView.swift
//  PadelClub
//
//  Created by Laurent Morvillier on 25/10/2024.
//

import SwiftUI
import LeStorage

struct NetworkStatusView: View {
    
    @State private var isConnected = false
    @State private var timer: Timer?
    
    var body: some View {
        Image(systemName: self.isConnected ? "network" : "network.slash")
            .resizable()
            .scaledToFit()
        
            .onAppear {
                self._defineStatus()
                // Start the timer when the view appears
                timer = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: true) { _ in
                    withAnimation {
                        self._defineStatus()
                    }
                }
            }
            .onDisappear {
                // Clean up timer when view disappears
                timer?.invalidate()
                timer = nil
            }
    }
 
    fileprivate func _defineStatus() {
        self.isConnected = LeStorage.NetworkMonitor.shared.isConnected
    }
    
}

#Preview {
    NetworkStatusView()
}
