Allows explicitly downloading corrupted record versions from RecordVersionsViewController

This commit is contained in:
Riley Testut 2023-08-10 19:21:51 -05:00
parent be047b28a6
commit 061f5abd3e
2 changed files with 67 additions and 23 deletions

View File

@ -23,6 +23,12 @@ public class GameSave: _GameSave
}
}
extension GameSave
{
// Hacky, but YOLO I'm under time crunch.
public static var ignoredCorruptedIDs = Set<String>()
}
extension GameSave: Syncable
{
public static var syncablePrimaryKey: AnyKeyPath {
@ -70,6 +76,8 @@ extension GameSave: Syncable
}
public func awakeFromSync(_ record: AnyRecord) throws
{
do
{
guard let game = self.game else { throw SyncValidationError.incorrectGame(nil) }
@ -94,4 +102,21 @@ extension GameSave: Syncable
throw SyncValidationError.incorrectGame(game.name)
}
}
catch let error as SyncValidationError
{
guard GameSave.ignoredCorruptedIDs.contains(self.identifier) else { throw error }
let fetchRequest = Game.fetchRequest()
fetchRequest.predicate = NSPredicate(format: "%K == %@", #keyPath(Game.identifier), self.identifier)
if let correctGame = try self.managedObjectContext?.fetch(fetchRequest).first
{
self.game = correctGame
}
else
{
throw ValidationError.nilRelationshipObjects(keys: [#keyPath(GameSave.game)])
}
}
}
}

View File

@ -258,6 +258,8 @@ private extension RecordVersionsViewController
{
DispatchQueue.main.async {
GameSave.ignoredCorruptedIDs.remove(self.record.recordID.identifier)
CATransaction.begin()
CATransaction.setCompletionBlock {
@ -287,6 +289,22 @@ private extension RecordVersionsViewController
}
catch
{
switch error
{
case RecordError.other(let record, let error as SyncValidationError):
// Only allow restoring corrupted records with incorrect games.
guard case .incorrectGame = error else { fallthrough }
let message = NSLocalizedString("Would you like to download this version anyway?", comment: "")
let alertController = UIAlertController(title: NSLocalizedString("Record Version Corrupted", comment: ""), message: message, preferredStyle: .alert)
alertController.addAction(.cancel)
alertController.addAction(UIAlertAction(title: NSLocalizedString("Download Anyway", comment: ""), style: .destructive) { _ in
GameSave.ignoredCorruptedIDs.insert(record.recordID.identifier)
self.restoreVersion()
})
self.present(alertController, animated: true, completion: nil)
default:
let title: String
switch self.mode
@ -299,6 +317,7 @@ private extension RecordVersionsViewController
alertController.addAction(.ok)
self.present(alertController, animated: true, completion: nil)
}
}
CATransaction.commit()
}