-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some async functions to StorageAPI
- Loading branch information
1 parent
701aef5
commit ae7c96f
Showing
8 changed files
with
284 additions
and
57 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
145 changes: 145 additions & 0 deletions
145
TowerForge/TowerForge/StorageAPI/RemoteStorage+Access.swift
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,145 @@ | ||
// | ||
// RemoteStorage+Metadata.swift | ||
// TowerForge | ||
// | ||
// Created by Rubesh on 21/4/24. | ||
// | ||
|
||
import Foundation | ||
|
||
/// This class adds utility methods specifically for access operations. Given | ||
/// the nature of the remote backend, closures are used for async operations. | ||
/// This class abstracts away the closure invocation with default access | ||
/// functions, for both storage and metadata. | ||
extension RemoteStorage { | ||
|
||
/// Checks if a player's metadata exists without requiring a closure input | ||
/*static func checkIfPlayerMetadataExists(for playerId: String) -> Bool { | ||
var exists = false | ||
|
||
Self.remoteStorageExists(for: .Metadata, player: playerId) { bool in | ||
exists = bool | ||
} | ||
|
||
return exists | ||
}*/ | ||
|
||
static func checkIfPlayerMetadataExistsAsync(for playerId: String) async -> Bool { | ||
await withCheckedContinuation { continuation in | ||
Self.remoteStorageExists(for: .Metadata, player: playerId) { exists in | ||
continuation.resume(returning: exists) | ||
} | ||
} | ||
} | ||
|
||
/// Checks if a player's statistics exists without requiring a closure input | ||
static func checkIfPlayerStorageExists(for playerId: String) -> Bool { | ||
var exists = false | ||
|
||
Self.remoteStorageExists(for: .Statistics, player: playerId) { bool in | ||
exists = bool | ||
} | ||
|
||
return exists | ||
} | ||
|
||
static func saveMetadataToFirebase(player: String, with inputData: Metadata) { | ||
let metadataCompletion: (Error?) -> Void = { error in | ||
if let error = error { | ||
Logger.log("Saving metadata to firebase error: \(error)", self) | ||
} else { | ||
Logger.log("Saving metadata to firebase success", self) | ||
} | ||
} | ||
|
||
Self.saveDataToFirebase(for: .Metadata, | ||
player: player, | ||
with: inputData, | ||
completion: metadataCompletion) | ||
} | ||
|
||
static func saveStorageToFirebase(player: String, with inputData: StatisticsDatabase) { | ||
let storageCompletion: (Error?) -> Void = { error in | ||
if let error = error { | ||
Logger.log("Saving storage to firebase error: \(error)", self) | ||
} else { | ||
Logger.log("Saving storage to firebase success", self) | ||
} | ||
} | ||
|
||
Self.saveDataToFirebase(for: .Statistics, | ||
player: player, | ||
with: inputData, | ||
completion: storageCompletion) | ||
} | ||
|
||
/// Deletes metadata for the specified player from firebase | ||
static func deleteMetadataFromFirebase(player: String) { | ||
let completion: (Error?) -> Void = { error in | ||
if let error = error { | ||
Logger.log("Deleting metadata from firebase error: \(error)", self) | ||
} else { | ||
Logger.log("Saving Metadata from firebase success", self) | ||
} | ||
} | ||
|
||
Self.deleteDataFromFirebase(for: .Metadata, player: player, completion: completion) | ||
} | ||
|
||
/// Deletes storage for the specific player from Firebase | ||
static func deleteStorageFromFirebase(player: String) { | ||
let completion: (Error?) -> Void = { error in | ||
if let error = error { | ||
Logger.log("Deleting storage from firebase error: \(error)", self) | ||
} else { | ||
Logger.log("Saving storage from firebase success", self) | ||
} | ||
} | ||
|
||
Self.deleteDataFromFirebase(for: .Statistics, player: player, completion: completion) | ||
} | ||
|
||
static func loadStorageFromFirebase(player: String) -> StatisticsDatabase? { | ||
guard Self.checkIfPlayerStorageExists(for: player) else { | ||
return nil | ||
} | ||
|
||
var stats: StatisticsDatabase? | ||
Self.loadDataFromFirebase(for: .Statistics, | ||
player: player) { (statisticsDatabase: StatisticsDatabase?, error: Error?) in | ||
if let error = error { | ||
Logger.log("Error loading storage from firebase: \(error)", self) | ||
} else if let statistics = statisticsDatabase { | ||
Logger.log("Successfully loaded statistics from firebase", self) | ||
stats = statistics | ||
} else { | ||
// No error and no database implies that database is empty, return nil | ||
Logger.log("No error and empty database, new one will NOT be auto-created", self) | ||
} | ||
} | ||
|
||
return stats | ||
} | ||
|
||
static func loadMetadataFromFirebase(player: String) async -> Metadata? { | ||
guard await Self.checkIfPlayerMetadataExistsAsync(for: player) else { | ||
return nil | ||
} | ||
|
||
var metadata: Metadata? | ||
Self.loadDataFromFirebase(for: .Statistics, | ||
player: player) { (remoteMetadata: Metadata?, error: Error?) in | ||
if let error = error { | ||
Logger.log("Error loading storage from firebase: \(error)", self) | ||
} else if let currentMetadata = remoteMetadata { | ||
Logger.log("Successfully loaded statistics from firebase", self) | ||
metadata = currentMetadata | ||
} else { | ||
// No error and no database implies that database is empty, return nil | ||
Logger.log("No error and empty database, new one will NOT be auto-created", self) | ||
} | ||
} | ||
|
||
return metadata | ||
} | ||
} |
23 changes: 0 additions & 23 deletions
23
TowerForge/TowerForge/StorageAPI/RemoteStorage+Metadata.swift
This file was deleted.
Oops, something went wrong.
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.