Skip to content
This repository was archived by the owner on Jul 2, 2022. It is now read-only.

Commit 53453a3

Browse files
committed
Refactoring
1 parent 583b6ec commit 53453a3

File tree

10 files changed

+63
-35
lines changed

10 files changed

+63
-35
lines changed

CodeHub.Core/Services/IFeaturesService.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Threading.Tasks;
2+
using System.Collections.Generic;
23

34
namespace CodeHub.Core.Services
45
{
@@ -7,7 +8,15 @@ public interface IFeaturesService
78
bool IsPushNotificationsActivated { get; set; }
89

910
void Activate(string id);
11+
1012
bool IsActivated(string id);
13+
14+
Task<IEnumerable<string>> GetAvailableFeatureIds();
15+
}
16+
17+
public static class FeatureIds
18+
{
19+
public const string PushNotifications = "com.dillonbuchanan.codehub.push";
1120
}
1221
}
1322

CodeHub.Core/ViewModels/Accounts/LoginViewModel.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using System.Threading.Tasks;
88
using CodeHub.Core.Factories;
99
using Cirrious.CrossCore;
10+
using CodeFramework.Core.Services;
11+
using System.Linq;
1012

1113
namespace CodeHub.Core.ViewModels.Accounts
1214
{
@@ -16,6 +18,7 @@ public class LoginViewModel : BaseViewModel
1618
public const string ClientSecret = "9253ab615f8c00738fff5d1c665ca81e581875cb";
1719
public static readonly string RedirectUri = "http://dillonbuchanan.com/";
1820
private readonly ILoginFactory _loginFactory;
21+
private readonly IFeaturesService _featuresService;
1922

2023
private bool _isLoggingIn;
2124
public bool IsLoggingIn
@@ -57,9 +60,10 @@ public ICommand GoBackCommand
5760
get { return new MvxCommand(() => ChangePresentation(new MvxClosePresentationHint(this))); }
5861
}
5962

60-
public LoginViewModel(ILoginFactory loginFactory)
63+
public LoginViewModel(ILoginFactory loginFactory, IFeaturesService featuresService)
6164
{
6265
_loginFactory = loginFactory;
66+
_featuresService = featuresService;
6367
}
6468

6569
public void Init(NavObject navObject)
@@ -112,12 +116,25 @@ public async Task Login(string code)
112116
}
113117

114118
LoginData loginData = null;
119+
bool shouldPromptPush = false;
115120

116121
try
117122
{
118123
IsLoggingIn = true;
119124
var account = AttemptedAccount;
120125
loginData = await _loginFactory.LoginWithToken(ClientId, ClientSecret, code, RedirectUri, WebDomain, apiUrl, account);
126+
127+
if (!_featuresService.IsPushNotificationsActivated && !IsEnterprise)
128+
{
129+
try
130+
{
131+
var ids = await _featuresService.GetAvailableFeatureIds();
132+
shouldPromptPush = ids.Contains(FeatureIds.PushNotifications);
133+
}
134+
catch
135+
{
136+
}
137+
}
121138
}
122139
catch (Exception e)
123140
{
@@ -131,16 +148,11 @@ public async Task Login(string code)
131148

132149
try
133150
{
134-
if (!IsEnterprise)
135-
{
136-
var features = Mvx.Resolve<IFeaturesService>();
137-
if (!features.IsPushNotificationsActivated)
138-
{
139-
await Mvx.Resolve<IFeatureFactory>().PromptPushNotificationFeature();
140-
}
141-
}
151+
// Only prompt if we're allowing that to be enabled.
152+
if (shouldPromptPush)
153+
await Mvx.Resolve<IFeatureFactory>().PromptPushNotificationFeature();
142154
}
143-
catch
155+
catch
144156
{
145157
// Don't do anything...
146158
}

CodeHub.Core/ViewModels/App/UpgradesViewModel.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
using System;
21
using CodeFramework.Core.ViewModels;
32
using System.Threading.Tasks;
4-
using CodeFramework.Core.Services;
5-
using System.Collections.Generic;
3+
using CodeHub.Core.Services;
4+
using System.Linq;
65

76
namespace CodeHub.Core.ViewModels.App
87
{
98
public class UpgradesViewModel : LoadableViewModel
109
{
11-
private readonly IHttpClientService _httpClientService;
12-
private readonly IJsonSerializationService _jsonSerializationService;
10+
private readonly IFeaturesService _featuresService;
1311
private string[] _keys;
1412

1513
public string[] Keys
@@ -22,19 +20,14 @@ private set
2220
}
2321
}
2422

25-
public UpgradesViewModel(IHttpClientService httpClientService, IJsonSerializationService jsonSerializationService)
23+
public UpgradesViewModel(IFeaturesService featuresService)
2624
{
27-
_httpClientService = httpClientService;
28-
_jsonSerializationService = jsonSerializationService;
25+
_featuresService = featuresService;
2926
}
3027

3128
protected override async Task Load(bool forceCacheInvalidation)
3229
{
33-
var client = _httpClientService.Create();
34-
client.Timeout = new TimeSpan(0, 0, 30);
35-
var response = await client.GetAsync("http://162.243.15.10/in-app");
36-
var data = await response.Content.ReadAsStringAsync();
37-
Keys = _jsonSerializationService.Deserialize<List<string>>(data).ToArray();
30+
Keys = (await _featuresService.GetAvailableFeatureIds()).ToArray();
3831
}
3932
}
4033
}

CodeHub.iOS/AppDelegate.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public override bool FinishedLaunching(UIApplication app, NSDictionary options)
9797

9898
// Automatic activations in debug mode!
9999
#if DEBUG
100-
Mvx.Resolve<CodeFramework.Core.Services.IDefaultValueService>().Set(InAppPurchases.PushNotificationsId, true);
100+
Mvx.Resolve<CodeFramework.Core.Services.IDefaultValueService>().Set(FeatureIds.PushNotifications, true);
101101
#endif
102102

103103

@@ -121,7 +121,7 @@ void HandlePurchaseSuccess (object sender, string e)
121121
{
122122
Mvx.Resolve<CodeFramework.Core.Services.IDefaultValueService>().Set(e, true);
123123

124-
if (string.Equals(e, InAppPurchases.PushNotificationsId))
124+
if (string.Equals(e, FeatureIds.PushNotifications))
125125
{
126126
const UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge;
127127
UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);

CodeHub.iOS/InAppPurchases.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ namespace CodeHub.iOS
88
public class InAppPurchases
99
{
1010
private static InAppPurchases _instance;
11-
public const string PushNotificationsId = "com.dillonbuchanan.codehub.push";
1211
private readonly TransactionObserver _observer;
1312

1413
public event EventHandler<string> PurchaseSuccess;

CodeHub.iOS/Services/FeaturesService.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
11
using CodeHub.Core.Services;
22
using CodeFramework.Core.Services;
3+
using System.Collections.Generic;
4+
using System;
5+
using System.Threading.Tasks;
36

47
namespace CodeHub.iOS.Services
58
{
69
public class FeaturesService : IFeaturesService
710
{
811
private readonly IDefaultValueService _defaultValueService;
12+
private readonly IHttpClientService _httpClientService;
13+
private readonly IJsonSerializationService _jsonSerializationService;
914

10-
public FeaturesService(IDefaultValueService defaultValueService)
15+
16+
public FeaturesService(IDefaultValueService defaultValueService, IHttpClientService httpClientService, IJsonSerializationService jsonSerializationService)
1117
{
1218
_defaultValueService = defaultValueService;
19+
_httpClientService = httpClientService;
20+
_jsonSerializationService = jsonSerializationService;
1321
}
1422

1523
public bool IsPushNotificationsActivated
1624
{
1725
get
1826
{
19-
return IsActivated(InAppPurchases.PushNotificationsId);
27+
return IsActivated(FeatureIds.PushNotifications);
2028
}
2129
set
2230
{
23-
_defaultValueService.Set(InAppPurchases.PushNotificationsId, value);
31+
_defaultValueService.Set(FeatureIds.PushNotifications, value);
2432
}
2533
}
2634

@@ -34,6 +42,15 @@ public bool IsActivated(string id)
3442
bool value;
3543
return _defaultValueService.TryGet<bool>(id, out value) && value;
3644
}
45+
46+
public async Task<IEnumerable<string>> GetAvailableFeatureIds()
47+
{
48+
var client = _httpClientService.Create();
49+
client.Timeout = new TimeSpan(0, 0, 15);
50+
var response = await client.GetAsync("http://push.codehub-app.com/in-app");
51+
var data = await response.Content.ReadAsStringAsync();
52+
return _jsonSerializationService.Deserialize<List<string>>(data).ToArray();
53+
}
3754
}
3855
}
3956

CodeHub.iOS/Views/App/EnablePushNotificationsViewController.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
using System;
22
using System.Drawing;
3-
using MonoTouch.Foundation;
43
using MonoTouch.UIKit;
54
using Cirrious.CrossCore;
65
using CodeHub.Core.Services;
76
using CodeFramework.iOS.Utils;
8-
using CodeFramework.Core.Services;
97

108
namespace CodeHub.iOS.Views.App
119
{
@@ -90,7 +88,7 @@ private void EnablePushNotifications(object sender, EventArgs e)
9088
{
9189
_hud.Show("Enabling...");
9290
var featureService = Mvx.Resolve<IFeaturesService>();
93-
featureService.Activate(InAppPurchases.PushNotificationsId);
91+
featureService.Activate(FeatureIds.PushNotifications);
9492
}
9593
}
9694
}

CodeHub.iOS/Views/Gists/GistView.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private void ShareButtonPress()
128128
ViewModel.GoToHtmlUrlCommand.Execute(null);
129129
}
130130
}
131-
catch (Exception ex)
131+
catch
132132
{
133133
}
134134
};

CodeHub.iOS/Views/Source/ChangesetView.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ private void ShowExtraMenu()
196196
// ViewModel.GoToHtmlUrlCommand.Execute(null);
197197
// }
198198
}
199-
catch (Exception ex)
199+
catch
200200
{
201201
}
202202
};

CodeHub.iOS/Views/Source/FileSourceView.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ private void ShowExtraMenu()
7676
ViewModel.GoToHtmlUrlCommand.Execute(null);
7777
}
7878
}
79-
catch (Exception ex)
79+
catch
8080
{
8181
}
8282
};

0 commit comments

Comments
 (0)