Skip to content

Commit

Permalink
Simplify persistence manager protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
mathebox committed Nov 27, 2018
1 parent 277c367 commit 6f78f9d
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 97 deletions.
12 changes: 9 additions & 3 deletions iOS/Base.lproj/Localizable.strings
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@

/* title of alert for stream download errors */
"alert.download-error.documents.title" = "Error while downloading document";

/* title of alert for slides download errors */
"alert.download-error.slides.title" = "Error while downloading slides";

/* title of alert for stream download errors */
"alert.download-error.stream.title" = "Error while downloading video";

/* alert action title to mark all announcements as read */
"announcement.alert.mark all as read" = "Mark all as read";

Expand Down Expand Up @@ -68,9 +77,6 @@
/* delete stream download for video */
"course-item.stream-download-action.delete-download.title" = "Delete video";

/* title to download error alert */
"course-item.stream-download-action.download-error.title" = "Error while downloading video";

/* start download of stream for video */
"course-item.stream-download-action.start-download.title" = "Download video";

Expand Down
2 changes: 1 addition & 1 deletion iOS/Data/Model/DocumentLocalization+Download.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extension DocumentLocalization: Persistable {

static let identifierKeyPath: WritableKeyPath<DocumentLocalization, String> = \DocumentLocalization.id

public override func prepareForDeletion() {
override public func prepareForDeletion() { // swiftlint:disable:this override_in_extension
super.prepareForDeletion()
DocumentsPersistenceManager.shared.prepareForDeletion(of: self)
}
Expand Down
2 changes: 1 addition & 1 deletion iOS/Data/Model/Video+Download.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extension Video: Persistable {

static let identifierKeyPath: WritableKeyPath<Video, String> = \Video.id

public override func prepareForDeletion() {
override public func prepareForDeletion() { // swiftlint:disable:this override_in_extension
super.prepareForDeletion()
StreamPersistenceManager.shared.prepareForDeletion(of: self)
SlidesPersistenceManager.shared.prepareForDeletion(of: self)
Expand Down
27 changes: 4 additions & 23 deletions iOS/Helpers/PersistenceManager/DocumentsPersistenceManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import UIKit

final class DocumentsPersistenceManager: NSObject, FilePersistenceManager {

static var shared = DocumentsPersistenceManager(keyPath: \DocumentLocalization.localFileBookmark)
static var downloadType = "documents"
static let shared = DocumentsPersistenceManager(keyPath: \DocumentLocalization.localFileBookmark)
static let downloadType = "documents"
static let titleForFailedDownloadAlert = NSLocalizedString("alert.download-error.documents.title",
comment: "title of alert for stream download errors")

lazy var persistentContainerQueue = self.createPersistenceContainerQueue()
lazy var session: URLSession = self.createURLSession(withIdentifier: "documents-download")
Expand All @@ -37,27 +39,6 @@ final class DocumentsPersistenceManager: NSObject, FilePersistenceManager {
self.startDownload(with: url, for: documentLocalization)
}

func didFailToDownloadResource(_ resource: DocumentLocalization, with error: NSError) {
ErrorManager.shared.remember((Resource.type, resource.id), forKey: "resource")
ErrorManager.shared.report(error)
log.error("Unknown asset download error (resource type: \(Resource.type) | resource id: \(resource.id) | domain: \(error.domain) | code: \(error.code)")

// show error
DispatchQueue.main.async {
let alertTitle = "Document Download Error"
let alertMessage = "Domain: \(error.domain)\nCode: \(error.code)"
let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)
let actionTitle = NSLocalizedString("global.alert.ok", comment: "title to confirm alert")
alert.addAction(UIAlertAction(title: actionTitle, style: .default) { _ in
alert.dismiss(animated: trueUnlessReduceMotionEnabled)
})

let rootViewController = AppDelegate.instance().window?.rootViewController
let presentingViewController = rootViewController?.presentedViewController ?? rootViewController
presentingViewController?.present(alert, animated: trueUnlessReduceMotionEnabled)
}
}

}

extension DocumentsPersistenceManager {
Expand Down
44 changes: 30 additions & 14 deletions iOS/Helpers/PersistenceManager/PersistenceManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ protocol PersistenceManager: AnyObject {

static var shared: Self { get }
static var downloadType: String { get }
static var titleForFailedDownloadAlert: String { get }

var persistentContainerQueue: OperationQueue { get }
var session: Session { get }
Expand All @@ -30,23 +31,10 @@ protocol PersistenceManager: AnyObject {
var progresses: [String: Double] { get set }
var didRestorePersistenceManager: Bool { get set }

// functionality
func restoreDownloads()
func startDownload(with url: URL, for resource: Resource)
func downloadState(for resource: Resource) -> DownloadState
func downloadProgress(for resource: Resource) -> Double?
func deleteDownload(for resource: Resource)
func cancelDownload(for resource: Resource)

func fileSize(for resource: Resource) -> UInt64?

// callbacks
func didCompleteDownloadTask(_ task: URLSessionTask, with error: Error?)
func didFinishDownloadTask(_ task: URLSessionTask, to location: URL)

// configuration
func downloadTask(with url: URL, for resource: Resource, on session: Session) -> URLSessionTask?
func didFailToDownloadResource(_ resource: Resource, with error: NSError)
func resourceModificationAfterStartingDownload(for resource: Resource)
func resourceModificationAfterDeletingDownload(for resource: Resource)

Expand Down Expand Up @@ -150,7 +138,7 @@ extension PersistenceManager {
}
}

func deleteDownload(for resource: Resource, in context: NSManagedObjectContext) {
private func deleteDownload(for resource: Resource, in context: NSManagedObjectContext) {
guard let localFileLocation = self.localFileLocation(for: resource) else { return }

let resourceIdentifier = resource[keyPath: Resource.identifierKeyPath]
Expand Down Expand Up @@ -307,6 +295,34 @@ extension PersistenceManager {
}
}

func didFailToDownloadResource(_ resource: Resource, with error: NSError) {
let resourceId = resource[keyPath: Resource.identifierKeyPath]

if error.domain == NSURLErrorDomain && error.code == NSURLErrorCancelled {
log.debug("Canceled download of resource (type: \(Resource.self) id: \(resourceId))")
return
}

ErrorManager.shared.remember((Resource.self, resourceId), forKey: "resource")
ErrorManager.shared.report(error)
log.error("Unknown asset download error (resource type: \(Resource.self) | resource id: \(resourceId) | domain: \(error.domain) | code: \(error.code)")

// show error
DispatchQueue.main.async {
let alertTitle = Self.titleForFailedDownloadAlert
let alertMessage = "Domain: \(error.domain)\nCode: \(error.code)"
let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)
let actionTitle = NSLocalizedString("global.alert.ok", comment: "title to confirm alert")
alert.addAction(UIAlertAction(title: actionTitle, style: .default) { _ in
alert.dismiss(animated: trueUnlessReduceMotionEnabled)
})

let rootViewController = AppDelegate.instance().window?.rootViewController
let presentingViewController = rootViewController?.presentedViewController ?? rootViewController
presentingViewController?.present(alert, animated: trueUnlessReduceMotionEnabled)
}
}

// MARK: configurations

func resourceModificationAfterStartingDownload(for resource: Resource) {}
Expand Down
27 changes: 4 additions & 23 deletions iOS/Helpers/PersistenceManager/SlidesPersistenceManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import UIKit

final class SlidesPersistenceManager: NSObject, FilePersistenceManager {

static var shared = SlidesPersistenceManager(keyPath: \Video.localSlidesBookmark)
static var downloadType = "slides"
static let shared = SlidesPersistenceManager(keyPath: \Video.localSlidesBookmark)
static let downloadType = "slides"
static let titleForFailedDownloadAlert = NSLocalizedString("alert.download-error.slides.title",
comment: "title of alert for slides download errors")

lazy var persistentContainerQueue = self.createPersistenceContainerQueue()
lazy var session: URLSession = self.createURLSession(withIdentifier: "slides-download")
Expand Down Expand Up @@ -56,27 +58,6 @@ final class SlidesPersistenceManager: NSObject, FilePersistenceManager {
TrackingHelper.shared.createEvent(.slidesDownloadFinished, resourceType: .video, resourceId: resource.id, context: self.trackingContext(for: resource))
}

func didFailToDownloadResource(_ resource: Video, with error: NSError) {
ErrorManager.shared.remember((Resource.type, resource.id), forKey: "resource")
ErrorManager.shared.report(error)
log.error("Unknown asset download error (resource type: \(Resource.type) | resource id: \(resource.id) | domain: \(error.domain) | code: \(error.code)")

// show error
DispatchQueue.main.async {
let alertTitle = "Slide Download Error"
let alertMessage = "Domain: \(error.domain)\nCode: \(error.code)"
let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)
let actionTitle = NSLocalizedString("global.alert.ok", comment: "title to confirm alert")
alert.addAction(UIAlertAction(title: actionTitle, style: .default) { _ in
alert.dismiss(animated: trueUnlessReduceMotionEnabled)
})

let rootViewController = AppDelegate.instance().window?.rootViewController
let presentingViewController = rootViewController?.presentedViewController ?? rootViewController
presentingViewController?.present(alert, animated: trueUnlessReduceMotionEnabled)
}
}

}

extension SlidesPersistenceManager {
Expand Down
33 changes: 4 additions & 29 deletions iOS/Helpers/PersistenceManager/StreamPersistenceManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ final class StreamPersistenceManager: NSObject, PersistenceManager {

typealias Session = AVAssetDownloadURLSession

static var shared = StreamPersistenceManager()
static var downloadType = "stream"
static let shared = StreamPersistenceManager()
static let downloadType = "stream"
static let titleForFailedDownloadAlert = NSLocalizedString("alert.download-error.stream.title",
comment: "title of alert for stream download errors")

let keyPath: ReferenceWritableKeyPath<Video, NSData?> = \Video.localFileBookmark

Expand Down Expand Up @@ -81,33 +83,6 @@ final class StreamPersistenceManager: NSObject, PersistenceManager {
TrackingHelper.shared.createEvent(.videoDownloadFinished, resourceType: .video, resourceId: resource.id, context: self.trackingContext(for: resource))
}

func didFailToDownloadResource(_ resource: Video, with error: NSError) {
if error.domain == NSURLErrorDomain && error.code == NSURLErrorCancelled {
log.debug("Canceled download of video (video id: \(resource.id))")
return
}

ErrorManager.shared.remember((Resource.type, resource.id), forKey: "resource")
ErrorManager.shared.report(error)
log.error("Unknown asset download error (video id: \(resource.id) | domain: \(error.domain) | code: \(error.code)")

// show error
DispatchQueue.main.async {
let alertTitle = NSLocalizedString("course-item.stream-download-action.download-error.title",
comment: "title to download error alert")
let alertMessage = "Domain: \(error.domain)\nCode: \(error.code)"
let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)
let actionTitle = NSLocalizedString("global.alert.ok", comment: "title to confirm alert")
alert.addAction(UIAlertAction(title: actionTitle, style: .default) { _ in
alert.dismiss(animated: trueUnlessReduceMotionEnabled)
})

let rootViewController = AppDelegate.instance().window?.rootViewController
let presentingViewController = rootViewController?.presentedViewController ?? rootViewController
presentingViewController?.present(alert, animated: trueUnlessReduceMotionEnabled)
}
}

}

extension StreamPersistenceManager {
Expand Down
12 changes: 9 additions & 3 deletions iOS/de.lproj/Localizable.strings
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@

/* title of alert for stream download errors */
"alert.download-error.documents.title" = "Fehler beim Dokument-Download";

/* title of alert for slides download errors */
"alert.download-error.slides.title" = "Fehler beim Folien-Download";

/* title of alert for stream download errors */
"alert.download-error.stream.title" = "Fehler beim Video-Download";

/* alert action title to mark all announcements as read */
"announcement.alert.mark all as read" = "Alle als gelesen markieren";

Expand Down Expand Up @@ -68,9 +77,6 @@
/* delete stream download for video */
"course-item.stream-download-action.delete-download.title" = "Video löschen";

/* title to download error alert */
"course-item.stream-download-action.download-error.title" = "Fehler beim Video-Download";

/* start download of stream for video */
"course-item.stream-download-action.start-download.title" = "Video herunterladen";

Expand Down

0 comments on commit 6f78f9d

Please sign in to comment.