public IEnumerable <GalleryItem> FilterTag(IEnumerable <GalleryItem> items, SkipCounterWrapper counter, Gallery gallery = null) { var option = SettingManager.LoadSetting <GlobalSetting>(); IEnumerable <Gallery> galleries = gallery == null?Container.Default.GetExportedValues <Gallery>() : new[] { gallery }; return(items.Where(x => { if (galleries.FirstOrDefault(g => g.GalleryName == x.GalleryName) is Gallery gallery && gallery.GetImageDetial(x) is GalleryImageDetail detail) { if (!option.EnableTagFilter) { return true; } //todo: optimze them switch (ViewType) { case GalleryViewType.Marked: if (!option.FilterTarget.HasFlag(TagFilterTarget.MarkedWindow)) { return true; } break; case GalleryViewType.Voted: if (!option.FilterTarget.HasFlag(TagFilterTarget.VotedWindow)) { return true; } break; case GalleryViewType.Main: if (!option.FilterTarget.HasFlag(TagFilterTarget.MainWindow)) { return true; } break; case GalleryViewType.SearchResult: if (!option.FilterTarget.HasFlag(TagFilterTarget.SearchResultWindow)) { return true; } break; case GalleryViewType.History: if (!option.FilterTarget.HasFlag(TagFilterTarget.HistoryWindow)) { return true; } break; default: break; } var filter_list = option.UseAllGalleryFilterList ? TagManager.FiltedTags : TagManager.FiltedTags.Where(x => x.FromGallery == gallery.GalleryName); foreach (var filter_tag in filter_list) { if (detail.Tags.FirstOrDefault(x => x == filter_tag.Tag.Name) is string captured_filter_tag) { Log.Debug($"Skip this item because of filter:{captured_filter_tag} -> {string.Join(" ", detail.Tags)}"); counter.Count++; return false; } } } return true; })); }
private async void TryRequestMoreItemFromLoadableSource() { if (is_requesting) { Log.Debug("Task is running"); return; } if (loadable_items == null) { return; } OnRequestMoreItemStarted?.Invoke(this); var option = SettingManager.LoadSetting <GlobalSetting>(); is_requesting = true; Gallery gallery = Gallery; IEnumerable <GalleryItem> source = loadable_items; var counter = new SkipCounterWrapper(); Log.Debug($"开始尝试获取列表({source.GetHashCode()})"); (GalleryItem[] list, bool success) = await Task.Run(() => { try { var l = FilterTag(source.Skip(current_index), counter, gallery).Where(x => { if (unique_items.Contains(x.GalleryItemID)) { return(false); } unique_items.Add(x.GalleryItemID); return(true); }).Take(option.GetPictureCountPerLoad).ToArray(); return(l, true); } catch (Exception e) { Toast.ShowMessage($"无法获取图片列表数据:{e.Message}"); return(new GalleryItem[0], false); } }); Log.Debug($"尝试获取列表结束({source.GetHashCode()} - {loadable_items?.GetHashCode()})"); if (source == loadable_items) { Log.Debug($"Skip({current_index}) Filter({counter.Count}) Take({option.GetPictureCountPerLoad}) ActualTake({list.Length})", "GridViewer_RequestMoreItems"); foreach (var item in list) { Items.Add(item); } if (success && list.Count() < option.GetPictureCountPerLoad) { Toast.ShowMessage("已到达图片队列末尾"); } current_index += list.Length + counter.Count; } OnRequestMoreItemFinished?.Invoke(this); is_requesting = false; if (source != loadable_items && loadable_items != null) { TryRequestMoreItemFromLoadableSource(); } }