diff --git a/apps/iOS/MNNLLMChat/MNNLLMiOS/Chat/ViewModels/LLMChatViewModel.swift b/apps/iOS/MNNLLMChat/MNNLLMiOS/Chat/ViewModels/LLMChatViewModel.swift index 5cbef04e..c95fb0cd 100644 --- a/apps/iOS/MNNLLMChat/MNNLLMiOS/Chat/ViewModels/LLMChatViewModel.swift +++ b/apps/iOS/MNNLLMChat/MNNLLMiOS/Chat/ViewModels/LLMChatViewModel.swift @@ -196,6 +196,9 @@ final class LLMChatViewModel: ObservableObject { NotificationCenter.default.post(name: .dismissKeyboard, object: nil) self.send(draft: draft, userType: .user) + + recordModelUsage() + if isModelLoaded { if modelInfo.modelName.lowercased().contains("diffusion") { self.getDiffusionResponse(draft: draft) @@ -390,9 +393,14 @@ final class LLMChatViewModel: ObservableObject { interactor.connect() self.setupLLM(modelPath: self.modelInfo.localPath) + + recordModelUsage() } func onStop() { + + recordModelUsage() + ChatHistoryManager.shared.saveChat( historyId: historyId, modelInfo: modelInfo, @@ -419,4 +427,14 @@ final class LLMChatViewModel: ObservableObject { .sink { _ in } .store(in: &subscriptions) } + + private func recordModelUsage() { + ModelStorageManager.shared.updateLastUsed(for: modelInfo.modelName) + + NotificationCenter.default.post( + name: .modelUsageUpdated, + object: nil, + userInfo: ["modelName": modelInfo.modelName] + ) + } } diff --git a/apps/iOS/MNNLLMChat/MNNLLMiOS/MainTab/ModelList/Models/ModelListViewModel.swift b/apps/iOS/MNNLLMChat/MNNLLMiOS/MainTab/ModelList/Models/ModelListViewModel.swift index 6f644535..5622ed8e 100644 --- a/apps/iOS/MNNLLMChat/MNNLLMiOS/MainTab/ModelList/Models/ModelListViewModel.swift +++ b/apps/iOS/MNNLLMChat/MNNLLMiOS/MainTab/ModelList/Models/ModelListViewModel.swift @@ -7,6 +7,7 @@ import Foundation import SwiftUI +import Combine class ModelListViewModel: ObservableObject { // MARK: - Published Properties @@ -23,6 +24,7 @@ class ModelListViewModel: ObservableObject { // MARK: - Private Properties private let modelClient = ModelClient.shared private let pinnedModelKey = "com.mnnllm.pinnedModelIds" + private var cancellables = Set() // MARK: - Model Data Access @@ -49,6 +51,17 @@ class ModelListViewModel: ObservableObject { Task { @MainActor in await fetchModels() } + + NotificationCenter.default + .publisher(for: .modelUsageUpdated) + .sink { [weak self] notification in + if let modelName = notification.userInfo?["modelName"] as? String { + Task { @MainActor in + self?.updateModelLastUsed(modelName: modelName) + } + } + } + .store(in: &cancellables) } // MARK: - Model Data Management @@ -490,4 +503,20 @@ class ModelListViewModel: ObservableObject { } } } + + @MainActor + private func updateModelLastUsed(modelName: String) { + if let index = models.firstIndex(where: { $0.modelName == modelName }) { + if let lastUsed = ModelStorageManager.shared.getLastUsed(for: modelName) { + models[index].lastUsedAt = lastUsed + sortModels(fetchedModels: &models) + } + } + } +} + +// MARK: - Notification Names + +extension Notification.Name { + static let modelUsageUpdated = Notification.Name("modelUsageUpdated") }