Getting Started
Getting Started
As developers, we often take internet connectivity and speed for granted. Mobile phones are taking HUGE sized photos these days that can take quite some time to upload.
Features
- Background Uploads & Downloads on each platform
- Supports transfer filtering based on metered connections (iOS & UWP only at the moment)
- Event Based Metrics
- Percentage Complete
- Total Bytes Expected
- Total Bytes Transferred
- Transfer Speed (Bytes Per Second)
- Estimated Completion Time
Setup
Background Delegate
using System;using System.Threading.Tasks;using Shiny;
namespace YourNamespace;
public partial class MyHttpTransferDelegate : IHttpTransferDelegate{
public async Task OnError(HttpTransferRequest request, Exception ex) { }
public async Task OnCompleted(HttpTransferRequest request) { }}
// in multitargeted solutions on Android, you can completely customize the foreground service notification#if ANDROIDpublic partial class TestHttpTransferDelegate : IAndroidForegroundServiceDelegate{ public void ConfigureNotification(AndroidX.Core.App.NotificationCompat.Builder builder) { builder .SetContentTitle("MyApp") .SetContentText("My App is sending images") .SetSmallIcon(Resource.Mipmap.youricon); }}
#endif
Creating a Transfer
IHttpTransferManager manager; // injected, resolved, etc
await manager.Queue(new HttpTransferRequest( "your UNIQUE identifier", "http://yourserver.com/upload", true, // is upload (true) or download (false) "local file path" // local file path to upload from or download to));
Canceling a Transfer or All Transfers
IHttpTransferManager manager; // injected, resolved, etc
await manager.Cancel("Your Transfer ID");
// OR
await manager.CancelAll();
Get All Pending Transfers
IHttpTransferManager manager; // injected, resolved, etc
var yourRequests = await manager.GetTransfers();
Foreground
Sending Multipart Data & Headers along with transfer
IHttpTransferManager manager; // injected, resolved, etc
await manager.Queue(new HttpTransferRequest( "Your Identifier", "https://yoururl.com", isUploadBoolean, "upload from or download to file path", Headers: new Dictionary<string, string> { { "Test", "Test" } }, HttpContent: TransferHttpContent.FromJson(new { Text = "This is test JSON" });));
Watching All Transfers (Progress)
IHttpTransferManager manager; // injected, resolved, etc
await manager.Queue(new HttpTransferRequest( "Your Identifier", "https://yoururl.com", isUploadBoolean, "upload from or download to file path", Headers: new Dictionary<string, string> { { "Test", "Test" } }, HttpContent: TransferHttpContent.FromJson(new { Text = "This is test JSON" });));
Watching a Specific Transfer
IHttpTransferManager manager; // injected, resolved, etc
var sub = manager.WatchTransfer("Your identifier string").Subscribe( x => { // this fires as progress and state changes come in //x.Progress.PercentComplete //x.Progress.BytesPerSecond }, ex => { // fires when an error outside of connectivity issues occur }, () => { // fires when the transfer is complete });
// dispose of the subscription if you don't want to observe any longer or let it completesub.Dispose();
Managed List
The managed list is meant manage a list of transfers that you can bind against in your UI. It manages all of the progress updates, completions, and errors. All you have to do is bind to the list and the models, it takes care of the rest.
- The list is managed in a thread safe manner
- As new transfers are added, the list is updated
HttpTransferMonitor monitor; // inject, resolve, etc
// Using MVVM, you can bind to the following:monitor.Transfers // (of type HttpTransferObject)
// now start the monitormonitor.Start( bool removeFinished = true, bool removeErrors = true, IScheduler? scheduler = null);
// after this, queuing or cancelling downloads will update list
The second part of this is binding to the HttpTransferObject
which is also an MVVM based notify object. Progress, state, & error changes will
all occur here. Everything you need to inform users of their transfer progress is here:
public class HttpTransferObject : NotifyPropertyChanged{ public string Identifier { get; } public string Uri { get; } public bool IsUpload { get; } public double PercentComplete { get; } public long BytesPerSecond { get; } public bool IsDeterministic { get; } public long? BytesToTransfer { get; } public long BytesTransferred { get; } public TimeSpan EstimatedTimeRemaining { get; } public HttpTransferState Status { get; }}