From 98d9e0f0cf9a656157c1fd82ff505ad93732c42a Mon Sep 17 00:00:00 2001 From: Aaron Schultz Date: Fri, 14 May 2021 12:49:30 +0200 Subject: [PATCH 1/2] Added a favorite function for Item List --- .../StatisticsAnalysisTool/App.config | 3 + .../Common/ItemController.cs | 47 +++++++++++++- .../Languages/de-DE.xml | 3 +- .../Languages/en-US.xml | 1 + .../StatisticsAnalysisTool/Models/Item.cs | 7 ++- .../Models/MainWindowTranslation.cs | 2 + .../Properties/Settings.Designer.cs | 12 ++++ .../Properties/Settings.settings | 3 + .../ViewModels/MainWindowViewModel.cs | 61 +++++++++++++++++-- .../Views/MainWindow.xaml | 34 +++++++---- .../Views/MainWindow.xaml.cs | 1 + 11 files changed, 153 insertions(+), 21 deletions(-) diff --git a/StatisticsAnalysisTool/StatisticsAnalysisTool/App.config b/StatisticsAnalysisTool/StatisticsAnalysisTool/App.config index 6ed98923c..71b3220fe 100644 --- a/StatisticsAnalysisTool/StatisticsAnalysisTool/App.config +++ b/StatisticsAnalysisTool/StatisticsAnalysisTool/App.config @@ -112,6 +112,9 @@ https://raw.githubusercontent.com/Triky313/AlbionOnline-StatisticsAnalysis/master/StatisticsAnalysisTool/StatisticsAnalysisTool/GameFiles/lootchests.json + + FavoriteItems.json + diff --git a/StatisticsAnalysisTool/StatisticsAnalysisTool/Common/ItemController.cs b/StatisticsAnalysisTool/StatisticsAnalysisTool/Common/ItemController.cs index 9de18d2ee..a3880dd43 100644 --- a/StatisticsAnalysisTool/StatisticsAnalysisTool/Common/ItemController.cs +++ b/StatisticsAnalysisTool/StatisticsAnalysisTool/Common/ItemController.cs @@ -1,3 +1,4 @@ +using log4net; using Newtonsoft.Json; using StatisticsAnalysisTool.Enumerations; using StatisticsAnalysisTool.Models; @@ -9,6 +10,7 @@ using System.Linq; using System.Net; using System.Net.Http; +using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows; @@ -19,6 +21,8 @@ namespace StatisticsAnalysisTool.Common { public class ItemController { + private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + public static ObservableCollection Items; private static readonly string FullItemInformationFilePath = @@ -81,7 +85,7 @@ public static int GetItemLevel(string uniqueName) return int.TryParse(uniqueName.Split('@')[1], out var number) ? number : 0; } - + public static int GetItemTier(Item item) { if (item?.UniqueName == null) return -1; @@ -269,6 +273,47 @@ private static async Task GetItemListFromWebAsync(string url) } } + public static void SetFavoriteItemsFromLocalFile() + { + var localFilePath = $"{AppDomain.CurrentDomain.BaseDirectory}{Settings.Default.FavoriteItemsFileName}"; + if (File.Exists(localFilePath)) + { + try + { + var localItemString = File.ReadAllText(localFilePath, Encoding.UTF8); + foreach (var uniqueName in JsonConvert.DeserializeObject>(localItemString)) + { + var item = Items.FirstOrDefault(i => i.UniqueName == uniqueName); + if (item != null) + { + item.IsFavorite = true; + } + } + } + catch (Exception e) + { + Log.Error(MethodBase.GetCurrentMethod().Name, e); + } + } + } + + public static void SaveFavoriteItemsToLocalFile() + { + var localFilePath = $"{AppDomain.CurrentDomain.BaseDirectory}{Settings.Default.FavoriteItemsFileName}"; + var favoriteItems = Items.Where(x => x.IsFavorite); + var toSaveFavoriteItems = favoriteItems.Select(x => x.UniqueName); + var fileString = JsonConvert.SerializeObject(toSaveFavoriteItems); + + try + { + File.WriteAllText(localFilePath, fileString, Encoding.UTF8); + } + catch (Exception e) + { + Log.Error(MethodBase.GetCurrentMethod().Name, e); + } + } + #endregion Item list #region ItemInformation diff --git a/StatisticsAnalysisTool/StatisticsAnalysisTool/Languages/de-DE.xml b/StatisticsAnalysisTool/StatisticsAnalysisTool/Languages/de-DE.xml index 19fca03fc..16e4c010e 100644 --- a/StatisticsAnalysisTool/StatisticsAnalysisTool/Languages/de-DE.xml +++ b/StatisticsAnalysisTool/StatisticsAnalysisTool/Languages/de-DE.xml @@ -310,7 +310,8 @@ Item Preis unterboten Der Preis von in - Zeige nur Items mit Alarm AN + Nur Items mit Alarm AN zeigen + Nur Favoriten zeigen ist unterboten worden Ist Alarm aktiv Zu unterbietender Preis diff --git a/StatisticsAnalysisTool/StatisticsAnalysisTool/Languages/en-US.xml b/StatisticsAnalysisTool/StatisticsAnalysisTool/Languages/en-US.xml index cc4c16fda..c88163aac 100644 --- a/StatisticsAnalysisTool/StatisticsAnalysisTool/Languages/en-US.xml +++ b/StatisticsAnalysisTool/StatisticsAnalysisTool/Languages/en-US.xml @@ -311,6 +311,7 @@ The price of in Show only items with alert on + Show only favorites has been undercut Is alert activate Undercutting price diff --git a/StatisticsAnalysisTool/StatisticsAnalysisTool/Models/Item.cs b/StatisticsAnalysisTool/StatisticsAnalysisTool/Models/Item.cs index 05d28f275..8793d29a9 100644 --- a/StatisticsAnalysisTool/StatisticsAnalysisTool/Models/Item.cs +++ b/StatisticsAnalysisTool/StatisticsAnalysisTool/Models/Item.cs @@ -1,7 +1,7 @@ -using System.Windows.Media; -using System.Windows.Media.Imaging; -using FontAwesome5; +using FontAwesome5; using StatisticsAnalysisTool.Common; +using System.Windows.Media; +using System.Windows.Media.Imaging; namespace StatisticsAnalysisTool.Models { @@ -29,6 +29,7 @@ public class Item public int AlertModeMinSellPriceIsUndercutPrice { get; set; } public bool IsAlertActive { get; set; } + public bool IsFavorite { get; set; } public EFontAwesomeIcon AlertToggle => IsAlertActive ? EFontAwesomeIcon.Solid_ToggleOn : EFontAwesomeIcon.Solid_ToggleOff; public Brush AlertToggleColor => IsAlertActive ? ItemController.ToggleOnColor : ItemController.ToggleOffColor; diff --git a/StatisticsAnalysisTool/StatisticsAnalysisTool/Models/MainWindowTranslation.cs b/StatisticsAnalysisTool/StatisticsAnalysisTool/Models/MainWindowTranslation.cs index f4eb91f1c..41e7e8063 100644 --- a/StatisticsAnalysisTool/StatisticsAnalysisTool/Models/MainWindowTranslation.cs +++ b/StatisticsAnalysisTool/StatisticsAnalysisTool/Models/MainWindowTranslation.cs @@ -13,6 +13,7 @@ public class MainWindowTranslation public string Name => LanguageController.Translation("NAME"); public string LoadFullItemInfo => LanguageController.Translation("LOAD_FULL_ITEM_INFO"); public string ShowOnlyItemsWithAlertOn => LanguageController.Translation("SHOW_ONLY_ITEMS_WITH_ALERT_ON"); + public string ShowOnlyFavorites => LanguageController.Translation("SHOW_ONLY_FAVORITES"); public string IsAlertActive => LanguageController.Translation("IS_ALERT_ACTIVATE"); public string UndercuttingPrice => LanguageController.Translation("UNDERCUTTING_PRICE"); public string IsTrackingActive => LanguageController.Translation("IS_TRACKING_ACTIVE"); @@ -44,5 +45,6 @@ public class MainWindowTranslation public string SortByDps => LanguageController.Translation("SORT_BY_DPS"); public string SortByName => LanguageController.Translation("SORT_BY_NAME"); public string Settings => LanguageController.Translation("SETTINGS"); + } } \ No newline at end of file diff --git a/StatisticsAnalysisTool/StatisticsAnalysisTool/Properties/Settings.Designer.cs b/StatisticsAnalysisTool/StatisticsAnalysisTool/Properties/Settings.Designer.cs index c06025037..6b5dfc974 100644 --- a/StatisticsAnalysisTool/StatisticsAnalysisTool/Properties/Settings.Designer.cs +++ b/StatisticsAnalysisTool/StatisticsAnalysisTool/Properties/Settings.Designer.cs @@ -448,5 +448,17 @@ public string DiscordUrl { return ((string)(this["DiscordUrl"])); } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("FavoriteItems.json")] + public string FavoriteItemsFileName { + get { + return ((string)(this["FavoriteItemsFileName"])); + } + set { + this["FavoriteItemsFileName"] = value; + } + } } } diff --git a/StatisticsAnalysisTool/StatisticsAnalysisTool/Properties/Settings.settings b/StatisticsAnalysisTool/StatisticsAnalysisTool/Properties/Settings.settings index f36227d38..abc5502c2 100644 --- a/StatisticsAnalysisTool/StatisticsAnalysisTool/Properties/Settings.settings +++ b/StatisticsAnalysisTool/StatisticsAnalysisTool/Properties/Settings.settings @@ -110,5 +110,8 @@ https://discord.com/invite/sahSrSPmaJ + + FavoriteItems.json + \ No newline at end of file diff --git a/StatisticsAnalysisTool/StatisticsAnalysisTool/ViewModels/MainWindowViewModel.cs b/StatisticsAnalysisTool/StatisticsAnalysisTool/ViewModels/MainWindowViewModel.cs index f53a81eae..6c5ba6622 100644 --- a/StatisticsAnalysisTool/StatisticsAnalysisTool/ViewModels/MainWindowViewModel.cs +++ b/StatisticsAnalysisTool/StatisticsAnalysisTool/ViewModels/MainWindowViewModel.cs @@ -122,6 +122,7 @@ public class MainWindowViewModel : INotifyPropertyChanged private ObservableCollection _factionPointStats = new ObservableCollection() { new MainStatObject() { Value = "0", ValuePerHour = "0", CityFaction = CityFaction.Unknown } }; private string _mainTrackerTimer; private Visibility _isMainTrackerPopupVisible = Visibility.Hidden; + private bool _isShowOnlyFavoritesActive; public MainWindowViewModel(MainWindow mainWindow) { @@ -378,10 +379,14 @@ private async void InitMainWindowData() var isItemListLoaded = await ItemController.GetItemListFromJsonAsync().ConfigureAwait(true); if (!isItemListLoaded) + { MessageBox.Show(LanguageController.Translation("ITEM_LIST_CAN_NOT_BE_LOADED"), LanguageController.Translation("ERROR")); + } if (isItemListLoaded) { + ItemController.SetFavoriteItemsFromLocalFile(); + await ItemController.GetItemInformationListFromLocalAsync(); IsFullItemInformationCompleteCheck(); @@ -737,22 +742,39 @@ public void SetDamageMeterSort() private void ItemsViewFilter() { - if (ItemsView == null) return; + if (ItemsView == null) + { + return; + } if (IsFullItemInfoSearchActive) ItemsView.Filter = i => { var item = i as Item; if (IsShowOnlyItemsWithAlertOnActive) + { return item?.FullItemInformation != null && item.LocalizedNameAndEnglish.ToLower().Contains(SearchText?.ToLower() ?? string.Empty) && (item.FullItemInformation?.CategoryObject?.ParentCategory == SelectedItemParentCategory || SelectedItemParentCategory == ParentCategory.Unknown) - && (item.FullItemInformation?.CategoryObject?.Category == SelectedItemCategory || - SelectedItemCategory == Category.Unknown) + && (item.FullItemInformation?.CategoryObject?.Category == SelectedItemCategory || SelectedItemCategory == Category.Unknown) && ((ItemTier) item.FullItemInformation?.Tier == SelectedItemTier || SelectedItemTier == ItemTier.Unknown) && ((ItemLevel) item.FullItemInformation?.Level == SelectedItemLevel || SelectedItemLevel == ItemLevel.Unknown) && item.IsAlertActive; + } + + if (IsShowOnlyFavoritesActive) + { + return item?.FullItemInformation != null && + item.LocalizedNameAndEnglish.ToLower().Contains(SearchText?.ToLower() ?? string.Empty) + && (item.FullItemInformation?.CategoryObject?.ParentCategory == SelectedItemParentCategory || + SelectedItemParentCategory == ParentCategory.Unknown) + && (item.FullItemInformation?.CategoryObject?.Category == SelectedItemCategory || SelectedItemCategory == Category.Unknown) + && ((ItemTier)item.FullItemInformation?.Tier == SelectedItemTier || SelectedItemTier == ItemTier.Unknown) + && ((ItemLevel)item.FullItemInformation?.Level == SelectedItemLevel || SelectedItemLevel == ItemLevel.Unknown) + && item.IsFavorite; + } + return item?.FullItemInformation != null && item.LocalizedNameAndEnglish.ToLower().Contains(SearchText?.ToLower() ?? string.Empty) && (item.FullItemInformation?.CategoryObject?.ParentCategory == SelectedItemParentCategory || @@ -767,8 +789,15 @@ private void ItemsViewFilter() var item = i as Item; if (IsShowOnlyItemsWithAlertOnActive) - return (item?.LocalizedNameAndEnglish.ToLower().Contains(SearchText?.ToLower() ?? string.Empty) ?? false) && - item.IsAlertActive; + { + return (item?.LocalizedNameAndEnglish.ToLower().Contains(SearchText?.ToLower() ?? string.Empty) ?? false) && item.IsAlertActive; + } + + if (IsShowOnlyFavoritesActive) + { + return (item?.LocalizedNameAndEnglish.ToLower().Contains(SearchText?.ToLower() ?? string.Empty) ?? false) && item.IsFavorite; + } + return item?.LocalizedNameAndEnglish.ToLower().Contains(SearchText?.ToLower() ?? string.Empty) ?? false; }; @@ -1189,6 +1218,28 @@ public bool IsShowOnlyItemsWithAlertOnActive set { _isShowOnlyItemsWithAlertOnActive = value; + + if (value) + { + IsShowOnlyFavoritesActive = false; + } + + ItemsViewFilter(); + ItemsView?.Refresh(); + OnPropertyChanged(); + } + } + + public bool IsShowOnlyFavoritesActive { + get => _isShowOnlyFavoritesActive; + set { + _isShowOnlyFavoritesActive = value; + + if (value) + { + IsShowOnlyItemsWithAlertOnActive = false; + } + ItemsViewFilter(); ItemsView?.Refresh(); OnPropertyChanged(); diff --git a/StatisticsAnalysisTool/StatisticsAnalysisTool/Views/MainWindow.xaml b/StatisticsAnalysisTool/StatisticsAnalysisTool/Views/MainWindow.xaml index a00395915..645c59968 100644 --- a/StatisticsAnalysisTool/StatisticsAnalysisTool/Views/MainWindow.xaml +++ b/StatisticsAnalysisTool/StatisticsAnalysisTool/Views/MainWindow.xaml @@ -33,15 +33,15 @@ - - - + + + - - - - + + + + @@ -52,7 +52,7 @@ - - + + + + @@ -124,7 +136,7 @@