Pharmacist icon indicating copy to clipboard operation
Pharmacist copied to clipboard

feature: Add base interface

Open FinHorsley opened this issue 5 years ago • 1 comments

Describe the solution you'd like

Add an interface that event classes can inherit from.

// A better name may be something like IRx or IReactive?
public interface IEvent<T>
{
    T Data { get; }
}

the implementation could then look like the following

public class RxUIControlEvents : IEvent<UIControl>
{
       public UIControl Data => _data;

       private readonly global::UIKit.UIControl _data;
       /// <summary>
       /// Initializes a new instance of the <see cref = "global::UIKit.UIControl"/> class.
       /// </summary>
       /// <param name = "data">The class that is being wrapped.</param>
       public RxUIControlEvents(global::UIKit.UIControl data)
       {
           _data = data;
       }

       // Event bindings removed for brevity
}

Why would this be useful? This would then make it easy to create static helper methods for updating UI elements based on observables

public static Action<bool> Enabled(this IEvent<UIControl> reactive)
{
     return (enabled) =>
     {
           reactive.Data.Enabled = enabled;
     };
}

from the view code (e.g. UIViewController in iOS) this would enable

button = new UIButton();

ViewModel.Command
    .CanExecute
    .Subscribe(button.Events().Enabled())
    .DisposeWith(disp);

Additional Considerations

  1. I'm coming from an iOS/Android perspective, so understand that the interface might not apply to all situations; pharmacist is about creating observables from events, so the IEvents idea might be a level on top of pharmacist?

  2. RxSwift uses Rx where Pharmacist uses Events by default; adding the base interface would mean that Pharmacist wouldn't just be for events

Using the example above

ViewModel.Command
    .CanExecute
    // Confusing as this isn't an event
    .Subscribe(button.Events().Enabled())
    .DisposeWith(disp);

the Rx naming now makes more sense?

ViewModel.Command
    .CanExecute
    // More like the RxSwift style
    .Subscribe(button.Rx().Enabled())
    .DisposeWith(disp);

FinHorsley avatar Mar 30 '20 14:03 FinHorsley

Hey @FinHorsley :wave:,

Thank you for opening an issue. We will get back to you as soon as we can. Also, check out our Open Collective and consider contributing financially.

https://opencollective.com/reactiveui

PS.: We offer priority support for all financial contributors. Don't forget to add priority label once you start contributing :smile:

An advanced, composable, functional reactive model-view-viewmodel framework for all .NET platforms!