示例#1
0
        public async Task OnNavigatedTo()
        {
            // initialize firebase helper
            _firebaseHelper = new FirebaseHelper();

            // initialize list for storing favorites cities, which will be shown on the page
            _favoriteCitiesTmp = new List <CityForecastItem>(); // tmp list
            FavoriteCities     = new ObservableCollection <CityForecastItem>();

            await RefreshFavoriteCitiesList();
        }
        /// <summary>
        /// Checks if the city is in favorites list and sets favorite icon depending on that
        /// </summary>
        /// <returns></returns>
        private async Task IsCityInFavorites()
        {
            // get the current favorites list
            FirebaseHelper firebaseHelper = new FirebaseHelper();
            await firebaseHelper.GetFavoritesCities();

            if (await firebaseHelper.CityIsInFavorites(_weatherForecastService.CityId))
            {
                FavoriteIconPath = Constants.FavoriteIconPath;
            }
            else
            {
                FavoriteIconPath = Constants.FavoriteBorderIconPath;
            }
        }
        /// <summary>
        /// Adds city to the favorite cities list if the city doesn't belong to it
        /// Deletes city from the favorite cities list if the city belong to it
        /// </summary>
        /// <returns></returns>
        private async Task AddOrDeleteCityFromFavorites()
        {
            // get the current favorites list
            FirebaseHelper firebaseHelper = new FirebaseHelper();
            await firebaseHelper.GetFavoritesCities();

            // Add or delete the city form favorites
            if (await firebaseHelper.CityIsInFavorites(_weatherForecastService.CityId))
            {
                await firebaseHelper.DeleteCityFromFavorites(_weatherForecastService.CityId);
            }
            else
            {
                await firebaseHelper.PutFavoriteCity(
                    _weatherForecastService.CityName,
                    _weatherForecastService.CityId
                    );
            }

            // reload favorite icon
            await IsCityInFavorites();
        }