Share via

dependecy injection help on checking internet

Eduardo Gomez 4,316 Reputation points
2023-11-27T13:46:22.67+00:00

I have my interface

interface IAppServices {

    Task NavigateAsync(string pageName, bool iAnimated, Dictionary<string, object> Object);

    Task NavigateAsync(string pageName, bool iAnimated);

    Task DisplayAlertAsync(string title, string message, string CancelMessage);

    Task DisplayToastAsync(string message, ToastDuration toastDuration, double fontSize);
}


and my implementation

namespace Demy_IA.Services;
class AppServices : IAppServices {
    public async Task DisplayAlertAsync(string title, string message, string CancelMessage) {
        await Shell.Current.DisplayAlert(title, message, CancelMessage);
    }

    public async Task DisplayToastAsync(string message, ToastDuration toastDuration, double fontSize) {
        var toast = Toast.Make(message, toastDuration, fontSize);
        await toast.Show();
    }

    public async Task NavigateAsync(string pageName, bool iAnimated, Dictionary<string, object> Object) {
       await Shell.Current.GoToAsync(pageName, iAnimated, Object);
    }

    public async Task NavigateAsync(string pageName, bool iAnimated) {
        await Shell.Current.GoToAsync(pageName, iAnimated);
    }
}

How can I check internet conenectivity in the creation on mauiProgram

    public static MauiApp CreateMauiApp() {

        // Check internet connectivity before starting the app



        var builder = MauiApp.CreateBuilder();

        var firebaseAuthConfig = new FirebaseAuthConfig() {
            ApiKey = "AIzaSyBm8iocSck29uHfMZ7tI_B5TD3z-9hpvhQ",
            AuthDomain = "demy-ia-6ca24.firebaseapp.com",
            Providers = new FirebaseAuthProvider[] {
                new EmailProvider()
            }
        };


        builder
            .UseMauiApp<App>()
            .ConfigureSyncfusionCore()
                .UseMauiCommunityToolkit()
                .ConfigureFonts(fonts => {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                    fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
                });

#if DEBUG
        builder.Logging.AddDebug();
#endif
        builder.Services.AddSingleton<HttpClient>();
        builder.Services.AddSingleton<IAppServices, AppServices>();
        builder.Services.AddSingleton<IHttpService, HttpService>();
        builder.Services.AddSingleton<AppShell>();
        builder.Services.AddTransient<AppShellViewModel>();
        builder.Services.AddSingleton<LoginPage>();
        builder.Services.AddSingleton(Connectivity.Current);

        var firebaseAuthClient = new FirebaseAuthClient(firebaseAuthConfig);

        builder.Services.AddSingleton(firebaseAuthClient);

        builder.Services.AddSingleton<IAuthenticationService>(serviceProvider => {
            var authService = serviceProvider.GetRequiredService<FirebaseAuthClient>();
            return new AuthenticationService(authService);
        });

        return builder.Build();
    }
}

Developer technologies | .NET | .NET Multi-platform App UI

Answer accepted by question author

Anonymous
2023-11-29T07:25:37.2033333+00:00

You cannot display an alert or navigate to other pages in the MauiProgram.cs when application is starting. But you can popup alert in the first contentpage's OnAppearing method like following code. If no internet, it will pop up an alert with your services, then users click Ok, this application will be closed.

protected override async void OnAppearing()
{
    base.OnAppearing();


   var connectivity = Connectivity.Current;
    if (connectivity.NetworkAccess != NetworkAccess.Internet)
    {
        // Handle the lack of internet connection
        // For example: Display an alert or take any appropriate action
        AppServices appServices=    new AppServices();
        await appServices.DisplayAlertAsync("Alert","No Internet","OK").ContinueWith((e) => { Application.Current.Quit(); });              
    }
   
}

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.