-
Notifications
You must be signed in to change notification settings - Fork 0
/
AffiseDemo.cs
284 lines (248 loc) · 9.26 KB
/
AffiseDemo.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using AffiseAttributionLib;
using AffiseAttributionLib.Events.Subscription;
using AffiseAttributionLib.Module.Subscription;
using AffiseAttributionLib.Referrer;
using UnityEngine;
using UnityEngine.UIElements;
namespace AffiseDemo
{
public class AffiseDemo : BaseUi
{
private readonly DefaultEventsFactory _eventsFactory = new ();
private readonly AffiseApiFactory _apiFactory = new();
private VisualElement? _productsView;
protected override void Init()
{
_apiFactory.Output = Output;
_apiFactory.Alert = Alert;
AffiseInit();
}
private void AffiseInit()
{
// Initialize https://github.com/affise/sdk-unity#manual
// For manual init delete "Affise Settings.asset" or disable [isActive] flag
// Affise
// .Settings(
// affiseAppId: "129",
// secretKey: "93a40b54-6f12-443f-a250-ebf67c5ee4d2"
// )
// .SetProduction(false) //To enable debug methods set Production to false
// .Start(); // Start Affise SDK
// Debug: network request/response
Affise.Debug.Network((request, response) =>
{
// Debug.Log($"Affise: {request}");
Debug.Log($"Affise: {response}");
});
// Deeplinks https://github.com/affise/sdk-unity#deeplinks
Affise.RegisterDeeplinkCallback(value =>
{
var list = value.Parameters.Select(h => $"{h.Key}=[{string.Join(", ", h.Value)}]").ToList();
var parameters = string.Join(", ", list);
Alert("Deeplink", $"{value.Deeplink}\n\n" +
$"scheme={value.Scheme}\n" +
$"host={value.Host}\n" +
$"path={value.Path}\n" +
$"parameters=[{parameters}]"
);
});
}
protected override void ApiView(VisualElement view)
{
view.Clear();
foreach (var (apiName, apiCall) in _apiFactory.Create())
{
if (apiName == "Get Referrer Value")
{
AddDropdown(view, "", ReferrerValues(), value =>
{
_apiFactory.ReferrerValue = value;
});
}
AddButton(view, apiName,null, apiCall);
}
}
protected override void EventsView(VisualElement view)
{
view.Clear();
foreach (var affiseEvent in _eventsFactory.CreateEvents())
{
var styleClass = (affiseEvent is BaseSubscriptionEvent)
? "affise-subscription-event"
: null;
AddButton(view, ToCamelCase(affiseEvent.GetName()), styleClass, () => {
// Events tracking https://github.com/affise/sdk-unity#events-tracking
// Send event
affiseEvent.Send();
// or
// Affise.SendEvent(affiseEvent);
// or
// affiseEvent.SendNow(() =>
// {
// Output($"Send {affiseEvent.GetName()} success");
// }, (status) =>
// {
// Output($"Send {affiseEvent.GetName()} failed {status}");
// });
});
}
}
#region Store
protected override void OnTabShow(int index)
{
if (index == 2)
{
FetchProducts();
UpdateProductsView(_products);
}
}
private readonly Dictionary<string, AffiseProductType> _storeData = new()
{
{ "com.test.invalid", AffiseProductType.CONSUMABLE },
{ "com.test.prod_1", AffiseProductType.CONSUMABLE },
{ "com.test.prod_2", AffiseProductType.CONSUMABLE },
{ "com.test.prod_3", AffiseProductType.NON_CONSUMABLE },
{ "com.test.sub_1", AffiseProductType.NON_RENEWABLE_SUBSCRIPTION },
{ "com.test.sub_2", AffiseProductType.RENEWABLE_SUBSCRIPTION },
{ "com.test.sub_3", AffiseProductType.RENEWABLE_SUBSCRIPTION },
};
private List<AffiseProduct> _products = new();
protected override void StoreView(VisualElement view)
{
view.Clear();
AddButton(view, "Fetch Products",null, () =>
{
FetchProducts(false);
});
_productsView = new ScrollView();
view.Add(_productsView);
}
private void UpdateProductsView(List<AffiseProduct> affiseProducts)
{
if (_productsView is null) return;
_productsView.Clear();
foreach (var product in affiseProducts)
{
AddProductView(_productsView, product, () =>
{
Purchase(product);
});
}
}
private void AddProductView(VisualElement scrollView, AffiseProduct product, Action buy)
{
AddView(scrollView, (elem) =>
{
var container = new VisualElement
{
style =
{
paddingLeft = 8,
paddingRight = 8,
paddingTop = 8,
paddingBottom = 8,
marginBottom = 8,
borderBottomLeftRadius = 8,
borderBottomRightRadius = 8,
borderTopLeftRadius = 8,
borderTopRightRadius = 8,
flexDirection = FlexDirection.Row,
alignItems = Align.Center,
backgroundColor = new StyleColor(new Color(0.0f, 0.0f, 0.0f, 0.1f))
}
};
var desc = new VisualElement
{
style =
{
flexGrow = 1
}
};
desc.Add(new Label(product.Title)
{
style =
{
color = new StyleColor(Color.white)
}
});
desc.Add(new Label(product.Description)
{
style =
{
color = new StyleColor(Color.gray)
}
});
if (product.Subscription is not null)
{
desc.Add(new Label($"{product.Subscription.NumberOfUnits} {product.Subscription.TimeUnit?.ToValue()}")
{
style =
{
color = new StyleColor(Color.white)
}
});
}
container.Add(desc);
container.Add(new Label(product.Price?.FormattedPrice)
{
style =
{
color = new StyleColor(Color.white)
}
});
AddButton(container, "Buy",null, buy);
elem.Add(container);
});
}
private void FetchProducts(bool skipCheck = true)
{
Affise.Module.FetchProducts(_storeData.Keys.ToList(), (result) =>
{
if (result.IsSuccess)
{
var value = result.AsSuccess;
_products = value.Products;
UpdateProductsView(_products);
var invalidIds = value.InvalidIds;
if (!skipCheck && invalidIds.Count > 0)
{
Alert("Invalid Ids", string.Join("\n", invalidIds));
}
}
else
{
var error = result.AsFailure;
Alert("Error", error);
}
});
}
private void Purchase(AffiseProduct product)
{
var type = _storeData.GetValueOrDefault(product.ProductId);
Affise.Module.Purchase(product, type, (result) =>
{
if (result.IsSuccess)
{
var info = result.AsSuccess;
}
else
{
var error = result.AsFailure;
Alert("Error", error);
}
});
}
#endregion
private static List<string> ReferrerValues()
{
return Enum.GetValues(typeof(ReferrerKey))
.Cast<ReferrerKey>()
.Select(s => s.ToString())
.ToList();
}
}
}