diff --git a/PogoLocationFeeder.GUI/Common/PokemonFilter.cs b/PogoLocationFeeder.GUI/Common/PokemonFilter.cs index c58c6c8..fa2f804 100644 --- a/PogoLocationFeeder.GUI/Common/PokemonFilter.cs +++ b/PogoLocationFeeder.GUI/Common/PokemonFilter.cs @@ -23,7 +23,7 @@ public static List GetPokemons() { $"pack://application:,,,/PogoLocationFeeder.GUI;component/Assets/icons/{(int) id}.png", UriKind.Absolute)); img.Freeze(); - pokes.Add(new PokemonFilterModel(id.ToString(), img, true)); + pokes.Add(new PokemonFilterModel(id, img)); } return pokes; } @@ -33,7 +33,7 @@ public static List GetPokemons() { public static void Save() { var list = new List(); foreach (var pokemonFilterModel in GlobalVariables.PokemonToFeedFilterInternal) { - list.Add(pokemonFilterModel.Name); + list.Add(pokemonFilterModel.Id.ToString()); } var output = JsonConvert.SerializeObject(list, Formatting.Indented, new StringEnumConverter {CamelCaseText = true}); @@ -57,18 +57,19 @@ public static void Load() { File.WriteAllText(ConfigFile, output); } GlobalSettings.PokekomsToFeedFilter = GlobalSettings.LoadFilter(); - var set = GlobalSettings.PokekomsToFeedFilter; + var set = GlobalSettings.PokekomsToFeedFilter.OrderBy(x => PokemonParser.ParsePokemon(x)); foreach (var s in set) { try { - var id = PokemonParser.ParsePokemon(s, true); + var id = PokemonParser.ParsePokemon(s, false); var img = new BitmapImage( new Uri( $"pack://application:,,,/PogoLocationFeeder.GUI;component/Assets/icons/{(int)id}.png", UriKind.Absolute)); img.Freeze(); - GlobalVariables.PokemonToFeedFilterInternal.Add(new PokemonFilterModel(id.ToString(), img, true)); + GlobalVariables.PokemonToFeedFilterInternal.Add(new PokemonFilterModel(id, img)); + GlobalVariables.AllPokemonsInternal.Remove(GlobalVariables.AllPokemonsInternal.Single(x => x.Id == id)); } catch (Exception e) { diff --git a/PogoLocationFeeder.GUI/GlobalVariables.cs b/PogoLocationFeeder.GUI/GlobalVariables.cs index b49c74e..52d0b62 100644 --- a/PogoLocationFeeder.GUI/GlobalVariables.cs +++ b/PogoLocationFeeder.GUI/GlobalVariables.cs @@ -5,8 +5,17 @@ namespace PogoLocationFeeder.GUI { + public enum SortMode + { + AlphabeticalAsc, + AlphabeticalDesc, + IdAsc, + IdDesc + } public static class GlobalVariables { + public static SortMode SortMode = SortMode.IdAsc; + public static ObservableCollection PokemonsInternal = new ObservableCollection(); public static ObservableCollection AllPokemonsInternal = diff --git a/PogoLocationFeeder.GUI/Models/PokemonFilterModel.cs b/PogoLocationFeeder.GUI/Models/PokemonFilterModel.cs index 5ff9c90..e899fc8 100644 --- a/PogoLocationFeeder.GUI/Models/PokemonFilterModel.cs +++ b/PogoLocationFeeder.GUI/Models/PokemonFilterModel.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using System.Windows.Media.Imaging; using Newtonsoft.Json; +using POGOProtos.Enums; using PropertyChanged; namespace PogoLocationFeeder.GUI.Models { @@ -12,11 +13,11 @@ namespace PogoLocationFeeder.GUI.Models { public class PokemonFilterModel { [JsonIgnore] public BitmapImage Image { get; set; } - public string Name { get; set; } + public PokemonId Id { get; set; } - public PokemonFilterModel(string name, BitmapImage img, bool filtered) { + public PokemonFilterModel(PokemonId id, BitmapImage img) { Image = img; - Name = name; + Id = id; } } } diff --git a/PogoLocationFeeder.GUI/ViewModels/MainWindowViewModel.cs b/PogoLocationFeeder.GUI/ViewModels/MainWindowViewModel.cs index 19ce3fe..6a12160 100644 --- a/PogoLocationFeeder.GUI/ViewModels/MainWindowViewModel.cs +++ b/PogoLocationFeeder.GUI/ViewModels/MainWindowViewModel.cs @@ -9,6 +9,7 @@ using System.Windows; using System.Windows.Controls; using System.Windows.Input; +using System.Windows.Media; using log4net.Config; using MaterialDesignThemes.Wpf; using PogoLocationFeeder.Common; @@ -38,8 +39,8 @@ public MainWindowViewModel() { PayPalCommand = new ActionCommand(OpenPaypal); BitcoinCommand = new ActionCommand(OpenBitcoin); FilterCommand = new ActionCommand(ShowFilter); - FilterAddCommand = new ActionCommand(AddToFilter); - FilterRemoveCommand = new ActionCommand(RemoveFromFilter); + SortAlphabeticalCommand = new ActionCommand(SortByAlphabetical); + SortIdCommand = new ActionCommand(SortById); Settings.Default.DebugOutput = ""; //var poke = new SniperInfo { // Id = PokemonId.Missingno, @@ -68,8 +69,8 @@ public MainWindowViewModel() { public PackIconKind PausePlayButtonIcon { get; set; } = PackIconKind.Pause; public ReadOnlyObservableCollection Pokemons { get; } - public ReadOnlyObservableCollection PokemonFilter { get; } - public ReadOnlyObservableCollection PokemonToFilter { get; } + public ReadOnlyObservableCollection PokemonFilter { get; set; } + public ReadOnlyObservableCollection PokemonToFilter { get; set; } public ICommand SettingsComand { get; } public ICommand DebugComand { get; } @@ -79,8 +80,8 @@ public MainWindowViewModel() { public ICommand PayPalCommand { get; } public ICommand BitcoinCommand { get; } public ICommand FilterCommand { get; } - public ICommand FilterAddCommand { get; } - public ICommand FilterRemoveCommand { get; } + public ICommand SortAlphabeticalCommand { get; } + public ICommand SortIdCommand { get; } public string CustomIp { get; set; } = "localhost"; @@ -99,6 +100,8 @@ public MainWindowViewModel() { public PokemonFilterModel SelectedPokemonFilter { get; set; } public PokemonFilterModel SelectedPokemonFiltered { get; set; } public int IndexPokemonToFilter { get; set; } + public SolidColorBrush SortAlphaActive { get; set; } + public SolidColorBrush SortIdActive { get; set; } = new SolidColorBrush(Colors.DimGray); public Visibility ColVisibility { get { @@ -160,6 +163,7 @@ public void ShowDebug() { public void ShowFilter() { if (TransitionerIndex != 0) { TransitionerIndex = 0; + Common.PokemonFilter.Save(); return; } TransitionerIndex = 3; @@ -195,20 +199,36 @@ public void OpenBitcoin() { } } - public void AddToFilter() { - var filter = GlobalVariables.PokemonToFeedFilterInternal; - if (SelectedPokemonFilter != null && !filter.Contains(SelectedPokemonFilter)) { - filter.Add(SelectedPokemonFilter); - GlobalSettings.PokekomsToFeedFilter.Add(SelectedPokemonFilter.Name); - Common.PokemonFilter.Save(); - } + public void SortByAlphabetical() + { + SortIdActive = new SolidColorBrush(Colors.Transparent); + SortAlphaActive = new SolidColorBrush(Colors.DimGray); + + GlobalVariables.SortMode = SortMode.AlphabeticalAsc; + var list = GlobalVariables.AllPokemonsInternal; + GlobalVariables.AllPokemonsInternal = new ObservableCollection(list.OrderBy(x => x.Id.ToString())); + + list = GlobalVariables.PokemonToFeedFilterInternal; + GlobalVariables.PokemonToFeedFilterInternal = new ObservableCollection(list.OrderBy(x => x.Id.ToString())); + + PokemonFilter = new ReadOnlyObservableCollection(GlobalVariables.AllPokemonsInternal); + PokemonToFilter = new ReadOnlyObservableCollection(GlobalVariables.PokemonToFeedFilterInternal); } - public void RemoveFromFilter() { - if (IndexPokemonToFilter == -1) return; - GlobalSettings.PokekomsToFeedFilter.Remove(SelectedPokemonFiltered.Name); - GlobalVariables.PokemonToFeedFilterInternal.Remove(SelectedPokemonFiltered); - Common.PokemonFilter.Save(); + public void SortById() + { + SortIdActive = new SolidColorBrush(Colors.DimGray); + SortAlphaActive = new SolidColorBrush(Colors.Transparent); + + GlobalVariables.SortMode = SortMode.IdAsc; + var list = GlobalVariables.AllPokemonsInternal; + GlobalVariables.AllPokemonsInternal = new ObservableCollection(list.OrderBy(x => x.Id)); + + list = GlobalVariables.PokemonToFeedFilterInternal; + GlobalVariables.PokemonToFeedFilterInternal = new ObservableCollection(list.OrderBy(x => x.Id)); + + PokemonFilter = new ReadOnlyObservableCollection(GlobalVariables.AllPokemonsInternal); + PokemonToFilter = new ReadOnlyObservableCollection(GlobalVariables.PokemonToFeedFilterInternal); } } diff --git a/PogoLocationFeeder.GUI/Views/FilterView.xaml b/PogoLocationFeeder.GUI/Views/FilterView.xaml index 5ea21eb..df571e1 100644 --- a/PogoLocationFeeder.GUI/Views/FilterView.xaml +++ b/PogoLocationFeeder.GUI/Views/FilterView.xaml @@ -18,28 +18,28 @@ - - - + + + - - + + - + - - + + - + diff --git a/PogoLocationFeeder.GUI/Views/FilterView.xaml.cs b/PogoLocationFeeder.GUI/Views/FilterView.xaml.cs index 1b6c6d4..6a6e23a 100644 --- a/PogoLocationFeeder.GUI/Views/FilterView.xaml.cs +++ b/PogoLocationFeeder.GUI/Views/FilterView.xaml.cs @@ -15,6 +15,7 @@ using PogoLocationFeeder.Config; using PogoLocationFeeder.GUI.Common; using PogoLocationFeeder.GUI.Models; +using PogoLocationFeeder.Helper; namespace PogoLocationFeeder.GUI.Views { /// @@ -25,24 +26,39 @@ public FilterView() { InitializeComponent(); } - private void ListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e) { - if(AllPokes.SelectedIndex == -1) return; - var filter = GlobalVariables.PokemonToFeedFilterInternal; + private void AllPokes_PreviewMouseDown(object sender, MouseButtonEventArgs e) + { + if (AllPokes.SelectedIndex == -1) return; + + var right = GlobalVariables.PokemonToFeedFilterInternal; + var left = GlobalVariables.AllPokemonsInternal; + var selected = (PokemonFilterModel)AllPokes.SelectedItem; - if(!filter.Contains(selected)) { - filter.Add(selected); - GlobalSettings.PokekomsToFeedFilter.Add(selected.Name); - PokemonFilter.Save(); - } + GlobalSettings.PokekomsToFeedFilter.Add(selected.Id.ToString()); + + int i = right.Count(x => (GlobalVariables.SortMode == SortMode.AlphabeticalAsc) ? + x.Id.ToString().CompareTo(selected.Id.ToString()) < 0 : + x.Id < selected.Id); + right.Insert(i, selected); + left.Remove(selected); + } - private void ListBox_MouseDoubleClick_1(object sender, MouseButtonEventArgs e) { - if(FilterPokes.SelectedIndex == -1) return; - var filter = GlobalVariables.PokemonToFeedFilterInternal; + private void FilterPokes_PreviewMouseDown(object sender, MouseButtonEventArgs e) + { + if (FilterPokes.SelectedIndex == -1) return; + + var right = GlobalVariables.PokemonToFeedFilterInternal; + var left = GlobalVariables.AllPokemonsInternal; + var selected = (PokemonFilterModel)FilterPokes.SelectedItem; - filter.Remove(selected); - GlobalSettings.PokekomsToFeedFilter.Remove(selected.Name); - PokemonFilter.Save(); + GlobalSettings.PokekomsToFeedFilter.Remove(selected.Id.ToString()); + + int i = left.Count(x => (GlobalVariables.SortMode == SortMode.AlphabeticalAsc) ? + x.Id.ToString().CompareTo(selected.Id.ToString()) < 0 : + x.Id < selected.Id); + left.Insert(i, selected); + right.Remove(selected); } } } diff --git a/PogoLocationFeeder/Config/Settings.cs b/PogoLocationFeeder/Config/Settings.cs index 05e4310..5fec2dc 100644 --- a/PogoLocationFeeder/Config/Settings.cs +++ b/PogoLocationFeeder/Config/Settings.cs @@ -3,11 +3,13 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using Microsoft.Win32; using Newtonsoft.Json; using Newtonsoft.Json.Converters; using PogoLocationFeeder.Common; using PogoLocationFeeder.Helper; +using POGOProtos.Enums; #endregion @@ -123,7 +125,10 @@ public static List LoadFilter() { jsonSettings.Converters.Add(new StringEnumConverter {CamelCaseText = true}); jsonSettings.ObjectCreationHandling = ObjectCreationHandling.Replace; jsonSettings.DefaultValueHandling = DefaultValueHandling.Populate; - return JsonConvert.DeserializeObject>(input, jsonSettings); + return JsonConvert.DeserializeObject>(input, jsonSettings). + Where(x => PokemonParser.ParsePokemon(x, true) != PokemonId.Missingno). + GroupBy(x => PokemonParser.ParsePokemon(x)). + Select(y => y.FirstOrDefault()).ToList(); } else { var output = JsonConvert.SerializeObject(DefaultPokemonsToFeed, Formatting.Indented, new StringEnumConverter { CamelCaseText = true }); diff --git a/PogoLocationFeeder/Helper/PokemonParser.cs b/PogoLocationFeeder/Helper/PokemonParser.cs index 2a68b78..2c0272d 100644 --- a/PogoLocationFeeder/Helper/PokemonParser.cs +++ b/PogoLocationFeeder/Helper/PokemonParser.cs @@ -16,7 +16,7 @@ public class PokemonParser new HashSet {"Mime"}) }; - public static PokemonId ParsePokemon(string input, bool throwException = false) + public static PokemonId ParsePokemon(string input, bool showError = false, bool throwException = false) { foreach (var name in Enum.GetNames(typeof(PokemonId))) { @@ -49,6 +49,10 @@ public static PokemonId ParsePokemon(string input, bool throwException = false) } } + if (showError) + { + Log.Error($"No pokemon found with name {input}"); + } if (throwException) { throw new Exception($"No pokemon found with name {input}");