private void BookmarkButton_Click(object sender, EventArgs e) { int lineNum = mContext.lines.IndexOf(line); long bookmarkPos = mContext.GetPosition(mContext.lines, lineNum + 1, wordIndex, true); int bookmarksPos = Bookmark.SearchClosest(bookmarkPos, mContext.mBookmarks); if (mContext.mBookmarks.Count > bookmarksPos && mContext.mBookmarks[bookmarksPos].mPosition == bookmarkPos) { mContext.mBookmarks.RemoveAt(bookmarksPos); if (!Bookmark.SaveToFile(mContext.mBookmarks, mContext.curFilePath + ".bookmarks")) { Toast.MakeText(mContext, "Bookmarks could not be stored. File location is not writable.", ToastLength.Long).Show(); } int foundBookmarksPos = Bookmark.SearchClosest(bookmarkPos, mContext.mFoundBookmarks); if (mContext.mFoundBookmarks.Count > foundBookmarksPos && mContext.mFoundBookmarks[foundBookmarksPos].mPosition == bookmarkPos) { mContext.mFoundBookmarks.RemoveAt(foundBookmarksPos); } mContext.linesAdapter.NotifyItemChanged(lineNum + 1); show(mContext.linesLayoutManager.FindViewByPosition(lineNum + 1), line, wordIndex, showX, false); } else { AlertDialog.Builder builder = new AlertDialog.Builder(mContext); builder.SetTitle("New bookmark"); EditText inputBookmark = new EditText(mContext); inputBookmark.InputType = InputTypes.TextFlagCapSentences; inputBookmark.Text = Dict.PinyinToTones(Dict.GetPinyin(entry)); inputBookmark.SelectAll(); builder.SetView(inputBookmark); builder.SetCancelable(true); builder.SetPositiveButton("OK", delegate { try { int lineNum1 = mContext.lines.IndexOf(line); long bookmarkPos1 = mContext.GetPosition(mContext.lines, lineNum1 + 1, wordIndex, true); int bookmarksPos1 = Bookmark.SearchClosest(bookmarkPos1, mContext.mBookmarks); int foundBookmarksPos1 = Bookmark.SearchClosest(bookmarkPos1, mContext.mFoundBookmarks); Bookmark newBookmark = new Bookmark(bookmarkPos1, inputBookmark.Text.ToString()); newBookmark.SetAnnotatedPosition(lineNum1, wordIndex); mContext.mFoundBookmarks.Insert(foundBookmarksPos1, newBookmark); mContext.mBookmarks.Insert(bookmarksPos1, newBookmark); if (!Bookmark.SaveToFile(mContext.mBookmarks, mContext.curFilePath + ".bookmarks")) { Toast.MakeText(mContext, "Bookmarks could not be stored. File location is not writable.", ToastLength.Long).Show(); } mContext.linesAdapter.NotifyItemChanged(lineNum1 + 1); show(mContext.linesLayoutManager.FindViewByPosition(lineNum1 + 1), line, wordIndex, showX, false); } catch (FormatException) { Toast.MakeText(mContext, "", ToastLength.Long).Show(); } }); builder.SetNegativeButton("Cancel", delegate { }); AlertDialog dialog = builder.Create(); dialog.Window.SetSoftInputMode(SoftInput.StateVisible); dialog.Show(); } }
public void OnCompleted(int task, int splitLineIndex, string pastedText, List <List <object> > tempLines, int curPos, long tempStartPos, long tempEndPos, bool isRemaining, List <Bookmark> foundBookmarks) { System.Console.WriteLine("UpdateLinesAnnTask => OnCompleted()"); if (Activity.curSaveName.Equals("") && Activity.curPos == 0) { //System.Console.WriteLine("UpdateLinesAnnTask => Step 1"); StringBuilder fName = new StringBuilder(); int p = 0, r = 0; while (fName.Length < 16 && r < tempLines.Count && (p < tempLines[r].Count || r < tempLines.Count - 1)) { var word = tempLines.ElementAt(r).ElementAt(p); if (word is int) { System.Console.WriteLine("UpdateLinesAnnTask => IS INT"); if (fName.Length > 0) { fName.Append("-"); } fName.Append(Regex.Replace(Dict.GetPinyin((int)word), @"[^a-zA-Z]+", "")); } else { System.Console.WriteLine("UpdateLinesAnnTask => IS STRING"); System.Console.WriteLine("word => " + (string)word); string s = Regex.Replace((string)word, @"[^a-zA-Z0-9]+", ""); fName.Append(s.Substring(0, Math.Min(s.Length, 16))); } if (++p == tempLines[r].Count) { p = 0; r++; } } Activity.curSaveName = fName.ToString(); } Activity.curPos = curPos; Activity.linesRecyclerView.SetItemAnimator(Activity.defaultItemAnimator); switch (task) { case AnnTask.TASK_ANNOTATE: case AnnTask.TASK_SPLIT: if (isRemaining) { Activity.linesAdapter.ShowFooter = true; } else { Activity.linesAdapter.ShowFooter = false; } int firstVisiblePosition = Activity.linesLayoutManager.FindFirstVisibleItemPosition(); View firstVisible = Activity.linesLayoutManager.FindViewByPosition(firstVisiblePosition); int top = firstVisible != null ? firstVisible.Top : 0; if (task == AnnTask.TASK_SPLIT) { Activity.linesRecyclerView.SetItemAnimator(null); int toRemove = Activity.lines.Count - splitLineIndex; while (toRemove-- > 0) { Activity.lines.RemoveAt(splitLineIndex); Activity.linesAdapter.NotifyItemRemoved(splitLineIndex + 1); } if (Activity.annoMode == ANNOTATE_FILE && Activity.mBookmarks.Count > 0) { int bookmarksRemoveFrom = Bookmark.SearchClosest(Activity.endPos, Activity.mFoundBookmarks); while (Activity.mFoundBookmarks.Count > bookmarksRemoveFrom) { Activity.mFoundBookmarks.RemoveAt(bookmarksRemoveFrom); } } } int rmCount = -1; if (Activity.annoMode == ANNOTATE_FILE && !Activity.isFirstFileAnnotation && firstVisiblePosition > AnnTask.visibleLines) { rmCount = firstVisiblePosition - AnnTask.visibleLines; tempStartPos = Activity.GetPosition(Activity.lines, rmCount + 1, 0, true); for (int i = 0; i < rmCount; i++) { Activity.lines.RemoveAt(0); Activity.linesAdapter.NotifyItemRemoved(1); } int bookmarksRemoveUntil = Bookmark.SearchClosest(tempStartPos, Activity.mFoundBookmarks); for (int i = 0; i < bookmarksRemoveUntil; i++) { Activity.mFoundBookmarks.RemoveAt(0); } for (int i = 0; i < Activity.mFoundBookmarks.Count; i++) { Activity.mFoundBookmarks[i].mLine -= rmCount; } } for (int i = 0; i < foundBookmarks.Count; i++) { foundBookmarks[i].mLine += Activity.lines.Count; } Activity.mFoundBookmarks.AddRange(foundBookmarks); Activity.lines.AddRange(tempLines); Activity.linesAdapter.NotifyItemRangeInserted(Activity.lines.Count - tempLines.Count + 1, tempLines.Count); if (tempStartPos > 0) { Activity.linesAdapter.ShowHeader = true; if (Activity.isFirstFileAnnotation) { Activity.linesLayoutManager.ScrollToPositionWithOffset(1, 0); } } else { Activity.linesAdapter.ShowHeader = false; } if (rmCount > -1) { Activity.linesLayoutManager.ScrollToPositionWithOffset(AnnTask.visibleLines, top); } tempLines.Clear(); break; case AnnTask.TASK_ANNOTATE_BACK: int remainCount = Activity.linesLayoutManager.FindFirstVisibleItemPosition() + AnnTask.visibleLines * 2; if (Activity.lines.Count > remainCount) { rmCount = Activity.lines.Count - remainCount; for (int i = 0; i < rmCount; i++) { List <object> rmLine = Activity.lines[remainCount]; Activity.lines.RemoveAt(remainCount); Activity.linesAdapter.NotifyItemRemoved(remainCount + 1); tempEndPos -= LineView.GetLineSize(rmLine, Activity.annoMode == ANNOTATE_FILE); } if (Activity.annoMode == ANNOTATE_FILE && Activity.mFoundBookmarks.Count > 0) { int bookmarksRemoveFrom = Bookmark.SearchClosest(tempEndPos, Activity.mFoundBookmarks); while (Activity.mFoundBookmarks.Count > bookmarksRemoveFrom) { Activity.mFoundBookmarks.RemoveAt(bookmarksRemoveFrom); } } } firstVisiblePosition = Activity.linesLayoutManager.FindFirstVisibleItemPosition(); int newFirstVisiblePosition = firstVisiblePosition + tempLines.Count; top = Activity.linesLayoutManager.FindViewByPosition(firstVisiblePosition).Top + Activity.linesLayoutManager.FindViewByPosition(firstVisiblePosition).Height - Activity.linesLayoutManager.FindViewByPosition(firstVisiblePosition + 1).Height; if (tempStartPos == 0) { Activity.linesAdapter.ShowHeader = false; } for (int i = 0; i < Activity.mFoundBookmarks.Count; i++) { Activity.mFoundBookmarks[i].mLine += tempLines.Count; } Activity.mFoundBookmarks.InsertRange(0, foundBookmarks); Activity.lines.InsertRange(0, tempLines); Activity.linesAdapter.NotifyItemRangeInserted(1, tempLines.Count); Activity.linesLayoutManager.ScrollToPositionWithOffset(newFirstVisiblePosition, top); tempLines.Clear(); break; } Activity.isFirstAnnotation = false; Activity.isFirstFileAnnotation = false; Activity.startPos = tempStartPos; Activity.endPos = tempEndPos; if (Activity.settingsChanged) { Activity.Redraw(); } //update header and footer Activity.linesAdapter.NotifyItemChanged(0); Activity.linesAdapter.NotifyItemChanged(Activity.linesAdapter.ItemCount - 1); }