Skip to content

Commit

Permalink
Add iOS implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkatz6 committed Jun 24, 2022
1 parent eacd679 commit 3ddd068
Show file tree
Hide file tree
Showing 4 changed files with 406 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/iOS/Avalonia.iOS/AvaloniaView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
using Avalonia.Input;
using Avalonia.Input.Raw;
using Avalonia.Input.TextInput;
using Avalonia.iOS.Storage;
using Avalonia.Platform;
using Avalonia.Platform.Storage;
using Avalonia.Rendering;
using CoreAnimation;
using Foundation;
Expand Down Expand Up @@ -41,9 +43,10 @@ public AvaloniaView()
);
_topLevelImpl.Surfaces = new[] {new EaglLayerSurface(l)};
MultipleTouchEnabled = true;
AddSubviews(new UIView[] { new UIKit.UIButton(UIButtonType.InfoDark) });
}

internal class TopLevelImpl : ITopLevelImplWithTextInputMethod, ITopLevelImplWithNativeControlHost
internal class TopLevelImpl : ITopLevelImplWithTextInputMethod, ITopLevelImplWithNativeControlHost, ITopLevelImplWithStorageProvider
{
private readonly AvaloniaView _view;
public AvaloniaView View => _view;
Expand All @@ -52,6 +55,7 @@ public TopLevelImpl(AvaloniaView view)
{
_view = view;
NativeControlHost = new NativeControlHostImpl(_view);
StorageProvider = new IOSStorageProvider(view);
}

public void Dispose()
Expand Down Expand Up @@ -113,7 +117,8 @@ public void SetTransparencyLevelHint(WindowTransparencyLevel transparencyLevel)
new AcrylicPlatformCompensationLevels();

public ITextInputMethodImpl? TextInputMethod => _view;
public INativeControlHostImpl NativeControlHost { get; }
public INativeControlHostImpl NativeControlHost { get; }
public IStorageProvider StorageProvider { get; }
}

[Export("layerClass")]
Expand Down
66 changes: 66 additions & 0 deletions src/iOS/Avalonia.iOS/Storage/IOSSecurityScopedStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System.IO;

using Foundation;

using UIKit;

#nullable enable

namespace Avalonia.iOS.Storage;

internal sealed class IOSSecurityScopedStream : Stream
{
private readonly UIDocument _document;
private readonly FileStream _stream;
private readonly NSUrl _url;

internal IOSSecurityScopedStream(NSUrl url, FileAccess access)
{
_document = new UIDocument(url);
var path = _document.FileUrl.Path;
_url = url;
_url.StartAccessingSecurityScopedResource();
_stream = File.Open(path, FileMode.Open, access);
}

public override bool CanRead => _stream.CanRead;

public override bool CanSeek => _stream.CanSeek;

public override bool CanWrite => _stream.CanWrite;

public override long Length => _stream.Length;

public override long Position
{
get => _stream.Position;
set => _stream.Position = value;
}

public override void Flush() =>
_stream.Flush();

public override int Read(byte[] buffer, int offset, int count) =>
_stream.Read(buffer, offset, count);

public override long Seek(long offset, SeekOrigin origin) =>
_stream.Seek(offset, origin);

public override void SetLength(long value) =>
_stream.SetLength(value);

public override void Write(byte[] buffer, int offset, int count) =>
_stream.Write(buffer, offset, count);

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);

if (disposing)
{
_stream.Dispose();
_document.Dispose();
_url.StopAccessingSecurityScopedResource();
}
}
}
121 changes: 121 additions & 0 deletions src/iOS/Avalonia.iOS/Storage/IOSStorageItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Threading.Tasks;
using Avalonia.Logging;
using Avalonia.Platform.Storage;
using Foundation;

using UIKit;

#nullable enable

namespace Avalonia.iOS.Storage;

internal abstract class IOSStorageItem : IStorageBookmarkItem
{
private readonly string _filePath;

protected IOSStorageItem(NSUrl url)
{
Url = url ?? throw new ArgumentNullException(nameof(url));

using (var doc = new UIDocument(url))
{
_filePath = doc.FileUrl?.Path ?? url.FilePathUrl.Path;
Name = doc.LocalizedName ?? Path.GetFileName(_filePath) ?? url.FilePathUrl.LastPathComponent;
}
}

internal NSUrl Url { get; }

public bool CanBookmark => true;

public string Name { get; }

public Task<StorageItemProperties> GetBasicPropertiesAsync()
{
var attributes = NSFileManager.DefaultManager.GetAttributes(_filePath, out var error);
if (error is not null)
{
Logger.TryGet(LogEventLevel.Error, LogArea.IOSPlatform)?.
Log(this, "GetBasicPropertiesAsync returned an error: {ErrorCode} {ErrorMessage}", error.Code, error.LocalizedFailureReason);
}
return Task.FromResult(new StorageItemProperties(attributes?.Size, (DateTime)attributes?.CreationDate, (DateTime)attributes?.ModificationDate));
}

public Task<IStorageFolder?> GetParentAsync()
{
return Task.FromResult<IStorageFolder?>(new IOSStorageFolder(Url.RemoveLastPathComponent()));
}

public Task ReleaseBookmark()
{
// no-op
return Task.CompletedTask;
}

public Task<string?> SaveBookmark()
{
try
{
if (!Url.StartAccessingSecurityScopedResource())
{
return Task.FromResult<string?>(null);
}

var newBookmark = Url.CreateBookmarkData(NSUrlBookmarkCreationOptions.SuitableForBookmarkFile, Array.Empty<string>(), null, out var bookmarkError);
if (bookmarkError is not null)
{
Logger.TryGet(LogEventLevel.Error, LogArea.IOSPlatform)?.
Log(this, "SaveBookmark returned an error: {ErrorCode} {ErrorMessage}", bookmarkError.Code, bookmarkError.LocalizedFailureReason);
return Task.FromResult<string?>(null);
}

return Task.FromResult<string?>(
newBookmark.GetBase64EncodedString(NSDataBase64EncodingOptions.None));
}
finally
{
Url.StopAccessingSecurityScopedResource();
}
}

public bool TryGetUri([NotNullWhen(true)] out Uri uri)
{
uri = Url;
return uri is not null;
}

public void Dispose()
{
}
}

internal sealed class IOSStorageFile : IOSStorageItem, IStorageBookmarkFile
{
public IOSStorageFile(NSUrl url) : base(url)
{
}

public bool CanOpenRead => true;

public bool CanOpenWrite => true;

public Task<Stream> OpenRead()
{
return Task.FromResult<Stream>(new IOSSecurityScopedStream(Url, FileAccess.Read));
}

public Task<Stream> OpenWrite()
{
return Task.FromResult<Stream>(new IOSSecurityScopedStream(Url, FileAccess.Write));
}
}

internal sealed class IOSStorageFolder : IOSStorageItem, IStorageBookmarkFolder
{
public IOSStorageFolder(NSUrl url) : base(url)
{
}
}
Loading

0 comments on commit 3ddd068

Please sign in to comment.