This repository was archived by the owner on Jul 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 610
Expand file tree
/
Copy pathUpgradeViewController.cs
More file actions
153 lines (129 loc) · 5.04 KB
/
UpgradeViewController.cs
File metadata and controls
153 lines (129 loc) · 5.04 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using UIKit;
using Foundation;
using CodeHub.iOS.Services;
using System.Threading.Tasks;
using System.Linq;
using CodeHub.Core.Services;
using BigTed;
using System.Reactive.Disposables;
using CodeHub.WebViews;
using Splat;
namespace CodeHub.iOS.ViewControllers.Application
{
public class UpgradeViewController : BaseWebViewController
{
private readonly IFeaturesService _featuresService
= Locator.Current.GetService<IFeaturesService>();
private readonly IInAppPurchaseService _inAppPurchaseService
= Locator.Current.GetService<IInAppPurchaseService>();
private UIActivityIndicatorView _activityView;
public UpgradeViewController() : base(false, false)
{
Title = "Pro Upgrade";
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
_activityView = new UIActivityIndicatorView
{
Color = Theme.CurrentTheme.PrimaryColor,
AutoresizingMask = UIViewAutoresizing.FlexibleWidth,
};
_activityView.Frame = new CoreGraphics.CGRect(0, 44, View.Frame.Width, 88f);
Load().ToBackground();
}
private async Task Load()
{
Web.UserInteractionEnabled = false;
Web.LoadHtmlString("", NSBundle.MainBundle.BundleUrl);
_activityView.Alpha = 1;
_activityView.StartAnimating();
View.Add(_activityView);
try
{
var response = await _inAppPurchaseService
.RequestProductData(FeaturesService.ProEdition)
.WithTimeout(TimeSpan.FromSeconds(30));
var productData = response.Products.FirstOrDefault();
var enabled = _featuresService.IsProEnabled;
var model = new UpgradeDetailsModel(productData?.LocalizedPrice(), enabled);
var viewModel = new UpgradeDetailsWebView { Model = model };
LoadContent(viewModel.GenerateString());
}
catch (Exception e)
{
AlertDialogService.ShowAlert("Error Loading Upgrades", e.Message);
}
finally
{
Web.UserInteractionEnabled = true;
UIView.Animate(0.2f, 0, UIViewAnimationOptions.BeginFromCurrentState | UIViewAnimationOptions.CurveEaseInOut,
() => _activityView.Alpha = 0, () =>
{
_activityView.RemoveFromSuperview();
_activityView.StopAnimating();
});
}
}
protected override bool ShouldStartLoad(WebKit.WKWebView webView, WebKit.WKNavigationAction navigationAction)
{
var url = navigationAction.Request.Url;
if (url.Scheme.Equals("app"))
{
var func = url.Host;
if (string.Equals(func, "buy", StringComparison.OrdinalIgnoreCase))
{
Activate(_featuresService.ActivatePro).ToBackground();
}
else if (string.Equals(func, "restore", StringComparison.OrdinalIgnoreCase))
{
Activate(_featuresService.RestorePro).ToBackground();
}
return false;
}
if (url.Scheme.Equals("mailto", StringComparison.OrdinalIgnoreCase))
{
UIApplication.SharedApplication.OpenUrl(url);
return false;
}
if (url.Scheme.Equals("file"))
{
return true;
}
if (url.Scheme.Equals("http") || url.Scheme.Equals("https"))
{
var view = new WebBrowserViewController(url.AbsoluteString);
PresentViewController(view, true, null);
return false;
}
return false;
}
private async Task Activate(Func<Task> activation)
{
try
{
BTProgressHUD.ShowContinuousProgress("Activating...", ProgressHUD.MaskType.Gradient);
using (Disposable.Create(BTProgressHUD.Dismiss))
await activation();
Load().ToBackground();
}
catch (Exception e)
{
AlertDialogService.ShowAlert("Error", e.Message);
}
}
public static UpgradeViewController Present(UIViewController parent)
{
var vc = new UpgradeViewController();
var nav = new ThemedNavigationController(vc);
var navObj = new UIBarButtonItem(
UIBarButtonSystemItem.Cancel,
(_, __) => parent.DismissViewController(true, null));
vc.Appearing.Subscribe(_ => vc.NavigationItem.LeftBarButtonItem = navObj);
vc.Disappeared.Subscribe(_ => vc.NavigationItem.LeftBarButtonItem = null);
parent.PresentViewController(nav, true, null);
return vc;
}
}
}