Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
IntentService.cs
1using System.Collections.Concurrent;
2using System.Threading.Tasks;
3using ExCSS;
5using Waher.Events;
7
9{
30 [Singleton]
32 {
33 // Thread-safe queue for storing incoming intents. Usually there will always be only one or none intents in the queue.
34 // ConcurrentQueue might not be needed, but this is not performance-critical code.
35 private ConcurrentQueue<AppIntent> intentQueue = new();
36
41 public void QueueIntent(AppIntent intent)
42 {
43 try
44 {
45 this.intentQueue.Enqueue(intent);
46 }
47 catch (Exception Ex)
48 {
49 ServiceRef.LogService.LogException(Ex);
50 }
51 }
52
58 public async Task ProcessQueuedIntentsAsync()
59 {
60 try
61 {
62 while (this.intentQueue.TryDequeue(out AppIntent? Intent))
63 {
64 await this.ProcessIntentAsync(Intent);
65 }
66 }
67 catch (Exception Ex)
68 {
69 ServiceRef.LogService.LogException(Ex);
70 }
71 }
72
78 public async Task ProcessIntentAsync(AppIntent intent)
79 {
80 try
81 {
82 switch (intent.Action)
83 {
85 if (!string.IsNullOrEmpty(intent.Data))
86 {
87 // Process a url.
88 if (App.Current is not null)
89 await App.Current.InitCompleted;
90 App.OpenUrlSync(intent.Data);
91 }
92 break;
93
95 if (intent.Payload is NfcTag NfcTag)
96 {
97 // Resolve your shared NFC service and pass the NFC tag.
98 INfcService NfcService = App.Instantiate<INfcService>();
99 await NfcService.TagDetected(NfcTag);
100 }
101 break;
102
103 default:
104 ServiceRef.LogService.LogWarning($"Unhandled intent action: {intent.Action}");
105 break;
106 }
107 }
108 catch (Exception Ex)
109 {
110 ServiceRef.LogService.LogException(Ex);
111 }
112 }
113
117 public async Task<string?> TryGetAndDequeueOnboardingUrl()
118 {
119 ConcurrentQueue<AppIntent> newIntents = new();
120
121 string? OnboardingUrl = null;
122
123 while (this.intentQueue.TryDequeue(out AppIntent? Intent))
124 {
125 if (Intent.Action == Constants.IntentActions.OpenUrl)
126 {
127 if (!string.IsNullOrEmpty(Intent.Data) && string.Equals(Constants.UriSchemes.GetScheme(Intent.Data), Constants.UriSchemes.Onboarding, StringComparison.OrdinalIgnoreCase))
128 {
129 OnboardingUrl = Intent.Data;
130 continue;
131 }
132 }
133
134 newIntents.Enqueue(Intent);
135 }
136
137 this.intentQueue = newIntents;
138 return OnboardingUrl;
139 }
140 }
141}
Represents an instance of the Neuro-Access app.
Definition: App.xaml.cs:125
static new? App Current
Gets the current application instance.
Definition: App.xaml.cs:169
Contains intent action constants used for inter-component communication within the app.
Definition: Constants.cs:1060
const string OpenUrl
Action used to open a URL, typically triggered by deep linking.
Definition: Constants.cs:1064
const string NfcTagDiscovered
Action triggered when an NFC tag is discovered.
Definition: Constants.cs:1069
const string Onboarding
Onboarding URI Scheme (obinfo)
Definition: Constants.cs:183
static ? string GetScheme(string Url)
Gets the predefined scheme from an IoT Code
Definition: Constants.cs:200
A set of never changing property constants and helpful values.
Definition: Constants.cs:24
Represents an application intent containing an action, optional data, and an optional payload.
string? Data
Gets or sets the optional string data associated with the intent. This might be used for deep links.
string Action
Gets or sets the action associated with the intent.
object? Payload
Gets or sets an optional payload containing additional information (e.g. a cross‑platform NFC tag).
The IntentService class is responsible for handling and processing application intents in a cross‑pla...
void QueueIntent(AppIntent intent)
Queues an intent for later processing.
async Task ProcessQueuedIntentsAsync()
Processes all queued intents asynchronously. This method will dequeue and process each intent until t...
async Task< string?> TryGetAndDequeueOnboardingUrl()
Processes a OpenUrl intent with onboarding by returning value
async Task ProcessIntentAsync(AppIntent intent)
Processes a single intent asynchronously.
Near-Field Communication (NFC) Service.
Definition: NfcService.cs:29
async Task TagDetected(INfcTag Tag)
Method called when a new NFC Tag has been detected.
Definition: NfcService.cs:44
Base class that references services in the app.
Definition: ServiceRef.cs:43
static ILogService LogService
Log service.
Definition: ServiceRef.cs:214
Defines the contract for a service that handles application intents.
Interface for the Near-Field Communication (NFC) Service.
Definition: INfcService.cs:11