redth-org / AndHUD

Android Progress HUD and Dialog helpers for Xamarin.Android apps!
Apache License 2.0
179 stars 69 forks source link

Can't use AndHUD in Xamarin Forms Android project #25

Closed noelfernandes closed 7 years ago

noelfernandes commented 7 years ago

Hi there,

I managed to get the BTProgressHUD working in my Xamarin forms (iOS) project but could not get AndHUD to work in the Xamarin Forms (Android) project. I created an interface in the PCL with project level implementations i.e. in iOS I used BTProgressHUD and Android I used AndHUD.

For Android, the hud doesn't show up at all. The code is as follows:

IHudService.cs

public interface IHudService
{
    void ShowHud(string ProgressText = "Loading...");
    void HideHud();
    void SetText(string Text);
    void SetProgress(double Progress, string ProgressText = "");
    void Toast(string message = "Hello....", bool showToastCentered = true, double timeoutMs = 10000);
}

Android implementation of IHudService i.e. HudService.cs

[assembly: Dependency(typeof(HudService))]
namespace WhatDoISay.Mobile.Droid.Services
{
    public class HudService : IHudService
    {
        Android.Views.View _load;
        bool isHudVisible;

        public void HideHud()
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                AndHUD.Shared.Dismiss(Forms.Context);
                if (_load != null)
                    _load.Visibility = ViewStates.Gone;
                isHudVisible = false;
            });
        }

        public void SetProgress(double Progress, string ProgressText = "")
        {
            if (!isHudVisible)
                return;
            Device.BeginInvokeOnMainThread(() =>
            {
                int progress = (int) (Progress * 100);
                AndHUD.Shared.Show(Forms.Context, ProgressText + progress + "%", progress, MaskType.Black);
            });
        }

        public void SetText(string Text)
        {
            if (!isHudVisible)
                return;
            Device.BeginInvokeOnMainThread(() =>
            {
                AndHUD.Shared.Show(Forms.Context, Text, maskType: MaskType.Black);
            });
        }

        public void ShowHud(string ProgressText = "Loading...")
        {
            Device.BeginInvokeOnMainThread(() =>
            {
                AndHUD.Shared.Show(Forms.Context, ProgressText, maskType: MaskType.Black);
                isHudVisible = true;
            });
        }

        public void Toast(string message = "Hello....", bool showToastCentered = true, double timeoutMs = 10000)
        {
            AndHUD.Shared.ShowToast(Forms.Context, message, MaskType.Black, TimeSpan.FromMilliseconds(timeoutMs), showToastCentered);
        }

        Android.Views.View CustomLoadingView(string ProgressText)
        {
            Android.Views.View loadingView = new Android.Views.View(Forms.Context);

            return loadingView;
        }
    }
}

I have a page called CategoriesPage.xaml. In this page I have a scrollview. I am invoking the hudService in the Scrolled event of the scrollview on this page as follows:

public partial class CategoriesPage : ContentPage
{
    public IHudService hudService { get; } = DependencyService.Get<IHudService>();
    private async void CategoriesScroller_Scrolled(object sender, ScrolledEventArgs e)
    {
        if (e.ScrollY < 5)
        {
            Task task = Task.Run(() =>
            {
                hudService.ShowHud();
                App.Locator.CategoriesVM.DeleteCategoriesCommand.Execute(null);
                hudService.HideHud();
            });
        }
    } 
}

Basically, I'm trying to have a "scroll down to refresh" behavior. Upon debugging, I can see that it is able to execute all the 3 lines and the methods therein but for some reason the hud does not show on Android at all.

Any ideas assistance would really be appreciated.

Thanks Noel

Redth commented 7 years ago

How about invoking it from the XHUD namespace?

What I do in my Forms app is XHUD.HUD.MyActivity = this; in my Android app's MainActivity, and then I can call XHUD api's as I please:

XHUD.HUD.Show ("Signing in...", maskType: XHUD.MaskType.Black);