//
//  SQSkeletonView.swift
//  Allovoisins
//
//  Created by Victor on 14/06/2024.
//  Copyright © 2024 AlloVoisins. All rights reserved.
//

import SwiftUI

struct BlinkViewModifier: ViewModifier {
    let duration: Double
    @State private var blinking: Bool = false

    func body(content: Content) -> some View {
        content
            .opacity(blinking ? 0.3 : 1)
            .animation(.easeInOut(duration: duration).repeatForever(), value: blinking)
            .onAppear {
                blinking.toggle()
            }
    }
}

extension View {
    func blinking(duration: Double = 1) -> some View {
        modifier(BlinkViewModifier(duration: duration))
    }
}

struct SkeletonCellView: View {
    let primaryColor = Color(.init(gray: 0.9, alpha: 1.0))
    let secondaryColor  = Color(.init(gray: 0.8, alpha: 1.0))
    
    var body: some View {
        RoundedRectangle(cornerRadius: 8)
            .fill(secondaryColor)
            .frame(height: 10)
            .blinking(duration: 0.75)
    }
}

#Preview {
    SkeletonCellView()
        .padding()
}
