diff --git a/HiyoshiCfhClient/Client.cs b/HiyoshiCfhClient/Client.cs index c7924b7..a2423ce 100644 --- a/HiyoshiCfhClient/Client.cs +++ b/HiyoshiCfhClient/Client.cs @@ -48,9 +48,11 @@ public Client(string tokenType, string accessToken, DebugConsole debugConsole) _DebugConsole = debugConsole; } - private void ResetContext() + internal void ResetContext() { Context = new Container(new Uri("https://hiyoshicfhweb.azurewebsites.net/odata")); + GC.Collect(); + Context.MergeOption = MergeOption.PreserveChanges; if (TokenType != null && AccessToken != null) { Context.SendingRequest2 += (sender, eventArgs) => @@ -104,6 +106,7 @@ void InitAdmiral() try { Context.SaveChanges(); + DetachAll(); } catch (DataServiceRequestException ex) { @@ -153,6 +156,7 @@ void UpdateAdmiral() try { Context.SaveChanges(); + DetachAll(); } catch (DataServiceRequestException ex) { @@ -188,6 +192,7 @@ void UpdateShipTypes() } } Context.SaveChanges(); + DetachAll(); } catch (Exception ex) { @@ -205,6 +210,7 @@ void UpdateShipInfoes() (x, y) => x.Id == y.ShipInfoId, x => new WebShipInfo(x), x => Context.AddToShipInfoes(x)); Context.SaveChanges(); + DetachAll(); } catch (Exception ex) { @@ -222,6 +228,7 @@ void UpdateSlotItemInfoes() (x, y) => x.Id == y.SlotItemInfoId, x => new WebSlotItemInfo(x), x => Context.AddToSlotItemInfoes(x)); Context.SaveChanges(); + DetachAll(); } catch (Exception ex) { @@ -251,6 +258,7 @@ await factory.StartNew(() => ); OutDebugConsole("Saving ship data"); Context.SaveChanges(); + DetachAll(); OutDebugConsole("Saved ship data"); } catch (DataServiceRequestException ex) @@ -289,6 +297,7 @@ await factory.StartNew(() => ); OutDebugConsole("Saving SlotItem data"); Context.SaveChanges(); + DetachAll(); OutDebugConsole("Saved SlotItem data"); } catch (DataServiceRequestException ex) @@ -325,11 +334,8 @@ await factory.StartNew(() => ); OutDebugConsole("Saving quest data"); Context.SaveChanges(); + DetachAll(); OutDebugConsole("Saved quest data"); - foreach (var quest in quests) - { - Context.Detach(quest); - } } catch (DataServiceRequestException ex) { @@ -354,6 +360,7 @@ await factory.StartNew(() => record.AdmiralId = Admiral.AdmiralId; Context.AddToMaterialRecords(record); Context.SaveChanges(); + DetachAll(); } catch (DataServiceRequestException ex) { @@ -418,6 +425,19 @@ void SyncWithOData(IEnumerable localList, IEnumerable odataList, } } } + + void DetachAll() + { + foreach (var entity in Context.Entities.ToList()) + { + Context.Detach(entity.Entity); + } + + foreach (var link in Context.Links.ToList()) + { + Context.DetachLink(link.Source, link.SourceProperty, link.Target); + } + } } #region 例外クラス diff --git a/HiyoshiCfhClient/HiyoshiCfhClient.csproj b/HiyoshiCfhClient/HiyoshiCfhClient.csproj index cc0bf43..3c49b91 100644 --- a/HiyoshiCfhClient/HiyoshiCfhClient.csproj +++ b/HiyoshiCfhClient/HiyoshiCfhClient.csproj @@ -70,17 +70,17 @@ ..\packages\LivetCask.1.3.1.0\lib\net45\Microsoft.Expression.Interactions.dll - - ..\packages\Microsoft.OData.Client.7.4.1\lib\net45\Microsoft.OData.Client.dll + + ..\packages\Microsoft.OData.Client.7.4.3\lib\net45\Microsoft.OData.Client.dll - - ..\packages\Microsoft.OData.Core.7.4.1\lib\portable-net45+win8+wpa81\Microsoft.OData.Core.dll + + ..\packages\Microsoft.OData.Core.7.4.3\lib\portable-net45+win8+wpa81\Microsoft.OData.Core.dll - - ..\packages\Microsoft.OData.Edm.7.4.1\lib\portable-net45+win8+wpa81\Microsoft.OData.Edm.dll + + ..\packages\Microsoft.OData.Edm.7.4.3\lib\portable-net45+win8+wpa81\Microsoft.OData.Edm.dll - - ..\packages\Microsoft.Spatial.7.4.1\lib\portable-net45+win8+wpa81\Microsoft.Spatial.dll + + ..\packages\Microsoft.Spatial.7.4.3\lib\portable-net45+win8+wpa81\Microsoft.Spatial.dll ..\packages\Nekoxy.1.5.3.21\lib\net45\Nekoxy.dll diff --git a/HiyoshiCfhClient/ViewModels/ClientViewModel.cs b/HiyoshiCfhClient/ViewModels/ClientViewModel.cs index ad8697c..4c49752 100644 --- a/HiyoshiCfhClient/ViewModels/ClientViewModel.cs +++ b/HiyoshiCfhClient/ViewModels/ClientViewModel.cs @@ -3,8 +3,8 @@ using HiyoshiCfhClient.Models; using HiyoshiCfhClient.Utils; using Livet; -using Livet.EventListeners; using Livet.Messaging; +using StatefulModel.EventListeners; using System; using System.IO; using System.Reactive.Linq; @@ -90,6 +90,7 @@ public bool EnableAutoUpdate PropertyChangedEventListener ItemyardListener = null; Client Client = null; QuestsTracker QuestsTracker; + IDisposable QuestSubscription; public ClientViewModel() { @@ -100,7 +101,7 @@ public ClientViewModel() TokenType = Settings.Current.TokenType; EnableAutoUpdate = true; var kccListener = new PropertyChangedEventListener(KanColleClient.Current); - kccListener.RegisterHandler(() => KanColleClient.Current.IsStarted, (_, __) => + kccListener.RegisterHandler("IsStarted", (_, __) => { if (!IsInited && KanColleClient.Current.IsStarted) { @@ -114,9 +115,10 @@ void InitHandlers() { if (OrganizationListener == null || ItemyardListener == null) { + OutDebugConsole("InitHandlers"); #region 艦娘の変更検知 OrganizationListener = new PropertyChangedEventListener(KanColleClient.Current.Homeport.Organization); - OrganizationListener.RegisterHandler(() => KanColleClient.Current.Homeport.Organization.Ships, + OrganizationListener.RegisterHandler("Ships", async (s, h) => { try @@ -143,7 +145,7 @@ void InitHandlers() #endregion #region 装備の変更検知 ItemyardListener = new PropertyChangedEventListener(KanColleClient.Current.Homeport.Itemyard); - ItemyardListener.RegisterHandler(() => KanColleClient.Current.Homeport.Itemyard.SlotItems, + ItemyardListener.RegisterHandler("SlotItems", async (s, h) => { try @@ -164,7 +166,7 @@ void InitHandlers() #endregion #region 任務の取得検知 var proxy = KanColleClient.Current.Proxy; - proxy.api_get_member_questlist + QuestSubscription = proxy.api_get_member_questlist .Select(QuestsTracker.QuestListSerialize) .Where(x => x != null && x.api_count >= 0) .Subscribe(async x => { await this.HandleQuests(x); }); @@ -176,6 +178,19 @@ void InitHandlers() } } + public void ResetHandlers() + { + OutDebugConsole("ResetHandlers"); + OrganizationListener.Dispose(); + OrganizationListener = null; + ItemyardListener.Dispose(); + ItemyardListener = null; + KanColleClient.Current.Homeport.Materials.PropertyChanged -= MaterialsChanged; + QuestSubscription.Dispose(); + IsInited = false; + InitHandlers(); + } + private async void MaterialsChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { var materials = sender as Materials; @@ -339,6 +354,21 @@ async Task PrepareClient() } } + public void ResetClient() + { + OutDebugConsole("ResetClient"); + Client = null; + PrepareClient(); + } + + public void ResetContext() + { + if (Client != null) + { + Client.ResetContext(); + } + } + void InitClient() { if (CheckToken()) diff --git a/HiyoshiCfhClient/Views/ClientView.xaml b/HiyoshiCfhClient/Views/ClientView.xaml index 1fc9021..be1a22a 100644 --- a/HiyoshiCfhClient/Views/ClientView.xaml +++ b/HiyoshiCfhClient/Views/ClientView.xaml @@ -62,20 +62,41 @@ - - + + + diff --git a/HiyoshiCfhClient/packages.config b/HiyoshiCfhClient/packages.config index 6090323..ca7ab8a 100644 --- a/HiyoshiCfhClient/packages.config +++ b/HiyoshiCfhClient/packages.config @@ -14,10 +14,10 @@ - - - - + + + +