Skip to content

Commit 5de9e1c

Browse files
authored
fix: iOS - issues related to blank instance (#356)
* Fetcher - get headers | make sure to reissue the instance id if null * Gems - | prevent nasty exceptions when GetGems() returns null
1 parent 4923db8 commit 5de9e1c

4 files changed

Lines changed: 52 additions & 12 deletions

File tree

App/App.xaml.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,11 @@ public void OpenPopUp(Popup popUp, Page page = null)
294294
#endif
295295
}
296296

297-
#if ANDROID
298-
private void SetupInstance()
299-
#elif IOS
300-
private async Task SetupInstance()
301-
#endif
297+
/// <summary>
298+
/// Setup the instance for a device
299+
/// </summary>
300+
/// <returns>a task returning the id of the instance</returns>
301+
public async Task<string> SetupInstance()
302302
{
303303

304304
#if IOS
@@ -308,17 +308,24 @@ private async Task SetupInstance()
308308
await SecureStorage.Default.SetAsync(AppConstant.InstanceIdKey, instanceID = UIKit.UIDevice.CurrentDevice.IdentifierForVendor.ToString().ToLower().Replace("-", string.Empty).Substring(0,30));
309309

310310
}
311-
#endif
312311

312+
#if DEBUG
313+
Debug.WriteLine($"Instance: {instanceID}");
314+
#endif
315+
return
316+
#endif
313317
InstanceID =
314318
#if ANDROID
315319
Secure.GetString(Android.App.Application.Context.ContentResolver, Secure.AndroidId);
316-
#elif IOS
317-
instanceID;
318-
#endif
319320
#if DEBUG
320321
Debug.WriteLine($"Instance: {InstanceID}");
321322
#endif
323+
await SecureStorage.Default.SetAsync(AppConstant.InstanceIdKey, InstanceID);
324+
return InstanceID;
325+
#elif IOS
326+
instanceID;
327+
#endif
328+
322329
}
323330
/// <summary>
324331
/// Load all the partners

App/Services/Fetcher.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -781,13 +781,20 @@ private bool EvaluateCurrentSession()
781781
private async Task<Dictionary<string,string>> GetHeaders()
782782
{
783783
var apiKey = Csign.GenerateApiKey();
784+
785+
string instanceID = await SecureStorage.Default.GetAsync(AppConstant.InstanceIdKey);
786+
if (string.IsNullOrEmpty(instanceID))
787+
{
788+
// Reissue an instanceID
789+
instanceID = await (App.Current as App).SetupInstance();
790+
}
784791
#if DEBUG
785792
Debug.WriteLine($"ApiKey: {apiKey}");
786793
#endif
787794
return new Dictionary<string, string>
788795
{
789796
{ "x-api-key", apiKey},
790-
{ "instance", await SecureStorage.Default.GetAsync(AppConstant.InstanceIdKey)},
797+
{ "instance", instanceID},
791798
};
792799

793800
}
@@ -1063,7 +1070,7 @@ public async Task<List<Gem>> GetGems()
10631070
return Gems = (await WebService.Get<UserGemsResponse>(controller: "gems",
10641071
singleUseHeaders: headers,
10651072
unSuccessCallback: e => _ = HandleHttpException(e)
1066-
))?.Data;
1073+
))?.Data ?? new List<Gem>();
10671074
}
10681075
#endif
10691076

App/ViewModels/AppShellViewModel.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,19 @@ public async Task UpdateDeals()
185185
/// <returns></returns>
186186
public async Task UpdateGems()
187187
{
188-
Gems = new (await dataFetcher.GetGems());
188+
try
189+
{
190+
Gems = new(await dataFetcher.GetGems());
191+
192+
}
193+
catch (Exception ex)
194+
{
195+
#if DEBUG
196+
Debug.WriteLine(ex);
197+
#elif RELEASE
198+
SentrySdk.CaptureException(ex);
199+
#endif
200+
}
189201

190202
}
191203
#endif

App/ViewModels/GiveawayViewModel.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
using GamHubApp.Services;
33
using GamHubApp.Views;
44
using System.Collections.ObjectModel;
5+
#if DEBUG
6+
using System.Diagnostics;
7+
#endif
58
#if IOS
69
using CommunityToolkit.Mvvm.Messaging;
710
using GamHubApp.Services.ChangedMessages;
@@ -76,7 +79,18 @@ public GiveawayViewModel(Fetcher fetch,
7679
/// <returns></returns>
7780
public async Task UpdateGems()
7881
{
82+
try
83+
{
7984
Gems = new(await _fetcher.GetGems());
85+
}
86+
catch (Exception ex)
87+
{
88+
#if DEBUG
89+
Debug.WriteLine(ex);
90+
#elif RELEASE
91+
SentrySdk.CaptureException(ex);
92+
#endif
93+
}
8094

8195
}
8296

0 commit comments

Comments
 (0)