Any pub/usb library?

rrodriguezrrodriguez USMember
edited December 2013 in Cross Platform with Xamarin

Have you used any good pub/sub library you can recommend?

I will also explain my problem, maybe there is a better way to tackle this w/o actually using or implementing a pub/sub.

When the user logs in the first time in my app, there is content the app needs to download that is important but not crucial to the app.

Since the app can run w/o this content, I decided to download this asynchronously and don't make the user wait.
Since this content is required for all the apps, regardless the platform, then it is happening in the shared code I'm using for authentication.

By the time this content is downloaded, the user will be in a different controller/screen so I need to be able to broadcast a message from this authentication shared code to the app, letting the active screen know the content is there to display.

How would you handle this?

Thanks,
R.

Posts

  • GuillermoGutierrezGuillermoGutierrez ESMember ✭✭✭

    If you're using MvvmCross, take a look at MvvmCross Messenger Plugin. I guess that you could use it even if you're not using MvvmCross anyway.

  • rrodriguezrrodriguez USMember

    Thanks @GuillermoGutierrez, I ended up structuring the app in a way I won't need this yet.

    My gut feeling is that I would need this eventually, so thanks for the pointer.

  • Just a naive and regular usage of MVVM will do the work, it's a great design pattern fitting perfectly to what you need,you can borrow some code from the Field Service app provided by the good people of xamarin,

    in the ViewModel you would have a property for done downloading, something like

    public bool IsDoneDownlaoding
    {
    get{return _isDoneDownloading; }
    set{
    _isDoneDownloading = value;
    OnPropertyChanged("done");
    }
    }

    in the activity/fragment when you'll need to catch this message you'll hold an instance of the ViewModel containing the property you've created, and will subscribe to the event like this.

    YourViewModel _vm = YourViewModel.Instance;

    _vm.PropertyChanged += delegate(object sender, PropertyChangedEventArgs e){
    if (e.ProperyName.Equals("done"))
    {
    DoSomethingUseful();
    }
    };

    }

  • rrodriguezrrodriguez USMember
    edited December 2013

    @israeltabadi.2584 it is more complicated than that.

    By the time the content is downloaded, the user will be in a screen different than the original one where the downloaded was started.

    I would need to either pass the first VM around all the screens -not an option, really :)
    Or create a singleton that takes care of downloading the images and that exposes an Action the active screen can implement and that is called when the content is downloaded.
    I went with this last option.

    A third alternative would be to implement/use a simple pub/sub mechanism so the screens can observe a subject that let's them know the content is available.

  • I see,
    then maybe - consider this:
    the action of download ended needs to captured in a place like a fragment or activity right? i hope that's the case for you.

    have activities/Fragments inherit from a base class YourActivity, and put the listener there, that will allow you to have a global hook on the event fired, and mainly it will allow you to handle the event differently depending on where the user is in the application.

  • rrodriguezrrodriguez USMember

    Kind of what I did, thanks!

  • Sure :) glad to be able to help

Sign In or Register to comment.