IOS_Boss/IOS_study/Profile/ProfileViewModel+.swift
2025-07-27 12:33:06 +08:00

92 lines
2.7 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.

//
// 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
}
}