Skip to content

Commit

Permalink
improvments
Browse files Browse the repository at this point in the history
  • Loading branch information
dhindrik committed Nov 25, 2022
1 parent 472b83d commit 167acbb
Show file tree
Hide file tree
Showing 15 changed files with 209 additions and 208 deletions.
20 changes: 10 additions & 10 deletions ServiceBusManager.UnitTests/ConnectViewModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public async Task OnParameterSetTest()
{
//Arrange
var connectionService = Substitute.For<IConnectionService>();
var featureService = Substitute.For<IFeatureService>();
var featureService = Substitute.For<IPremiumService>();
var logService = Substitute.For<ILogService>();

var viewModel = new ConnectViewModel(connectionService, featureService, logService);
Expand All @@ -26,7 +26,7 @@ public async Task OnParameterSetTest_InvalidValue()
{
//Arrange
var connectionService = Substitute.For<IConnectionService>();
var featureService = Substitute.For<IFeatureService>();
var featureService = Substitute.For<IPremiumService>();
var logService = Substitute.For<ILogService>();

var viewModel = new ConnectViewModel(connectionService, featureService, logService);
Expand All @@ -51,7 +51,7 @@ public async Task OnAppearingTest()
new Models.ConnectionInfo()
}));

var featureService = Substitute.For<IFeatureService>();
var featureService = Substitute.For<IPremiumService>();
var logService = Substitute.For<ILogService>();

var viewModel = new ConnectViewModel(connectionService, featureService, logService);
Expand All @@ -71,7 +71,7 @@ public async Task OnAppearing_NoSavedConnectionsTest()
var connectionService = Substitute.For<IConnectionService>();
connectionService.Get().Returns(Task.FromResult(new List<Models.ConnectionInfo>()));

var featureService = Substitute.For<IFeatureService>();
var featureService = Substitute.For<IPremiumService>();
var logService = Substitute.For<ILogService>();

var viewModel = new ConnectViewModel(connectionService, featureService, logService);
Expand All @@ -91,7 +91,7 @@ public void OpenConnectionCommandTest()
//Arrange
var connectionService = Substitute.For<IConnectionService>();

var featureService = Substitute.For<IFeatureService>();
var featureService = Substitute.For<IPremiumService>();
var logService = Substitute.For<ILogService>();

TinyNavigation.Current = Substitute.For<TinyMvvm.INavigation>();
Expand All @@ -115,7 +115,7 @@ public void SaveAndConnectToNewCommandTest()
//Arrange
var connectionService = Substitute.For<IConnectionService>();

var featureService = Substitute.For<IFeatureService>();
var featureService = Substitute.For<IPremiumService>();
var logService = Substitute.For<ILogService>();

TinyNavigation.Current = Substitute.For<TinyMvvm.INavigation>();
Expand Down Expand Up @@ -144,7 +144,7 @@ public void ConnectToNewCommandTest()
//Arrange
var connectionService = Substitute.For<IConnectionService>();

var featureService = Substitute.For<IFeatureService>();
var featureService = Substitute.For<IPremiumService>();
var logService = Substitute.For<ILogService>();

TinyNavigation.Current = Substitute.For<TinyMvvm.INavigation>();
Expand Down Expand Up @@ -172,7 +172,7 @@ public void ValidateSaveTest()
//Arrange
var connectionService = Substitute.For<IConnectionService>();

var featureService = Substitute.For<IFeatureService>();
var featureService = Substitute.For<IPremiumService>();
var logService = Substitute.For<ILogService>();

var viewModel = new ConnectViewModel(connectionService, featureService, logService);
Expand All @@ -193,7 +193,7 @@ public void ValidateConnectTest()
//Arrange
var connectionService = Substitute.For<IConnectionService>();

var featureService = Substitute.For<IFeatureService>();
var featureService = Substitute.For<IPremiumService>();
var logService = Substitute.For<ILogService>();

var viewModel = new ConnectViewModel(connectionService, featureService, logService);
Expand All @@ -214,7 +214,7 @@ public async Task RemoveCommandTest()
//Arrange
var connectionService = Substitute.For<IConnectionService>();

var featureService = Substitute.For<IFeatureService>();
var featureService = Substitute.For<IPremiumService>();
var logService = Substitute.For<ILogService>();


Expand Down
5 changes: 1 addition & 4 deletions ServiceBusManager/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

public static class Constants
{
public static class Features
{
public const string Premium = nameof(Premium);
}
public const string Premium = nameof(Premium);

public static class Products
{
Expand Down
2 changes: 1 addition & 1 deletion ServiceBusManager/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static MauiApp CreateMauiApp()
private static void RegisterServices(this MauiAppBuilder builder)
{
builder.Services.AddSingleton<ILogService, FileLogService>();
builder.Services.AddSingleton<IFeatureService, FeatureService>();
builder.Services.AddSingleton<IPremiumService, PremiumService>();

builder.Services.AddSingleton<IServiceBusService, ServiceBusService>();

Expand Down
163 changes: 110 additions & 53 deletions ServiceBusManager/Platforms/MacCatalyst/AppDelegate.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using BackgroundTasks;
using System.Threading.Tasks;
using AVFoundation;
using BackgroundTasks;
using Foundation;
using Microsoft.Maui.Controls.Compatibility.Platform.iOS;
using ServiceBusManager.Platforms.MacCatalyst;
Expand All @@ -10,114 +12,168 @@ namespace ServiceBusManager;
[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{
private const string BgProcessingIdentifier = "se.hindrikes.azservicebus.fetch";

protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();

public override bool FinishedLaunching(UIKit.UIApplication application, NSDictionary launchOptions)
{
base.FinishedLaunching(application, launchOptions);

var service = Resolver.Resolve<ILogService>();
try
{
UNUserNotificationCenter.Current.Delegate = new UserNotificationCenterDelegate();

// Check we're at least v10.14
if (NSProcessInfo.ProcessInfo.IsOperatingSystemAtLeastVersion(new NSOperatingSystemVersion(10, 14, 0)))
{
// Request notification permissions from the user
UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert |
UNAuthorizationOptions.Badge |
UNAuthorizationOptions.Sound, (approved, err) =>
// Request notification permissions from the user
UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert |
UNAuthorizationOptions.Badge |
UNAuthorizationOptions.Sound, async (approved, err) =>
{
if (service != null)
{

});
}

CheckFeature();
if (err != null)
{
await service!.LogException(new Exception(err.ToString()));
}
}
});

//CheckPremium();
}
catch (Exception ex)
{
var service = Resolver.Resolve<ILogService>();


if (service != null)
{
service.LogException(ex);
}
}

_ = Task.Run(async () =>
{
var premiumService = Resolver.Resolve<IPremiumService>();

while (true)
{
#if DEBUG
await Task.Delay(10000);
#else
await Task.Delay(300000);
#endif

if (premiumService!.HasPremium())
{
await service!.LogEvent("StartingAsyncTask");

await CheckForDeadLetters();
}
}
});

return true;
}


private bool CheckFeature()
public override void DidEnterBackground(UIApplication application)
{
var service = Resolver.Resolve<IFeatureService>();
base.DidEnterBackground(application);

if (service == null)
var service = Resolver.Resolve<ILogService>();

if (service != null)
{
throw new ArgumentNullException("Add IFeatureService to IoC");
service.LogEvent(nameof(DidEnterBackground));
}
}

private async static Task<bool> CheckForDeadLetters()
{
var log = Resolver.Resolve<ILogService>();

if (service.HasFeature(Constants.Features.Premium))
try
{
var dateTime = DateTimeOffset.UtcNow;

var service = Resolver.Resolve<IServiceBusService>();

BGTaskScheduler.Shared.Register("se.hindrikes.azservicebus.fetch", null, async (task) =>
if (service == null)
{
var log = Resolver.Resolve<ILogService>();
return false;
}

if (log != null)
{
await log.LogEvent("BackgroundFetchStarting");
}
var compareDate = Preferences.Get("LastDeadLetter", dateTime.DateTime);

var dateTime = DateTimeOffset.UtcNow;
var count = await service.CheckNewDeadLetters(new DateTimeOffset(compareDate));

var service = Resolver.Resolve<IServiceBusService>();
Preferences.Set("LastDeadLetter", dateTime.DateTime);

if (count > 0)
{
SendNotification(count);
}

if (service == null)
{
task.SetTaskCompleted(false);
return;
}
return true;
}
catch (Exception ex)
{
if (log != null)
{
await log.LogException(ex);
}
}

return false;
}

private bool CheckPremium()
{
var service = Resolver.Resolve<IPremiumService>();

if (service == null)
{
throw new ArgumentNullException("Add IFeatureService to IoC");
}

var compareDate = Preferences.Get("LastDeadLetter", dateTime.DateTime);
if (service.HasPremium())
{

var count = await service.CheckNewDeadLetters(new DateTimeOffset(compareDate));
var result = BGTaskScheduler.Shared.Register(BgProcessingIdentifier, null, async (task) =>
{
var log = Resolver.Resolve<ILogService>();

if (count > 0)
if (log != null)
{
SendNotification(count);
await log.LogEvent("BackgroundFetchStarting");
}

Preferences.Set("LastDeadLetter", dateTime.DateTime);
var result = await CheckForDeadLetters();

task.SetTaskCompleted(true);
task.SetTaskCompleted(result);

if (log != null)
{
await log.LogEvent("BackgroundFetchCompleted");
}


ScheduleAppRefresh();
ScheduleBackgroundCheck();
});

ScheduleAppRefresh();
ScheduleBackgroundCheck();

return true;
}
else
{
service.FeatureChanged += Service_FeatureChanged;
service.PremiumChanged += Service_FeatureChanged;
}

return false;
}

private void ScheduleAppRefresh()
//e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"se.hindrikes.azservicebus.fetch"]
private void ScheduleBackgroundCheck()
{
var request = new BGProcessingTaskRequest("se.hindrikes.azservicebus.fetch");
request.EarliestBeginDate = DateTime.Now.AddSeconds(30).ToNSDate();
var request = new BGProcessingTaskRequest(BgProcessingIdentifier);
request.RequiresNetworkConnectivity = true;
request.RequiresExternalPower = false;

Expand All @@ -133,28 +189,28 @@ private void ScheduleAppRefresh()

if (log != null)
{
log.LogEvent(nameof(ScheduleAppRefresh));
log.LogEvent(nameof(ScheduleBackgroundCheck));
}
}

private void Service_FeatureChanged(object? sender, EventArgs e)
{
var service = Resolver.Resolve<IFeatureService>();
var service = Resolver.Resolve<IPremiumService>();

if (CheckFeature())
if (CheckPremium())
{
if (service == null)
{
throw new ArgumentNullException("Add IFeatureService to IoC");
}

service.FeatureChanged -= Service_FeatureChanged;
service.PremiumChanged -= Service_FeatureChanged;
}
}



private void SendNotification(int badgeNumber)
private static void SendNotification(int badgeNumber)
{
var shouldSend = Preferences.Default.Get<bool>("Notifications", false);

Expand All @@ -180,7 +236,8 @@ private void SendNotification(int badgeNumber)
{
if (err != null)
{
// Do something with error...
var log = Resolver.Resolve<ILogService>();
log!.LogException(new Exception(err.ToString()));
}
});
#endif
Expand Down
Loading

0 comments on commit 167acbb

Please sign in to comment.