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

222 lines
7.3 KiB
Swift
Raw Normal View History

2025-06-20 17:39:06 +08:00
//
2025-08-29 16:20:05 +08:00
// MainTabView.swift
// MNNLLMiOS
2025-06-20 17:39:06 +08:00
//
// Created by () on 2025/06/20.
//
import SwiftUI
struct MainTabView: View {
2025-08-29 16:20:05 +08:00
@State private var histories: [ChatHistory] = []
@State private var showHistory = false
@State private var showHistoryButton = true
2025-08-29 16:20:05 +08:00
@State private var selectedHistory: ChatHistory? = nil
2025-06-24 17:11:07 +08:00
@State private var showSettings = false
2025-08-29 16:20:05 +08:00
@State private var navigateToSettings = false
2025-08-29 16:20:05 +08:00
@State private var navigateToChat = false
@State private var selectedTab: Int = 0
2025-08-29 16:20:05 +08:00
@State private var hasConfiguredAppearance = false
@StateObject private var modelListViewModel = ModelListViewModel()
2025-07-21 16:06:09 +08:00
private var titles: [String] {
[
NSLocalizedString("Local Model", comment: "本地模型标签"),
NSLocalizedString("Model Market", comment: "模型市场标签"),
NSLocalizedString("Benchmark", comment: "基准测试标签")
]
}
2025-06-20 17:39:06 +08:00
var body: some View {
2025-06-25 17:12:04 +08:00
ZStack {
2025-08-29 16:20:05 +08:00
TabView(selection: $selectedTab) {
2025-08-29 16:20:05 +08:00
createTabContent(
content: LocalModelListView(viewModel: modelListViewModel),
title: titles[0],
icon: "house.fill",
tag: 0
)
2025-08-29 16:20:05 +08:00
createTabContent(
content: ModelListView(viewModel: modelListViewModel),
title: titles[1],
icon: "doc.text.fill",
tag: 1
)
2025-08-29 16:20:05 +08:00
createTabContent(
content: BenchmarkView(),
title: titles[2],
icon: "clock.fill",
tag: 2
)
}
.onAppear {
2025-08-29 16:20:05 +08:00
setupAppearanceOnce()
loadHistoriesIfNeeded()
}
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-08-29 16:20:05 +08:00
handleHistoryToggle(newValue)
}
.onChange(of: modelListViewModel.selectedModel) { oldValue, newValue in
if newValue != nil {
navigateToChat = true
}
2025-06-25 17:12:04 +08:00
}
2025-08-29 16:20:05 +08:00
.onChange(of: selectedHistory) { oldValue, newValue in
if newValue != nil {
navigateToChat = true
2025-06-24 17:11:07 +08:00
}
}
2025-08-29 16:52:42 +08:00
.onChange(of: navigateToChat) { oldValue, newValue in
if !newValue && oldValue {
refreshHistories()
modelListViewModel.selectedModel = nil
selectedHistory = nil
}
}
}
// 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-08-29 16:52:42 +08:00
LLMChatView(modelInfo: history.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
}
2025-08-29 16:20:05 +08:00
// MARK: - Private Methods
/// Creates a reusable tab content with navigation and common configurations.
@ViewBuilder
private func createTabContent<Content: View>(
content: Content,
title: String,
icon: String,
tag: Int
) -> some View {
NavigationStack {
content
.navigationTitle(title)
.navigationBarTitleDisplayMode(.inline)
.navigationBarHidden(false)
.toolbar {
CommonToolbarView(
showHistory: $showHistory,
showHistoryButton: $showHistoryButton
)
}
.navigationDestination(isPresented: $navigateToChat) {
chatDestination
}
.navigationDestination(isPresented: $navigateToSettings) {
SettingsView()
}
.toolbar((navigateToChat || navigateToSettings) ? .hidden : .visible, for: .tabBar)
}
.tabItem {
Image(systemName: icon)
Text(title)
}
.tag(tag)
}
2025-08-29 16:20:05 +08:00
/// Configures UI appearance only once to prevent memory issues.
private func setupAppearanceOnce() {
guard !hasConfiguredAppearance else { return }
hasConfiguredAppearance = true
setupNavigationBarAppearance()
setupTabBarAppearance()
}
2025-08-29 16:20:05 +08:00
/// Loads chat histories if not already loaded.
private func loadHistoriesIfNeeded() {
if histories.isEmpty {
histories = ChatHistoryManager.shared.getAllHistory()
}
}
2025-08-29 16:52:42 +08:00
/// Refreshes the histories array.
private func refreshHistories() {
histories = ChatHistoryManager.shared.getAllHistory()
}
2025-08-29 16:20:05 +08:00
/// Handles history toggle with proper memory management.
private func handleHistoryToggle(_ isShowing: Bool) {
if isShowing {
2025-08-29 16:52:42 +08:00
refreshHistories()
2025-08-29 16:20:05 +08:00
} else {
Task { @MainActor in
try? await Task.sleep(nanoseconds: 300_000_000) // 0.3 seconds
withAnimation {
self.showHistoryButton = true
}
}
2025-08-29 16:20:05 +08:00
}
}
/// 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
}