Skip to content

Commit e6e9e3e

Browse files
committed
Add RequestButton example
Fix debounce timer Adjust QR Code textures
1 parent e84ca41 commit e6e9e3e

4 files changed

Lines changed: 204 additions & 31 deletions

File tree

Allow2/Allow2.cs

Lines changed: 154 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static class Allow2
4141
//HashSet<string> checkers = new HashSet<string>(); // contains uuids for running autocheckers (abort if your uuid is missing)
4242
static string checkerUuid = null; // uuid for the current checker
4343
static IEnumerator checker = null; // the current autochecker
44-
static Coroutine qrCall = null;
44+
static IEnumerator qrCall = null;
4545
static DateTime qrDebounce = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
4646
static string pairingUuid = null;
4747

@@ -112,6 +112,13 @@ public static string Timezone
112112
}
113113
}
114114

115+
/// <summary>
116+
/// Gets or sets the device token.
117+
/// The device token is mandatory, this needs to be set before making any calls to the sdk/api.
118+
/// Generate your device token for free at https://developer.allow2.com
119+
/// Use it to manage your app/game/device, promote it on the Allow2 platform and track downloads and usage.
120+
/// </summary>
121+
/// <value>The device token.</value>
115122
public static string DeviceToken
116123
{
117124
get
@@ -140,8 +147,15 @@ private static void Persist()
140147
// no persistence here
141148
private static Dictionary<string, Allow2CheckResult> resultCache = new Dictionary<string, Allow2CheckResult>();
142149

150+
/// <summary>
151+
/// A result closure provides the result from a call to the Allow2 platform.
152+
/// </summary>
143153
public delegate void resultClosure(string err, Allow2CheckResult result);
144-
public delegate void imageClosure(string err, Texture qrCode);
154+
155+
/// <summary>
156+
/// An image closure provides the image returned by the Allow2 platform.
157+
/// </summary>
158+
public delegate void imageClosure(string err, Texture2D image);
145159

146160
static Allow2()
147161
{
@@ -186,17 +200,21 @@ static IEnumerator CheckForBrokenPairing()
186200
}
187201
}
188202

189-
/*
190-
* Pairing
191-
*
192-
*/
193-
public static void Pair(MonoBehaviour behavior,
203+
/// <summary>
204+
/// Pair your game/app/device to a parents Allow2 account.
205+
/// </summary>
206+
/// <param name="behaviour">Provide a (any) MonoBehaviour for the sdk to use to call the platform.</param>
207+
/// <param name="user">The email address of the Allow2 account being paired.</param>
208+
/// <param name="pass">The associated password for the Allow2 account being paired.</param>
209+
/// <param name="deviceName">The name the user would like to use to identify this app/game/device.</param>
210+
/// <param name="callback">Provides the image of the QR Code.</param>
211+
public static void Pair(MonoBehaviour behaviour,
194212
string user, // ie: "fred@gmail.com",
195213
string pass, // ie: "my super secret password",
196214
string deviceName, // ie: "Fred's iPhone"
197-
resultClosure callback )
215+
resultClosure callback)
198216
{
199-
behavior.StartCoroutine(_Pair(user, pass, deviceName, callback));
217+
behaviour.StartCoroutine(_Pair(user, pass, deviceName, callback));
200218
}
201219

202220
static IEnumerator _Pair(string user, // ie: "fred@gmail.com",
@@ -270,37 +288,46 @@ resultClosure callback
270288

271289
const int QRDebounceDelay = 500;
272290

273-
public static void GetQR(MonoBehaviour behavior, string name, imageClosure callback) {
274-
DateTime now = new DateTime();
291+
/// <summary>
292+
/// Gets a new QR Code texture to show to the user to enable them to pair your game/app/device with Allow2.
293+
/// Call this to get a new QR Code any time the user changes the name of the device.
294+
/// Note this is debounced automatically, so just keep calling it immediately (even if the user is still typing).
295+
/// </summary>
296+
/// <param name="behaviour">Provide a (any) MonoBehaviour for the sdk to use to call the platform.</param>
297+
/// <param name="deviceName">The name the user would like to use to identify this app/game/device.</param>
298+
/// <param name="callback">Callback.</param>
299+
public static void GetQR(MonoBehaviour behaviour, string deviceName, imageClosure callback) {
300+
DateTime now = DateTime.Now;
275301
Debug.Log(qrDebounce.CompareTo(now));
276302
if ((qrCall != null) && (qrDebounce.CompareTo(now) > 0)) {
277-
Debug.Log("debounce");
278-
Coroutine oldCall = qrCall;
303+
Debug.Log("debounce " + qrDebounce.ToShortTimeString() + " < " + now.ToShortTimeString());
304+
IEnumerator oldCall = qrCall;
279305
qrCall = null;
280-
behavior.StopCoroutine(oldCall);
306+
behaviour.StopCoroutine(oldCall);
281307
}
282308
qrDebounce = now.AddMilliseconds(QRDebounceDelay);
283-
qrCall = behavior.StartCoroutine(_GetQR(name, callback));
309+
qrCall = _GetQR(deviceName, callback);
310+
behaviour.StartCoroutine(qrCall);
284311
}
285312

286-
static IEnumerator _GetQR(string name, imageClosure callback)
313+
static IEnumerator _GetQR(string deviceName, imageClosure callback)
287314
{
288315
yield return new WaitForSeconds(QRDebounceDelay/1000);
289316
string qrURL = ApiUrl + "/genqr/" +
290317
UnityWebRequest.EscapeURL(_deviceToken) + "/" +
291318
UnityWebRequest.EscapeURL(uuid) + "/" +
292-
UnityWebRequest.EscapeURL(name);
319+
UnityWebRequest.EscapeURL(deviceName);
293320
UnityWebRequest www = UnityWebRequestTexture.GetTexture(qrURL);
294321
yield return www.SendWebRequest();
295322

296323
if (www.isNetworkError || www.isHttpError)
297324
{
298-
Debug.Log(www.error);
325+
Debug.Log("QR LOAD ERROR: " + www.error);
299326
Texture errorImage = Resources.Load("Allow2/QRError") as Texture2D;
300327
callback(www.error, null);
301328
yield break;
302329
}
303-
Texture qrCode = ((DownloadHandlerTexture)www.downloadHandler).texture;
330+
Texture2D qrCode = DownloadHandlerTexture.GetContent(www);
304331
callback(null, qrCode);
305332
}
306333

@@ -325,6 +352,17 @@ private static Dictionary<int, string> ParseChildren(JSONNode json)
325352
return children;
326353
}
327354

355+
/// <summary>
356+
/// Check if the specified child can use the current activities and optionally log usage.
357+
/// Note that if you specify log as true, usage will be recorded even if the child is technically not allowed to use one of the
358+
/// <paramref name="activities"/>. This is to allow you the ability to be flexible in allowing usage, but should be used sparingly.
359+
/// If you are, for instance, just checking if something CAN be done at this time, then make sure you supply false for the log parameter.
360+
/// </summary>
361+
/// <param name="behaviour">Provide a (any) MonoBehaviour for the sdk to use to call the platform.</param>
362+
/// <param name="childId">Id of the child for which you wish to check (and possibly log) activities.</param>
363+
/// <param name="activities">The activity ids to check.</param>
364+
/// <param name="callback">Provides the result of the check.</param>
365+
/// <param name="log">If set to <c>true</c>, then log the usage of these activities as well.</param>
328366
public static void Check(MonoBehaviour behaviour,
329367
int childId, // childId == 0 ? Get Updated Child List and confirm Pairing
330368
int[] activities,
@@ -335,6 +373,26 @@ public static void Check(MonoBehaviour behaviour,
335373
behaviour.StartCoroutine(_Check(null, childId, activities, callback, log));
336374
}
337375

376+
/// <summary>
377+
/// Check if the specified child can use the current activities and optionally log usage.
378+
/// You should ALWAYS log usage when the child is using the activities, otherwise their usage will not be debited from their quota.
379+
/// Note that if you specify log as true, usage will be recorded even if the child is technically not allowed to use one of the
380+
/// <paramref name="activities"/>. This is to allow you the ability to be flexible in allowing usage, but should be used sparingly.
381+
/// If you are, for instance, just checking if something CAN be done at this time, then make sure you supply false for the log parameter.
382+
/// </summary>
383+
/// <param name="behaviour">Provide a (any) MonoBehaviour for the sdk to use to call the platform.</param>
384+
/// <param name="activities">The activity ids to check.</param>
385+
/// <param name="callback">Provides the result of the check.</param>
386+
/// <param name="log">If set to <c>true</c>, then log the usage of these activities as well.</param>
387+
public static void Check(MonoBehaviour behaviour,
388+
int[] activities,
389+
resultClosure callback,
390+
bool log = false
391+
)
392+
{
393+
behaviour.StartCoroutine(_Check(null, childId, activities, callback, log));
394+
}
395+
338396
static IEnumerator _Check(string myUuid,
339397
int childId, // childId == 0 ? Get Updated Child List and confirm Pairing
340398
int[] activities,
@@ -484,7 +542,21 @@ private static IEnumerator CheckLoop(string myUuid,
484542
}
485543
}
486544

487-
public static void StartChecking(MonoBehaviour behavior,
545+
/// <summary>
546+
/// Start checking (and optionally logging) the ability for the child to use the given activities.
547+
/// This starts a process that regularly checks (and optionally logs) usage until stopped using Allow2.StopChecking.
548+
/// You can call this repeatedly and change the child id at any time, but there will only ever be one process and
549+
/// it will continue to use the last provided child id.
550+
/// Note, that if the child is unable or disallowed to use any of the <paramref name="activities"/>, they will still be continually checked/logged until Allow2.StopChecking() is called.
551+
/// This is to allow you to selectively allow the child to finish an activity, but will put them in negative credit (which will come off future usage).
552+
/// You should ALWAYS log usage when the child is using the activities, otherwise their usage will not be debited from their quota.
553+
/// </summary>
554+
/// <param name="behaviour">Provide a (any) MonoBehaviour for the sdk to use to call the platform.</param>
555+
/// <param name="childId">The child for which the activites are being checked (and optionally logged).</param>
556+
/// <param name="activities">The activity ids to check.</param>
557+
/// <param name="callback">Provides the result of the check.</param>
558+
/// <param name="log">If set to <c>true</c>, then log the usage of these activities as well.</param>
559+
public static void StartChecking(MonoBehaviour behaviour,
488560
int childId,
489561
int[] activities,
490562
resultClosure callback,
@@ -496,20 +568,62 @@ public static void StartChecking(MonoBehaviour behavior,
496568
if (changed || (checkerUuid == null)) {
497569
//switch checker
498570
checkerUuid = System.Guid.NewGuid().ToString(); // this will abort the current checker and kill it.
499-
/*checker = */ behavior.StartCoroutine(CheckLoop(checkerUuid, childId, activities, callback, log));
571+
/*checker = */ behaviour.StartCoroutine(CheckLoop(checkerUuid, childId, activities, callback, log));
500572
}
501573
}
502574

575+
/// <summary>
576+
/// Stop checking (and logging) usage.
577+
/// If there is no current checking/logging process started with Allow2.StartChecking(), this call has no effect.
578+
/// </summary>
503579
public static void StopChecking()
504580
{
505581
checkerUuid = null; // this will kill the running checker
506582
}
507583

508-
public static IEnumerator Request(
509-
int dayTypeId,
510-
int[] lift,
511-
string message,
512-
resultClosure callback)
584+
/// <summary>
585+
/// Submit a request on behalf of the current child.
586+
/// </summary>
587+
/// <returns>Results of the request.</returns>
588+
/// <param name="behaviour">Provide a (any) MonoBehaviour for the sdk to use to call the platform.</param>
589+
/// <param name="childId">The Id of the child making the request.</param>
590+
/// <param name="dayTypeId">(optional)The Id of the day type they are requesting.</param>
591+
/// <param name="lift">(optional) An Array of ids for Bans they are asking to be lifted.</param>
592+
/// <param name="message">(optional) Message to send with the request.</param>
593+
/// <param name="callback">callback that will return the response success or error.</param>
594+
public static void Request(MonoBehaviour behaviour,
595+
int childId,
596+
int dayTypeId,
597+
int[] lift,
598+
string message,
599+
resultClosure callback)
600+
{
601+
behaviour.StartCoroutine(_Request(childId, dayTypeId, lift, message, callback));
602+
}
603+
604+
/// <summary>
605+
/// Submit a request on behalf of the current child.
606+
/// </summary>
607+
/// <returns>Results of the request.</returns>
608+
/// <param name="behaviour">Provide a (any) MonoBehaviour for the sdk to use to call the platform.</param>
609+
/// <param name="dayTypeId">(optional)The Id of the day type they are requesting.</param>
610+
/// <param name="lift">(optional) An Array of ids for Bans they are asking to be lifted.</param>
611+
/// <param name="message">(optional) Message to send with the request.</param>
612+
/// <param name="callback">callback that will return the response success or error.</param>
613+
public static void Request(MonoBehaviour behaviour,
614+
int dayTypeId,
615+
int[] lift,
616+
string message,
617+
resultClosure callback)
618+
{
619+
behaviour.StartCoroutine(_Request(childId, dayTypeId, lift, message, callback));
620+
}
621+
622+
static IEnumerator _Request(int childId,
623+
int dayTypeId,
624+
int[] lift,
625+
string message,
626+
resultClosure callback)
513627
{
514628
if (!IsPaired)
515629
{
@@ -554,13 +668,25 @@ public static IEnumerator Request(
554668
}
555669
}
556670

557-
public static void StartPairing(MonoBehaviour behavior, resultClosure callback)
671+
/// <summary>
672+
/// Use this routine to notify Allow2 you are starting a pairing session for a QR Code pairing.
673+
/// Call this when you are about to display a QR code to the user to allow them to pair with ALlow2.
674+
/// Get the appropriate QR Code using Allow2.GetQR().
675+
///
676+
/// </summary>
677+
/// <param name="behaviour">Provide a (any) MonoBehaviour for the sdk to use to call the platform.</param>
678+
/// <param name="callback">Callback that will return response success or error</param>
679+
public static void StartPairing(MonoBehaviour behaviour, resultClosure callback)
558680
{
559681
//switch checker
560682
pairingUuid = System.Guid.NewGuid().ToString(); // this will abort the current poll and kill it.
561-
behavior.StartCoroutine(PairingLoop(pairingUuid, callback));
683+
behaviour.StartCoroutine(PairingLoop(pairingUuid, callback));
562684
}
563685

686+
/// <summary>
687+
/// Tell Allow2 the QR Code for pairing is no longer being displayed.
688+
/// Call this when you stop showing the QR code and therefore the user can no longer scan it.
689+
/// </summary>
564690
public static void StopPairing()
565691
{
566692
pairingUuid = null; // this will kill the running checker

Allow2/Examples/DeviceNameInput.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ void Awake()
3232

3333
public void InputValueChanged(string input)
3434
{
35-
Allow2.GetQR(this, input, delegate (string err, Texture qrCode)
35+
Allow2.GetQR(this, input, delegate (string err, Texture2D qrCode)
3636
{
37-
Debug.Log("qrcode error: " + (err ?? "No Error") + " : " + (qrCode ? "yes" : "no"));
37+
Debug.Log("qrcode error: " + (err ?? "No Error") + " : " + (qrCode != null ? qrCode.dimension.ToString() : "no"));
3838
qrImage.GetComponent<RawImage>().texture = qrCode;
3939
});
4040
}

Allow2/Examples/RequestButton.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// Copyright (C) 2019 Allow2
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
// the License.
6+
//
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
12+
// an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
//
14+
// See the License for the specific language governing permissions and limitations under the License.
15+
//
16+
namespace Allow2.Allow2Examples
17+
{
18+
19+
using UnityEngine;
20+
21+
public class RequestButton : MonoBehaviour
22+
{
23+
24+
public int dayTypeId = 23;
25+
public int[] bansToLift = {};
26+
27+
public void Request()
28+
{
29+
Debug.Log("Request");
30+
Allow2.childId = 68;
31+
Allow2.Request(
32+
this,
33+
dayTypeId,
34+
bansToLift,
35+
"test",
36+
delegate (string err, Allow2CheckResult result)
37+
{
38+
Debug.Log("Request Error" + err);
39+
if (result != null)
40+
{
41+
Debug.Log(result.Explanation);
42+
}
43+
});
44+
}
45+
}
46+
}

Allow2/Examples/SceneClass.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ void Awake()
4242
Debug.Log("Children: " + Allow2.Children);
4343

4444
// and in the pairing interface, we need a QR code to make the process simple for our users
45-
Allow2.GetQR(this, SystemInfo.deviceName, delegate (string err, Texture qrCode)
45+
Allow2.GetQR(this, SystemInfo.deviceName, delegate (string err, Texture2D qrCode)
4646
{
4747
Debug.Log("qrcode error: " + (err ?? "No Error") + " : " + (qrCode ? "yes" : "no"));
48+
Debug.Log(qrImage.GetComponent<RawImage>());
4849
qrImage.GetComponent<RawImage>().texture = qrCode;
4950
});
5051
}

0 commit comments

Comments
 (0)