Beispiel #1
0
        public DictDataModel GetDictFromDBByName(string DictName)
        {
            DictDataModel ddm = new DictDataModel();

            ddm.sDictName = DictName;
            string sDictID = "";

            //m_dbConnection.Open();

            //SqliteCommand command = new SqliteCommand("select * from DictList where DictName = '" + DictName + "'" , m_dbConnection);
            command.CommandText = "select DictID from DictList where DictName = '" + DictName + "'";
            var reader1 = command.ExecuteScalar();

            if (reader1 == null)
            {
                return(null);
            }
            sDictID = reader1.ToString();

            command.CommandText = "select URL from URLList where DictID = '" + sDictID + "'";
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    if (!ddm.sSourceLinks.Contains(reader["URL"].ToString()))
                    {
                        ddm.sSourceLinks.Add(reader["URL"].ToString());
                    }
                }
            }

            command.CommandText = "select * from Vocabulary where WordID in (select WordID from DictWord where DictID = '" + sDictID + "' )";
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    /*
                     * VocabularyDataModel vdm = new VocabularyDataModel();
                     * vdm.sVocabulary = reader["WordName"].ToString();
                     * vdm.sPhonics = reader["Phonics"].ToString();
                     * vdm.sEnglishDefinition = new List<string> (reader["EnglishDefinition"].ToString().Split(new char [] { '\n' }));
                     * vdm.sChineseDefinition = new List<string>(reader["ChineseDefinition"].ToString().Split(new char [] { '\n' }));
                     */
                    VocabularyDataModel vdm = JsonConvert.DeserializeObject <VocabularyDataModel>(reader["WordObject"].ToString());

                    ddm.DictWordList.Add(vdm);
                }
            }

            //m_dbConnection.Close();
            return(ddm);
        }
Beispiel #2
0
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            var view = inflater.Inflate(Resource.Layout.fragment_dictlist, container, false);
            //EditText etDictName = view.FindViewById<EditText>(Resource.Id.etDictListDictName);
            TextView tvHint = view.FindViewById <TextView>(Resource.Id.tvHint);

            Button btDelete   = view.FindViewById <Button>(Resource.Id.btDelete);
            Button btOpenDict = view.FindViewById <Button>(Resource.Id.btOpenDict);
            Button btRefresh  = view.FindViewById <Button>(Resource.Id.btRefresh);
            Button btRecreate = view.FindViewById <Button>(Resource.Id.btRecreate);
            //btDelete.Enabled = false;

            RadioGroup rgDictList = view.FindViewById <RadioGroup>(Resource.Id.rgDictList);

            int iSelectedID = 0;

            DatabaseManager dm        = new DatabaseManager();
            string          sDictName = dm.GetDefaultValueByKey("DictName");


            List <string> dictList = dm.GetDictNameList();

            for (int i = 0; i < dictList.Count; i++)
            {
                RadioButton rbNew = new RadioButton(view.Context);
                rbNew.Id = View.GenerateViewId();//start from 1.
                //set attributes "wrap_content" dynamically.(but it has been set default.
                //LinearLayout.LayoutParams parmsWrapContent = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
                //rbNew.LayoutParameters = parmsWrapContent;

                rbNew.Text = dictList[i];
                if (rbNew.Text.Equals(sDictName))
                {
                    iSelectedID = rbNew.Id;
                }
                rgDictList.AddView(rbNew);
            }


            rgDictList.CheckedChange += (sender, e) => {
                btDelete.Enabled = true;
                iSelectedID      = e.CheckedId;

                RadioButton rbNew = view.FindViewById <RadioButton>(e.CheckedId);
                if (rbNew is null)
                {
                    return;
                }
                sDictName = rbNew.Text;
                dm.SetDefaultValue("DictName", sDictName);
                tvHint.Text = "Dictionary: " + sDictName + " is selected! " + dm.GetTotalWordsNumberByDictName(sDictName) + " words.";
            };
            //must be executed after listener "CheckedChange" definited.
            rgDictList.Check(iSelectedID);

            /*
             * for (int i = 0; i < rgDictList.ChildCount; i++)
             * {
             *  RadioButton rbNew = view.FindViewById<RadioButton>(rgDictList.GetChildAt(i).Id);
             *  if(rbNew.Text.Equals(sDictName))
             *  {
             *      iSelectedID = rgDictList.GetChildAt(i).Id;
             *      rgDictList.Check(iSelectedID);
             *      break;
             *  }
             * }
             */



            btOpenDict.Click += delegate
            {
                Fragment fragment = new OpenDict();
                FragmentManager.BeginTransaction().Replace(Resource.Id.flContent, fragment).Commit();
            };

            //remove words which is in "Trash" or "PassDict" from selected dictionary.
            btRefresh.Click += delegate
            {
                if (sDictName.Equals("Trash") || sDictName.Equals("PassDict"))
                {
                    return;
                }

                List <string> lSelectedDict = dm.GetWordListByDictName(sDictName);
                List <string> lTrash        = dm.GetWordListByDictName("Trash");
                List <string> lPassDict     = dm.GetWordListByDictName("PassDict");

                foreach (string sWord in lSelectedDict)
                {
                    if (lTrash.Contains(sWord))
                    {
                        dm.RemoveWordFromDict(sDictName, sWord);
                    }
                    if (lPassDict.Contains(sWord))
                    {
                        dm.RemoveWordFromDict(sDictName, sWord);
                    }
                }

                tvHint.Text = "Dictionary: " + sDictName + " is refreshed! " + dm.GetTotalWordsNumberByDictName(sDictName) + " words.";
            };

            btRecreate.Click += delegate
            {
                if (sDictName.Equals("Trash") || sDictName.Equals("PassDict") || sDictName.Equals("NewWord"))
                {
                    return;
                }

                DictDataModel ddm = dm.GetDictFromDBByName(sDictName);
                if (ddm is null)
                {
                    ddm           = new DictDataModel();
                    ddm.sDictName = sDictName;
                }

                ddm.UpdateDictWord();
                dm.StoreDictToDB(ddm);

                tvHint.Text = "Dictionary: " + sDictName + " is recreated! " + dm.GetTotalWordsNumberByDictName(sDictName) + " words.";
            };

            //delete dictionary. name can be set manually
            btDelete.Click += delegate
            {
                for (int i = 0; i < rgDictList.ChildCount; i++)
                {
                    if (rgDictList.GetChildAt(i).Id == iSelectedID)
                    {
                        if (!sDictName.Equals(""))
                        {
                            dm.RemoveDictByName(sDictName);
                            dm.SetDefaultValue("DictName", "");
                        }
                        rgDictList.RemoveViewAt(i);
                        break;
                    }
                }
                btDelete.Enabled = false;
                dm.RemoveDictByName(sDictName);
                DictDataModel ddm = dm.GetDictFromDBByName(sDictName);
            };


            return(view);

            //return base.OnCreateView(inflater, container, savedInstanceState);
        }
Beispiel #3
0
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            // Use this to return your custom view for this Fragment

            var view = inflater.Inflate(Resource.Layout.fragment_opendicts, container, false);

            btOpenDictMoveToTrash = view.FindViewById <Button>(Resource.Id.btOpenDictMoveToTrash);
            btOpenDictMoveToPass  = view.FindViewById <Button>(Resource.Id.btOpenDictMoveToPass);
            btOpenDictDelete      = view.FindViewById <Button>(Resource.Id.btOpenDictDelete);
            btOpenDictRefresh     = view.FindViewById <Button>(Resource.Id.btOpenDictRefresh);

            tvWordName          = view.FindViewById <TextView>(Resource.Id.tvWordName);
            tvPhonics           = view.FindViewById <TextView>(Resource.Id.tvPhonics);
            tvChineseDefinition = view.FindViewById <TextView>(Resource.Id.tvChineseDefinition);
            tvEnglishDefinition = view.FindViewById <TextView>(Resource.Id.tvEnglishDefinition);
            tvSentences         = view.FindViewById <TextView>(Resource.Id.tvSentences);
            TextView   tvOpenDictDictName = view.FindViewById <TextView>(Resource.Id.tvOpenDictDictName);
            ScrollView svWord             = view.FindViewById <ScrollView>(Resource.Id.svWord);

            //LinearLayout llContainer = view.FindViewById<LinearLayout>(Resource.Id.llContainer);
            Spinner spDictList = view.FindViewById <Spinner>(Resource.Id.spDictList);

            //ArrayAdapter<string> arrayAdapter = new ArrayAdapter<string>( this.Context, Android.Resource.Layout.SimpleSpinnerDropDownItem, dm.GetDictNameList());
            ArrayAdapter <string> arrayAdapter = new ArrayAdapter <string>(this.Context, Android.Resource.Layout.SimpleSpinnerDropDownItem, dm.GetDictNameList());

            spDictList.Adapter = arrayAdapter;
            spDictList.SetSelection(arrayAdapter.GetPosition("PassDict"));

            string sDictName = dm.GetDefaultValueByKey("DictName");

            if (sDictName.Equals(""))
            {
                tvOpenDictDictName.Text = "Please select a Dictionary first in DictList menu.";

                /*
                 * tvWordName.Text = "";
                 * tvPhonics.Text = "";
                 * tvChineseDefinition.Text = "";
                 * tvEnglishDefinition.Text = "";
                 * tvSentences.Text = "";
                 */
                btOpenDictMoveToTrash.Visibility = Android.Views.ViewStates.Invisible;
                btOpenDictMoveToPass.Visibility  = Android.Views.ViewStates.Invisible;
                spDictList.Visibility            = Android.Views.ViewStates.Invisible;
                return(view);
            }

            string sDictCount = dm.GetTotalWordsNumberByDictName(sDictName);

            tvOpenDictDictName.Text = "Dictionary: " + sDictName + ", " + sDictCount + " words.";
            //no more words in dictionary.
            if (sDictCount.Equals("0"))
            {
                //btDelete.Enabled = false;
                //btPass.Enabled = false;
                btOpenDictMoveToTrash.Visibility = Android.Views.ViewStates.Invisible;
                btOpenDictMoveToPass.Visibility  = Android.Views.ViewStates.Invisible;
                spDictList.Visibility            = Android.Views.ViewStates.Invisible;
                return(view);
            }

            DictDataModel ddm = dm.GetDictFromDBByName(sDictName);

            if (ddm is null)
            {
                tvWordName.Text = "There is no dictionary. Please create one first!";
                return(view);
            }

            if (ddm.DictWordList.Count > 0)
            {
                _CurrentWordID = 0;


                ResetControlText(ddm.DictWordList[_CurrentWordID]);

                /*
                 * tvWordName.Text = ddm.DictWordList[_CurrentWordID].sVocabulary;
                 * tvPhonics.Text = ddm.DictWordList[_CurrentWordID].sPhonics;
                 * tvChineseDefinition.Text = string.Join(Environment.NewLine, ddm.DictWordList[_CurrentWordID].sChineseDefinition.ToArray());
                 * tvEnglishDefinition.Text = string.Join(Environment.NewLine, ddm.DictWordList[_CurrentWordID].sEnglishDefinition.ToArray());
                 * tvSentences.Text = string.Join(Environment.NewLine, ddm.DictWordList[_CurrentWordID].sSentences.ToArray());
                 */
            }


            spDictList.ItemSelected += delegate {
                _targetDictName = spDictList.SelectedItem.ToString();
            };

            btOpenDictRefresh.Click += delegate
            {
                dm.RemoveWordFromAllDict(ddm.DictWordList[_CurrentWordID].sVocabulary);
                VocabularyDataModel vdm = new VocabularyDataModel();
                vdm.ExtractDefinitionFromDicCN(ddm.DictWordList[_CurrentWordID].sVocabulary);
                dm.SaveWordToDict(sDictName, vdm);
                ddm.DictWordList[_CurrentWordID] = vdm;

                ResetControlText(ddm.DictWordList[_CurrentWordID]);
            };

            btOpenDictDelete.Click += delegate
            {
                dm.RemoveWordFromAllDict(ddm.DictWordList[_CurrentWordID].sVocabulary);
                ddm.DictWordList.Remove(ddm.DictWordList[_CurrentWordID]);

                //no more words in dictionary.
                if (ddm.DictWordList.Count == 0)
                {
                    tvOpenDictDictName.Text       = "Dictionary: " + sDictName + ", 0 words.";
                    btOpenDictMoveToTrash.Enabled = false;
                    btOpenDictMoveToPass.Enabled  = false;
                    btOpenDictDelete.Visibility   = ViewStates.Invisible;
                    btOpenDictRefresh.Visibility  = ViewStates.Invisible;
                    return;
                }

                if (_CurrentWordID == ddm.DictWordList.Count)
                {
                    _CurrentWordID = 0;
                }
                ResetControlText(ddm.DictWordList[_CurrentWordID]);
            };

            btOpenDictMoveToTrash.Click += delegate
            {
                if (!ddm.sDictName.Equals("Trash"))
                {
                    dm.SaveWordToDict("Trash", ddm.DictWordList[_CurrentWordID]);
                }
                dm.RemoveWordFromDict(ddm.sDictName, ddm.DictWordList[_CurrentWordID].sVocabulary);
                ddm.DictWordList.Remove(ddm.DictWordList[_CurrentWordID]);

                //no more words in dictionary.
                if (ddm.DictWordList.Count == 0)
                {
                    tvOpenDictDictName.Text       = "Dictionary: " + sDictName + ", 0 words.";
                    btOpenDictMoveToTrash.Enabled = false;
                    btOpenDictMoveToPass.Enabled  = false;
                    btOpenDictDelete.Visibility   = ViewStates.Invisible;
                    btOpenDictRefresh.Visibility  = ViewStates.Invisible;
                    return;
                }

                if (_CurrentWordID == ddm.DictWordList.Count)
                {
                    _CurrentWordID = 0;
                }
                ResetControlText(ddm.DictWordList[_CurrentWordID]);
            };

            btOpenDictMoveToPass.Click += delegate
            {
                if (ddm.sDictName.Equals(_targetDictName))
                {
                    return;                                        //To move to itself is forbidden.
                }
                dm.SaveWordToDict(_targetDictName, ddm.DictWordList[_CurrentWordID]);
                dm.RemoveWordFromDict(ddm.sDictName, ddm.DictWordList[_CurrentWordID].sVocabulary);
                ddm.DictWordList.Remove(ddm.DictWordList[_CurrentWordID]);

                //no more words in dictionary.
                if (ddm.DictWordList.Count == 0)
                {
                    tvOpenDictDictName.Text       = "Dictionary: " + sDictName + ", 0 words.";
                    btOpenDictMoveToTrash.Enabled = false;
                    btOpenDictMoveToPass.Enabled  = false;
                    btOpenDictDelete.Visibility   = ViewStates.Invisible;
                    btOpenDictRefresh.Visibility  = ViewStates.Invisible;
                    return;
                }

                if (_CurrentWordID == ddm.DictWordList.Count)
                {
                    _CurrentWordID = 0;
                }
                ResetControlText(ddm.DictWordList[_CurrentWordID]);
            };

            svWord.Touch += (s, e) =>
            {
                var handled = false;

                switch (e.Event.Action)
                {
                case MotionEventActions.Down:
                    _viewX  = e.Event.GetX();
                    handled = true;
                    break;

                case MotionEventActions.Up:
                    var left = (int)(_viewX - e.Event.GetX());
                    if (left > 150)
                    {
                        _CurrentWordID++;
                        if (_CurrentWordID == ddm.DictWordList.Count)
                        {
                            _CurrentWordID = 0;
                        }
                    }
                    if (left < -150)
                    {
                        _CurrentWordID--;
                        if (_CurrentWordID < 0)
                        {
                            _CurrentWordID = ddm.DictWordList.Count - 1;
                        }
                    }


                    /*
                     * tvWordName.Text = ddm.DictWordList[_CurrentWordID].sVocabulary;
                     * tvPhonics.Text = ddm.DictWordList[_CurrentWordID].sPhonics;
                     * tvChineseDefinition.Text = string.Join(Environment.NewLine, ddm.DictWordList[_CurrentWordID].sChineseDefinition.ToArray());
                     * tvEnglishDefinition.Text = string.Join(Environment.NewLine, ddm.DictWordList[_CurrentWordID].sEnglishDefinition.ToArray());
                     * tvSentences.Text = string.Join(Environment.NewLine, ddm.DictWordList[_CurrentWordID].sSentences.ToArray());
                     */
                    if (ddm.DictWordList.Count > 0)
                    {
                        ResetControlText(ddm.DictWordList[_CurrentWordID]);
                    }

                    handled = true;
                    break;
                }



                /*
                 * if (e.Event.Action == MotionEventActions.Down)
                 * {
                 *  // do stuff
                 *  handled = true;
                 * }
                 * else if (e.Event.Action == MotionEventActions.Up)
                 * {
                 *  // do other stuff
                 *  handled = true;
                 * }
                 */

                e.Handled = handled;
            };



            return(view);

            //return base.OnCreateView(inflater, container, savedInstanceState);
        }
Beispiel #4
0
        //http://m.dict.cn/msearch.php?q=embryo, Mobile
        public bool ExtractDefinitionFromDicCN(string sWord)
        {
            DictDataModel ddm = new DictDataModel();
            //string sDicCNIn = ddm.LoadWebPage("http://www.dict.cn/" + sWord);
            string sDicCNIn = ddm.LoadWebPage("http://m.dict.cn/msearch.php?q=" + sWord.ToLower());//only search lower case word.

            if (sDicCNIn is null)
            {
                return(false);
            }

            HtmlDocument doc = new HtmlDocument();

            doc.LoadHtml(sDicCNIn);

            sVocabulary = sWord;

            //extrict phonics
            List <HtmlNode> photicList = doc.DocumentNode.Descendants().Where(x => (x.Name == "div" && x.Attributes["class"] != null && x.Attributes["class"].Value.Contains("phonetic"))).ToList();

            if (photicList.Count > 0)
            {
                if (photicList[0].Descendants("bdo").ToList().Count > 0)
                {
                    sPhonics = photicList[0].Descendants("bdo").ToList()[0].InnerText;
                }
            }

            //extrict Chinese definition
            List <HtmlNode> divCNList = doc.DocumentNode.Descendants().Where(x => (x.Name == "div" && x.Attributes["class"] != null && x.Attributes["class"].Value.Contains("layout basic"))).ToList();

            if (divCNList.Count > 0)
            {
                if (divCNList[0].Descendants("li").ToList().Count > 0)
                {
                    sChineseDefinition.Add(divCNList[0].Descendants("li").ToList()[0].InnerText.Replace("\t", ""));
                }
            }
            else
            {
                DatabaseManager dm = new DatabaseManager();
                dm.SaveWordToDict("Trash", this);
                return(false);
            }
            divCNList = doc.DocumentNode.Descendants().Where(x => (x.Name == "div" && x.Attributes["class"] != null && x.Attributes["class"].Value.Contains("layout dual"))).ToList();
            if (divCNList.Count > 0)
            {
                var liCN = divCNList[0].Descendants("li").ToList();
                foreach (var li in liCN)
                {
                    sChineseDefinition.Add(li.InnerText.Replace("\t", ""));
                }
            }

            //extrict English definition
            List <HtmlNode> divENList = doc.DocumentNode.Descendants().Where(x => (x.Name == "div" && x.Attributes["class"] != null && x.Attributes["class"].Value.Contains("layout en"))).ToList();

            if (divENList.Count > 0)
            {
                var liEN = divENList[0].Descendants("li").ToList();
                foreach (var li in liEN)
                {
                    sEnglishDefinition.Add(li.InnerText.Replace("\t", ""));
                }
            }

            //extrict sentences
            List <HtmlNode> divSTList = doc.DocumentNode.Descendants().Where(x => (x.Name == "div" && x.Attributes["class"] != null && x.Attributes["class"].Value.Contains("layout sort"))).ToList();

            if (divSTList.Count > 0)
            {
                var liST = divSTList[0].Descendants("li").ToList();
                foreach (var li in liST)
                {
                    sSentences.Add(li.InnerText.Replace("\t", ""));
                }
            }

            return(true);
        }
Beispiel #5
0
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            // Use this to return your custom view for this Fragment
            // return inflater.Inflate(Resource.Layout.YourFragment, container, false);
            var      view                = inflater.Inflate(Resource.Layout.fragment_lookuplocal, container, false);
            EditText etWordToLookup      = view.FindViewById <EditText>(Resource.Id.etWordToLookup);
            TextView tvWordName          = view.FindViewById <TextView>(Resource.Id.tvWordName);
            TextView tvPhonics           = view.FindViewById <TextView>(Resource.Id.tvPhonics);
            TextView tvChineseDefinition = view.FindViewById <TextView>(Resource.Id.tvChineseDefinition);
            TextView tvEnglishDefinition = view.FindViewById <TextView>(Resource.Id.tvEnglishDefinition);
            TextView tvSentences         = view.FindViewById <TextView>(Resource.Id.tvSentences);
            CheckBox cbOnline            = view.FindViewById <CheckBox>(Resource.Id.cbOnline);

            Button btLookupLocal = view.FindViewById <Button>(Resource.Id.btLookupLocal);
            Button btDeleteWord  = view.FindViewById <Button>(Resource.Id.btDeleteWord);

            btDeleteWord.Visibility = Android.Views.ViewStates.Invisible;
            DatabaseManager dm = new DatabaseManager();

            //trigger lookup action when hit on Enter key.
            etWordToLookup.KeyPress += (s, e) =>
            {
                if (e.Event.Action == KeyEventActions.Down && e.KeyCode == Keycode.Enter)
                {
                    e.Handled = true;
                    btLookupLocal.CallOnClick();
                }
                ;
            };


            btLookupLocal.Click += delegate
            {
                string sWord = etWordToLookup.Text.Trim();
                if (sWord.Equals(""))
                {
                    return;
                }

                VocabularyDataModel vdm = dm.GetWordDefinition(sWord);
                if (vdm is null)
                {
                    if (cbOnline.Checked)
                    {
                        DictDataModel ddm = new DictDataModel();
                        vdm = ddm.LookupWordOnline(sWord);
                        if (vdm is null)
                        {
                            btDeleteWord.Visibility  = Android.Views.ViewStates.Invisible;
                            tvWordName.Text          = "No such word. Does it spell right?";
                            tvPhonics.Text           = "";
                            tvChineseDefinition.Text = "";
                            tvEnglishDefinition.Text = "";
                            tvSentences.Text         = "";
                            return;
                        }
                        dm.SaveWordToDict("NewWord", vdm);
                    }
                    else
                    {
                        btDeleteWord.Visibility  = Android.Views.ViewStates.Invisible;
                        tvWordName.Text          = "It's a new word. Please select Online checkbox. ";
                        tvPhonics.Text           = "";
                        tvChineseDefinition.Text = "";
                        tvEnglishDefinition.Text = "";
                        tvSentences.Text         = "";
                        return;
                    }
                }
                btDeleteWord.Visibility  = Android.Views.ViewStates.Visible;
                tvWordName.Text          = vdm.sVocabulary;
                tvPhonics.Text           = vdm.sPhonics;
                tvChineseDefinition.Text = string.Join(Environment.NewLine, vdm.sChineseDefinition.ToArray());
                tvEnglishDefinition.Text = string.Join(Environment.NewLine, vdm.sEnglishDefinition.ToArray());
                tvSentences.Text         = string.Join(Environment.NewLine, vdm.sSentences.ToArray());
                //hide keyboard.
                InputMethodManager imm = (InputMethodManager)view.Context.GetSystemService(Context.InputMethodService);
                imm.HideSoftInputFromWindow(view.WindowToken, HideSoftInputFlags.NotAlways);
            };

            btDeleteWord.Click += delegate
            {
                dm.RemoveWordFromAllDict(tvWordName.Text);

                btDeleteWord.Visibility  = Android.Views.ViewStates.Invisible;
                tvWordName.Text          = "";
                tvPhonics.Text           = "";
                tvChineseDefinition.Text = "";
                tvEnglishDefinition.Text = "";
                tvSentences.Text         = "";
            };
            return(view);
        }
Beispiel #6
0
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            var view = inflater.Inflate(Resource.Layout.fragment_createdict, container, false);

            EditText etDictName     = view.FindViewById <EditText>(Resource.Id.etDictName);
            EditText etURL          = view.FindViewById <EditText>(Resource.Id.etURL);
            EditText etURLList      = view.FindViewById <EditText>(Resource.Id.etURLList);
            TextView tvResult       = view.FindViewById <TextView>(Resource.Id.tvResult);
            TextView tvURLList      = view.FindViewById <TextView>(Resource.Id.tvURLList);
            Button   btAdd          = view.FindViewById <Button>(Resource.Id.btAdd);
            Button   btCreate       = view.FindViewById <Button>(Resource.Id.btCreate);
            CheckBox cbCreateByBulk = view.FindViewById <CheckBox>(Resource.Id.cbCreateByBulk);

            InputMethodManager imm = (InputMethodManager)view.Context.GetSystemService(Context.InputMethodService);

            imm.HideSoftInputFromWindow(view.WindowToken, HideSoftInputFlags.NotAlways);

            DatabaseManager dm = new DatabaseManager();

            cbCreateByBulk.CheckedChange += delegate
            {
                if (cbCreateByBulk.Checked)
                {
                    tvURLList.Text = "Bulk Text:";
                }
                else
                {
                    tvURLList.Text = "URL List:";
                }
            };

            etURL.KeyPress += (s, e) =>
            {
                if (e.Event.Action == KeyEventActions.Down && e.KeyCode == Keycode.Enter)
                {
                    e.Handled = true;
                    btAdd.CallOnClick();
                }
                ;
            };


            btAdd.Click += delegate
            {
                imm.HideSoftInputFromWindow(view.WindowToken, HideSoftInputFlags.NotAlways);

                if (etURLList.Text.Contains(etURL.Text))
                {
                    return;
                }
                DictDataModel ddm = dm.GetDictFromDBByName(etDictName.Text);
                if (ddm != null)
                {
                    foreach (var url in ddm.sSourceLinks)
                    {
                        if (etURLList.Text.Contains(url) || url.Equals(etURL.Text))
                        {
                            continue;
                        }
                        etURLList.Text += url + "\r\n";
                    }
                }
                if (!etURLList.Text.Contains(etURL.Text))
                {
                    etURLList.Text += etURL.Text + "\r\n";
                }
                //hide keyboard.
            };

            btCreate.Click += delegate
            {
                btCreate.Enabled = false;
                dm.SetDefaultValue("DictName", etDictName.Text);
                DictDataModel ddm = dm.GetDictFromDBByName(etDictName.Text);
                if (ddm is null)
                {
                    ddm           = new DictDataModel();
                    ddm.sDictName = etDictName.Text;
                }
                //only for test.
                //etURLList.Text = "http://localhost:8080";
                if (cbCreateByBulk.Checked)
                {
                    ddm.UpdateDictWordByList(ddm.GetWordListFromString(etURLList.Text));
                }
                else
                {
                    foreach (string s in etURLList.Text.Split("\r\n"))
                    {
                        if (!s.Equals(""))
                        {
                            if (!ddm.sSourceLinks.Contains(s))
                            {
                                ddm.sSourceLinks.Add(s);
                            }
                        }
                    }

                    ddm.UpdateDictWord();
                }
                dm.StoreDictToDB(ddm);
                tvResult.Text = "Dictionary: " + ddm.sDictName + " is created. Total " + ddm.DictWordList.Count + " words added.";
                Toast.MakeText(view.Context, "Dictionary is created!", ToastLength.Long).Show();
                btCreate.Enabled = true;
            };

            // Use this to return your custom view for this Fragment
            return(view);

            //return base.OnCreateView(inflater, container, savedInstanceState);
        }
Beispiel #7
0
        public void StoreDictToDB(DictDataModel ddm)
        {
            string sDictID = "";

            //m_dbConnection.Open();

            //insert DictList
            //SqliteCommand command = new SqliteCommand("insert into DictList (DictName,DictDescription) values ('" + ddm.sDictName + "','" + ddm.sDictDescription +  "')", m_dbConnection);
            command.CommandText = "select DictID from DictList where DictName = '" + ddm.sDictName + "'";
            var reader = command.ExecuteScalar();

            if (reader == null)
            {
                command.CommandText = "insert into DictList (DictName,DictDescription) values ('" + ddm.sDictName + "','" + ddm.sDictDescription + "')";
                command.ExecuteNonQuery().ToString();
                command.CommandText = "select last_insert_rowid()";
                sDictID             = ((Int64)command.ExecuteScalar()).ToString();
            }
            else
            {
                sDictID = reader.ToString();
            }


            //insert URLList
            for (int i = 0; i < ddm.sSourceLinks.Count; i++)
            {
                command.CommandText = "select DictID from URLList where URL = '" + ddm.sSourceLinks[i] + "'";
                reader = command.ExecuteScalar();

                if (reader != null && reader.ToString().Equals(sDictID))
                {
                    continue;
                }
                command.CommandText = "insert into URLList (DictID, URL) values ('" + sDictID + "','" + ddm.sSourceLinks[i] + "')";
                command.ExecuteNonQuery();
            }

            //insert Vocabulary and DictWord
            for (int i = 0; i < ddm.DictWordList.Count; i++)
            {
                string sWordID = "";

                /*
                 * string sE = "";
                 * string sC = "";
                 *
                 * for(int j = 0; j < ddm.DictWordList[i].sChineseDefinition.Count; j++)
                 * {
                 *  sC = sC + ddm.DictWordList[i].sChineseDefinition[j] + "\n";
                 * }
                 *
                 * for (int j = 0; j < ddm.DictWordList[i].sEnglishDefinition.Count; j++)
                 * {
                 *  sE = sE + ddm.DictWordList[i].sEnglishDefinition[j] + "\n";
                 * }
                 *
                 *
                 * command.CommandText = "insert into Vocabulary (WordName,Phonics,ChineseDefinition,EnglishDefinition) values ('"
                 + ddm.DictWordList[i].sVocabulary + "','"
                 + ddm.DictWordList[i].sPhonics.Replace("'", "''") + "','"
                 + sC.Replace("'", "''") + "','"
                 + sE.Replace("'", "''") + "')";
                 */
                command.CommandText = "select WordID from Vocabulary where WordName = '" + ddm.DictWordList[i].sVocabulary + "'";
                reader = command.ExecuteScalar();

                if (reader != null)
                {
                    sWordID = reader.ToString();
                }
                else
                {
                    string sWordObject = JsonConvert.SerializeObject(ddm.DictWordList[i]);
                    command.CommandText = "insert into Vocabulary (WordName,WordObject) values ('"
                                          + ddm.DictWordList[i].sVocabulary + "','"
                                          + sWordObject.Replace("'", "''") + "')";
                    command.ExecuteNonQuery().ToString();

                    command.CommandText = "select last_insert_rowid()";
                    sWordID             = ((Int64)command.ExecuteScalar()).ToString();
                }

                try
                {
                    command.CommandText = "insert into DictWord (WordID,DictID) values ('" + sWordID + "','" + sDictID + "')";
                    command.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    //duplicate record.
                    System.Console.WriteLine(e.ToString());
                }
            }

            //m_dbConnection.Close();
        }