This commit is contained in:
世界
2023-09-24 13:23:39 +08:00
parent 58bc9baa05
commit e65680dd55
4 changed files with 38 additions and 28 deletions
@@ -5,28 +5,34 @@ import Library
#if os(iOS) || os(tvOS) #if os(iOS) || os(tvOS)
public class UIProfileUpdateTask: BGAppRefreshTask { public class UIProfileUpdateTask: BGAppRefreshTask {
private static let taskSchedulerPermittedIdentifier = "\(FilePath.packageName).update_profiles" private static let taskSchedulerPermittedIdentifier = "\(FilePath.packageName).update_profiles"
private static var registered = false
public static func configure() async throws { private actor Register {
if !registered { private var registered = false
let success = BGTaskScheduler.shared.register(forTaskWithIdentifier: taskSchedulerPermittedIdentifier, using: nil) { task in func configure() async throws {
NSLog("profile update task started") if !registered {
Task { let success = BGTaskScheduler.shared.register(forTaskWithIdentifier: taskSchedulerPermittedIdentifier, using: nil) { task in
await getAndupdateProfiles(task) NSLog("profile update task started")
Task {
await UIProfileUpdateTask.getAndupdateProfiles(task)
}
} }
if !success {
throw NSError(domain: "register failed", code: 0)
}
registered = true
} }
if !success { BGTaskScheduler.shared.cancelAllTaskRequests()
throw NSError(domain: "register failed", code: 0) let profiles = try await ProfileManager.listAutoUpdateEnabled()
if profiles.isEmpty {
return
} }
registered = true try scheduleUpdate(ProfileUpdateTask.calculateEarliestBeginDate(profiles))
} }
BGTaskScheduler.shared.cancelAllTaskRequests() }
let profiles = try await ProfileManager.listAutoUpdateEnabled()
if profiles.isEmpty { private static let register = Register()
return public static func configure() async throws {
} try await register.configure()
try scheduleUpdate(ProfileUpdateTask.calculateEarliestBeginDate(profiles))
} }
private nonisolated static func getAndupdateProfiles(_ task: BGTask) async { private nonisolated static func getAndupdateProfiles(_ task: BGTask) async {
@@ -170,6 +170,7 @@ public struct EditProfileView: View {
do { do {
try await Task.sleep(nanoseconds: UInt64(100 * Double(NSEC_PER_MSEC))) try await Task.sleep(nanoseconds: UInt64(100 * Double(NSEC_PER_MSEC)))
try await profile.updateRemoteProfile() try await profile.updateRemoteProfile()
await performCallback()
} catch { } catch {
alert = Alert(error) alert = Alert(error)
} }
@@ -189,11 +190,11 @@ public struct EditProfileView: View {
private func saveProfile() async { private func saveProfile() async {
do { do {
_ = try await ProfileManager.update(profile) _ = try await ProfileManager.update(profile)
#if os(iOS) || os(tvOS) #if os(iOS) || os(tvOS)
try await UIProfileUpdateTask.configure() try await UIProfileUpdateTask.configure()
#else #else
try await ProfileUpdateTask.configure() try await ProfileUpdateTask.configure()
#endif #endif
} catch { } catch {
alert = Alert(error) alert = Alert(error)
return return
@@ -248,11 +248,11 @@ public struct NewProfileView: View {
lastUpdated: lastUpdated lastUpdated: lastUpdated
)) ))
if profileType == .remote { if profileType == .remote {
#if os(iOS) || os(tvOS) #if os(iOS) || os(tvOS)
try await UIProfileUpdateTask.configure() try await UIProfileUpdateTask.configure()
#else #else
try await ProfileUpdateTask.configure() try await ProfileUpdateTask.configure()
#endif #endif
} }
} }
} }
@@ -34,7 +34,7 @@ public struct ProfileView: View {
public init() {} public init() {}
public var body: some View { public var body: some View {
viewBuilder { VStack {
if isLoading { if isLoading {
ProgressView().onAppear { ProgressView().onAppear {
Task { Task {
@@ -222,6 +222,9 @@ public struct ProfileView: View {
isLoading = false isLoading = false
} }
do { do {
if !profileList.isEmpty {
profileList.removeAll()
}
profileList = try await ProfileManager.list() profileList = try await ProfileManager.list()
} catch { } catch {
alert = Alert(error) alert = Alert(error)