Skip to content

Commit

Permalink
Filter improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
5andr0 committed Aug 9, 2016
1 parent aa32f69 commit e550462
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 51 deletions.
11 changes: 6 additions & 5 deletions PogoLocationFeeder.GUI/Common/PokemonFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static List<PokemonFilterModel> 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;
}
Expand All @@ -33,7 +33,7 @@ public static List<PokemonFilterModel> GetPokemons() {
public static void Save() {
var list = new List<string>();
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});
Expand All @@ -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)
{
Expand Down
9 changes: 9 additions & 0 deletions PogoLocationFeeder.GUI/GlobalVariables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SniperInfoModel> PokemonsInternal =
new ObservableCollection<SniperInfoModel>();
public static ObservableCollection<PokemonFilterModel> AllPokemonsInternal =
Expand Down
7 changes: 4 additions & 3 deletions PogoLocationFeeder.GUI/Models/PokemonFilterModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using Newtonsoft.Json;
using POGOProtos.Enums;
using PropertyChanged;

namespace PogoLocationFeeder.GUI.Models {
[ImplementPropertyChanged]
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;
}
}
}
56 changes: 38 additions & 18 deletions PogoLocationFeeder.GUI/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -68,8 +69,8 @@ public MainWindowViewModel() {

public PackIconKind PausePlayButtonIcon { get; set; } = PackIconKind.Pause;
public ReadOnlyObservableCollection<SniperInfoModel> Pokemons { get; }
public ReadOnlyObservableCollection<PokemonFilterModel> PokemonFilter { get; }
public ReadOnlyObservableCollection<PokemonFilterModel> PokemonToFilter { get; }
public ReadOnlyObservableCollection<PokemonFilterModel> PokemonFilter { get; set; }
public ReadOnlyObservableCollection<PokemonFilterModel> PokemonToFilter { get; set; }

public ICommand SettingsComand { get; }
public ICommand DebugComand { get; }
Expand All @@ -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";

Expand All @@ -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 {
Expand Down Expand Up @@ -160,6 +163,7 @@ public void ShowDebug() {
public void ShowFilter() {
if (TransitionerIndex != 0) {
TransitionerIndex = 0;
Common.PokemonFilter.Save();
return;
}
TransitionerIndex = 3;
Expand Down Expand Up @@ -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<PokemonFilterModel>(list.OrderBy(x => x.Id.ToString()));

list = GlobalVariables.PokemonToFeedFilterInternal;
GlobalVariables.PokemonToFeedFilterInternal = new ObservableCollection<PokemonFilterModel>(list.OrderBy(x => x.Id.ToString()));

PokemonFilter = new ReadOnlyObservableCollection<PokemonFilterModel>(GlobalVariables.AllPokemonsInternal);
PokemonToFilter = new ReadOnlyObservableCollection<PokemonFilterModel>(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<PokemonFilterModel>(list.OrderBy(x => x.Id));

list = GlobalVariables.PokemonToFeedFilterInternal;
GlobalVariables.PokemonToFeedFilterInternal = new ObservableCollection<PokemonFilterModel>(list.OrderBy(x => x.Id));

PokemonFilter = new ReadOnlyObservableCollection<PokemonFilterModel>(GlobalVariables.AllPokemonsInternal);
PokemonToFilter = new ReadOnlyObservableCollection<PokemonFilterModel>(GlobalVariables.PokemonToFeedFilterInternal);
}
}

Expand Down
18 changes: 9 additions & 9 deletions PogoLocationFeeder.GUI/Views/FilterView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,28 @@
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Row="0" Grid.ColumnSpan="2" Orientation="Horizontal">
<Label Margin="10, 0">Sort by:</Label>
<Button Style="{DynamicResource MaterialDesignFlatButton}" Command="{Binding SortAlphabeticalCommand}">Alphabetical</Button>
<Button Style="{DynamicResource MaterialDesignFlatButton}" Command="{Binding SortIdCommand}" Margin="10,0">Id</Button>
<Label Margin="10, 0" VerticalAlignment="center">Sort by:</Label>
<Button Style="{DynamicResource MaterialDesignFlatButton}" Command="{Binding SortAlphabeticalCommand}" Background="{Binding SortAlphaActive}">Alphabetical</Button>
<Button Style="{DynamicResource MaterialDesignFlatButton}" Command="{Binding SortIdCommand}" Margin="10,0" Background="{Binding SortIdActive}">Id</Button>
</StackPanel>
<Label Grid.Row="1" Grid.Column="0" HorizontalAlignment="Center">All Pokemon's</Label>
<ListBox x:Name="AllPokes" ItemsSource="{Binding PokemonFilter}" Grid.Column="0" Grid.Row="2" Margin="5" SelectedItem="{Binding SelectedPokemonFilter, Mode=TwoWay}" MouseDoubleClick="ListBox_MouseDoubleClick">
<Label Grid.Row="1" Grid.Column="0" HorizontalAlignment="Center" Content="All Pokemon's" FontWeight="Bold" FontSize="16"></Label>
<ListBox x:Name="AllPokes" ItemsSource="{Binding PokemonFilter}" Grid.Column="0" Grid.Row="2" Margin="5" SelectedItem="{Binding SelectedPokemonFilter, Mode=TwoWay}" MouseUp="AllPokes_PreviewMouseDown">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="10,-8" Orientation="Horizontal">
<Image Source="{Binding Image}" Height="25" Width="40"/>
<TextBlock Text="{Binding Name}" VerticalAlignment="Center"/>
<TextBlock Text="{Binding Id}" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Label Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center">Pokemon's to feed</Label>
<ListBox x:Name="FilterPokes" ItemsSource="{Binding PokemonToFilter}" Grid.Row="2" Grid.Column="1" Margin="5" SelectedItem="{Binding SelectedPokemonFiltered, Mode=TwoWay}" SelectedIndex="{Binding IndexPokemonToFilter, Mode=TwoWay}" MouseDoubleClick="ListBox_MouseDoubleClick_1">
<Label Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center" Content="Pokemon's to feed" FontWeight="Bold" FontSize="16"></Label>
<ListBox x:Name="FilterPokes" ItemsSource="{Binding PokemonToFilter}" Grid.Row="2" Grid.Column="1" Margin="5" SelectedItem="{Binding SelectedPokemonFiltered, Mode=TwoWay}" MouseUp="FilterPokes_PreviewMouseDown">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="10,-8" Orientation="Horizontal">
<Image Source="{Binding Image}" Height="25" Width="40"/>
<TextBlock Text="{Binding Name}" VerticalAlignment="Center"/>
<TextBlock Text="{Binding Id}" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
Expand Down
44 changes: 30 additions & 14 deletions PogoLocationFeeder.GUI/Views/FilterView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using PogoLocationFeeder.Config;
using PogoLocationFeeder.GUI.Common;
using PogoLocationFeeder.GUI.Models;
using PogoLocationFeeder.Helper;

namespace PogoLocationFeeder.GUI.Views {
/// <summary>
Expand All @@ -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);
}
}
}
7 changes: 6 additions & 1 deletion PogoLocationFeeder/Config/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -123,7 +125,10 @@ public static List<string> LoadFilter() {
jsonSettings.Converters.Add(new StringEnumConverter {CamelCaseText = true});
jsonSettings.ObjectCreationHandling = ObjectCreationHandling.Replace;
jsonSettings.DefaultValueHandling = DefaultValueHandling.Populate;
return JsonConvert.DeserializeObject<List<string>>(input, jsonSettings);
return JsonConvert.DeserializeObject<List<string>>(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 });
Expand Down
6 changes: 5 additions & 1 deletion PogoLocationFeeder/Helper/PokemonParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class PokemonParser
new HashSet<string> {"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)))
{
Expand Down Expand Up @@ -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}");
Expand Down

0 comments on commit e550462

Please sign in to comment.