Skip to content

Commit

Permalink
chore: add new logic with multi users
Browse files Browse the repository at this point in the history
  • Loading branch information
eyatsenkoperpetio committed Feb 13, 2024
1 parent 35e7cbe commit a596140
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 149 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="22522" systemVersion="22G91" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<entity name="CDDownloadData" representedClassName="CDDownloadData" syncable="YES" codeGenerationType="class">
<attribute name="blockId" optional="YES" attributeType="String"/>
<attribute name="courseId" optional="YES" attributeType="String"/>
<attribute name="displayName" optional="YES" attributeType="String"/>
<attribute name="fileName" optional="YES" attributeType="String"/>
Expand All @@ -11,6 +12,7 @@
<attribute name="state" optional="YES" attributeType="String"/>
<attribute name="type" optional="YES" attributeType="String"/>
<attribute name="url" optional="YES" attributeType="String"/>
<attribute name="userId" optional="YES" attributeType="Integer 32" defaultValueString="0" usesScalarValueType="YES"/>
<uniquenessConstraints>
<uniquenessConstraint>
<constraint value="id"/>
Expand Down
6 changes: 4 additions & 2 deletions Core/Core/Data/Persistence/CorePersistenceProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import CoreData
import Combine

public protocol CorePersistenceProtocol {
func set(userId: Int)
func getUserID() -> Int?
func publisher() -> AnyPublisher<Int, Never>
func addToDownloadQueue(blocks: [CourseBlock], downloadQuality: DownloadQuality)
func getNextBlockForDownloading() -> DownloadDataTask?
func nextBlockForDownloading() -> DownloadDataTask?
func updateDownloadState(id: String, state: DownloadState, resumeData: Data?)
func deleteDownloadDataTask(id: String) throws
func saveDownloadDataTask(data: DownloadDataTask)
func saveDownloadDataTask(_ task: DownloadDataTask)
func downloadDataTask(for blockId: String) -> DownloadDataTask?
func downloadDataTask(for blockId: String, completion: @escaping (DownloadDataTask?) -> Void)
func getDownloadDataTasks(completion: @escaping ([DownloadDataTask]) -> Void)
Expand Down
52 changes: 44 additions & 8 deletions Core/Core/Network/DownloadManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public enum DownloadType: String {
public struct DownloadDataTask: Identifiable, Hashable {
public let id: String
public let courseId: String
public let blockId: String
public let userId: Int
public let url: String
public let fileName: String
public let displayName: String
Expand All @@ -52,7 +54,9 @@ public struct DownloadDataTask: Identifiable, Hashable {

public init(
id: String,
blockId: String,
courseId: String,
userId: Int,
url: String,
fileName: String,
displayName: String,
Expand All @@ -64,6 +68,8 @@ public struct DownloadDataTask: Identifiable, Hashable {
) {
self.id = id
self.courseId = courseId
self.blockId = blockId
self.userId = userId
self.url = url
self.fileName = fileName
self.displayName = displayName
Expand All @@ -73,6 +79,21 @@ public struct DownloadDataTask: Identifiable, Hashable {
self.type = type
self.fileSize = fileSize
}

public init(sourse: CDDownloadData) {
self.id = sourse.id ?? ""
self.blockId = sourse.blockId ?? ""
self.courseId = sourse.courseId ?? ""
self.userId = Int(sourse.userId)
self.url = sourse.url ?? ""
self.fileName = sourse.fileName ?? ""
self.displayName = sourse.displayName ?? ""
self.progress = sourse.progress
self.resumeData = sourse.resumeData
self.state = DownloadState(rawValue: sourse.state ?? "") ?? .waiting
self.type = DownloadType(rawValue: sourse.type ?? "") ?? .video
self.fileSize = Int(sourse.fileSize)
}
}

public class NoWiFiError: LocalizedError {
Expand Down Expand Up @@ -138,6 +159,9 @@ public class DownloadManager: DownloadManagerProtocol {
connectivity: ConnectivityProtocol
) {
self.persistence = persistence
if let userId = appStorage.user?.id {
self.persistence.set(userId: userId)
}
self.appStorage = appStorage
self.connectivity = connectivity
self.backgroundTask()
Expand Down Expand Up @@ -202,7 +226,10 @@ public class DownloadManager: DownloadManagerProtocol {
downloadRequest?.cancel()

let downloaded = await getDownloadTasksForCourse(courseId).filter { $0.state == .finished }
let blocksForDelete = blocks.filter { block in downloaded.first(where: { $0.id == block.id }) == nil }
let blocksForDelete = blocks
.filter {
block in downloaded.first(where: { $0.blockId == block.id }) == nil
}

await deleteFile(blocks: blocksForDelete)
downloaded.forEach {
Expand All @@ -226,11 +253,11 @@ public class DownloadManager: DownloadManagerProtocol {
}

public func cancelDownloading(courseId: String) async throws {
let downloads = await getDownloadTasksForCourse(courseId)
for downloadData in downloads {
let tasks = await getDownloadTasksForCourse(courseId)
for task in tasks {
do {
try persistence.deleteDownloadDataTask(id: downloadData.id)
if let fileUrl = await fileUrl(for: downloadData.id) {
try persistence.deleteDownloadDataTask(id: task.id)
if let fileUrl = await fileUrl(for: task.id) {
try FileManager.default.removeItem(at: fileUrl)
}
} catch {
Expand Down Expand Up @@ -297,7 +324,7 @@ public class DownloadManager: DownloadManagerProtocol {
guard userCanDownload() else {
throw NoWiFiError()
}
guard let downloadTask = persistence.getNextBlockForDownloading() else {
guard let downloadTask = persistence.nextBlockForDownloading() else {
isDownloadingInProgress = false
return
}
Expand Down Expand Up @@ -394,8 +421,8 @@ public class DownloadManager: DownloadManagerProtocol {

lazy var videosFolderUrl: URL? = {
let documentDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let directoryURL = documentDirectoryURL.appendingPathComponent("Files", isDirectory: true)
let directoryURL = documentDirectoryURL.appendingPathComponent(folderPathComponent, isDirectory: true)

if FileManager.default.fileExists(atPath: directoryURL.path) {
return URL(fileURLWithPath: directoryURL.path)
} else {
Expand All @@ -413,6 +440,13 @@ public class DownloadManager: DownloadManagerProtocol {
}
}()

private var folderPathComponent: String {
if let id = appStorage.user?.id {
return "\(id)_Files"
}
return "Files"
}

private func saveFile(fileName: String, data: Data, folderURL: URL) {
let fileURL = folderURL.appendingPathComponent(fileName)
do {
Expand Down Expand Up @@ -522,7 +556,9 @@ public class DownloadManagerMock: DownloadManagerProtocol {
.canceled(
.init(
id: "",
blockId: "",
courseId: "",
userId: 0,
url: "",
fileName: "",
displayName: "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ public class CourseContainerViewModel: BaseCourseViewModel {
for vertical in sequential.childs where vertical.isDownloadable {
var verticalsChilds: [DownloadViewState] = []
for block in vertical.childs where block.isDownloadable {
if let download = courseDownloadTasks.first(where: { $0.id == block.id }) {
if let download = courseDownloadTasks.first(where: { $0.blockId == block.id }) {
switch download.state {
case .waiting, .inProgress:
sequentialsChilds.append(.downloading)
Expand Down
Loading

0 comments on commit a596140

Please sign in to comment.