예제 #1
0
        public HomePage([Optional] string url)
        {
            InitializeComponent();

            if (Device.RuntimePlatform == Device.iOS && !string.IsNullOrWhiteSpace(url) && url.StartsWith("safe://"))
            {
                _launchUrl = url;
            }

            AddressBarEntry.Focused     += AddressBarEntryFocused;
            AddressBarEntry.Unfocused   += AddressBarEntryUnfocused;
            AddressBarEntry.TextChanged += AddressBarEntryTextChanged;
            AddressBarEntry.Completed   += AddressBarTextChangeCompleted;

            MessagingCenter.Subscribe <BookmarksModalPageViewModel, string>(
                this,
                MessageCenterConstants.BookmarkUrl,
                (sender, args) =>
            {
                _viewModel.LoadUrl(args);
            });
            MessagingCenter.Subscribe <App, string>(
                this,
                MessageCenterConstants.LoadSafeWebsite,
                (sender, args) =>
            {
                _viewModel.LoadUrl(args);
            });
            MessagingCenter.Subscribe <MenuPopUpViewModel>(
                this,
                MessageCenterConstants.ReloadMessage,
                (sender) =>
            {
                _viewModel.IsNavigating = true;
                _viewModel.ReloadCommand.Execute(null);
            });
            MessagingCenter.Subscribe <HomePageViewModel>(
                this,
                MessageCenterConstants.ShowErrorPage,
                async(sender) =>
            {
                if (((UrlWebViewSource)HybridWebViewControl.Source).Url != _viewModel.WelcomePageUrl)
                {
                    HybridWebViewControl.Source = _viewModel.WelcomePageUrl;
                }
                else
                {
                    await UpdateHTMLPageToShowErrorAsync();
                    _viewModel.IsErrorState = false;
                    if (_viewModel.IsNavigating)
                    {
                        _viewModel.IsNavigating = false;
                    }
                }
            });
            MessagingCenter.Subscribe <HomePageViewModel>(
                this,
                MessageCenterConstants.UpdateErrorMsg,
                async(sender) =>
            {
                await UpdateHTMLPageToShowErrorAsync();
            });
            MessagingCenter.Subscribe <App>(
                this,
                MessageCenterConstants.InitialiseSession,
                async(sender) =>
            {
                await _viewModel.InitilizeSessionAsync();
            });
            MessagingCenter.Subscribe <App>(
                this,
                MessageCenterConstants.SessionReconnect,
                async(sender) =>
            {
                if (App.AppSession.IsDisconnected)
                {
                    await ReconnectSessionAsync();
                }
            });
            MessagingCenter.Subscribe <App>(
                this,
                MessageCenterConstants.UpdateWelcomePageTheme,
                async(sender) =>
            {
                var theme        = Xamarin.Essentials.Preferences.Get(Constants.AppThemePreferenceKey, false);
                var jsToEvaluate = "ChangeBackgroundColor (" + $"'{theme.ToString()}'" + ")";
                await HybridWebViewControl.EvaluateJavaScriptAsync(jsToEvaluate);
            });
        }
예제 #2
0
        public HomePage([Optional] string url)
        {
            InitializeComponent();

            if (Device.RuntimePlatform == Device.iOS && !string.IsNullOrWhiteSpace(url) && url.StartsWith("safe://"))
            {
                _launchUrl = url;
            }

            AddressBarEntry.Focused     += AddressBarEntryFocused;
            AddressBarEntry.Unfocused   += AddressBarEntryUnfocused;
            AddressBarEntry.TextChanged += AddressBarEntryTextChanged;
            AddressBarEntry.Completed   += AddressBarTextChangeCompleted;

            MessagingCenter.Subscribe <App, string>(
                this,
                MessageCenterConstants.LoadSafeWebsite,
                (sender, args) =>
            {
                _viewModel.LoadUrl(args);
            });
            MessagingCenter.Subscribe <MenuPopUpViewModel>(
                this,
                MessageCenterConstants.ReloadMessage,
                (sender) =>
            {
                _viewModel.IsNavigating = true;
                _viewModel.ReloadCommand.Execute(null);
            });
            MessagingCenter.Subscribe <HomePageViewModel>(
                this,
                MessageCenterConstants.ShowErrorPage,
                async(sender) =>
            {
                if (((UrlWebViewSource)HybridWebViewControl.Source).Url != _viewModel.WelcomePageUrl)
                {
                    HybridWebViewControl.Source = _viewModel.WelcomePageUrl;
                }
                else
                {
                    await UpdateHTMLPageToShowErrorAsync();
                    _viewModel.IsErrorState = false;
                    if (_viewModel.IsNavigating)
                    {
                        _viewModel.IsNavigating = false;
                    }
                }
            });
            MessagingCenter.Subscribe <HomePageViewModel>(
                this,
                MessageCenterConstants.UpdateErrorMsg,
                async(sender) =>
            {
                await UpdateHTMLPageToShowErrorAsync();
            });
            MessagingCenter.Subscribe <App>(
                this,
                MessageCenterConstants.InitialiseSession,
                async(sender) =>
            {
                await _viewModel.InitilizeSessionAsync();
            });
            MessagingCenter.Subscribe <App>(
                this,
                MessageCenterConstants.SessionReconnect,
                async(sender) =>
            {
                if (App.AppSession.IsDisconnected)
                {
                    await ReconnectSessionAsync();
                }
            });
            MessagingCenter.Subscribe <App>(
                this,
                MessageCenterConstants.UpdateWelcomePageTheme,
                async(sender) =>
            {
                try
                {
                    var theme        = Xamarin.Essentials.Preferences.Get(Constants.AppThemePreferenceKey, false);
                    var jsToEvaluate = "ChangeBackgroundColor (" + $"'{theme}'" + ")";
                    await HybridWebViewControl?.EvaluateJavaScriptAsync(jsToEvaluate);
                }
                catch (Exception ex)
                {
                    Logger.Error(ex);
                }
            });
            MessagingCenter.Subscribe <AuthenticationService>(
                this,
                MessageCenterConstants.Authenticated,
                (sender) =>
            {
                _viewModel.UpdateAuthenticationState();
            });

            // Todo: Required to add no internet connection status in the top navigation bar
            // MessagingCenter.Subscribe<App>(
            //    this,
            //    MessageCenterConstants.NoInternet,
            //    (sender) =>
            //    {
            //        _viewModel.UpdateAuthenticationState(true);
            //    });

            HybridWebViewControl.Navigated += HybridWebViewControl_NavigatedAsync;
        }