static void MigrateLocalMylistToPlaylistRepository(PlaylistRepository playlistRepository) { var groups = LocalMylistDb.GetLocalMylistGroups(); if (groups.Count > 0) { System.Diagnostics.Debug.WriteLine("start migrating LocalMylist."); foreach (var legacyLocalPlaylist in groups) { var newPlaylist = new PlaylistEntity() { Id = LiteDB.ObjectId.NewObjectId().ToString(), Label = legacyLocalPlaylist.Label, Count = legacyLocalPlaylist.Items.Count, PlaylistOrigin = Interfaces.PlaylistOrigin.Local }; playlistRepository.Upsert(newPlaylist); playlistRepository.AddItems(newPlaylist.Id, legacyLocalPlaylist.Items); LocalMylistDb.Remove(legacyLocalPlaylist); System.Diagnostics.Debug.WriteLine($"migrated: {newPlaylist.Label} ({newPlaylist.Count})"); } System.Diagnostics.Debug.WriteLine("migrating LocalMylist done."); } }
static private List <LocalMylistGroup> RestoreLocalMylistGroupsFromLocalDatabase() { var groups = LocalMylistDb.GetLocalMylistGroups(); return(groups .Select(x => new LocalMylistGroup(x.Id, x.Label, new ObservableCollection <string>(x.Items))) .ToList() ); }
public LocalMylistManager( ) { Mylists = new ObservableCollection <LocalMylistGroup>(); Mylists.CollectionChangedAsObservable() .Subscribe(e => { if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) { foreach (var newItem in e.NewItems.Cast <LocalMylistGroup>()) { CompositeDisposable disposables = new CompositeDisposable(); new[] { newItem.ObserveProperty(x => x.Label).ToUnit(), newItem.CollectionChangedAsObservable().ToUnit() } .Merge() .Throttle(TimeSpan.FromSeconds(1)) .Subscribe(_ => { LocalMylistDb.AddOrUpdate(new LocalMylistData() { Id = newItem.Id, Label = newItem.Label, Items = newItem.ToList(), SortIndex = newItem.SortIndex }); }) .AddTo(disposables); LocalMylistPropertyChangedObserverMap.Add(newItem.Id, disposables); } } else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove) { foreach (var removeItem in e.NewItems.Cast <LocalMylistGroup>()) { LocalMylistDb.Get(removeItem.Id); if (LocalMylistPropertyChangedObserverMap.TryGetValue(removeItem.Id, out var disposer)) { disposer.Dispose(); LocalMylistPropertyChangedObserverMap.Remove(removeItem.Id); } } } }); foreach (var restoreItem in RestoreLocalMylistGroupsFromLocalDatabase()) { Mylists.Add(restoreItem); } }