Ejemplo n.º 1
0
        public void OnNextPageSelected(object sender, EventArgs args)
        {
            int index = CurrentChapter.Index;

            if (index >= Chapters.Count || Chapters.Count == 0 || _isLoading)
            {
                return;
            }
            var next = Chapters.Where(p => p.Index == index + 1).FirstOrDefault();

            if (next == null)
            {
                return;
            }
            if (ReaderType == ReaderType.Txt)
            {
                string content = string.Empty;
                if (next.Index == Chapters.Count - 1)
                {
                    content = _txtContent.Substring(next.StartLength);
                }
                else
                {
                    content = _txtContent.Substring(next.StartLength, Chapters[next.Index + 1].StartLength - next.StartLength);
                }
                _txtView.SetContent(content, ReaderStartMode.First);
            }
            else if (ReaderType == ReaderType.Custom)
            {
                var detail = CustomChapterDetailList.Where(p => p.Index == next.Index).FirstOrDefault();
                if (detail != null)
                {
                    _txtView.SetContent(detail.Content, ReaderStartMode.First);
                }
                else
                {
                    CustomContentRequest?.Invoke(this, new CustomRequestEventArgs(ReaderStartMode.First, next));
                }
            }
            else
            {
                var orders = _epubContent.SpecialResources.HtmlInReadingOrder;
                if (_tempEpubChapterIndex > orders.Count - 2)
                {
                    return;
                }
                _tempEpubChapterIndex += 1;
                var nextOrder = orders[_tempEpubChapterIndex];
                next = GetLastEpubChapter(nextOrder);
                string content = nextOrder?.TextContent ?? next.Title;
                _epubView.SetContent(content, ReaderStartMode.First);
            }

            if (!next.Equals(CurrentChapter))
            {
                CurrentChapter = next;
                ChapterChanged?.Invoke(this, next);
            }
        }
Ejemplo n.º 2
0
        public void OnPrevPageSelected(object sender, EventArgs args)
        {
            if (CurrentChapter == null)
            {
                return;
            }
            int index = CurrentChapter.Index;

            if (index < 1 || Chapters.Count == 0 || _isLoading)
            {
                return;
            }
            var prev = Chapters.Where(p => p.Index == index - 1).FirstOrDefault();

            if (prev == null)
            {
                return;
            }
            if (ReaderType == ReaderType.Txt)
            {
                string content = _txtContent.Substring(prev.StartLength, CurrentChapter.StartLength - prev.StartLength);
                _readerView.SetContent(content, ReaderStartMode.Last);
            }
            else if (ReaderType == ReaderType.Custom)
            {
                var detail = CustomChapterDetailList.Where(p => p.Index == prev.Index).FirstOrDefault();
                if (detail != null)
                {
                    _readerView.SetContent(detail.GetReadContent(), ReaderStartMode.Last);
                }
                else
                {
                    CustomContentRequest?.Invoke(this, new CustomRequestEventArgs(ReaderStartMode.Last, prev));
                }
            }
            else
            {
                var orders = _epubContent.SpecialResources.HtmlInReadingOrder;
                if (_tempEpubChapterIndex < 1)
                {
                    return;
                }
                _tempEpubChapterIndex -= 1;
                var prevOrder = orders[_tempEpubChapterIndex];
                prev = GetLastEpubChapter(prevOrder);
                string content = prevOrder?.TextContent ?? prev.Title;
                _readerView.SetContent(content, ReaderStartMode.Last);
            }

            if (!prev.Equals(CurrentChapter))
            {
                CurrentChapter = prev;
                ChapterChanged?.Invoke(this, prev);
            }
        }
Ejemplo n.º 3
0
 private void SetProgress(Chapter chapter, int addonLength = 0)
 {
     if (Chapters == null || Chapters.Count == 0 || !Chapters.Any(p => p.Equals(chapter)))
     {
         throw new ArgumentOutOfRangeException("The chapter list don't have this chapter");
     }
     if (chapter != CurrentChapter)
     {
         ChapterChanged?.Invoke(this, chapter);
     }
     CurrentChapter = Chapters.Where(p => p.Equals(chapter)).FirstOrDefault();
     if (ReaderType == Enums.ReaderType.Txt)
     {
         int    nextIndex = CurrentChapter.Index + 1;
         string content   = "";
         if (nextIndex >= Chapters.Count)
         {
             content = _txtContent.Substring(CurrentChapter.StartLength);
         }
         else
         {
             content = _txtContent.Substring(CurrentChapter.StartLength, Chapters[nextIndex].StartLength - CurrentChapter.StartLength);
         }
         _readerView.SetContent(content, Enums.ReaderStartMode.First, addonLength);
     }
     else if (ReaderType == Enums.ReaderType.Custom)
     {
         var detail = CustomChapterDetailList.Where(p => p.Index == CurrentChapter.Index).FirstOrDefault();
         if (detail != null)
         {
             _readerView.SetContent(detail.GetReadContent(), Enums.ReaderStartMode.First, addonLength);
         }
         else
         {
             CustomContentRequest?.Invoke(this, new CustomRequestEventArgs(Enums.ReaderStartMode.First, CurrentChapter, addonLength));
         }
     }
     else
     {
         var    info    = _epubContent.SpecialResources.HtmlInReadingOrder.Where(p => p.AbsolutePath.Equals(chapter.Link, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
         string content = string.Empty;
         if (info != null)
         {
             content = info.TextContent;
             _tempEpubChapterIndex = _epubContent.SpecialResources.HtmlInReadingOrder.IndexOf(info);
         }
         else
         {
             content = chapter.Title;
         }
         _readerView.SetContent(content, Enums.ReaderStartMode.First, addonLength);
     }
     RaiseProgressChanged(addonLength);
 }
Ejemplo n.º 4
0
        public string GetHTML(int chapterNum)
        {
            string htmlString = string.Empty;

            // if 0, do everything, otherwise, a specific chapter
            if (chapterNum > 0)
            {
                Chapter chapter = Chapters.Where(c => c.Number == chapterNum).FirstOrDefault( );
                if (chapter != null)
                {
                    htmlString = chapter.GetHTML( );
                }
            }
            else
            {
                foreach (Chapter chapter in Chapters)
                {
                    htmlString += chapter.GetHTML( );
                }
            }

            return(htmlString);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Обновляет VMs по найденным болезням
        /// </summary>
        /// <param name="results"></param>
        private void MakeVms(ObservableCollection <IcdDisease> results)
        {
            var doctor = AuthorityController.CurrentDoctor;

            Func <IcdDisease, bool> diseaseClause = d => true;

            if (IcdTopLevelOnly)
            {
                // без уточненных болезней
                diseaseClause = d => !d.IsSubdivision;
            }
            Func <IcdBlock, bool> blockClause = b => true;

            if (doctor != null && doctor.Speciality != null)
            {
                // блоки для специальности доктора
                blockClause = b => doctor.Speciality.IcdBlocks.Contains(b);
            }

            Dictionary <IcdChapter, Dictionary <IcdBlock, IEnumerable <IcdDisease> > >
            dict = (from d in results
                    group d by d.IcdBlock.IcdChapter into gr
                    let blocks = (from be in gr
                                  group be by be.IcdBlock into grr
                                  where blockClause(grr.Key)
                                  select new
            {
                Block = grr.Key,
                Diseases = grr.Where(diseaseClause)
            }).ToDictionary(x => x.Block, x => x.Diseases)
                                 where blocks.Count > 0 // без пустых классов
                                 select new
            {
                Chapter = gr.Key,
                Blocks = blocks
            }).ToDictionary(x => x.Chapter, x => x.Blocks);

            // для каждого класса, блока и болезни ищем существующую VM или создаем
            // разворачиваем
            // синхронизируем с детьми уровня выше
            Func <DiagnosisViewModel, IIcdEntity> compareKey = (vm) => vm.Icd;
            var inMaking = true;
            var chVms    = dict.Keys.Select(ch =>
            {
                var chVm = Chapters.Where(i => i.Icd as IcdChapter == ch).FirstOrDefault();
                if (chVm == null)
                {
                    chVm = new DiagnosisViewModel(ch);
                    chVm.PropertyChanged += (s, e) =>
                    {
                        // сохраняем выбор пользователя
                        if (e.PropertyName == "IsExpanded" && !inMaking)
                        {
                            chVm.UserExpaneded = chVm.IsExpanded;
                        }
                    };
                }

                var bVms = dict[ch].Keys.Select(b =>
                {
                    var bVm = chVm.Children.Where(i => i.Icd as IcdBlock == b).FirstOrDefault();
                    if (bVm == null)
                    {
                        bVm = new DiagnosisViewModel(b);
                        bVm.PropertyChanged += (s, e) =>
                        {
                            // сохраняем выбор пользователя
                            if (e.PropertyName == "IsExpanded" && !inMaking)
                            {
                                bVm.UserExpaneded = bVm.IsExpanded;
                            }
                        };
                    }
                    var dVms = dict[ch][b].Select(d =>
                    {
                        var dVm = bVm.Children.Where(i => i.Icd as IcdDisease == d)
                                  .FirstOrDefault() ?? new DiagnosisViewModel(d);

                        return(dVm);
                    }).ToList();

                    // блоки остаются в состоянии, выбранном пользователем
                    bVm.IsExpanded = (bVm.UserExpaneded ?? false) ? true :           // был раскрыт пользователем
                                     (TypedEnough() && (bVm.UserExpaneded ?? true)); // или запрос достаточно точный и блок не был свернут

                    bVm.Children.SyncWith(dVms);
                    return(bVm);
                }).ToList();

                // глава раскрыта, если не была свернута пользователем
                chVm.IsExpanded = chVm.UserExpaneded ?? true;

                chVm.Children.SyncWith(bVms);
                return(chVm);
            }).ToList();

            inMaking = false;

            // всегда раскрыта глава и блок с выбранной болезнью (hieratchicalbase)

            // TODO длинный запрос — vm удаляются, сохранять UserExpaneded для каждой

            Chapters.SyncWith(chVms);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 内部查询
        /// </summary>
        /// <param name="searchText">关键词(可以作为正则表达式)</param>
        public async Task <List <InsideSearchItem> > GetInsideSearchResultAsync(string searchText)
        {
            if (Chapters == null || Chapters.Count == 0)
            {
                throw new InvalidCastException("Chapters not loaded");
            }
            else if (string.IsNullOrEmpty(searchText))
            {
                throw new ArgumentNullException("Search text is null or empty");
            }

            var result = new List <InsideSearchItem>();
            var regex  = new Regex(searchText, RegexOptions.IgnoreCase);

            if (ReaderType == ReaderType.Txt && !string.IsNullOrEmpty(_txtContent))
            {
                var matches = regex.Matches(_txtContent);
                if (matches.Count > 0)
                {
                    var tasks = new List <Task>();
                    for (int i = 0; i < matches.Count; i++)
                    {
                        int    index = matches[i].Index;
                        string value = matches[i].Value;
                        if (value.Length > 60)
                        {
                            value = value.Substring(0, 60).Trim();
                        }
                        tasks.Add(Task.Run(() =>
                        {
                            string display = GetDisplayText(_txtContent, index, value);
                            var chapter    = Chapters.Where(p => p.StartLength <= index).OrderByDescending(p => p.Index).FirstOrDefault();
                            if (chapter == null)
                            {
                                return;
                            }
                            var item         = new InsideSearchItem();
                            item.Chapter     = chapter;
                            item.DisplayText = display;
                            item.SearchText  = value;
                            item.AddonLength = index - chapter.StartLength;
                            item.DisplayText = item.DisplayText.Trim();
                            result.Add(item);
                        }));
                    }
                    await Task.WhenAll(tasks.ToArray());
                }
            }
            else if (ReaderType == ReaderType.Epub && _epubContent != null)
            {
                var orders = _epubContent.SpecialResources.HtmlInReadingOrder;
                var tasks  = new List <Task>();
                foreach (var order in orders)
                {
                    tasks.Add(Task.Run(() =>
                    {
                        string text = Regex.Replace(order.TextContent, @"<[^>]*>", string.Empty);
                        var matches = regex.Matches(text);
                        if (matches.Count > 0)
                        {
                            var chapter = Chapters.Where(p => p.Link.Contains(order.AbsolutePath, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
                            if (chapter == null)
                            {
                                chapter = GetLastEpubChapter(order);
                                if (chapter == null)
                                {
                                    return;
                                }
                            }
                            for (int i = 0; i < matches.Count; i++)
                            {
                                int index    = matches[i].Index;
                                string value = matches[i].Value;
                                if (value.Length > 60)
                                {
                                    value = value.Substring(0, 60).Trim();
                                }
                                string display   = GetDisplayText(text, index, value);
                                var item         = new InsideSearchItem();
                                item.Chapter     = chapter;
                                item.DisplayText = display.Trim();
                                item.SearchText  = value;
                                item.AddonLength = index;
                                result.Add(item);
                            }
                        }
                    }));
                }
                await Task.WhenAll(tasks.ToArray());
            }
            else
            {
                var chapters = Chapters.Where(p => regex.IsMatch(p.Title));
                if (chapters.Count() > 0)
                {
                    foreach (var chapter in chapters)
                    {
                        var item = new InsideSearchItem();
                        item.Chapter     = chapter;
                        item.DisplayText = chapter.Title;
                        item.SearchText  = regex.Match(chapter.Title).Value;
                        item.AddonLength = 0;
                        result.Add(item);
                    }
                }
            }
            return(result.OrderBy(p => p.Chapter.Index).ThenBy(p => p.AddonLength).ToList());
        }