Exemplo n.º 1
0
        /// <summary>
        /// Refreshes the list of available tips.
        /// </summary>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation. </returns>
        private async Task RefreshTipsAsync()
        {
            try
            {
                // TODO: allow selecting the language and level classification
                Tips tipsEndpoint = new Tips(App.OAuth2Account, SupportedLanguage.English, LanguageLevelClassification.B1);
                this._tipsTask = Task.Run(async() => await tipsEndpoint.CallEndpointAsObjectAsync());
                await this._tipsTask;
            }
            catch
            {
            }

            this.OnPropertyChanged("IsNotCompleted");

            if (this._tipsTask.IsFaulted)
            {
                this.OnPropertyChanged("IsFaulted");
                this.OnPropertyChanged("ErrorMessage");
            }

            if (this._tipsTask.IsCanceled || this._tipsTask.IsFaulted)
            {
                this.ApplicationTips = new ReadOnlyObservableCollection <Tip>(new ObservableCollection <Tip>(new List <Tip>()));
            }
            else
            {
                this.ApplicationTips = new ReadOnlyObservableCollection <Tip>(new ObservableCollection <Tip>(this._tipsTask.Result));
                this.OnPropertyChanged("IsSuccessfullyCompleted");
            }

            this.OnPropertyChanged("SingleRandomTip");
        }
Exemplo n.º 2
0
        public static async Task <Tip> GetSingleTipAsync()
        {
            if (TipsDataModel.tipsCache.Count == 0)
            {
                // FIXME: allow choosing the correct language and language level
                Tips tipsEndpoint = new Tips(App.OAuth2Account, SupportedLanguage.English, LanguageLevelClassification.B1);
                TipsDataModel.tipsCache = await Task.Run(async() => await tipsEndpoint.CallEndpointAsObjectAsync());
            }

            int choosen = new Random().Next(TipsDataModel.tipsCache.Count - 1);

            if (TipsDataModel.tipsCache.Count == 0)
            {
                Tools.Logger.Log("TipsController", "Empty list, something went wrong.");
                return(new Tip()
                {
                    Id = -1,
                    Text = "An error occurred. Please report this message to the dev team.",
                });
            }
            else
            {
                return(TipsDataModel.tipsCache[choosen]);
            }
        }