Skip to content

Commit

Permalink
Merge branch 'master' into v3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
aritchie committed Nov 8, 2023
2 parents 6726941 + 2d50ac4 commit 3e259e1
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 48 deletions.
23 changes: 20 additions & 3 deletions samples/Sample.Maui/Gps/MyGpsDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,35 @@
namespace Sample.Gps;


public partial class MyGpsDelegate : IGpsDelegate
public partial class MyGpsDelegate : GpsDelegate
{
readonly SampleSqliteConnection conn;
public MyGpsDelegate(SampleSqliteConnection conn) => this.conn = conn;
public MyGpsDelegate(ILogger<MyGpsDelegate> logger, SampleSqliteConnection conn) : base(logger)
{
this.conn = conn;
this.MinimumDistance = Distance.FromMeters(10);
this.MinimumTime = TimeSpan.FromSeconds(10);
}

public Task OnReading(GpsReading reading) => this.conn.Log(
protected override Task OnGpsReading(GpsReading reading) => this.conn.Log(
"GPS",
$"{reading.Position.Latitude} / {reading.Position.Longitude} - H: {reading.Heading}",
$"Accuracy: {reading.PositionAccuracy} - SP: {reading.Speed}",
reading.Timestamp
);
}
//public partial class MyGpsDelegate : IGpsDelegate
//{
// readonly SampleSqliteConnection conn;
// public MyGpsDelegate(SampleSqliteConnection conn) => this.conn = conn;

// public Task OnReading(GpsReading reading) => this.conn.Log(
// "GPS",
// $"{reading.Position.Latitude} / {reading.Position.Longitude} - H: {reading.Heading}",
// $"Accuracy: {reading.PositionAccuracy} - SP: {reading.Speed}",
// reading.Timestamp
// );
//}

#if ANDROID
public partial class MyGpsDelegate : IAndroidForegroundServiceDelegate
Expand Down
46 changes: 33 additions & 13 deletions samples/Sample.Maui/Jobs/SampleJob.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,52 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Shiny;
using Shiny.Jobs;
using Shiny.Jobs;
using Shiny.Notifications;

namespace Sample;


public class SampleJob : IJob
public partial class SampleJob : Job
{
readonly INotificationManager notificationManager;
public SampleJob(INotificationManager notificationManager)
=> this.notificationManager = notificationManager;

// TODO: test new object binding
public SampleJob(ILogger<SampleJob> logger, INotificationManager notificationManager) : base(logger)
{
this.notificationManager = notificationManager;
this.MinimumTime = TimeSpan.FromSeconds(50);
}

public async Task Run(JobInfo jobInfo, CancellationToken cancelToken)
protected override async Task Run(CancellationToken cancelToken)
{
await this.notificationManager.Send(
"Jobs",
$"Job Started - {jobInfo.Identifier}"
$"Job Started - {this.JobInfo!.Identifier}"
);
//await Task.Delay(TimeSpan.FromSeconds(seconds), cancelToken);

await this.notificationManager.Send(
"Jobs",
$"Job Finished - {jobInfo.Identifier}"
$"Job Finished - {this.JobInfo!.Identifier}"
);
}
}

//public class SampleJob : IJob
//{
// readonly INotificationManager notificationManager;
// public SampleJob(INotificationManager notificationManager)
// => this.notificationManager = notificationManager;

// // TODO: test new object binding

// public async Task Run(JobInfo jobInfo, CancellationToken cancelToken)
// {
// await this.notificationManager.Send(
// "Jobs",
// $"Job Started - {jobInfo.Identifier}"
// );
// //await Task.Delay(TimeSpan.FromSeconds(seconds), cancelToken);

// await this.notificationManager.Send(
// "Jobs",
// $"Job Finished - {jobInfo.Identifier}"
// );
// }
//}
2 changes: 1 addition & 1 deletion samples/Sample.Maui/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static MauiAppBuilder RegisterLogging(this MauiAppBuilder builder)
static MauiAppBuilder RegisterShinyServices(this MauiAppBuilder builder)
{
var s = builder.Services;
s.AddJob(typeof(SampleJob));
s.AddJob(typeof(SampleJob), runInForeground: true);

// shiny.jobs
//s.AddJobs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
using System.Linq;
using System.Threading.Tasks;
using System.Reactive.Linq;
using Foundation;
using CoreBluetooth;
using CoreLocation;
using Foundation;

namespace Shiny.BluetoothLE.Hosting;


public partial class BleHostingManager : IBleHostingManager
{
CBPeripheralManager _manager;
CBPeripheralManager manager;
protected CBPeripheralManager Manager
{
get
{
this._manager ??= new();
return this._manager;
this.manager ??= new();
return this.manager;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#if ANDROID
using Java.Util;
using Shiny.BluetoothLE.Hosting.Internals;
using Shiny.Reflection;
#endif

namespace Shiny.BluetoothLE.Hosting;
Expand All @@ -40,16 +39,23 @@ IServiceProvider services
#if ANDROID
this.context = new GattServerContext(platform);
#endif
this.keyStore = keyStore;
this.logger = logger;
this.keyStore = keyStore;
this.gattChars = services.GetLazyService<IEnumerable<BleGattCharacteristic>>();
}


const string REG_KEY = "BleHostingManager.IsRegisteredServicesAttached";
public bool IsRegisteredServicesAttached
{
get => this.keyStore.DefaultStore.Get<bool>(nameof(this.IsRegisteredServicesAttached), false);
private set => this.keyStore.DefaultStore.Set(nameof(this.IsRegisteredServicesAttached), value);
get => this.keyStore.DefaultStore.Get(REG_KEY, false);
set
{
if (value)
this.keyStore.DefaultStore.Set(REG_KEY, true);
else
this.keyStore.DefaultStore.Remove(REG_KEY);
}
}


Expand All @@ -64,14 +70,17 @@ public async void Start()
}
catch (Exception ex)
{
this.logger.LogWarning("Unable to reattach BLE hosted characteristics", ex);
this.logger.LogWarning(ex, "Unable to reattach BLE hosted characteristics");
}
}
}


public async Task AttachRegisteredServices()
{
if (this.IsRegisteredServicesAttached)
return;

(await this.RequestAccess()).Assert();

this.gattServices ??= CollectServices(this.gattChars.Value);
Expand All @@ -90,12 +99,13 @@ public void DetachRegisteredServices()
if (!this.IsRegisteredServicesAttached)
return;

this.IsRegisteredServicesAttached = false;
foreach (var serviceUuid in this.gattServices!.Keys)
this.RemoveService(serviceUuid);

foreach (var ch in this.gattChars.Value)
ch.OnStop(); // TODO: error trap this for user?

this.IsRegisteredServicesAttached = true;
}


Expand Down Expand Up @@ -194,7 +204,7 @@ await characteristic
}
catch (Exception ex)
{
this.logger.LogError("Error executing BleGattService notification subscription", ex);
this.logger.LogError(ex, "Error executing BleGattService notification subscription");
}
}, attribute.Notifications);
}
Expand All @@ -220,7 +230,7 @@ await characteristic
}
catch (Exception ex)
{
this.logger.LogError("Error executing BleGattService request", ex);
this.logger.LogError(ex, "Error executing BleGattService request");
if (!writeSuccess)
request.Respond(GattState.Failure);
}
Expand Down
30 changes: 14 additions & 16 deletions src/Shiny.Jobs/Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,27 @@ public async Task Run(JobInfo jobInfo, CancellationToken cancelToken)
{
var fireJob = true;
this.JobInfo = jobInfo;

try
{
if (this.MinimumTime != null && this.LastRunTime != null)
{
var timeDiff = this.LastRunTime.Value.Subtract(DateTimeOffset.UtcNow);
fireJob = timeDiff >= this.MinimumTime;
this.Logger.LogDebug($"Time Difference: {timeDiff} - Firing Job: {fireJob}");
}

if (fireJob)
await this.Run(cancelToken).ConfigureAwait(false);

if (this.MinimumTime != null && this.LastRunTime != null)
{
var timeDiff = DateTimeOffset.UtcNow.Subtract(this.LastRunTime.Value);
fireJob = timeDiff >= this.MinimumTime;
this.Logger.LogDebug($"Time Difference: {timeDiff} - Firing Job: {fireJob}");
}
finally

if (fireJob)
{
if (fireJob)
this.LastRunTime = DateTimeOffset.UtcNow;
this.Logger.LogDebug("Running Job");
await this.Run(cancelToken).ConfigureAwait(false);

// if the job errors, we will keep trying
this.LastRunTime = DateTimeOffset.UtcNow;
}
}


protected abstract Task Run(CancellationToken cancelToken);
protected JobInfo? JobInfo { get; private set; }
protected JobInfo JobInfo { get; private set; }

/// <summary>
/// Last runtime of this job. Null if never run before.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Android.Content;
using AndroidX.Core.App;

namespace Shiny.Notifications;

Expand All @@ -10,6 +11,7 @@ public class AndroidNotification : Notification
public bool OnGoing { get; set; }
public string? Ticket { get; set; }

public string? Category { get; set; }
public string? SmallIconResourceName { get; set; }
public string? LargeIconResourceName { get; set; }
public string? ColorResourceName { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public virtual NotificationCompat.Builder CreateNativeBuilder(AndroidNotificatio
.SetAutoCancel(notification.AutoCancel)
.SetOngoing(notification.OnGoing);

if (!notification.Category.IsEmpty())
builder.SetCategory(notification.Category);

if (!notification.Thread.IsEmpty())
builder.SetGroup(notification.Thread);

Expand Down
10 changes: 8 additions & 2 deletions src/Shiny.Notifications/Platforms/Android/NotificationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Reactive.Threading.Tasks;
using System.Threading.Tasks;
using Android.Content;
using Android.OS;
using Microsoft.Extensions.Logging;
using Shiny.Hosting;
using Shiny.Locations;
Expand All @@ -14,12 +15,14 @@
namespace Shiny.Notifications;


public partial class NotificationManager : INotificationManager, IAndroidLifecycle.IOnActivityNewIntent
public partial class NotificationManager : INotificationManager,
IAndroidLifecycle.IOnActivityOnCreate,
IAndroidLifecycle.IOnActivityNewIntent
{
readonly Lazy<AndroidNotificationProcessor> processor;
readonly AndroidPlatform platform;
readonly IChannelManager channelManager;
readonly AndroidNotificationManager manager;
readonly IChannelManager channelManager;
readonly IRepository repository;
readonly IGeofenceManager geofenceManager;
readonly IKeyValueStore settings;
Expand Down Expand Up @@ -206,4 +209,7 @@ await this.processor.Value
this.logger.LogError(ex, "Error trying to process intent");
}
}

public void ActivityOnCreate(Android.App.Activity activity, Bundle? savedInstanceState)
=> this.Handle(activity, activity.Intent!);
}
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "3.1.1",
"version": "3.1.2",
"assemblyVersion": {
"precision": "revision"
},
Expand Down

0 comments on commit 3e259e1

Please sign in to comment.