using Foundation;
|
using Intents;
|
using System;
|
using System.Linq;
|
|
namespace Other.Siri
|
{
|
public class SceneDateManager : DataManager<NSMutableArray<SiriScene>>
|
{
|
public SceneDateManager() : base(new UserDefaultsStorageDescriptor(NSUserDefaultsHelper.StorageKeys.OrderHistory), new NSMutableArray<SiriScene>()) { }
|
|
// Converts an `Order` into `OrderSoupIntent` and donates it as an
|
// interaction to the system so that this order can be suggested in the
|
// future or turned into a voice shortcut for quickly placing the same
|
// order in the future.
|
void DonateInteraction(SiriScene order)
|
{
|
var interaction = new INInteraction(order.Intent, null);
|
interaction.Identifier = order.Id.ToString();
|
interaction.DonateInteraction((error) =>
|
{
|
if (!(error is null))
|
{
|
Console.WriteLine($"Interaction donation failed: {error}");
|
}
|
else
|
{
|
Console.WriteLine("Successfully donated interaction.");
|
}
|
});
|
}
|
|
#region Public API for clients of `SoupOrderDataManager`
|
// Convenience method to access the data with a property name that makes
|
// sense in the caller's context.
|
public NSMutableArray<SiriScene> OrderHistory
|
{
|
get
|
{
|
return ManagedData as NSMutableArray<SiriScene>;
|
}
|
}
|
|
public void PlaceOrder(SiriScene order)
|
{
|
// Access to `ManagedDataBackingInstance` is only valid on
|
// `DataAccessQueue`.
|
DataAccessQueue.DispatchSync(() =>
|
{
|
if (ManagedDataBackingInstance.Count == 0)
|
{
|
// Getting an error trying to insert at 0 for an empty
|
// NSMutableArray<Order>
|
ManagedDataBackingInstance.Add(order);
|
}
|
else
|
{
|
ManagedDataBackingInstance.Insert(order, 0);
|
}
|
});
|
|
// Access to UserDefaults is gated behind a separate access queue.
|
WriteData();
|
|
// Donate an interaction to the system.
|
DonateInteraction(order);
|
}
|
#endregion
|
|
#region Support methods for unarchiving saved data
|
override protected void FinishUnarchiving(NSObject unarchivedData)
|
{
|
var array = (NSArray)unarchivedData;
|
SiriScene[] orders = NSArray.FromArray<SiriScene>(array);
|
ManagedDataBackingInstance = new NSMutableArray<SiriScene>(orders);
|
}
|
#endregion
|
|
|
public void SetIsLoginValue(bool value)
|
{
|
UserDefaults.SetBool(value, NSUserDefaultsHelper.StorageKeys.GLOBAL_GIsLogin);
|
}
|
public void SetAccessTokenValue(string accessToken)
|
{
|
UserDefaults.SetString(accessToken, NSUserDefaultsHelper.StorageKeys.GLOBAL_GAccessToken);
|
}
|
public void SetRefreshTokenValue(string refreshToken)
|
{
|
UserDefaults.SetString(refreshToken, NSUserDefaultsHelper.StorageKeys.GLOBAL_GRefreshToken);
|
}
|
public void SetRegionUrlValue(string regionUrl)
|
{
|
UserDefaults.SetString(regionUrl, NSUserDefaultsHelper.StorageKeys.GLOBAL_GRegionUrl);
|
}
|
}
|
}
|