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";
                }
            }
        }
Esempio n. 2
0
        public async Task <AnswerInfo> GetAnswerForQuestion(int questionId)
        {
            if (!CrossConnectivity.Current.IsConnected)
            {
                throw new NoInternetException();
            }

            var decompressedContent = await this.CallAndDecompress(string.Format(ANSWER_URL, questionId));

            var deserializedContent = JsonConvert.DeserializeObject <AnswerResponse> (decompressedContent);

            AnswerInfo theAnswer = null;

            if (deserializedContent != null && deserializedContent.items != null && deserializedContent.items.Count > 0)
            {
                var currAnswer = deserializedContent.items [0];

                theAnswer = new AnswerInfo {
                    AnswerID      = currAnswer.answer_id,
                    QuestionID    = currAnswer.question_id,
                    AnswerBody    = currAnswer.body,
                    LoadedFromWeb = true
                };
            }

            return(theAnswer);
        }
        public QuestionDetailPage(int questionId)
        {
            _theAnswer = new AnswerInfo();

            Title = "Possible Answer";

            Label loadedFromLabel = new Label {
                MinimumHeightRequest = 50,
                XAlign = TextAlignment.Center
            };

            HtmlWebViewSource webSource = new HtmlWebViewSource();

            webSource.BindingContext = _theAnswer;
            webSource.SetBinding(HtmlWebViewSource.HtmlProperty, new Binding("AnswerBody"));

            _theFullAnswer = new WebView {
                Source          = webSource,
                VerticalOptions = LayoutOptions.FillAndExpand
            };

            Content = new StackLayout {
                VerticalOptions = LayoutOptions.FillAndExpand,
                Children        =
                {
                    _theFullAnswer
                }
            };

            Task.Run(async() => {
                await LoadAnswer(questionId);
            });
        }
Esempio n. 4
0
        public async Task SaveAnswer(AnswerInfo answer)
        {
            int answerId = answer.AnswerID;

            var dbRecord = await Table <AnswerInfo> ()
                           .Where(ai => ai.AnswerID == answerId)
                           .FirstOrDefaultAsync().ConfigureAwait(false);

            if (dbRecord == null)
            {
                await InsertAsync(answer).ConfigureAwait(false);
            }
            else
            {
                await UpdateAsync(answer).ConfigureAwait(false);
            }
        }