forked from microsoft/react-native-code-push
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsManager.cs
More file actions
133 lines (117 loc) · 4.29 KB
/
SettingsManager.cs
File metadata and controls
133 lines (117 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
#if WINDOWS_UWP
using Windows.Storage;
#else
using CodePush.Net46.Adapters.Storage;
using System.IO;
#endif
namespace CodePush.ReactNative
{
internal class SettingsManager
{
private static ApplicationDataContainer Settings = null;
static SettingsManager ()
{
#if WINDOWS_UWP
Settings = ApplicationData.Current.LocalSettings.CreateContainer(CodePushConstants.CodePushPreferences, ApplicationDataCreateDisposition.Always);
#else
var folder = UpdateUtils.GetCodePushFolderAsync().Result;
Settings = new ApplicationDataContainer(Path.Combine(folder.Path, CodePushConstants.CodePushPreferences));
#endif
}
public static JArray GetFailedUpdates()
{
var failedUpdatesString = (string)Settings.Values[CodePushConstants.FailedUpdatesKey];
if (failedUpdatesString == null)
{
return new JArray();
}
try
{
return JArray.Parse(failedUpdatesString);
}
catch (Exception)
{
var emptyArray = new JArray();
Settings.Values[CodePushConstants.FailedUpdatesKey] = JsonConvert.SerializeObject(emptyArray);
return emptyArray;
}
}
internal static JObject GetPendingUpdate()
{
var pendingUpdateString = (string)Settings.Values[CodePushConstants.PendingUpdateKey];
if (pendingUpdateString == null)
{
return null;
}
try
{
return JObject.Parse(pendingUpdateString);
}
catch (Exception)
{
// Should not happen.
CodePushUtils.Log("Unable to parse pending update metadata " + pendingUpdateString +
" stored in SharedPreferences");
return null;
}
}
internal static bool IsFailedHash(string packageHash)
{
JArray failedUpdates = SettingsManager.GetFailedUpdates();
if (packageHash != null)
{
foreach (var failedPackage in failedUpdates)
{
var failedPackageHash = (string)failedPackage[CodePushConstants.PackageHashKey];
if (packageHash.Equals(failedPackageHash))
{
return true;
}
}
}
return false;
}
internal static bool IsPendingUpdate(string packageHash)
{
JObject pendingUpdate = SettingsManager.GetPendingUpdate();
return pendingUpdate != null &&
!(bool)pendingUpdate[CodePushConstants.PendingUpdateIsLoadingKey] &&
(packageHash == null || ((string)pendingUpdate[CodePushConstants.PendingUpdateHashKey]).Equals(packageHash));
}
internal static void RemoveFailedUpdates()
{
Settings.Values.Remove(CodePushConstants.FailedUpdatesKey);
}
internal static void RemovePendingUpdate()
{
Settings.Values.Remove(CodePushConstants.PendingUpdateKey);
}
internal static void SaveFailedUpdate(JObject failedPackage)
{
var failedUpdatesString = (string)Settings.Values[CodePushConstants.FailedUpdatesKey];
JArray failedUpdates;
if (failedUpdatesString == null)
{
failedUpdates = new JArray();
}
else
{
failedUpdates = JArray.Parse(failedUpdatesString);
}
failedUpdates.Add(failedPackage);
Settings.Values[CodePushConstants.FailedUpdatesKey] = JsonConvert.SerializeObject(failedUpdates);
}
internal static void SavePendingUpdate(string packageHash, bool isLoading)
{
var pendingUpdate = new JObject()
{
{ CodePushConstants.PendingUpdateHashKey, packageHash },
{ CodePushConstants.PendingUpdateIsLoadingKey, isLoading }
};
Settings.Values[CodePushConstants.PendingUpdateKey] = JsonConvert.SerializeObject(pendingUpdate);
}
}
}