Replace lazy AppStorage calls with AccountManager
This commit is contained in:
+12
-6
@@ -13,13 +13,13 @@ enum WCMeteriosMessageTypes: String {
|
||||
case BookDrink
|
||||
}
|
||||
|
||||
class WCManager: NSObject, WCSessionDelegate {
|
||||
@AppStorage("meteHostAddr") private var meteHostAddr = ""
|
||||
@AppStorage("meteUserID") private var meteUserID = -1
|
||||
|
||||
/// **WCManager** managed the Meterios-specific communication between an iOS and an watchOS device
|
||||
class WCManager: NSObject, WCSessionDelegate, ObservableObject {
|
||||
private var accountManager: AccountManager
|
||||
private let session: WCSession = WCSession.default
|
||||
|
||||
override init() {
|
||||
self.accountManager = AccountManager.default
|
||||
super.init()
|
||||
|
||||
if (WCSession.isSupported()) {
|
||||
@@ -39,12 +39,18 @@ class WCManager: NSObject, WCSessionDelegate {
|
||||
// As Meterios currently only needs messages from watchOS to iOS and responses back to the watch, this function currently only implements iOS-related responses.
|
||||
func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) {
|
||||
#if os(iOS)
|
||||
// Prevent crashes when WCSession is received before AccountManager is ready
|
||||
if accountManager.CurrentAccount() == nil {
|
||||
print("Request encountered in WCSession, but AccountManager is not ready.")
|
||||
return
|
||||
}
|
||||
|
||||
// Check for the different possible message types
|
||||
switch message["request"] as! String {
|
||||
case WCMeteriosMessageTypes.DrinkList.rawValue:
|
||||
// Request for a list of available drinks
|
||||
Task {
|
||||
let drinks = try await BackendConnector().GetDrinks(baseAddr: meteHostAddr)
|
||||
let drinks = try await accountManager.CurrentAccount()!.GetClient().GetDrinks()
|
||||
let favouriteDrinks = drinks.filter({ FavouritesManager.default.IsFavourite($0.id) && $0.active })
|
||||
let availableDrinks = drinks.filter({ !FavouritesManager.default.IsFavourite($0.id) && $0.active })
|
||||
// note that drinks which are not active/available are not sent to the watch - the screen is too small to waste pixels.
|
||||
@@ -64,7 +70,7 @@ class WCManager: NSObject, WCSessionDelegate {
|
||||
do {
|
||||
// Book
|
||||
let drinkID = message["drinkID"] as! Int
|
||||
try await BackendConnector().BookDrink(baseAddr: meteHostAddr, userID: meteUserID, drinkID: drinkID)
|
||||
try await accountManager.CurrentAccount()!.GetClient().BookDrink(drinkID)
|
||||
replyHandler([
|
||||
"salut": SalutManager.getSalute()
|
||||
])
|
||||
|
||||
Reference in New Issue
Block a user