Untitled

mail@pastecode.io avatar
unknown
plain_text
16 days ago
3.8 kB
3
Indexable
Never
import SwiftUI
import Cosmos

struct FacesStackView: View {
    @State var items: [String] = []
    @State var borderColor: Color = Color.white
    @State var widthSize: CGFloat = 24
    @State var label: String = ""
    
    var body: some View {
        VStack {
            if items.isEmpty {
                EmptyView()
            } else {
                HStack(spacing: 16) {
                    ForEach(createIcons(items), id: \.self) { icon in
                        icon
                    }
                    Spacer(minLength: 18)
                    Text(label)
                        .modifier(StyleNames.shareFacesModify)
                }
            }
        }
    }
    
    func layout(_ newItems: [String]) {
        items = newItems
        if items.isEmpty {
            return
        }
        label = String(format: NSLocalizedString("%d user", comment: "%d"), max(0, items.count - createIcons(items).count))
    }
    
    func layoutFriends(_ newItems: [String]) {
        items = newItems
        if items.isEmpty {
            return
        }
        label = String(format: NSLocalizedString("%d followersJoin", comment: "%d"), max(0, items.count))
    }
    
    func layoutHosts(_ organizers: EventOrganizers) {
        items = organizers.hosts.map { $0.avatar }
        if items.isEmpty {
            return
        }
        if items.count == 1 {
            layoutHost(organizer: organizers.hosts.first!, rating: organizers.rating)
            return
        }
        label = String(format: NSLocalizedString("%d host", comment: "%d"), max(0, organizers.hosts.count)).uppercased()
    }
    
    private func createSpacing(_ space: CGFloat) -> some View {
        return Spacer().frame(width: space)
    }
    
    private func layoutHost(organizer: CompactOrganizer, rating: Double) {
        items = [organizer.avatar]
        label = organizer.displayName
        if organizer.status.superhost {
            // Handle superhost layout
        }
    }
    
    private func createRatingView(_ rating: Double) -> some View {
        return CosmosView(settings: CosmosSettings.default).frame(height: 6).rating(rating)
    }
    
    private func createIcons(_ items: [String]) -> [some View] {
        return items.prefix(4).map { url in
            ZStack {
                ImageView(url: url, placeholder: "userPlaceholder")
                    .frame(width: widthSize, height: widthSize)
                    .border(Color(borderColor), width: 2)
                    .clipShape(Circle())
                    .background(Color.white)
                    .cornerRadius(widthSize / 2)
                    .shadow(color: Color.gray.opacity(0.5), radius: 10, x: 2, y: 1)
            }
            .frame(width: 14, height: widthSize)
        }
    }
}

struct ImageView: View {
    let url: String
    let placeholder: String
    
    var body: some View {
        // Use appropriate image loading library here, e.g. SDWebImageSwiftUI
        AsyncImage(url: URL(string: url)) { phase in
            if let image = phase.image {
                image.resizable().scaledToFill()
            } else {
                Image(placeholder).resizable().scaledToFill()
            }
        }
    }
}

// Placeholder for styles and other utility structures
struct StyleNames {
    static let shareFacesModify = ViewModifier { content in
        content.font(.system(size: 12))
    }
}

// Placeholder for EventOrganizers, CompactOrganizer, etc.
struct EventOrganizers {
    var hosts: [CompactOrganizer]
    var rating: Double
}

struct CompactOrganizer {
    var avatar: String
    var displayName: String
    var status: Status
}

struct Status {
    var superhost: Bool
}

struct FacesStackView_Previews: PreviewProvider {
    static var previews: some View {
        FacesStackView()
    }
}
Leave a Comment