-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathAppDevice.cs
110 lines (98 loc) · 2.91 KB
/
AppDevice.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
using System;
using System.Threading;
using System.Threading.Tasks;
using EduCATS.Helpers.Devices.Interfaces;
using EduCATS.Helpers.Speech;
using EduCATS.Themes;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace EduCATS.Helpers.Devices
{
/// <summary>
/// <see cref="IDevice"/> implementation.
/// </summary>
public class AppDevice : IDevice
{
/// <summary>
/// Open url.
/// </summary>
/// <param name="url">Url to open.</param>
/// <returns>Task.</returns>
public async Task OpenUri(string url)
{
await Browser.OpenAsync(url, new BrowserLaunchOptions {
LaunchMode = BrowserLaunchMode.SystemPreferred,
TitleMode = BrowserTitleMode.Show,
PreferredToolbarColor = Color.FromHex(Theme.Current.AppNavigationBarBackgroundColor),
PreferredControlColor = Color.FromHex(Theme.Current.BaseAppColor)
});
}
/// <summary>
/// Get app data directory path.
/// </summary>
/// <remarks>Used to store cache files.</remarks>
/// <returns>App data directory.</returns>
public string GetAppDataDirectory() =>
FileSystem.AppDataDirectory;
/// <summary>
/// Invoke on main thread.
/// </summary>
/// <param name="action">Action to invoke.</param>
public void MainThread(Action action) =>
Device.BeginInvokeOnMainThread(action);
/// <summary>
/// Set timer.
/// </summary>
/// <param name="interval">Interval.</param>
/// <param name="callback">Callback.</param>
public void SetTimer(TimeSpan interval, Func<bool> callback) =>
Device.StartTimer(interval, callback);
/// <summary>
/// Share file.
/// </summary>
/// <param name="title">Title.</param>
/// <param name="filePath">File path.</param>
/// <returns>Task.</returns>
public async Task ShareFile(string title, string filePath) => await Share.RequestAsync(
new ShareFileRequest {
Title = title,
File = new ShareFile(filePath)
});
/// <summary>
/// Get application version.
/// </summary>
/// <returns>Application version.</returns>
public string GetVersion() => AppInfo.VersionString;
/// <summary>
/// Get application build.
/// </summary>
/// <returns>Application build.</returns>
public string GetBuild() => AppInfo.BuildString;
/// <summary>
/// Speech cancellation token source.
/// </summary>
CancellationTokenSource _speechCancellationSource;
/// <summary>
/// Text-to-speech.
/// </summary>
/// <param name="text">Text.</param>
/// <returns>Task.</returns>
public async Task Speak(string text)
{
_speechCancellationSource = new CancellationTokenSource();
var options = await SpeechController.GetSettings();
await TextToSpeech.SpeakAsync(text, options, _speechCancellationSource.Token);
}
/// <summary>
/// Cancel speech.
/// </summary>
public void CancelSpeech()
{
if (_speechCancellationSource?.IsCancellationRequested ?? true) {
return;
}
_speechCancellationSource.Cancel();
_speechCancellationSource.Dispose();
}
}
}