添加提交任务流程并抽取资料编辑页

This commit is contained in:
2026-07-07 10:12:20 +08:00
parent af450fb4cc
commit ef1d3b4af5
23 changed files with 3186 additions and 170 deletions

View File

@ -0,0 +1,164 @@
//
// CloudStoragePickForTaskViewModel.swift
// suixinkan
//
import Foundation
/// ViewModel Android
final class CloudStoragePickForTaskViewModel {
private(set) var pathStack: [CloudPathItem] = [CloudPathItem(id: 0, name: "全部文件")]
private(set) var files: [CloudFile] = []
private(set) var selectedFiles: [Int: CloudFile] = [:]
private(set) var searchText = ""
private(set) var filterType = 0
private(set) var sortType = 2
private(set) var isLoading = false
private(set) var isRefreshing = false
private(set) var canLoadMore = false
var onStateChange: (() -> Void)?
var onShowMessage: ((String) -> Void)?
private var currentPage = 1
private var totalCount = 0
private let pageSize = 20
private let importedFileIDs: Set<Int>
init(importedFileIDs: Set<Int> = []) {
self.importedFileIDs = importedFileIDs
}
/// ID
var currentFolderID: Int {
pathStack.last?.id ?? 0
}
///
var selectedFileList: [CloudFile] {
Array(selectedFiles.values)
}
///
func isImported(_ fileID: Int) -> Bool {
importedFileIDs.contains(fileID)
}
///
func isSelected(_ fileID: Int) -> Bool {
selectedFiles[fileID] != nil
}
///
func updateSearchText(_ text: String) {
searchText = text
notifyStateChange()
}
///
func updateFilterType(_ type: Int) {
filterType = type
notifyStateChange()
}
///
func updateSortType(_ type: Int) {
sortType = type
notifyStateChange()
}
///
func refresh(api: TaskAPI) async {
currentPage = 1
isRefreshing = true
notifyStateChange()
await loadPage(api: api, reset: true)
isRefreshing = false
notifyStateChange()
}
///
func loadMore(api: TaskAPI) async {
guard canLoadMore, !isLoading else { return }
currentPage += 1
await loadPage(api: api, reset: false)
}
///
func handleItemTap(_ file: CloudFile, api: TaskAPI) async {
if file.isFolder {
pathStack.append(CloudPathItem(id: file.id, name: file.name))
searchText = ""
await refresh(api: api)
return
}
guard file.isSelectableMedia else { return }
if isImported(file.id) {
onShowMessage?("已选择")
return
}
if selectedFiles[file.id] != nil {
selectedFiles.removeValue(forKey: file.id)
} else {
selectedFiles[file.id] = file
}
notifyStateChange()
}
///
func navigateToPathIndex(_ index: Int, api: TaskAPI) async {
guard index >= 0, index < pathStack.count else { return }
pathStack = Array(pathStack.prefix(index + 1))
searchText = ""
await refresh(api: api)
}
///
func navigateBack(api: TaskAPI) async -> Bool {
guard pathStack.count > 1 else { return false }
pathStack.removeLast()
searchText = ""
await refresh(api: api)
return true
}
private func loadPage(api: TaskAPI, reset: Bool) async {
isLoading = true
notifyStateChange()
defer {
isLoading = false
notifyStateChange()
}
do {
let response = try await api.cloudFileList(
parentFolderId: currentFolderID,
name: searchText,
type: filterType,
orderBy: sortType,
page: currentPage,
pageSize: pageSize
)
totalCount = response.total
if reset {
files = response.list
} else {
files.append(contentsOf: response.list)
}
canLoadMore = files.count < totalCount
} catch is CancellationError {
return
} catch {
if reset {
files = []
}
canLoadMore = false
onShowMessage?(error.localizedDescription)
}
}
private func notifyStateChange() {
onStateChange?()
}
}