Exemplo n.º 1
0
        /// <summary>
        /// Return a filtered list, based on the given query.
        /// </summary>
        /// <param name="query">The query to filter the list.</param>
        /// <returns>A filtered list, can be empty when nothing was found.</returns>
        public List <Result> Query(Query query)
        {
            if (_windowsSettings?.Settings is null)
            {
                return(new List <Result>(0));
            }

            var filteredList = _windowsSettings.Settings
                               .Where(Predicate)
                               .OrderBy(found => found.Name);

            var newList = ResultHelper.GetResultList(filteredList, query.Search, _defaultIconPath);

            return(newList);

            bool Predicate(WindowsSetting found)
            {
                if (string.IsNullOrWhiteSpace(query.Search))
                {
                    // If no search string is entered skip query comparison.
                    return(true);
                }

                if (found.Name.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase))
                {
                    return(true);
                }

                if (!(found.Areas is null))
                {
                    foreach (var area in found.Areas)
                    {
                        // Search for areas on normal queries.
                        if (area.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase))
                        {
                            return(true);
                        }

                        // Search for Area only on queries with action char.
                        if (area.Contains(query.Search.Replace(":", string.Empty), StringComparison.CurrentCultureIgnoreCase) &&
                            query.Search.EndsWith(":"))
                        {
                            return(true);
                        }
                    }
                }

                if (!(found.AltNames is null))
                {
                    foreach (var altName in found.AltNames)
                    {
                        if (altName.Contains(query.Search, StringComparison.CurrentCultureIgnoreCase))
                        {
                            return(true);
                        }
                    }
                }

                // Search by key char '>' for app name and settings path
                if (query.Search.Contains('>'))
                {
                    return(ResultHelper.FilterBySettingsPath(found, query.Search));
                }

                return(false);
            }
        }