using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Threading.Tasks; using Serilog; using Serilog.Events; using TINK.Model; using TINK.Model.Connector; using TINK.Model.User.Account; using TINK.MultilingualResources; using TINK.Repository.Exception; using TINK.Services; using TINK.Services.BluetoothLock; using TINK.Services.Geolocation; using TINK.Settings; using TINK.View; using TINK.ViewModel.Map; using TINK.ViewModel.Settings; using Xamarin.Forms; namespace TINK.ViewModel { /// /// View model for settings. /// public class SettingsPageViewModel : INotifyPropertyChanged { /// /// Reference on view service to show modal notifications and to perform navigation. /// private IViewService m_oViewService; /// /// Fired if a property changes. /// public event PropertyChangedEventHandler PropertyChanged; /// Object to manage update of view model objects from Copri. private IPollingUpdateTaskManager m_oViewUpdateManager; /// List of copri server uris. public CopriServerUriListViewModel CopriServerUriList { get; } /// Manages selection of locks services. public LocksServicesViewModel LocksServices { get; } /// Settings determining the startup behavior of the app. public ServicesViewModel StartupSettings { get; } /// Manages selection of geolocation services. public ServicesViewModel GeolocationServices { get; } /// /// Object to switch logging level. /// private LogEventLevel m_oMinimumLogEventLevel; /// Gets a value indicating whether reporting level is verbose or not. public bool IsReportLevelVerbose { get; set; } /// List of copri server uris. public ServicesViewModel Themes { get; } /// Reference on the tink app instance. private ITinkApp TinkApp { get; } IServicesContainer GeolocationServicesContainer { get; } /// Constructs a settings page view model object. /// Reference to tink app model. /// /// Interface to view public SettingsPageViewModel( ITinkApp tinkApp, IServicesContainer geolocationServicesContainer, IViewService viewService) { TinkApp = tinkApp ?? throw new ArgumentException("Can not instantiate settings page view model- object. No tink app object available."); GeolocationServicesContainer = geolocationServicesContainer ?? throw new ArgumentException($"Can not instantiate {nameof(SettingsPageViewModel)}- object. Geolocation services container object must not be null."); m_oViewService = viewService ?? throw new ArgumentException("Can not instantiate settings page view model- object. No user view service available."); m_oMinimumLogEventLevel = TinkApp.MinimumLogEventLevel; IsReportLevelVerbose = TinkApp.IsReportLevelVerbose; CenterMapToCurrentLocation = TinkApp.CenterMapToCurrentLocation; ExternalFolder = TinkApp.ExternalFolder; IsLogToExternalFolderVisible = !string.IsNullOrEmpty(ExternalFolder); LogToExternalFolderDisplayValue = IsLogToExternalFolderVisible ? TinkApp.LogToExternalFolder : false; IsSiteCachingOnDisplayValue = TinkApp.IsSiteCachingOn; if (TinkApp.Uris == null || TinkApp.Uris.Uris.Count <= 0) { throw new ArgumentException("Can not instantiate settings page view model- object. No uri- list available."); } if (string.IsNullOrEmpty(TinkApp.NextActiveUri.AbsoluteUri)) { throw new ArgumentException("Can not instantiate settings page view model- object. Next active uri must not be null or empty."); } GroupFilter = new SettingsBikeFilterViewModel( TinkApp.FilterGroupSetting, TinkApp.ActiveUser.IsLoggedIn ? TinkApp.ActiveUser.Group : null); GroupFilter.PropertyChanged += (s, e) => { // Serialize on value changed. On iOS OnDisappearing might not be invoked (when app is directly closed before leaving settings page). try { var filterGroup = GroupFilter.ToDictionary(x => x.Key, x => x.State); TinkApp.FilterGroupSetting = new GroupFilterSettings(filterGroup.Count > 0 ? filterGroup : null); // Update map page filter. // Reasons for which map page filter has to be updated: // - user activated/ deactivated a group (cargo/ city bikes) TinkApp.GroupFilterMapPage = GroupFilterMapPageHelper.CreateUpdated( TinkApp.GroupFilterMapPage, TinkApp.ActiveUser.DoFilter(TinkApp.FilterGroupSetting.DoFilter())); TinkApp.Save(); } catch (Exception ex) { Log.ForContext().Error(ex, "Serializing startup page failed."); } }; m_oViewUpdateManager = new IdlePollingUpdateTaskManager(); Polling = new PollingViewModel(TinkApp.Polling); ExpiresAfterTotalSeconds = Convert.ToInt32(TinkApp.ExpiresAfter.TotalSeconds); CopriServerUriList = new CopriServerUriListViewModel(TinkApp.Uris); Themes = new ServicesViewModel( TinkApp.Themes.Select(x => x.GetType().FullName), new Dictionary { { typeof(Themes.Konrad).FullName, "Mein konrad" /* display name in picker */}, { typeof(Themes.ShareeBike).FullName, "sharee.bike" /* display name in picker */}, { typeof(Themes.LastenradBayern).FullName, "LastenradBayern" /* display name in picker */} }, TinkApp.Themes.Active.GetType().FullName); Themes.PropertyChanged += OnThemesChanged; LocksServices = new LocksServicesViewModel( TinkApp.LocksServices.Active.TimeOut.MultiConnect, new ServicesViewModel( TinkApp.LocksServices, new Dictionary { { typeof(LocksServiceInReach).FullName, "Simulation - AllLocksInReach" }, { typeof(LocksServiceOutOfReach).FullName, "Simulation - AllLocksOutOfReach" }, { typeof(Services.BluetoothLock.BLE.LockItByScanServiceEventBased).FullName, "Live - Scan" }, { typeof(Services.BluetoothLock.BLE.LockItByScanServicePolling).FullName, "Live - Scan (Polling)" }, { typeof(Services.BluetoothLock.BLE.LockItByGuidService).FullName, "Live - Guid" }, /* { typeof(Services.BluetoothLock.Arendi.LockItByGuidService).FullName, "Live - Guid (Arendi)" }, { typeof(Services.BluetoothLock.Arendi.LockItByScanService).FullName, "Live - Scan (Arendi)" }, { typeof(Services.BluetoothLock.Bluetoothle.LockItByGuidService).FullName, "Live - Guid (Ritchie)" }, */ }, TinkApp.LocksServices.Active.GetType().FullName)); GeolocationServices = new ServicesViewModel( GeolocationServicesContainer.Select(x => x.GetType().FullName), new Dictionary { { typeof(LastKnownGeolocationService).FullName, "LastKnowGeolocation" }, { typeof(GeolocationAccuracyMediumService).FullName, "Medium Accuracy" }, { typeof(GeolocationAccuracyHighService).FullName, "High Accuracy" }, { typeof(GeolocationAccuracyBestService).FullName, "Best Accuracy" }, { typeof(SimulatedGeolocationService).FullName, "Simulation-AlwaysSamePosition" } }, GeolocationServicesContainer.Active.GetType().FullName); StartupSettings = new PickerViewModel( new Dictionary { { ViewTypes.MapPage.ToString(), AppResources.MarkingMapPage }, { ViewTypes.FindBikePage.ToString(), AppResources.MarkingFindBike }, }, tinkApp.StartupSettings.StartupPage.ToString()); StartupSettings.PropertyChanged += (s, e) => { // Serialize on value changed. On iOS OnDisappearing might not be invoked (when app is directly closed before leaving settings page). try { TinkApp.StartupSettings.StartupPage = Enum.TryParse(StartupSettings.Active, out ViewTypes startupPage) ? startupPage : Model.Settings.StartupSettings.DefaultStartupPage; TinkApp.Save(); } catch (Exception ex) { Log.ForContext().Error(ex, "Serializing startup page failed."); } }; } /// /// User switches scheme. /// private void OnThemesChanged(object sender, PropertyChangedEventArgs e) { // Set active theme (leads to switch of title) TinkApp.Themes.SetActive(Themes.Active); // Switch theme. ICollection mergedDictionaries = Application.Current.Resources.MergedDictionaries; if (mergedDictionaries == null) { Log.ForContext().Error("No merged dictionary available."); return; } mergedDictionaries.Clear(); if (Themes.Active == typeof(Themes.Konrad).FullName) { mergedDictionaries.Add(new Themes.Konrad()); } else if (Themes.Active == typeof(Themes.ShareeBike).FullName) { mergedDictionaries.Add(new Themes.ShareeBike()); } else if (Themes.Active == typeof(Themes.LastenradBayern).FullName) { mergedDictionaries.Add(new Themes.LastenradBayern()); } else { Log.ForContext().Debug($"No theme {Themes.Active} found."); } } /// Holds information whether app is connected to web or not. private bool? isConnected = null; /// Exposes the is connected state. private bool IsConnected { get => isConnected ?? false; set { isConnected = value; } } /// Holds a value indicating whether group filters GUI are visible or not public bool IsGroupFilterVisible => GroupFilter.Count > 0; /// Holds the bike types to fade out or show public SettingsBikeFilterViewModel GroupFilter { get; } /// Gets the value to path were copri mock files are located (for debugging purposes). public string ExternalFolder { get; } /// /// Gets the value to path were copri mock files are located (for debugging purposes). /// public string InternalPath => TinkApp.SettingsFileFolder; /// /// Gets the value of device identifier (for debugging purposes). /// public string DeviceIdentifier => TinkApp.SmartDevice.Identifier; /// /// Invoked when page is shutdown. /// Currently invoked by code behind, would be nice if called by XAML in future versions. /// public async Task OnDisappearing() { try { Log.ForContext().Information($"Entering {nameof(OnDisappearing)}..."); // Update model values. TinkApp.NextActiveUri = CopriServerUriList.NextActiveUri; TinkApp.Polling = new PollingParameters( new TimeSpan(0, 0, Polling.PeriodeTotalSeconds), Polling.IsActivated); TinkApp.ExpiresAfter = TimeSpan.FromSeconds(ExpiresAfterTotalSeconds); if (IsLogToExternalFolderVisible) { // If no external folder is available do not update model value. TinkApp.LogToExternalFolder = LogToExternalFolderDisplayValue; } TinkApp.IsSiteCachingOn = IsSiteCachingOnDisplayValue; TinkApp.MinimumLogEventLevel = m_oMinimumLogEventLevel; // Update value to be serialized. TinkApp.UpdateLoggingLevel(m_oMinimumLogEventLevel); // Update logging server. TinkApp.IsReportLevelVerbose = IsReportLevelVerbose; TinkApp.LocksServices.SetActive(LocksServices.Services.Active); GeolocationServicesContainer.SetActive(GeolocationServices.Active); TinkApp.LocksServices.SetTimeOut(TimeSpan.FromSeconds(LocksServices.ConnectTimeoutSec)); // Persist settings in case app is closed directly. TinkApp.Save(); TinkApp.UpdateConnector(); await m_oViewUpdateManager.StopUpdatePeridically(); Log.ForContext().Information($"{nameof(OnDisappearing)} done."); } catch (Exception l_oException) { await m_oViewService.DisplayAlert( "Fehler", $"Ein unerwarteter Fehler ist aufgetreten. \r\n{l_oException.Message}", "OK"); } } /// True if there is an error message to display. /// /// Exception which occurred getting bike information. /// protected Exception Exception { get; set; } /// /// If true debug controls are visible, false if not. /// public Permissions DebugLevel { get { return (Exception == null || Exception is WebConnectFailureException) ? TinkApp.ActiveUser.DebugLevel : Permissions.None; } } /// Empty if no user is logged in session cookie otherwise. public string SessionCookie => TinkApp.ActiveUser.IsLoggedIn ? TinkApp.ActiveUser.SessionCookie : ""; /// Polling period. public PollingViewModel Polling { get; } /// Active logging level public string SelectedLoggingLevel { get { return m_oMinimumLogEventLevel.ToString(); } set { if (!Enum.TryParse(value, out LogEventLevel l_oNewLevel)) { return; } m_oMinimumLogEventLevel = l_oNewLevel; } } bool _CenterMapToCurrentLocation = true; public bool CenterMapToCurrentLocation { get => _CenterMapToCurrentLocation; set { if (value == _CenterMapToCurrentLocation) { // Nothing to do. return; } _CenterMapToCurrentLocation = value; // Serialize on value changed. On iOS OnDisappearing might not be invoked (when app is directly closed before leaving settings page). TinkApp.CenterMapToCurrentLocation = CenterMapToCurrentLocation; TinkApp.Save(); } } /// Holds either /// - a value indicating whether to use external folder (e.g. SD card)/ or internal folder for storing log-files or /// - is false if external folder is not available /// public bool LogToExternalFolderDisplayValue { get; set; } public bool IsSiteCachingOnDisplayValue { get; set; } /// Holds a value indicating whether user can use external folder (e.g. SD card) for storing log-files. public bool IsLogToExternalFolderVisible { get; } /// /// Holds the logging level serilog provides. /// public List LoggingLevels { get { return new List { LogEventLevel.Verbose.ToString(), LogEventLevel.Debug.ToString(), LogEventLevel.Information.ToString(), LogEventLevel.Warning.ToString(), LogEventLevel.Error.ToString(), LogEventLevel.Fatal.ToString(), }; } } double expiresAfterTotalSeconds; public double ExpiresAfterTotalSeconds { get => expiresAfterTotalSeconds; set { if (value == expiresAfterTotalSeconds) { return; } expiresAfterTotalSeconds = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ExpiresAfterTotalSecondsText))); } } public string ExpiresAfterTotalSecondsText { get => expiresAfterTotalSeconds.ToString("0"); } } }