Ejemplo n.º 1
0
        public void LoadSettings()
        {
            _settings = new Dictionary <string, Setting>();

            if (_userProvider.Username != null && _localStorage.ContainKey("SettingsData." + _userProvider.Username))
            {
                var settings = _localStorage.GetItem <Dictionary <string, Setting> >("SettingsData." + _userProvider.Username);
                foreach (var setting in settings)
                {
                    _settings[setting.Key] = setting.Value;
                }
            }

            if (_localStorage.ContainKey("SettingsData"))
            {
                var settings = _localStorage.GetItem <Dictionary <string, Setting> >("SettingsData");
                foreach (var setting in settings)
                {
                    setting.Value.Inherited = _userProvider.Username == null;
                    _settings[setting.Key]  = setting.Value;
                }
            }

            SettingsUpdated?.Invoke(this, new SettingsEventArgs
            {
                Settings = _settings.Values.ToArray()
            });
        }
        public AuthCredentialsKeeper(ISyncLocalStorageService localStorage)
        {
            _localStorage = localStorage;

            if (_localStorage.ContainKey(INDEX_USERNAME) && _localStorage.ContainKey(INDEX_TOKEN))
            {
                Username = _localStorage.GetItem <string>(INDEX_USERNAME);
                Token    = _localStorage.GetItem <string>(INDEX_TOKEN);
            }
        }
 public bool Clear()
 {
     lock (thisLock)
     {
         if (localStorage.ContainKey(GetFileName()))
         {
             localStorage.RemoveItem(GetFileName());
             return(true);
         }
         else
         {
             return(false);
         }
     }
 }
Ejemplo n.º 4
0
        private List <CartItemModel> ReadCartItemsFromLocalStorage()
        {
            if (localStorage.ContainKey(LocalStorageConstants.CartItems))
            {
                return(localStorage.GetItem <List <CartItemModel> >(LocalStorageConstants.CartItems));
            }

            return(new List <CartItemModel>());
        }
Ejemplo n.º 5
0
 public Storage(ISyncLocalStorageService localStorage, Options options)
 {
     this.localStorage = localStorage;
     this.options      = options;
     if (!localStorage.ContainKey(FirstRunKey))
     {
         SetDefault();
     }
 }
Ejemplo n.º 6
0
        public ControllerService(ISyncLocalStorageService localStorageService)
        {
            this.httpClient = new HttpClient
            {
                Timeout = TimeSpan.FromSeconds(3)
            };
            this.localStorageService = localStorageService;
            macAddress  = localStorageService.ContainKey(nameof(MacAddress)) ? localStorageService.GetItemAsString(nameof(MacAddress)) : "";
            baseAddress = localStorageService.ContainKey(nameof(BaseAddress)) ? localStorageService.GetItemAsString(nameof(BaseAddress)) : "";
            pin         = localStorageService.ContainKey(nameof(PIN)) ? localStorageService.GetItemAsString(nameof(PIN)) : "";

            var cmds = Enum.GetValues(typeof(ControllerCommandType)).Cast <ControllerCommandType>()
                       .Select(o => new LabeledCommand(o.ToString(), new Command(() => InvokeCommandInBackground(o))))
                       .ToList();

            SupportedCommands = new[]
            {
                new LabeledCommand("WoL", new Command(() => WakeOnLan()))
            }
            .Concat(cmds)
            .ToList();

            pinger = new Ping();

            Task.Run(async() =>
            {
                while (!isDisposed)
                {
                    var start = DateTime.Now;

                    (Status, ErrorMessage) = await GetStatusAsync();

                    var time = TimeSpan.FromSeconds(3) - (DateTime.Now - start);
                    if (time.Ticks > 0)
                    {
                        await Task.Delay(time);
                    }
                    else
                    {
                        await Task.Yield();
                    }
                }
            });
        }
Ejemplo n.º 7
0
        private async Task LoadRouteData()
        {
            await updateLock.WaitAsync();

            try
            {
                if (_userProvider.UserDetails != null && !_shipProvider.DataRefreshing)
                {
                    Dictionary <int, AutoRoute> newRouteData = null;
                    if (_localStorage.ContainKey("RouteData." + _userProvider.Username))
                    {
                        newRouteData = _localStorage.GetItem <Dictionary <int, AutoRoute> >("RouteData." + _userProvider.Username);
                    }
                    newRouteData ??= new Dictionary <int, AutoRoute>();

                    if (newRouteData != null)
                    {
                        foreach (var route in newRouteData)
                        {
                            foreach (var routeShip in route.Value.Ships)
                            {
                                routeShip.ShipData = _shipProvider.GetShipData(routeShip.ShipId);
                            }
                        }
                        foreach (var route in newRouteData)
                        {
                            route.Value.Ships = route.Value.Ships.Where(t => t.ShipData != null).ToArray();
                        }
                    }

                    _routeData = newRouteData;

                    SaveRouteData();
                }
                else
                {
                    _routeData = null;
                }

                RoutesUpdated?.Invoke(this, new RouteEventArgs
                {
                    Routes        = GetRouteData(),
                    IsFullRefresh = true
                });
            }
            finally
            {
                updateLock.Release();
            }
        }
Ejemplo n.º 8
0
        public UserProvider(
            ISyncLocalStorageService localStorage,
            HttpClient http,
            JsonSerializerOptions serializerOptions,
            IConfiguration config
            )
        {
            _localStorage      = localStorage;
            _http              = http;
            _serializerOptions = serializerOptions;
            _config            = config;

            if (_localStorage.ContainKey("Token"))
            {
                _ = SetDetailsAsync(_localStorage.GetItemAsString("Token"));
            }
            else
            {
                StartingDetailsChecked = true;
            }
        }
Ejemplo n.º 9
0
        public async Task RefreshShipData()
        {
            DataRefreshing = true;

            if (_userProvider.UserDetails != null)
            {
                Dictionary <string, ShipData> newShipData = null;
                var shipResponse = await _http.GetFromJsonAsync <ShipsResponse>("/my/ships", _serializerOptions);

                var ships = shipResponse?.Ships;

                if (_localStorage.ContainKey("ShipData." + _userProvider.Username))
                {
                    newShipData = _localStorage.GetItem <Dictionary <string, ShipData> >("ShipData." + _userProvider.Username);
                }
                newShipData ??= new Dictionary <string, ShipData>();

                if (newShipData.Count > 0)
                {
                    if (ships == null)
                    {
                        newShipData.Clear();
                    }
                    else
                    {
                        var shipsToRemove = newShipData.Keys.Where(t => !ships.Any(s => t == s.Id)).ToArray();
                        foreach (var key in shipsToRemove)
                        {
                            newShipData.Remove(key);
                        }
                    }
                }

                var takenIds = new List <int>();
                foreach (var ship in newShipData)
                {
                    if (takenIds.Contains(ship.Value.Id))
                    {
                        ship.Value.Id = GetNewShipId(newShipData.Values.ToArray());
                    }
                    takenIds.Add(ship.Value.Id);
                }

                if (ships != null)
                {
                    var shipsToAdd = ships.Where(t => !newShipData.ContainsKey(t.Id)).ToArray();
                    var currentIds = newShipData.Select(s => s.Value.Id);
                    for (var x = 0; x < shipsToAdd.Length; x++)
                    {
                        newShipData.Add(shipsToAdd[x].Id, new ShipData
                        {
                            Id          = GetNewShipId(newShipData.Values.ToArray()),
                            DisplayName = shipsToAdd[x].Id,
                            ServerId    = shipsToAdd[x].Id,
                        });
                    }
                }

                var timeNow = DateTimeOffset.UtcNow;
                foreach (var ship in newShipData)
                {
                    ship.Value.Ship = ships.First(t => t.Id == ship.Key);

                    if (ship.Value.Ship.Location == null && ship.Value.LastFlightPlan?.Id != ship.Value.Ship.FlightPlanId)
                    {
                        ship.Value.LastFlightPlan = null;

                        try
                        {
                            var flightPlanResponse = await _http.GetFromJsonAsync <FlightResponse>("/my/flight-plans/" + ship.Value.Ship.FlightPlanId, _serializerOptions);

                            ship.Value.LastFlightPlan = flightPlanResponse.FlightPlan;
                        }
                        catch (Exception) { }
                    }

                    if (ship.Value.LastFlightPlan != null)
                    {
                        ship.Value.LastFlightPlan.TimeRemainingInSeconds = (int)Math.Ceiling(ship.Value.LastFlightPlan.ArrivesAt.Subtract(timeNow).TotalSeconds);
                    }
                    ship.Value.FlightEnded = ship.Value.Ship.Location != null || ship.Value.LastFlightPlan == null || ship.Value.LastFlightPlan?.TimeRemainingInSeconds < 0;
                }

                _shipData = newShipData;

                AssignShipNames();
                SaveShipData();
            }
            else
            {
                _shipData = null;
            }

            DataRefreshing = false;
            LastUpdate     = DateTimeOffset.UtcNow;

            ShipsUpdated?.Invoke(this, new ShipEventArgs
            {
                ShipDetails   = GetShipData(),
                IsFullRefresh = true
            });
        }
Ejemplo n.º 10
0
        internal bool GetVisible(string id)
        {
            var key = VisibleKey(id);

            return(!localStorage.ContainKey(key) || localStorage.GetItem <bool>(key));
        }
Ejemplo n.º 11
0
 public bool HasCredentials()
 {
     return(_localStorage.ContainKey(TokenKey));
 }
Ejemplo n.º 12
0
 public bool IsExistkey(string key)
 {
     return(_syncLocalStorageService.ContainKey(key));
 }
Ejemplo n.º 13
0
 public static bool HasFirstTimeLaunchKey(this ISyncLocalStorageService localStorage)
 => localStorage.ContainKey(key_firstTimeLaunch);
Ejemplo n.º 14
0
 public static bool HasDemoDataKey(this ISyncLocalStorageService localStorage)
 => localStorage.ContainKey(key_hasDemoData);
Ejemplo n.º 15
0
 public static bool HasRefreshToken(this ISyncLocalStorageService localStorage)
 {
     return(localStorage.ContainKey(LocalStorageConstants.RefreshToken));
 }
Ejemplo n.º 16
0
 public static bool HasAccessToken(this ISyncLocalStorageService localStorage)
 {
     return(localStorage.ContainKey(LocalStorageConstants.TokenType) &&
            localStorage.ContainKey(LocalStorageConstants.AccessToken));
 }