cloud.ludus 2.0.1

dotnet add package cloud.ludus --version 2.0.1
NuGet\Install-Package cloud.ludus -Version 2.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="cloud.ludus" Version="2.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add cloud.ludus --version 2.0.1
#r "nuget: cloud.ludus, 2.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install cloud.ludus as a Cake Addin
#addin nuget:?package=cloud.ludus&version=2.0.1

// Install cloud.ludus as a Cake Tool
#tool nuget:?package=cloud.ludus&version=2.0.1

ObservableViewModel

It allows the creation of background tasks with ViewModel in Xamarin Android.

In this section we provide you an example to implement this library in your xamarin android project. Before you do it, you can review an example from xamarin ViewModel repository. For better understanding how ViewModels work, go to android documentation. You need to know that this library doesn't use LiveData, in other hand, we use observables to handle threads and ViewModel to attach it to lifecycle activity or fragment.

Get Started

Import the nuget package in your project.

https://www.nuget.org/packages/cloud.ludus/

Config your ViewModel

After it, you need to create a ViewModel that extends from BaseViewModel as shown below. Here you need to override two methods. LoadInBackground to process the task in this thread and ProcesssResponse for incoming messages.


public class ExampleViewModel : BaseViewModel<MyResponseType>
{
    protected override MyResponseType LoadInBackground()
    {
        // Consume your rest service or doing here anything that you need

        return new MyResponseType();
    }

    protected override void ProcessResponse(MyResponseType response, IObserver<MyResponseType> observer)
    {
        /*
         * Here you have an observer object, then you can define when call to
         * OnError or OnNext callbacks to finalize successfully. We recommend to allways call
         * OnComplete callback to process common actions.
         */
    
        if (response == null)
        {
            observer.OnError(new Exception());
        }
        else
        {
            observer.OnNext(response);
        }

        observer.OnCompleted();
    }
}

Config on your activity

Now, initialize the viewModelManager to add the viewModel and actions to OnNext and OnError callbacks or optionally to OnComplete callback as shown below


var viewModelManager = new ViewModelManager();
var exampleViewModel = ViewModelProviders.Of(this).Get(Java.Lang.Class.FromType(typeof(ExampleViewModel))) as ExampleViewModel;

// Register ViewModel and actions
viewModelManager.AddViewModel(exampleViewModel, ExampleSuccess, ExampleError);

// Init the task
exampleViewModel.InitObserver();

These are examples methods to OnNext and OnError callbacks


private void ExampleSuccess(MyResponseType response)
{
    // Handle response here
}

private void ExampleError(Exception exception)
{
    // Handle exception here 
}

Product Compatible and additional computed target framework versions.
MonoAndroid monoandroid90 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.1 647 1/29/2020
2.0.0 447 1/14/2020
1.0.2 621 8/14/2019
1.0.1 519 8/5/2019
1.0.0 499 8/2/2019