Skip to content

Commit

Permalink
Make changed to background tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviuvsp committed Oct 17, 2024
1 parent 2348020 commit f62e09f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
8 changes: 3 additions & 5 deletions Permanent/App/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -422,11 +422,9 @@ extension AppDelegate {

func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
PhotoManager.shared.start()
}

func applicationDidBecomeActive(_ application: UIApplication) {
if PermSession.currentSession != nil {
PhotoManager.shared.start()
DispatchQueue.main.asyncAfter(deadline: .now() + 28) {
PhotoManager.shared.cancelBackgroundTask()
completionHandler(UIBackgroundFetchResult.newData)
}
}
}
32 changes: 16 additions & 16 deletions Permanent/Modules/PhotoAlbum/Managers/PhotoManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ class PhotoManager {

var backgroundTask: UIBackgroundTaskIdentifier = .invalid

init() {
getRoot(then: { status in
self.start()
})
}

func getRoot(then handler: @escaping ServerResponse) {
let currentArchive = AuthenticationManager.shared.session?.selectedArchive
let apiOperation = APIOperation(FilesEndpoint.getPublicRoot(archiveNbr: currentArchive!.archiveNbr!))
Expand Down Expand Up @@ -55,9 +49,11 @@ class PhotoManager {
}

func start() {
self.checkForNewPhotos { assets in
self.startUpload(assets: assets)
}
getRoot(then: { status in
self.checkForNewPhotos { assets in
self.startUpload(assets: assets)
}
})
}

func checkForNewPhotos(completion: @escaping ([PHAsset]) -> Void) {
Expand Down Expand Up @@ -85,30 +81,34 @@ class PhotoManager {

func startUpload(assets: [PHAsset]) {
Task {
let chunkedAssets = assets.chunked(into: 10)
let chunkedAssets = assets.chunked(into: 5)
for chunk in chunkedAssets {
isLoading = true
do {
backgroundTask = await UIApplication.shared.beginBackgroundTask(withName: "com.permanent.backgroundTask") {
OtherUploadManager.shared.uploadQueue.cancelAllOperations()
UIApplication.shared.endBackgroundTask(self.backgroundTask)
self.backgroundTask = .invalid
self.isLoading = false
print("canceling background task")
self.cancelBackgroundTask()
}

let fileInfos = try await getURLS(assets: chunk)
await OtherUploadManager.shared.upload(files: fileInfos) // Wait for upload to complete

await UIApplication.shared.endBackgroundTask(backgroundTask)
isLoading = false
backgroundTask = .invalid
isLoading = false
} catch {
print("Error processing chunk: \(error)")
}
}
}
}

func cancelBackgroundTask() {
OtherUploadManager.shared.uploadQueue.cancelAllOperations()
print("canceling background task")
self.isLoading = false
UIApplication.shared.endBackgroundTask(self.backgroundTask)
self.backgroundTask = .invalid
}

func getURLS(assets: [PHAsset]) async throws -> [FileInfo] {
try await withThrowingTaskGroup(of: FileInfo.self) { group in
Expand Down
5 changes: 5 additions & 0 deletions Permanent/Modules/PhotoAlbum/UI/PhotoGalery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Photos
struct PhotoGalery: View {

@EnvironmentObject var service: FetchAlbumsViewModel
@Environment(\.scenePhase) var scenePhase

var body: some View {
CustomNavigationView {
Expand All @@ -18,6 +19,10 @@ struct PhotoGalery: View {
.onAppear {
service.fetchAssets()
}
.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { (_) in
print("did become active")
service.fetchAssets()
}
}
.padding()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class FetchAlbumsViewModel: ObservableObject {
switch status {
case .authorized:
self?.fetchAssets()
PhotoManager.shared
default:
break
}
Expand All @@ -84,6 +83,7 @@ class FetchAlbumsViewModel: ObservableObject {
assets.append(FetchedAssets(asset: object, isUploaded: self.isUploaded(asset: object)))
}
self.assets = assets
PhotoManager.shared.start()
}
}

Expand Down

0 comments on commit f62e09f

Please sign in to comment.