Ejemplo n.º 1
0
        //Modify Existing Code on Main App
        //207 Lines of Code
        private void Search(string query)
        {
            int  docCount = 1;
            bool truncate = false;

            Log.Info("Search()", String.Format("Search Begins" + ""));
            searchFragmentActivity = new SearchFragmentActivity();
            // RadioButton radio = FindViewById<RadioButton>(Resource.Id.viewAllRadio);
            using (var conn = new SQLite.SQLiteConnection(dbPath))
            {
                var      cmd = new SQLite.SQLiteCommand(conn); var searchStr = new SQLite.SQLiteCommand(conn);
                bool     proofs = true, answers = true, searchAll = false, viewDocs = false;
                CheckBox answerCheck = FindViewById <CheckBox>(Resource.Id.AnswerBox), proofCheck = FindViewById <CheckBox>(Resource.Id.proofBox),
                         searchCheck = FindViewById <CheckBox>(Resource.Id.searchAllCheckBox);
                RadioButton viewRadio = FindViewById <RadioButton>(Resource.Id.viewAllRadio);
                Spinner     spinner = FindViewById <Spinner>(Resource.Id.spinner1), spinner2 = FindViewById <Spinner>(Resource.Id.spinner2);
                spinner.ItemSelected  += new EventHandler <AdapterView.ItemSelectedEventArgs>(Spinner1_ItemSelected);
                spinner2.ItemSelected += new EventHandler <AdapterView.ItemSelectedEventArgs>(Spinner2_ItemSelected);

                string fileString = "", accessString = "";
                accessString = TableAccess("");
                if (searchCheck.Checked)
                {
                    searchAll = true;
                }
                else
                {
                    searchAll = false; accessString = TableAccess(string.Format(" where documentname = '{0}' ", fileName));
                }
                //Data filters
                if (allOpen)
                {
                    if (searchAll)
                    {
                        fileString = TableAccess("");//"select * from Documenttitlelist";
                    }
                    else
                    {
                        fileString = TableAccess(string.Format("Where Documentname='{0}'", fileName));//String.Format("select * from DocumentTableList where DocumentName='{0}'", fileName);
                    }
                }
                if (catechismOpen)
                {
                    if (searchAll)
                    {
                        fileString = TableAccess("Where documentTypeName='CATECHISM'"); //"and DocumentTypeName='CATECHISM'");
                    }
                    else
                    {
                        fileString = TableAccess(String.Format("where DocumentTypeName='CATECHISM' and DocumentName='{0}' ", fileName));
                    }
                }
                if (confessionOpen)
                {
                    if (searchAll)
                    {
                        fileString = TableAccess("where DocumentTypeName='CONFESSION' ");
                    }
                    else
                    {
                        fileString = TableAccess(String.Format("where DocumentTypeName='CONFESSION' and DocumentName='{0}'  ", fileName));
                    }
                }
                if (creedOpen)
                {
                    if (searchAll)
                    {
                        fileString = TableAccess("where DocumentTypeName='CREED' ");
                    }
                    else
                    {
                        fileString = TableAccess(string.Format("where DocumentTypeName='CREED' and DocumentName='{0}' ", fileName));
                    }
                }
                //Proofs enabled
                if (proofCheck.Checked)
                {
                    proofs = true;
                }
                else
                {
                    proofs = false;
                }
                //Read Document
                if (viewRadio.Checked)
                {
                    viewDocs = true;
                }
                else
                {
                    viewDocs = false;
                }
                cmd.CommandText       = fileString;
                searchStr.CommandText = accessString;
                var r = cmd.ExecuteQuery <DocumentTitle>();
                var searchFields = searchStr.ExecuteQuery <Document>();
                documentList = new DocumentList();
                //Add Entries to DocumentList
                for (int x = 0; x < searchFields.Count; x++)
                {
                    DocumentTitle docTitle = new DocumentTitle();
                    docTitle.DocumentID = searchFields[x].DocumentID;
                    for (int y = 0; y < r.Count; y++)
                    {
                        if (!r[y].DocumentID.Equals(docTitle.DocumentID))
                        {
                            foreach (DocumentTitle doc in r)
                            {
                                if (doc.DocumentID == docTitle.DocumentID)
                                {
                                    docTitle.Title = doc.Title;
                                }
                                else
                                {
                                    continue;
                                }
                            }
                        }
                        else
                        {
                            docTitle.Title = r[y].CompareIDs(docTitle.DocumentID);
                        }
                    }
                    searchFields[x].DocumentName = docTitle.Title;
                    Document document = new Document();
                    document.ChName       = searchFields[x].ChName;
                    document.DocDetailID  = searchFields[x].DocDetailID;
                    document.DocumentText = Formatter(searchFields[x].DocumentText);
                    document.DocumentName = searchFields[x].DocumentName;
                    document.ChNumber     = searchFields[x].ChNumber;
                    document.ChProofs     = Formatter(searchFields[x].ChProofs);
                    document.Tags         = searchFields[x].Tags;
                    documentList.Add(document);
                }
                if (FindViewById <CheckBox>(Resource.Id.truncateCheck).Checked)
                {
                    truncate = true;
                }
                if (viewRadio.Checked != true && query != "" && FindViewById <RadioButton>(Resource.Id.topicRadio).Checked)
                {
                    if (FindViewById <RadioButton>(Resource.Id.topicRadio).Checked)
                    {
                        stopwatch.Start();
                        FilterResults(documentList, truncate, true, proofs, searchAll, query);
                        documentList.Reverse();
                        stopwatch.Stop();
                    }
                }
                else if (FindViewById <RadioButton>(Resource.Id.chapterRadio).Checked & query != "")
                {
                    int searchInt = Int32.Parse(query);
                    FilterResults(this.documentList, truncate, answers, proofs, searchAll, searchInt);
                }
                else if (viewDocs)
                {
                    if (!FindViewById <CheckBox>(Resource.Id.searchAllCheckBox).Checked)
                    {
                        query = "Results for All";
                    }
                    else
                    {
                        query = "View All";
                    }
                }
                if (documentList.Count > 1)
                {
                    SetContentView(Resource.Layout.search_results);
                    ViewPager     viewPager = FindViewById <ViewPager>(Resource.Id.viewpager);
                    SearchAdapter adapter   = new SearchAdapter(SupportFragmentManager, documentList, query, truncate);
                    searchFragmentActivity.DisplayResults(documentList, viewPager, adapter, query, 0, truncate);
                }
                else
                {
                    stopwatch.Stop();
                    if (this.documentList.Count == 0)
                    {
                        #region Error Logging
                        Log.Info("Search()", String.Format("No Results were found for {0}", query));
                        Toast.MakeText(this, String.Format("No results were found for  {0}", query), ToastLength.Long).Show();
                        #endregion
                        #region Variable Declaration and Assignment

                        SetContentView(Resource.Layout.errorLayout);
                        TextView errorMsg = FindViewById <TextView>(Resource.Id.errorTV);
                        errorMsg.Text = String.Format("No Search Results were found for {0}\r\n\r\n" +
                                                      "Go back to home page to search for another topic", query);


                        #endregion
                        #region Dialog Box
                        Android.App.AlertDialog.Builder alert = new Android.App.AlertDialog.Builder(this);
                        alert.SetTitle("No Results Found");
                        alert.SetMessage(String.Format("No Results were found for {0}.\r\n\r\n" +
                                                       "Do you want to go back and search for another topic?", query));
                        alert.SetPositiveButton("Yes", (senderAlert, args) =>
                        {
                            intent = new Intent(this, Class);
                            searchFragmentActivity = null;
                            this.OnStop();
                            this.Finish();
                            StartActivity(intent);
                        });
                        alert.SetNegativeButton("No", (senderAlert, args) => { alert.Dispose(); });

                        Dialog dialog = alert.Create();
                        dialog.Show();
                        #endregion
                    }
                    else
                    {
                        //SetTitle();
                        Document document = this.documentList[this.documentList.Count - 1];
                        SetContentView(Resource.Layout.confession_results);
                        TextView chapterBox  = FindViewById <TextView>(Resource.Id.chapterText);
                        TextView proofBox    = FindViewById <TextView>(Resource.Id.proofText);
                        TextView chNumbBox   = FindViewById <TextView>(Resource.Id.confessionChLabel);
                        TextView docTitleBox = FindViewById <TextView>(Resource.Id.documentTitleLabel);

                        chapterBox.Text  = document.DocumentText;
                        chNumbBox.Text   = String.Format("Chapter {0} : {1}", document.ChNumber.ToString(), document.ChName);
                        proofBox.Text    = document.ChProofs;
                        docTitleBox.Text = document.DocumentName;
                        TextView proofView = FindViewById <TextView>(Resource.Id.proofLabel);
                        ChangeColor(true, Android.Graphics.Color.Black, chapterBox, proofBox, chNumbBox, docTitleBox);
                        ChangeColor(proofView, false, Android.Graphics.Color.Black);
                        shareList = docTitleBox.Text + newLine + chNumbBox.Text + newLine + chapterBox.Text + newLine + "Proofs" + newLine + proofBox.Text;
                        FloatingActionButton fab = FindViewById <FloatingActionButton>(Resource.Id.shareActionButton);
                        ChangeColor(fab, Android.Graphics.Color.Black);

                        fab.Click += ShareContent;
                    }
                }
            }
        }