Files
suixinkan_uikit/suixinkan/Features/Task/ViewModels/CloudStoragePickForTaskViewModel.swift
汉秋 3c4ca7eefb Add submit-task flow aligned with Android and extract profile edit page.
Implement task creation with cloud/local media, order linking, and OSS upload; wire home entry and move profile nickname/avatar editing to ProfileEditViewController.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-07 10:12:20 +08:00

165 lines
4.4 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// 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?()
}
}