Skip to content

Commit

Permalink
1.0.22-beta - iOS Path provider implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
adospace committed Aug 27, 2024
1 parent 1444bb7 commit 084c06f
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

env:
Solution_Name: ./src/ReactorData.sln
Version: 1.0.21-beta
Version: 1.0.22-beta

steps:
- name: Checkout
Expand Down
42 changes: 0 additions & 42 deletions src/ReactorData.Maui/Platforms/Android/AndroidPathProvider.cs

This file was deleted.

39 changes: 39 additions & 0 deletions src/ReactorData.Maui/Platforms/Android/PathProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Android.App;
using System.IO;

//NOTE: part of this code is freely taken from https://github.com/reactiveui/Akavache/blob/main/src/Akavache.Core/Platforms/android/AndroidFilesystemProvider.cs

namespace ReactorData.Maui.Platforms.Android;


/// <summary>
/// The file system provider that understands the android.
/// </summary>
public class PathProvider : IPathProvider
{
/// <inheritdoc />
public string? GetDefaultLocalMachineCacheDirectory() => Application.Context.CacheDir?.AbsolutePath;

/// <inheritdoc />
public string? GetDefaultRoamingCacheDirectory() => Application.Context.FilesDir?.AbsolutePath;

/// <inheritdoc />
public string? GetDefaultSecretCacheDirectory()
{
var path = global::Android.App.Application.Context.FilesDir?.AbsolutePath;

if (path is null)
{
return null;
}

var di = new DirectoryInfo(Path.Combine(path, "Secret"));
if (!di.Exists)
{
di.CreateRecursive();
}

return di.FullName;
}
}

35 changes: 35 additions & 0 deletions src/ReactorData.Maui/Platforms/iOS/PathProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Foundation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ReactorData.Maui.Platforms.iOS;

internal class PathProvider : IPathProvider
{
public string GetDefaultLocalMachineCacheDirectory() => CreateAppDirectory(NSSearchPathDirectory.CachesDirectory);

/// <inheritdoc />
public string GetDefaultRoamingCacheDirectory() => CreateAppDirectory(NSSearchPathDirectory.ApplicationSupportDirectory);

/// <inheritdoc />
public string GetDefaultSecretCacheDirectory() => CreateAppDirectory(NSSearchPathDirectory.ApplicationSupportDirectory, "SecretCache");

private string CreateAppDirectory(NSSearchPathDirectory targetDir, string subDir = "Cache")
{
using var fm = new NSFileManager();
var url = fm.GetUrl(targetDir, NSSearchPathDomain.All, null, true, out _) ?? throw new DirectoryNotFoundException();
var rp = url.RelativePath ?? throw new DirectoryNotFoundException();
var ret = Path.Combine(rp, "ReactorData", subDir);
if (!Directory.Exists(ret))
{
Directory.CreateDirectory(ret);
}

return ret;
}
}
7 changes: 0 additions & 7 deletions src/ReactorData.Maui/Platforms/iOS/PlatformClass1.cs

This file was deleted.

4 changes: 3 additions & 1 deletion src/ReactorData.Maui/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public static MauiAppBuilder UseReactorData(this MauiAppBuilder appBuilder, Acti
appBuilder.Services.AddSingleton<IDispatcher>(dispatcher);

#if ANDROID
appBuilder.Services.AddSingleton<IPathProvider, AndroidPathProvider>();
appBuilder.Services.AddSingleton<IPathProvider, Platforms.Android.PathProvider>();
#elif IOS
appBuilder.Services.AddSingleton<IPathProvider, Platforms.iOS.PathProvider>();
#endif

serviceBuilderAction?.Invoke(appBuilder.Services);
Expand Down
3 changes: 2 additions & 1 deletion src/ReactorData.Sqlite/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public static void AddReactorData(this IServiceCollection services,
services.AddReactorData(modelContextConfigure);
services.AddSingleton<IStorage>(sp =>
{
if (!connectionStringOrDatabaseName.Trim().StartsWith("Data Source",StringComparison.CurrentCultureIgnoreCase))
if (!connectionStringOrDatabaseName.Trim()
.StartsWith("Data Source",StringComparison.CurrentCultureIgnoreCase))
{
var pathProvider = sp.GetService<IPathProvider>();
var connectionString = $"Data Source={Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), connectionStringOrDatabaseName)}";
Expand Down

0 comments on commit 084c06f

Please sign in to comment.