Exemplo n.º 1
0
        protected async Task LoadAnswer(int questionId)
        {
            AnswerInfo currentAnswer = null;

            // 1. Load from the database
            currentAnswer = await App.StackDataManager.Database.GetAnswerForQuestion(questionId);

            if (currentAnswer != null)
            {
                _theAnswer.AnswerID   = currentAnswer.AnswerID;
                _theAnswer.QuestionID = currentAnswer.QuestionID;
                _theAnswer.AnswerBody = currentAnswer.AnswerBody;
            }
            else
            {
                // 2. No database record... Load answer from the web
                var answerAPI = new StackOverflowService();

                var downloadedAnswer = await answerAPI.GetAnswerForQuestion(questionId);

                if (downloadedAnswer != null)
                {
                    _theAnswer.AnswerID   = downloadedAnswer.AnswerID;
                    _theAnswer.QuestionID = downloadedAnswer.QuestionID;
                    _theAnswer.AnswerBody = downloadedAnswer.AnswerBody;

                    // 3. Save the answer for next time
                    await App.StackDataManager.Database.SaveAnswer(_theAnswer);
                }
                else
                {
                    _theAnswer.AnswerBody = "No answer found";
                }
            }
        }