Пример #1
0
        protected async Task LoadQuestions()
        {
            _displayQuestions.Clear();

            // 1. Get rid of anything too old for the cache
            await App.StackDataManager.Database.DeleteQuestionsAndAnswers();

            // 2. Load up cached questions from the database
            var databaseQuestions = await App.StackDataManager.Database.GetQuestions(_dateToDisplay);

            foreach (var item in databaseQuestions)
            {
                _displayQuestions.Add(item);
            }

            try {
                // 4. Load up new questions from web
                var questionAPI         = new StackOverflowService();
                var downloadedQuestions = await questionAPI.GetQuestions(_dateToDisplay);

                // 5. See which questions are new from web and only add those to the display and cache
                foreach (var question in downloadedQuestions)
                {
                    if (_displayQuestions.Contains(question) == false)
                    {
                        _displayQuestions.Insert(0, question);

                        // 6. Save the new question to the cache
                        await App.StackDataManager.Database.SaveQuestion(question);
                    }
                }
            } catch (NoInternetException) {
                await HandleException();
            }
        }
Пример #2
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";
                }
            }
        }