/// <summary> /// Sets the latest selected item from the server list and sets it to serverListSelectedItem. /// </summary> /// <param name="initialLoad">Are we currently loading the.</param> public void RefreshServerListSelectedItem(bool initialLoad = false) { // Cannot set selected item if server list contains no servers if (FxA.Cache.FxAServerList.GetServerList().Count() == 0) { return; } // Set the selected item of the server list to the server specificed in the current WireGuard configuration. If it doesn't exist, default to the first server try { var previouslySelectedServerIndex = 0; if (initialLoad) { // Get the saved WireGuard configuration var configuration = new WireGuard.Config(ProductConstants.FirefoxPrivateNetworkConfFile); previouslySelectedServerIndex = FxA.Cache.FxAServerList.GetServerIndexByIP(configuration.GetPeerEndpointWithoutPort()); } else { // Get the selected server prior to server list refresh previouslySelectedServerIndex = FxA.Cache.FxAServerList.GetServerIndexByIP(serverListSelectedItem.Endpoint); } serverListSelectedItem = FxA.Cache.FxAServerList.GetServerList()[previouslySelectedServerIndex]; } catch (Exception) { serverListSelectedItem = FxA.Cache.FxAServerList.GetServerList()[0]; } }
/// <summary> /// Sets the latest selected item from the server list and sets it to serverListSelectedItem. /// </summary> /// <param name="initialLoad">Are we currently loading the.</param> public void RefreshServerListSelectedItem(bool initialLoad = false) { // Cannot set selected item if server list contains no servers if (FxA.Cache.FxAServerList.GetServerCitiesList().Count() == 0) { return; } // Set the selected item of the server list to the server specificed in the current WireGuard configuration. If it doesn't exist, default to random server in US try { var previouslySelectedServerIndex = 0; if (initialLoad || newUserSignIn) { newUserSignIn = false; // Get the saved WireGuard configuration var configuration = new WireGuard.Config(ProductConstants.FirefoxPrivateNetworkConfFile); previouslySelectedServerIndex = FxA.Cache.FxAServerList.GetServerIndexByIP(configuration.GetPeerEndpointWithoutPort()); } else { // Get the selected server prior to server list refresh previouslySelectedServerIndex = FxA.Cache.FxAServerList.GetServerIndexByIP(ServerSelected.Endpoint); } var selectedServerCity = FxA.Cache.FxAServerList.GetServerItems()[previouslySelectedServerIndex].City; ServerCityListSelectedItem = FxA.Cache.FxAServerList.GetServerCitiesList().FirstOrDefault(x => x.City == selectedServerCity); } catch (Exception) { Random rand = new Random(); var serverCitiesInDefaultServerCounty = FxA.Cache.FxAServerList.GetServerCitiesList().Where(x => x.Country == DefaultServerCountry); if (serverCitiesInDefaultServerCounty.Count() > 0) { ServerCityListSelectedItem = serverCitiesInDefaultServerCounty.ElementAt(rand.Next(0, serverCitiesInDefaultServerCounty.Count())); } else { ServerCityListSelectedItem = FxA.Cache.FxAServerList.GetServerCitiesList()[rand.Next(0, FxA.Cache.FxAServerList.GetServerCitiesList().Count)]; } } finally { UpdateServerSelection(); } }