protected void LookupWordOnlineThread(string sWord) { VocabularyDataModel vdm = LookupWordOnline(sWord); if (vdm != null) { DictWordList.Add(vdm); } }
public VocabularyDataModel LookupWordOnline(string sWord) { VocabularyDataModel vdm = new VocabularyDataModel(); if (vdm.ExtractDefinitionFromDicCN(sWord)) { return(vdm); } return(null); }
public bool SaveWordToDict(string sDictName, VocabularyDataModel vdm) { string sDictID = ""; string sWordID = ""; //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 = '" + sDictName + "'"; var reader = command.ExecuteScalar(); if (reader == null) { command.CommandText = "insert into DictList (DictName,DictDescription) values ('" + sDictName + "','Default')"; command.ExecuteNonQuery().ToString(); command.CommandText = "select last_insert_rowid()"; sDictID = ((Int64)command.ExecuteScalar()).ToString(); } else { sDictID = reader.ToString(); } if (GetWordDefinition(vdm.sVocabulary) is null) { string sWordObject = JsonConvert.SerializeObject(vdm); command.CommandText = "insert into Vocabulary (WordName,WordObject) values ('" + vdm.sVocabulary + "','" + sWordObject.Replace("'", "''") + "')"; command.ExecuteNonQuery().ToString(); command.CommandText = "select last_insert_rowid()"; sWordID = ((Int64)command.ExecuteScalar()).ToString(); } else { sWordID = GetWordID(vdm.sVocabulary); } if (!sWordID.Equals("")) { command.CommandText = "select WordID from DictWord where WordID ='" + sWordID + "' and DictID = '" + sDictID + "'"; reader = command.ExecuteScalar(); if (reader is null) { command.CommandText = "insert into DictWord (WordID,DictID) values ('" + sWordID + "','" + sDictID + "')"; command.ExecuteNonQuery(); } return(true); } return(false); }
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); }
public VocabularyDataModel GetWordDefinition(string sWord) { //m_dbConnection.Open(); //SqliteCommand command = new SqliteCommand("select WordObject from Vocabulary where WordName = '" + sWord + "'", m_dbConnection); command.CommandText = "select WordObject from Vocabulary where WordName = '" + sWord + "'"; var reader = command.ExecuteScalar(); //ExecuteScalar returns the first column of the first row; //m_dbConnection.Close(); if (reader == null) { return(null); } VocabularyDataModel vdm = JsonConvert.DeserializeObject <VocabularyDataModel>(reader.ToString()); return(vdm); }
private void ResetControlText(VocabularyDataModel vdm) { if (vdm is null) { return; } /* * 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); */ 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()); btOpenDictDelete.Visibility = ViewStates.Visible; btOpenDictRefresh.Visibility = ViewStates.Visible; return; }
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); }
public void UpdateDictWordByList(List <string> wordList) { List <string> passList = new List <string>(); DatabaseManager dm = new DatabaseManager(); //meaningless words should be passed. passList.AddRange(dm.GetWordListByDictName("Trash")); //familiar words should be passed. passList.AddRange(dm.GetWordListByDictName("PassDict")); if (wordList is null) { return; } foreach (var v in wordList) { if (passList.Contains(v)) { continue; } //add word into dictionary if it does not exist. VocabularyDataModel vdm = LookupWordLocal(v); if (vdm == null) { /* * VocabularyDataModel vdm = LookupWordOnline(v); * if (vdm != null) * DictWordList.Add(vdm); */ bool bChooseThread = true; while (bChooseThread) { for (int i = 0; i < MAXTHREAD; i++) { if (DownLoadTask[i] == null || DownLoadTask[i].Status == TaskStatus.RanToCompletion) { DownLoadTask[i] = new Task(() => { LookupWordOnlineThread(v); }); DownLoadTask[i].Start(); bChooseThread = false; break; } } if (bChooseThread) { System.Threading.Thread.Sleep(1000); } } } else { DictWordList.Add(vdm); } } for (int i = 0; i < MAXTHREAD; i++) { if (DownLoadTask[i] != null && DownLoadTask[i].Status == TaskStatus.Running) { DownLoadTask[i].Wait(); } } }
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); }