-
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1126 from kiwix/live-activities-update
Use progress bar with timer updates for live activities
- Loading branch information
Showing
8 changed files
with
259 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// 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 Foundation | ||
import Combine | ||
|
||
struct DownloadState: Codable { | ||
let downloaded: Int64 | ||
let total: Int64 | ||
let resumeData: Data? | ||
|
||
var isPaused: Bool { | ||
resumeData != nil | ||
} | ||
|
||
static func empty() -> DownloadState { | ||
.init(downloaded: 0, total: 1, resumeData: nil) | ||
} | ||
|
||
init(downloaded: Int64, total: Int64, resumeData: Data?) { | ||
guard total >= downloaded, total > 0 else { | ||
assertionFailure("invalid download progress values: downloaded \(downloaded) total: \(total)") | ||
self.downloaded = downloaded | ||
self.total = downloaded | ||
self.resumeData = resumeData | ||
return | ||
} | ||
self.downloaded = downloaded | ||
self.total = total | ||
self.resumeData = resumeData | ||
} | ||
|
||
func updatedWith(downloaded: Int64, total: Int64) -> DownloadState { | ||
DownloadState(downloaded: downloaded, total: total, resumeData: resumeData) | ||
} | ||
|
||
func updatedWith(resumeData: Data?) -> DownloadState { | ||
DownloadState(downloaded: downloaded, total: total, resumeData: resumeData) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// 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 Foundation | ||
import Combine | ||
|
||
@MainActor | ||
final class DownloadTasksPublisher { | ||
|
||
let publisher: CurrentValueSubject<[UUID: DownloadState], Never> | ||
private var states = [UUID: DownloadState]() | ||
|
||
init() { | ||
publisher = CurrentValueSubject(states) | ||
if let jsonData = UserDefaults.standard.object(forKey: "downloadStates") as? Data, | ||
let storedStates = try? JSONDecoder().decode([UUID: DownloadState].self, from: jsonData) { | ||
states = storedStates | ||
publisher.send(states) | ||
} | ||
} | ||
|
||
func updateFor(uuid: UUID, downloaded: Int64, total: Int64) { | ||
if let state = states[uuid] { | ||
states[uuid] = state.updatedWith(downloaded: downloaded, total: total) | ||
} else { | ||
states[uuid] = DownloadState(downloaded: downloaded, total: total, resumeData: nil) | ||
} | ||
publisher.send(states) | ||
saveState() | ||
} | ||
|
||
func resetFor(uuid: UUID) { | ||
states.removeValue(forKey: uuid) | ||
publisher.send(states) | ||
saveState() | ||
} | ||
|
||
func isEmpty() -> Bool { | ||
states.isEmpty | ||
} | ||
|
||
func resumeDataFor(uuid: UUID) -> Data? { | ||
states[uuid]?.resumeData | ||
} | ||
|
||
func updateFor(uuid: UUID, withResumeData resumeData: Data?) { | ||
if let state = states[uuid] { | ||
states[uuid] = state.updatedWith(resumeData: resumeData) | ||
publisher.send(states) | ||
saveState() | ||
} else { | ||
assertionFailure("there should be a download task for: \(uuid)") | ||
} | ||
} | ||
|
||
private func saveState() { | ||
if let jsonStates = try? JSONEncoder().encode(states) { | ||
UserDefaults.standard.setValue(jsonStates, forKey: "downloadStates") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.