-
-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add live activities for download progress #1058
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import Foundation | ||
import ActivityKit | ||
|
||
struct DownloadActivityAttributes: ActivityAttributes { | ||
public struct ContentState: Codable, Hashable { | ||
var progress: Double | ||
var speed: Double | ||
} | ||
|
||
var fileID: UUID | ||
var fileName: String | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,8 @@ | ||
// This file is part of Kiwix for iOS & macOS. | ||
// | ||
// Kiwix is free software; you can redistribute it and/or modify it | ||
// under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation; either version 3 of the License, or | ||
// any later version. | ||
// | ||
// Kiwix is distributed in the hope that it will be useful, but | ||
// WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Kiwix; If not, see https://www.gnu.org/licenses/. | ||
|
||
// | ||
// DownloadService.swift | ||
// Kiwix | ||
|
||
import Combine | ||
import CoreData | ||
import UserNotifications | ||
import os | ||
import ActivityKit | ||
|
||
struct DownloadState: Codable { | ||
let downloaded: Int64 | ||
|
@@ -118,6 +100,7 @@ | |
operationQueue.underlyingQueue = queue | ||
return URLSession(configuration: configuration, delegate: self, delegateQueue: operationQueue) | ||
}() | ||
private var downloadActivity: Activity<DownloadActivityAttributes>? | ||
|
||
// MARK: - Heartbeat | ||
|
||
|
@@ -167,6 +150,19 @@ | |
task.countOfBytesClientExpectsToReceive = zimFile.size | ||
task.taskDescription = zimFileID.uuidString | ||
task.resume() | ||
|
||
// Start Live Activity | ||
let attributes = DownloadActivityAttributes(fileID: zimFileID, fileName: zimFile.name) | ||
let initialContentState = DownloadActivityAttributes.ContentState(progress: 0.0, speed: 0.0) | ||
do { | ||
downloadActivity = try Activity<DownloadActivityAttributes>.request( | ||
attributes: attributes, | ||
contentState: initialContentState, | ||
pushType: nil | ||
) | ||
} catch { | ||
print("Error starting Live Activity: \(error)") | ||
} | ||
} | ||
} | ||
|
||
|
@@ -214,6 +210,19 @@ | |
|
||
downloadTask.error = nil | ||
try? context.save() | ||
|
||
// Resume Live Activity | ||
let attributes = DownloadActivityAttributes(fileID: zimFileID, fileName: downloadTask.zimFile?.name ?? "") | ||
let initialContentState = DownloadActivityAttributes.ContentState(progress: 0.0, speed: 0.0) | ||
do { | ||
downloadActivity = try Activity<DownloadActivityAttributes>.request( | ||
attributes: attributes, | ||
contentState: initialContentState, | ||
pushType: nil | ||
) | ||
} catch { | ||
print("Error resuming Live Activity: \(error)") | ||
} | ||
} | ||
} | ||
|
||
|
@@ -337,6 +346,11 @@ | |
progress.updateFor(uuid: zimFileID, | ||
downloaded: totalBytesWritten, | ||
total: totalBytesExpectedToWrite) | ||
// Update Live Activity | ||
let progressPercentage = Double(totalBytesWritten) / Double(totalBytesExpectedToWrite) | ||
let speed = Double(bytesWritten) / 1024.0 / 1024.0 // Convert to MB/s | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to be incorrect. |
||
let contentState = DownloadActivityAttributes.ContentState(progress: progressPercentage, speed: speed) | ||
await downloadActivity?.update(using: contentState) | ||
} | ||
} | ||
|
||
|
@@ -378,6 +392,8 @@ | |
// schedule notification | ||
scheduleDownloadCompleteNotification(zimFileID: zimFileID) | ||
deleteDownloadTask(zimFileID: zimFileID) | ||
// End Live Activity | ||
await downloadActivity?.end(dismissalPolicy: .immediate) | ||
} | ||
} | ||
|
||
|
@@ -388,4 +404,4 @@ | |
self?.backgroundCompletionHandler?() | ||
} | ||
} | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,5 @@ | ||
// This file is part of Kiwix for iOS & macOS. | ||
// | ||
// Kiwix is free software; you can redistribute it and/or modify it | ||
// under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation; either version 3 of the License, or | ||
// any later version. | ||
// | ||
// Kiwix is distributed in the hope that it will be useful, but | ||
// WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Kiwix; If not, see https://www.gnu.org/licenses/. | ||
|
||
import SwiftUI | ||
import ActivityKit | ||
|
||
import Defaults | ||
|
||
|
@@ -150,6 +136,7 @@ struct Settings: View { | |
@Default(.libraryAutoRefresh) private var libraryAutoRefresh | ||
@Default(.searchResultSnippetMode) private var searchResultSnippetMode | ||
@Default(.webViewPageZoom) private var webViewPageZoom | ||
@Default(.enableLiveActivities) private var enableLiveActivities | ||
@EnvironmentObject private var library: LibraryViewModel | ||
|
||
enum Route { | ||
|
@@ -265,6 +252,7 @@ struct Settings: View { | |
SelectedLanaguageLabel() | ||
}.disabled(library.state != .complete) | ||
Toggle("library_settings.toggle.cellular".localized, isOn: $downloadUsingCellular) | ||
Toggle("library_settings.toggle.live_activities".localized, isOn: $enableLiveActivities) | ||
} header: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to be a nice addition, but the value of it isn't read anywhere, so it's not turning anything on / off atm. |
||
Text("library_settings.tab.library.title".localized) | ||
} footer: { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This struct is a duplicate.