Skip to content

Commit

Permalink
Enable to cancel ChatdollCamera and RequestProviders using camera
Browse files Browse the repository at this point in the history
Make it possible to cancel RequestProviders below when cancel word is detected by WakeWordListener:

- CameraRequestProvider
- QRCodeRequestProvider
  • Loading branch information
uezo committed Jul 31, 2020
1 parent e0ff6e0 commit 9bffcd1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion ChatdollKit/Scripts/Dialog/CameraRequestProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task<Request> GetRequestAsync(User user, Context context, Cancellat

if (chatdollCamera != null)
{
payloads.Add(await chatdollCamera.CaptureTextureWithTimerAsync(CameraCaption, SelfTimerSeconds));
payloads.Add(await chatdollCamera.CaptureTextureWithTimerAsync(CameraCaption, SelfTimerSeconds, token));
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion ChatdollKit/Scripts/Dialog/QRCodeRequestProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task<Request> GetRequestAsync(User user, Context context, Cancellat

if (chatdollCamera != null)
{
payloads.Add(await chatdollCamera.ReadCodeAsync());
payloads.Add(await chatdollCamera.ReadCodeAsync(token));
}
else
{
Expand Down
27 changes: 21 additions & 6 deletions ChatdollKit/Scripts/IO/ChatdollCamera.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
Expand Down Expand Up @@ -278,7 +279,7 @@ public void SetCaption(string caption)
}

// Self-timer
public async Task<Texture2D> CaptureTextureWithTimerAsync(string caption, int timerSeconds)
public async Task<Texture2D> CaptureTextureWithTimerAsync(string caption, int timerSeconds, CancellationToken token)
{
if (!Launch(caption))
{
Expand All @@ -296,9 +297,18 @@ public async Task<Texture2D> CaptureTextureWithTimerAsync(string caption, int ti

for (var i = timerSeconds; i > 0; i--)
{
if (token.IsCancellationRequested)
{
break;
}
selfTimerCounter.text = i.ToString();
OnTimer?.Invoke(i);
await Task.Delay(1000);
await Task.Delay(1000, token);
}

if (token.IsCancellationRequested)
{
return null;
}
selfTimerCounter.text = string.Empty;

Expand All @@ -308,7 +318,12 @@ public async Task<Texture2D> CaptureTextureWithTimerAsync(string caption, int ti
{
// Preview captured photo when preview time is longer than zero
previewWindow.texture = photo;
await Task.Delay((int)(PreviewTime * 1000));
await Task.Delay((int)(PreviewTime * 1000), token);
}

if (token.IsCancellationRequested)
{
return null;
}

return photo;
Expand All @@ -330,7 +345,7 @@ public async Task<Texture2D> CaptureTextureWithTimerAsync(string caption, int ti
}

// QRCode/Barcode reader
public async Task<string> ReadCodeAsync()
public async Task<string> ReadCodeAsync(CancellationToken token)
{
if (DecodeCode == null)
{
Expand All @@ -354,8 +369,8 @@ public async Task<string> ReadCodeAsync()
}

var startTime = Time.time;
// Exit loop when camera is closed
while (IsAlreadyLaunched)
// Exit loop when camera is closed or cancel requested
while (IsAlreadyLaunched && !token.IsCancellationRequested)
{
var texture = GetTexture();
result = DecodeCode(texture);
Expand Down

0 comments on commit 9bffcd1

Please sign in to comment.