MNN/apps/iOS/MNNLLMChat/MNNLLMiOS/MainTab/MainTabView.swift

188 lines
7.2 KiB
Swift
Raw Normal View History

2025-06-20 17:39:06 +08:00
//
// MNNLLMiOSApp.swift
// MainTabView
//
// Created by () on 2025/06/20.
//
import SwiftUI
struct MainTabView: View {
@State private var showHistory = false
@State private var selectedHistory: ChatHistory? = nil
@State private var histories: [ChatHistory] = ChatHistoryManager.shared.getAllHistory()
@State private var showHistoryButton = true
2025-06-24 17:11:07 +08:00
@State private var showSettings = false
@State private var showWebView = false
@State private var webViewURL: URL?
2025-07-01 17:14:40 +08:00
@State private var navigateToSettings = false //
2025-07-09 19:57:36 +08:00
@StateObject private var modelListViewModel = TBModelListViewModel()
@StateObject private var localModelListViewModel = ModelListViewModel()
@State private var selectedTab: Int = 0
2025-07-04 14:24:10 +08:00
@State private var titles = ["本地模型", "模型市场", "TB模型", "Benchmark"]
2025-06-20 17:39:06 +08:00
var body: some View {
2025-06-25 17:12:04 +08:00
ZStack {
NavigationView {
TabView(selection: $selectedTab) {
2025-07-09 19:57:36 +08:00
LocalModelListView(viewModel: localModelListViewModel)
.tabItem {
Image(systemName: "house.fill")
Text("本地模型")
}
.tag(0)
2025-07-09 19:57:36 +08:00
ModelListView(viewModel: localModelListViewModel)
.tabItem {
Image(systemName: "cart.fill")
Text("模型市场")
}
.tag(1)
2025-07-09 19:57:36 +08:00
TBModelListView(viewModel: modelListViewModel)
2025-07-04 14:24:10 +08:00
.tabItem {
Image(systemName: "doc.text.fill")
Text("TB模型")
}
.tag(2)
BenchmarkView()
.tabItem {
Image(systemName: "clock.fill")
Text("Benchmark")
}
2025-07-04 14:24:10 +08:00
.tag(3)
}
2025-07-01 17:14:40 +08:00
.background(
ZStack {
NavigationLink(destination: chatDestination, isActive: chatIsActiveBinding) { EmptyView() }
NavigationLink(destination: SettingsView(), isActive: $navigateToSettings) { EmptyView() }
}
)
2025-06-25 17:12:04 +08:00
.navigationTitle(titles[selectedTab])
.navigationBarTitleDisplayMode(.inline)
.navigationBarHidden(false)
2025-06-30 16:38:31 +08:00
.onAppear {
setupNavigationBarAppearance()
setupTabBarAppearance()
}
2025-06-25 17:12:04 +08:00
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
if showHistoryButton {
Button(action: {
showHistory = true
showHistoryButton = false
histories = ChatHistoryManager.shared.getAllHistory()
}) {
Image(systemName: "sidebar.left")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 20, height: 20)
2025-07-01 17:14:40 +08:00
.foregroundColor(.black)
}
}
2025-06-25 17:12:04 +08:00
}
2025-06-26 16:06:25 +08:00
2025-06-25 17:12:04 +08:00
ToolbarItem(placement: .navigationBarTrailing) {
2025-07-01 17:14:40 +08:00
Button(action: {
if let url = URL(string: "https://github.com/alibaba/MNN") {
UIApplication.shared.open(url)
2025-06-25 17:45:26 +08:00
}
2025-07-01 17:14:40 +08:00
}) {
Image(systemName: "star")
.aspectRatio(contentMode: .fit)
.frame(width: 20, height: 20)
.foregroundColor(.black)
}
}
}
}
2025-07-01 17:14:40 +08:00
.tint(.black)
2025-06-25 17:12:04 +08:00
if showHistory {
Color.black.opacity(0.5)
.edgesIgnoringSafeArea(.all)
.onTapGesture {
withAnimation {
2025-06-25 17:12:04 +08:00
showHistory = false
}
}
}
2025-06-25 17:12:04 +08:00
2025-07-01 17:14:40 +08:00
SideMenuView(isOpen: $showHistory,
selectedHistory: $selectedHistory,
histories: $histories,
navigateToMainSettings: $navigateToSettings)
.edgesIgnoringSafeArea(.all)
2025-06-25 17:12:04 +08:00
}
2025-07-01 17:14:40 +08:00
.onChange(of: showHistory) { oldValue, newValue in
2025-06-25 17:12:04 +08:00
if !newValue {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
withAnimation {
showHistoryButton = true
}
2025-06-20 17:39:06 +08:00
}
}
2025-06-25 17:12:04 +08:00
}
.sheet(isPresented: $showWebView) {
if let url = webViewURL {
WebView(url: url)
2025-06-24 17:11:07 +08:00
}
}
}
@ViewBuilder
private var chatDestination: some View {
2025-07-09 19:57:36 +08:00
if let model = localModelListViewModel.selectedModel {
LLMChatView(modelInfo: model)
} else if let history = selectedHistory {
let modelInfo = ModelInfo(
modelId: history.modelId,
createdAt: "",
downloads: 0,
tags: [],
isDownloaded: true
)
LLMChatView(modelInfo: modelInfo, history: history)
} else {
EmptyView()
2025-06-24 17:11:07 +08:00
}
2025-06-20 17:39:06 +08:00
}
private var chatIsActiveBinding: Binding<Bool> {
Binding<Bool>(
get: { modelListViewModel.selectedModel != nil || selectedHistory != nil },
set: { isActive in
if !isActive {
2025-07-09 19:57:36 +08:00
if let model = localModelListViewModel.selectedModel {
2025-06-26 16:06:25 +08:00
modelListViewModel.recordModelUsage(modelId: model.modelId)
}
modelListViewModel.selectedModel = nil
selectedHistory = nil
}
}
)
}
private func setupNavigationBarAppearance() {
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = .white
2025-07-01 17:14:40 +08:00
appearance.shadowColor = .clear
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().compactAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
}
2025-06-30 16:38:31 +08:00
private func setupTabBarAppearance() {
let appearance = UITabBarAppearance()
appearance.configureWithOpaqueBackground()
2025-07-01 11:01:49 +08:00
let selectedColor = UIColor(Color.primaryPurple)
appearance.stackedLayoutAppearance.selected.iconColor = selectedColor
appearance.stackedLayoutAppearance.selected.titleTextAttributes = [.foregroundColor: selectedColor]
2025-06-30 16:38:31 +08:00
UITabBar.appearance().standardAppearance = appearance
UITabBar.appearance().scrollEdgeAppearance = appearance
}
2025-07-09 19:57:36 +08:00
}