Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/LocalChatUI/Sources/Features/Models/ModelLibraryScreen.swift3// Description: The Models studio: installed models, Hugging Face discovery, downloads and device-aware recommendations.4import SwiftUI5import LocalChatCore67public struct ModelLibraryScreen: View {8 enum Segment: String, CaseIterable, Identifiable {9 case devices = "By device", installed = "Installed", discover = "Discover", downloads = "Downloads", recommended = "Recommended"10 var id: String { rawValue }11 var symbol: String {12 switch self { case .installed: "internaldrive"; case .devices: "macbook.and.iphone"; case .discover: "sparkle.magnifyingglass"; case .downloads: "arrow.down.circle"; case .recommended: "wand.and.stars" }13 }14 }15 @Environment(AppModel.self) private var app16 @State private var segment: Segment = .devices17 @Namespace private var selection18 public init() {}1920 public var body: some View {21 VStack(alignment: .leading, spacing: 0) {22 VStack(alignment: .leading, spacing: DS.S.md) {23 ScreenTitle("Models", subtitle: "Discover, download and manage the models that run on this device.")24 segments25 }26 .padding(.horizontal, DS.S.lg).padding(.top, DS.S.lg).padding(.bottom, DS.S.md)27 Rectangle().fill(DS.C.border).frame(height: 1)28 Group {29 switch segment {30 case .installed: InstalledModelsView(goDiscover: { withAnimation(DS.M.snappy) { segment = .discover } })31 case .devices: DeviceCatalogView(showDownloads: { withAnimation(DS.M.snappy) { segment = .downloads } })32 case .discover: DiscoverView(showDownloads: { withAnimation(DS.M.snappy) { segment = .downloads } })33 case .downloads: DownloadsView()34 case .recommended: RecommendedView()35 }36 }37 .frame(maxWidth: .infinity, maxHeight: .infinity)38 }39 .screenBackground()4041 }4243 private var activeDownloads: Int { app.downloadStates.filter { $0.phase.isActive }.count }4445 private var segments: some View {46 ScrollView(.horizontal, showsIndicators: false) {47 HStack(spacing: 4) {48 ForEach(Segment.allCases) { s in49 Button { withAnimation(DS.M.snappy) { segment = s } } label: {50 HStack(spacing: 6) {51 Image(systemName: s.symbol).font(.system(size: 12, weight: .semibold))52 Text(s.rawValue).font(.system(size: 13, weight: .medium))53 if s == .downloads, activeDownloads > 0 {54 Text("\(activeDownloads)").font(DS.F.micro).foregroundStyle(.white).padding(.horizontal, 5).padding(.vertical, 1).background(DS.C.accent, in: Capsule())55 }56 if s == .installed, !app.installedModels.isEmpty { Text("\(app.installedModels.count)").font(DS.F.micro).foregroundStyle(DS.C.muted) }57 }58 .foregroundStyle(segment == s ? DS.C.text : DS.C.text2)59 .padding(.horizontal, 12).padding(.vertical, 7)60 .background { if segment == s { Capsule().fill(DS.C.surface3).matchedGeometryEffect(id: "segment", in: selection) } }61 .contentShape(Capsule())62 }63 .buttonStyle(.plain).accessibilityAddTraits(segment == s ? .isSelected : [])64 }65 }66 .padding(3).background(DS.C.surface2, in: Capsule())67 }68 }69}70