forked from bitstadium/HockeySDK-Windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FeedbackManager.cs
228 lines (202 loc) · 8.52 KB
/
FeedbackManager.cs
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using System.Windows.Navigation;
using HockeyApp.Model;
using HockeyApp.Tools;
using System;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Windows;
namespace HockeyApp
{
public class FeedbackManager
{
private static readonly FeedbackManager instance = new FeedbackManager();
private string identifier = null;
private Application application = null;
private string usernameInitial;
private string emailInitial;
static FeedbackManager() { }
private FeedbackManager() { }
public static FeedbackManager Instance
{
get
{
return instance;
}
}
/// <summary>
/// Optional. Only needed if you don't configured a CrashHandler
/// </summary>
/// <param name="application"></param>
/// <param name="appIdentifier"></param>
public void Configure(Application application, string appIdentifier, string toptitle = null, string initialUsername = null, string initialEmail = null)
{
if (this.application == null)
{
this.identifier = appIdentifier;
this.application = application;
this.FeedbackPageTopTitle = toptitle;
this.usernameInitial = initialUsername;
this.emailInitial = initialEmail;
}
else
{
throw new InvalidOperationException("FeedbackManager was already configured!");
}
}
public string FeedbackPageTopTitle { get; private set; }
internal string AppIdentitfier { get { return this.identifier ?? CrashHandler.Instance.AppIdentitfier; } }
internal Application Application { get { return this.application ?? CrashHandler.Instance.Application; } }
public void NavigateToFeedbackUI(NavigationService navigationService)
{
navigationService.Navigate(new Uri("/HockeySDK;component/Views/FeedbackPage.xaml", UriKind.Relative));
}
private string threadToken;
public string ThreadToken
{
get
{
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
return settings.GetValue(Constants.FeedbackThreadKey) as string;
}
set { threadToken = value; }
}
public bool IsThreadOpen
{
get
{
return this.ThreadToken != null;
}
}
/// <summary>
///
/// </summary>
/// <returns>FeedbackThread or null if the thread got deleted</returns>
/// <exception cref="ApplicationException"></exception>
public async Task<FeedbackThread> GetActiveThreadAsync()
{
var request = WebRequest.CreateHttp(new Uri(Constants.ApiBase + "apps/" + this.AppIdentitfier + "/feedback/" + this.ThreadToken + ".json", UriKind.Absolute));
request.Method = HttpMethod.Get;
request.Headers[HttpRequestHeader.UserAgent] = Constants.UserAgentString;
try
{
var response = await request.GetResponseTaskAsync();
var fbResp = await TaskEx.Run<FeedbackResponseSingle>(() => FeedbackResponseSingle.FromJson(response.GetResponseStream()));
if (fbResp.status.Equals("success"))
{
return fbResp.feedback;
}
else
{
throw new Exception("Server error. Server returned status " + fbResp.status);
}
}
catch (Exception e)
{
var webex = e.InnerException as WebException;
if (webex != null)
{
if (webex.Response.ContentType.IsEmpty())
{
//Connection error during call
throw webex;
}
else
{
//404 Response from server => thread got deleted
ForgetThreadInfos();
return null;
}
}
else
{
throw;
}
}
}
public FeedbackThreadMetaInfos ThreadMetaInfos {
get
{
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
return new FeedbackThreadMetaInfos(
settings.GetValue(Constants.FeedbackThreadSubjectKey) as string,
settings.GetValue(Constants.FeedbackUsernameKey) as string ?? this.usernameInitial ?? CrashHandler.Instance.UserId,
settings.GetValue(Constants.FeedbackEmailKey) as string ?? this.emailInitial ?? CrashHandler.Instance.ContactInfo,
settings.GetValue(Constants.FeedbackThreadKey) as string);
}
}
protected void ForgetThreadInfos()
{
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
settings.RemoveValue(Constants.FeedbackThreadKey);
settings.RemoveValue(Constants.FeedbackThreadSubjectKey);
settings.Save();
}
protected void PersistThreadMetaInfos(FeedbackResponseSingle fbResponse, string user, string email)
{
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
settings.SetValue(Constants.FeedbackThreadKey, fbResponse.token);
settings.SetValue(Constants.FeedbackThreadSubjectKey, fbResponse.feedback.messages.First().subject);
settings.SetValue(Constants.FeedbackEmailKey, email);
settings.SetValue(Constants.FeedbackUsernameKey, user);
settings.Save();
}
protected async Task<FeedbackResponseSingle> PostAnswerAsync(FeedbackMessage message)
{
var request = WebRequest.CreateHttp(new Uri(Constants.ApiBase + "apps/" + this.AppIdentitfier + "/feedback/" + this.ThreadToken + "/", UriKind.Absolute));
request.Method = HttpMethod.Put;
request.Headers[HttpRequestHeader.UserAgent] = Constants.UserAgentString;
await request.SetPostDataAsync(message.SerializeToWwwForm());
var response = await request.GetResponseTaskAsync();
var fbResp = await TaskEx.Run<FeedbackResponseSingle>(() => FeedbackResponseSingle.FromJson(response.GetResponseStream()));
if (fbResp.status.Equals("success"))
{
return fbResp;
}
else
{
throw new Exception("Server error. Server returned status " + fbResp.status);
}
}
protected async Task<FeedbackResponseSingle> StartThreadAsync(FeedbackMessage message)
{
var request = WebRequest.CreateHttp(new Uri(Constants.ApiBase + "apps/" + this.AppIdentitfier + "/feedback", UriKind.Absolute));
request.Method = HttpMethod.Post;
request.Headers[HttpRequestHeader.UserAgent] = Constants.UserAgentString;
await request.SetPostDataAsync(message.SerializeToWwwForm());
var response = await request.GetResponseTaskAsync();
var fbResp = await TaskEx.Run<FeedbackResponseSingle>(() => FeedbackResponseSingle.FromJson(response.GetResponseStream()));
if (fbResp.status.Equals("success"))
{
return fbResp;
}
else
{
throw new Exception("Server error. Server returned status " + fbResp.status);
}
}
internal async Task<FeedbackMessage> SendFeedback(FeedbackMessage msg, string user = null, string email = null)
{
if (this.IsThreadOpen)
{
var fbResponse = await this.PostAnswerAsync(msg);
if (fbResponse != null)
{
return fbResponse.feedback.messages.Last();
}
}
else
{
var fbResponse = await this.StartThreadAsync(msg);
if (fbResponse != null)
{
this.ThreadToken = fbResponse.token;
this.PersistThreadMetaInfos(fbResponse, user, email);
return fbResponse.feedback.messages.Last();
}
}
return null;
}
}
}