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

231 lines
8.9 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
// MainTabView is the primary view of the app, containing the tab bar and navigation for main sections.
2025-06-20 17:39:06 +08:00
struct MainTabView: View {
// MARK: - State Properties
@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?
@State private var navigateToSettings = false
2025-07-10 11:16:25 +08:00
@StateObject private var modelListViewModel = ModelListViewModel()
@State private var selectedTab: Int = 0
2025-07-21 16:06:09 +08:00
private var titles: [String] {
[
NSLocalizedString("Local Model", comment: "本地模型标签"),
NSLocalizedString("Model Market", comment: "模型市场标签"),
NSLocalizedString("Benchmark", comment: "基准测试标签")
]
}
// MARK: - Body
2025-06-20 17:39:06 +08:00
var body: some View {
2025-06-25 17:12:04 +08:00
ZStack {
// Main TabView for navigation between Local Model, Model Market, and Benchmark
TabView(selection: $selectedTab) {
NavigationView {
2025-07-10 11:16:25 +08:00
LocalModelListView(viewModel: modelListViewModel)
.navigationTitle(titles[0])
.navigationBarTitleDisplayMode(.inline)
.navigationBarHidden(false)
.onAppear {
setupNavigationBarAppearance()
}
.toolbar {
CommonToolbarView(
showHistory: $showHistory,
showHistoryButton: $showHistoryButton,
)
}
.background(
ZStack {
NavigationLink(destination: chatDestination, isActive: chatIsActiveBinding) { EmptyView() }
NavigationLink(destination: SettingsView(), isActive: $navigateToSettings) { EmptyView() }
}
)
// Hide TabBar when entering chat or settings view
.toolbar((chatIsActiveBinding.wrappedValue || navigateToSettings) ? .hidden : .visible, for: .tabBar)
}
.tabItem {
Image(systemName: "house.fill")
Text(titles[0])
}
.tag(0)
NavigationView {
2025-07-10 11:16:25 +08:00
ModelListView(viewModel: modelListViewModel)
.navigationTitle(titles[1])
.navigationBarTitleDisplayMode(.inline)
.navigationBarHidden(false)
.onAppear {
setupNavigationBarAppearance()
}
.toolbar {
CommonToolbarView(
showHistory: $showHistory,
showHistoryButton: $showHistoryButton,
)
}
.background(
ZStack {
NavigationLink(destination: chatDestination, isActive: chatIsActiveBinding) { EmptyView() }
NavigationLink(destination: SettingsView(), isActive: $navigateToSettings) { EmptyView() }
}
)
}
.tabItem {
Image(systemName: "doc.text.fill")
Text(titles[1])
2025-06-30 16:38:31 +08:00
}
.tag(1)
NavigationView {
BenchmarkView()
.navigationTitle(titles[2])
.navigationBarTitleDisplayMode(.inline)
.navigationBarHidden(false)
.onAppear {
setupNavigationBarAppearance()
}
.toolbar {
CommonToolbarView(
showHistory: $showHistory,
showHistoryButton: $showHistoryButton,
)
}
.background(
ZStack {
NavigationLink(destination: chatDestination, isActive: chatIsActiveBinding) { EmptyView() }
NavigationLink(destination: SettingsView(), isActive: $navigateToSettings) { EmptyView() }
}
)
}
.tabItem {
Image(systemName: "clock.fill")
Text(titles[2])
}
.tag(2)
}
.onAppear {
setupTabBarAppearance()
}
2025-07-01 17:14:40 +08:00
.tint(.black)
2025-06-25 17:12:04 +08:00
// Overlay for dimming the background when history is shown
2025-06-25 17:12:04 +08:00
if showHistory {
Color.black.opacity(0.5)
.edgesIgnoringSafeArea(.all)
.onTapGesture {
withAnimation(.easeInOut(duration: 0.2)) {
showHistory = false
}
}
}
2025-06-25 17:12:04 +08:00
// Side menu for displaying chat history
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
}
}
}
// MARK: - View Builders
/// Destination view for chat, either from a new model or a history item.
@ViewBuilder
private var chatDestination: some View {
2025-07-10 11:16:25 +08:00
if let model = modelListViewModel.selectedModel {
LLMChatView(modelInfo: model)
.navigationBarHidden(false)
.navigationBarTitleDisplayMode(.inline)
} else if let history = selectedHistory {
2025-07-10 11:16:25 +08:00
let modelInfo = ModelInfo(modelId: history.modelId, isDownloaded: true)
LLMChatView(modelInfo: modelInfo, history: history)
.navigationBarHidden(false)
.navigationBarTitleDisplayMode(.inline)
} else {
EmptyView()
2025-06-24 17:11:07 +08:00
}
2025-06-20 17:39:06 +08:00
}
// MARK: - Bindings
/// Binding to control the activation of the chat view.
private var chatIsActiveBinding: Binding<Bool> {
Binding<Bool>(
get: {
2025-07-10 11:16:25 +08:00
return modelListViewModel.selectedModel != nil || selectedHistory != nil
},
set: { isActive in
if !isActive {
// Record usage when returning from chat
2025-07-10 11:16:25 +08:00
if let model = modelListViewModel.selectedModel {
modelListViewModel.recordModelUsage(modelName: model.modelName)
2025-06-26 16:06:25 +08:00
}
// Clear selections
2025-07-10 11:16:25 +08:00
modelListViewModel.selectedModel = nil
selectedHistory = nil
}
}
)
}
// MARK: - Private Methods
/// Configures the appearance of the navigation bar.
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
/// Configures the appearance of the tab bar.
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-10 11:16:25 +08:00
}