92 lines
2.7 KiB
Swift
92 lines
2.7 KiB
Swift
//
|
||
// ProfileViewModel+.swift
|
||
// IOS_study
|
||
//
|
||
// Created by CC-star on 2025/7/23.
|
||
//
|
||
|
||
import Foundation
|
||
|
||
extension ProfileViewModel {
|
||
@MainActor func getUser() async throws {
|
||
if isFcUser { return }
|
||
isFcUser = true
|
||
defer { isFcUser = false }
|
||
|
||
user = try await UserManager.shared.findUserByREST()
|
||
}
|
||
|
||
@MainActor func getPLJobs(isRefresh: Bool = false) async {
|
||
if isFcPLJobs { return }
|
||
isFcPLJobs = true
|
||
defer { isFcPLJobs = false }
|
||
if isRefresh {
|
||
currentPLPage = 0
|
||
isPLFinshed = false
|
||
}
|
||
do {
|
||
if user.hasPLJobs {
|
||
let skip = kJobLimitS * currentPLPage
|
||
let newJobs = try await PLJobManager.shared.findJobsByREST(by: userID, limit: kJobLimitS, skip: skip,)
|
||
|
||
// print(newJobs[0].title)
|
||
// print(newJobs[1].title)
|
||
if isRefresh { plJobs = newJobs } else {
|
||
plJobs.append(contentsOf: newJobs)//将一个数组添加到另外一个数组
|
||
}
|
||
|
||
if plJobs.isEmpty {//校准hasPLJobs
|
||
user.hasPLJobs = false
|
||
try await UserManager.shared.setTo(filed: DBUser.CodingKeys.hasPLJobs.rawValue, false)
|
||
}
|
||
|
||
currentPLPage += 1//分页
|
||
if newJobs.count < kJobLimitS || newJobs.isEmpty {
|
||
isPLFinshed = true
|
||
}
|
||
} else {
|
||
plJobs.removeAll()
|
||
}
|
||
} catch {
|
||
print("获取我发布的plJobs失败:\(error)")
|
||
}
|
||
}
|
||
@MainActor func getPLJobsCount() async {
|
||
if user.hasPLJobs {
|
||
async let aCount = PLJobManager.shared.findJobsCount(by: userID, onlyActive: true)
|
||
async let Count = PLJobManager.shared.findJobsCount(by: userID)
|
||
|
||
(aPLJobsCount, pLJobsCount) = await (aCount, Count)
|
||
}
|
||
}
|
||
@MainActor func logOut() {
|
||
AuthManager.shared.logOut()
|
||
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {//暂时用此方法,因为立刻reset的话旧画面仍旧会渲染,导致错误
|
||
self.reset()
|
||
}
|
||
}
|
||
@MainActor func deletUserAll() async {
|
||
let userID = AuthManager.shared.getUserID()
|
||
await PLJobManager.shared.removeJobs(by: userID)
|
||
await UserManager.shared.removeUser(id: userID)
|
||
|
||
if let lcuser = AuthManager.shared.getUser() {
|
||
try? lcuser.set("isNewUser", value: true)
|
||
try? await lcuser.save()//保存信息
|
||
}
|
||
AuthManager.shared.logOut()
|
||
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { self.reset() }
|
||
}
|
||
|
||
@MainActor func reset() {
|
||
viewDidLoad = false
|
||
user = DBUser()
|
||
plJobs.removeAll()
|
||
currentPLPage = 0
|
||
isPLFinshed = false
|
||
aPLJobsCount = 0
|
||
pLJobsCount = 0
|
||
}
|
||
|
||
}
|