forked from mazdak/AudioWhisper
-
Notifications
You must be signed in to change notification settings - Fork 0
Extract TranscriptionHistoryViewModel; remove dead UsageDashboardView (A2) #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import Foundation | ||
|
|
||
| /// ViewModel for `TranscriptionHistoryView`. Owns the paged record data, the | ||
| /// load/pagination state, and all `DataManager` interactions so the view never | ||
| /// touches the store directly (audit item A2). | ||
| /// | ||
| /// `dataManager` is injected via the initializer with a `DataManager.shared` | ||
| /// default, mirroring `DashboardHomeView`'s constructor injection so tests can | ||
| /// substitute a `MockDataManager`. | ||
| @MainActor | ||
| @Observable | ||
| final class TranscriptionHistoryViewModel { | ||
| // MARK: - Data + Load State | ||
|
|
||
| private(set) var records: [TranscriptionRecord] = [] | ||
| private(set) var page: Int = 0 | ||
| private(set) var hasMore: Bool = true | ||
| private(set) var isLoading: Bool = false | ||
| private(set) var hasLoadedOnce: Bool = false | ||
|
|
||
| var showError = false | ||
| var errorMessage = "" | ||
|
|
||
| // MARK: - Dependencies | ||
|
|
||
| private let dataManager: DataManagerProtocol | ||
| private let pageSize: Int | ||
|
|
||
| // MARK: - Initialization | ||
|
|
||
| init(dataManager: DataManagerProtocol = DataManager.shared, pageSize: Int = 50) { | ||
| self.dataManager = dataManager | ||
| self.pageSize = pageSize | ||
| } | ||
|
|
||
| // MARK: - Paginated Loading | ||
|
|
||
| /// Loads the next page of records (or the first page when `reset` is true). | ||
| /// `search` is the raw search text from the view; it is trimmed here and | ||
| /// treated as "no filter" when empty. | ||
| func loadRecords(reset: Bool = false, search: String = "") async { | ||
| guard !isLoading else { return } | ||
| isLoading = true | ||
| defer { | ||
| isLoading = false | ||
| hasLoadedOnce = true | ||
| } | ||
|
|
||
| if reset { | ||
| page = 0 | ||
| hasMore = true | ||
| } | ||
|
|
||
| let trimmed = search.trimmingCharacters(in: .whitespacesAndNewlines) | ||
| let searchTerm: String? = trimmed.isEmpty ? nil : trimmed | ||
|
|
||
| do { | ||
| let offset = page * pageSize | ||
| let batch = try await dataManager.fetchRecords( | ||
| limit: pageSize, | ||
| offset: offset, | ||
| search: searchTerm | ||
| ) | ||
|
|
||
| if reset { | ||
| records = batch | ||
| } else { | ||
| records.append(contentsOf: batch) | ||
| } | ||
|
|
||
| hasMore = batch.count == pageSize | ||
| page += 1 | ||
| } catch { | ||
| errorMessage = "Failed to load transcription history: \(error.localizedDescription)" | ||
| showError = true | ||
| hasMore = false | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Mutations | ||
|
|
||
| /// Deletes a single record and reloads the first page on success. | ||
| func deleteRecord(_ record: TranscriptionRecord, search: String = "") async { | ||
| do { | ||
| try await dataManager.deleteRecord(record) | ||
| await loadRecords(reset: true, search: search) | ||
| } catch { | ||
| errorMessage = "Failed to delete record: \(error.localizedDescription)" | ||
| showError = true | ||
| } | ||
| } | ||
|
|
||
| /// Deletes every record and reloads the first page. | ||
| func clearAllRecords(search: String = "") async { | ||
| isLoading = true | ||
| do { | ||
| try await dataManager.deleteAllRecords() | ||
| } catch { | ||
| errorMessage = "Failed to clear all records: \(error.localizedDescription)" | ||
| showError = true | ||
| } | ||
| isLoading = false | ||
| await loadRecords(reset: true, search: search) | ||
| } | ||
| } | ||
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reset search requests can be dropped while loading, causing stale results.
At Line 42,
guard !isLoading else { return }discards anyreset: truecall fired during an in-flight fetch. With rapid search updates, the latest query may never load.💡 Suggested fix (queue latest reset and replay after current load)
final class TranscriptionHistoryViewModel { + private var pendingResetSearch: String? @@ func loadRecords(reset: Bool = false, search: String = "") async { - guard !isLoading else { return } + if isLoading { + if reset { pendingResetSearch = search } + return + } isLoading = true defer { isLoading = false hasLoadedOnce = true } @@ do { @@ } catch { errorMessage = "Failed to load transcription history: \(error.localizedDescription)" showError = true hasMore = false } + + if let queuedSearch = pendingResetSearch { + pendingResetSearch = nil + Task { [weak self] in + await self?.loadRecords(reset: true, search: queuedSearch) + } + } } }🤖 Prompt for AI Agents