initial commit, iOS network native library
This commit is contained in:
88
Network/Platforms/iOS/Tasks/ContentTask.cs
Normal file
88
Network/Platforms/iOS/Tasks/ContentTask.cs
Normal file
@ -0,0 +1,88 @@
|
||||
using Foundation;
|
||||
|
||||
namespace Blahblah.Library.Network;
|
||||
|
||||
public abstract class ContentTask<T, TResult> : NetworkTask
|
||||
{
|
||||
public StepHandler<T>? Step { get; set; }
|
||||
|
||||
protected virtual bool AllowDefaultResult => false;
|
||||
|
||||
protected TaskCompletionSource<NetworkResult<TResult>>? taskSource;
|
||||
|
||||
protected long expectedContentLength;
|
||||
|
||||
protected ContentTask(string url, TaskCompletionSource<NetworkResult<TResult>> source, CancellationToken token) : base(url, token)
|
||||
{
|
||||
taskSource = source ?? throw new ArgumentNullException(nameof(source));
|
||||
}
|
||||
|
||||
protected override void OnCancelled()
|
||||
{
|
||||
taskSource?.TrySetCanceled();
|
||||
}
|
||||
|
||||
protected override void OnException(Exception ex)
|
||||
{
|
||||
taskSource?.TrySetResult(ex);
|
||||
}
|
||||
|
||||
public override bool OnResponsed(NSHttpUrlResponse response)
|
||||
{
|
||||
expectedContentLength = response.ExpectedContentLength;
|
||||
return true;
|
||||
}
|
||||
|
||||
protected virtual T? Update(float progress)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
public override void OnReceived(int length)
|
||||
{
|
||||
if (Step != null)
|
||||
{
|
||||
if (Data == null)
|
||||
{
|
||||
Step(0f, default);
|
||||
}
|
||||
else
|
||||
{
|
||||
float progress = (float)Data.Length / expectedContentLength;
|
||||
var update = Update(progress);
|
||||
Step(progress, update);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual TResult? Completed(NSHttpUrlResponse? response)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
protected override void OnCompleted(NSHttpUrlResponse? response)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = Completed(response);
|
||||
if (result == null && !AllowDefaultResult)
|
||||
{
|
||||
throw new NullReferenceException("Result is null");
|
||||
}
|
||||
taskSource?.TrySetResult(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
OnException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Disposing()
|
||||
{
|
||||
if (taskSource?.Task.IsCanceled == false)
|
||||
{
|
||||
taskSource.TrySetCanceled();
|
||||
taskSource = null;
|
||||
}
|
||||
}
|
||||
}
|
30
Network/Platforms/iOS/Tasks/DownloadTask.cs
Normal file
30
Network/Platforms/iOS/Tasks/DownloadTask.cs
Normal file
@ -0,0 +1,30 @@
|
||||
using Foundation;
|
||||
|
||||
namespace Blahblah.Library.Network;
|
||||
|
||||
public class DownloadTask<T> : ContentTask<T, bool>
|
||||
{
|
||||
public string FilePath { get; }
|
||||
|
||||
protected override bool AllowDefaultResult => true;
|
||||
|
||||
public DownloadTask(string url, string filePath, TaskCompletionSource<NetworkResult<bool>> source, CancellationToken token) : base(url, source, token)
|
||||
{
|
||||
FilePath = filePath;
|
||||
}
|
||||
|
||||
protected override bool Completed(NSHttpUrlResponse? response)
|
||||
{
|
||||
if (Data == null)
|
||||
{
|
||||
throw new NullReferenceException("Data is null");
|
||||
}
|
||||
var result = Data.Save(FilePath, NSDataWritingOptions.Atomic, out NSError? error);
|
||||
if (error != null)
|
||||
{
|
||||
throw new Exception(error?.ToString() ?? "UUnknown error while saving file");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
14
Network/Platforms/iOS/Tasks/FileTask.cs
Normal file
14
Network/Platforms/iOS/Tasks/FileTask.cs
Normal file
@ -0,0 +1,14 @@
|
||||
namespace Blahblah.Library.Network;
|
||||
|
||||
public class FileTask : DownloadTask<float>
|
||||
{
|
||||
public FileTask(string url, string filePath, TaskCompletionSource<NetworkResult<bool>> source, CancellationToken token) : base(url, filePath, source, token)
|
||||
{
|
||||
}
|
||||
|
||||
protected override float Update(float progress)
|
||||
{
|
||||
return progress;
|
||||
}
|
||||
}
|
||||
|
38
Network/Platforms/iOS/Tasks/ImageTask.cs
Normal file
38
Network/Platforms/iOS/Tasks/ImageTask.cs
Normal file
@ -0,0 +1,38 @@
|
||||
using CoreGraphics;
|
||||
using ImageIO;
|
||||
|
||||
namespace Blahblah.Library.Network;
|
||||
|
||||
public class ImageTask : DownloadTask<CGImage>
|
||||
{
|
||||
CGImageSource? imageSource;
|
||||
|
||||
public ImageTask(string url, string filePath, bool createImage, TaskCompletionSource<NetworkResult<bool>> source, CancellationToken token = default) : base(url, filePath, source, token)
|
||||
{
|
||||
if (createImage)
|
||||
{
|
||||
imageSource = CGImageSource.CreateIncremental(null);
|
||||
}
|
||||
}
|
||||
|
||||
protected override CGImage? Update(float progress)
|
||||
{
|
||||
if (imageSource == null || Data == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
imageSource.UpdateData(Data, Data.Length >= (uint)expectedContentLength);
|
||||
return imageSource.CreateImage(0, null!);
|
||||
}
|
||||
|
||||
protected override void Disposing()
|
||||
{
|
||||
if (imageSource != null)
|
||||
{
|
||||
imageSource.Dispose();
|
||||
imageSource = null;
|
||||
}
|
||||
base.Disposing();
|
||||
}
|
||||
}
|
||||
|
26
Network/Platforms/iOS/Tasks/StringTask.cs
Normal file
26
Network/Platforms/iOS/Tasks/StringTask.cs
Normal file
@ -0,0 +1,26 @@
|
||||
using Foundation;
|
||||
|
||||
namespace Blahblah.Library.Network;
|
||||
|
||||
public class StringTask : ContentTask<string, string>
|
||||
{
|
||||
public StringHandler? Process { get; set; }
|
||||
|
||||
public StringTask(string url, TaskCompletionSource<NetworkResult<string>> source, CancellationToken token) : base(url, source, token)
|
||||
{
|
||||
}
|
||||
|
||||
protected override string? Completed(NSHttpUrlResponse? response)
|
||||
{
|
||||
if (Data == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
string s = NSString.FromData(Data, NSStringEncoding.UTF8);
|
||||
if (Process == null)
|
||||
{
|
||||
return s;
|
||||
}
|
||||
return Process(s);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user