Exemple #1
0
        public void UpdateToMatchRaisesCollectionChangeOnlyOnceWithResetIfItemsAreRemoved()
        {
            var oc = new ObservableCollectionEx <string>
            {
                "Existing",
                "Items"
            };

            var toRemove = new List <string>
            {
                "Items"
            };

            var itemCount  = 0;
            var eventCount = 0;
            var action     = NotifyCollectionChangedAction.Add;

            oc.CollectionChanged += (sender, args) =>
            {
                ++eventCount;
                itemCount = oc.Count;
                action    = args.Action;
            };

            oc.UpdateToMatch(toRemove, s => s);

            itemCount.Should().Be(1);
            eventCount.Should().Be(1);
            action.Should().Be(NotifyCollectionChangedAction.Reset);
        }
Exemple #2
0
        public void UpdateToMatchUpdatesChangedItems()
        {
            var oc = new ObservableCollectionEx <CollectionItem>
            {
                new CollectionItem("First", 1),
                new CollectionItem("Second", 2),
                new CollectionItem("Third", 3)
            };

            var toChange = new List <CollectionItem>
            {
                new CollectionItem("First", 100),
                new CollectionItem("Second", 200),
                new CollectionItem("Third", 3)
            };

            Func <CollectionItem, CollectionItem, bool> updateAction = (i1, i2) =>
            {
                if (i1.Value != i2.Value)
                {
                    i1.Value = i2.Value;
                    return(true);
                }

                return(false);
            };

            oc.UpdateToMatch(toChange, i => i.Key, updateAction);

            oc.Single(i => i.Key == "First").Value.Should().Be(100);
            oc.Single(i => i.Key == "Second").Value.Should().Be(200);
            oc.Single(i => i.Key == "Third").Value.Should().Be(3);
        }
Exemple #3
0
        public void UpdateToMatchReturnsTrueIfItemsAreUpdated()
        {
            var oc = new ObservableCollectionEx <CollectionItem>
            {
                new CollectionItem("First", 1),
                new CollectionItem("Second", 2),
                new CollectionItem("Third", 3)
            };

            var toChange = new List <CollectionItem>
            {
                new CollectionItem("First", 1),
                new CollectionItem("Second", 200),
                new CollectionItem("Third", 3)
            };

            Func <CollectionItem, CollectionItem, bool> updateAction = (i1, i2) =>
            {
                if (i1.Value != i2.Value)
                {
                    i1.Value = i2.Value;
                    return(true);
                }

                return(false);
            };

            oc.UpdateToMatch(toChange, i => i.Key, updateAction).Should().BeTrue();
        }
        private async Task RefreshSessionAsync()
        {
            IList <Video>  videos;
            IList <Server> clients;

            try
            {
                videos = await GetNowPlayingAsync();
            }
            catch
            {
                videos = new List <Video>();
            }

            try
            {
                clients = await GetClientsAsync();
            }
            catch
            {
                clients = new List <Server>();
            }

            foreach (var video in videos)
            {
                var client = clients.FirstOrDefault(c => c.Key == video.Player.Key);
                if (client != null)
                {
                    video.Player.Client = client;
                }
            }

            _clients.UpdateToMatch(clients, v => v.Key, UpdateClient);
            _nowPlaying.UpdateToMatch(videos, v => v.Key, UpdateVideo);
        }
        protected override bool OnUpdateFrom(MediaContainer newValue, List <string> updatedPropertyNames)
        {
            var isUpdated = UpdateValue(() => PublicAddress, newValue, updatedPropertyNames);

            isUpdated = UpdateValue(() => FriendlyName, newValue, updatedPropertyNames) | isUpdated;
            isUpdated = UpdateValue(() => Platform, newValue, updatedPropertyNames) | isUpdated;

            if (Devices == null)
            {
                Devices = new ObservableCollectionEx <Device>();
            }
            if (Videos == null)
            {
                Videos = new ObservableCollectionEx <Video>();
            }
            if (Servers == null)
            {
                Servers = new ObservableCollectionEx <Server>();
            }

            isUpdated = Devices.UpdateToMatch(newValue.Devices, r => r.Key, (d1, d2) => d1.UpdateFrom(d2)) | isUpdated;
            isUpdated = Videos.UpdateToMatch(newValue.Videos, r => r.Key, (v1, v2) => v1.UpdateFrom(v2)) | isUpdated;
            isUpdated = Servers.UpdateToMatch(newValue.Servers, r => r.Key, (s1, s2) => s1.UpdateFrom(s2)) | isUpdated;

            return(isUpdated);
        }
 private void UpdateConnections()
 {
     lock (_syncObject)
     {
         var toAdd = _plexServerConnections.Select(p => new ServerConnection(p)).ToList();
         _allServerConnections.UpdateToMatch(toAdd, sc => sc.Key, (sc1, sc2) => sc1.UpdateFrom(sc2));
     }
 }
        protected override bool OnUpdateFrom(MediaContainer newValue, List <string> updatedPropertyNames)
        {
            var isUpdated = UpdateValue(() => PublicAddress, newValue, updatedPropertyNames);

            isUpdated = UpdateValue(() => FriendlyName, newValue, updatedPropertyNames) | isUpdated;
            isUpdated = UpdateValue(() => Platform, newValue, updatedPropertyNames) | isUpdated;
            isUpdated = Devices.UpdateToMatch(newValue.Devices, r => r.Key, (d1, d2) => d1.UpdateFrom(d2)) | isUpdated;
            isUpdated = Videos.UpdateToMatch(newValue.Videos, r => r.Key, (v1, v2) => v1.UpdateFrom(v2)) | isUpdated;
            isUpdated = Servers.UpdateToMatch(newValue.Servers, r => r.Key, (s1, s2) => s1.UpdateFrom(s2)) | isUpdated;

            return(isUpdated);
        }
Exemple #8
0
        public void UpdateToMatchWithANullKeyFuncThrows()
        {
            var oc    = new ObservableCollectionEx <string>();
            var toAdd = new List <string>
            {
                "Existing",
                "Items",
                "Foo",
                "Bar"
            };

            oc.UpdateToMatch <string>(toAdd, null);
        }
Exemple #9
0
        public void UpdateToMatchReturnsTrueIfItemsAreRemoved()
        {
            var oc = new ObservableCollectionEx <string>
            {
                "Existing",
                "Items"
            };

            var toRemove = new List <string>
            {
                "Items"
            };

            oc.UpdateToMatch(toRemove, s => s).Should().BeTrue();
        }
Exemple #10
0
        public void UpdateToMatchRaisesCollectionChangeOnlyOnceWithResetIfItemsAreAddedRemovedAndUpdated()
        {
            var oc = new ObservableCollectionEx <CollectionItem>
            {
                new CollectionItem("First", 1),
                new CollectionItem("Second", 2),
                new CollectionItem("Third", 3)
            };

            var toChange = new List <CollectionItem>
            {
                new CollectionItem("First", 1),
                new CollectionItem("Second", 200),
                new CollectionItem("Fourth", 4)
            };

            Func <CollectionItem, CollectionItem, bool> updateAction = (i1, i2) =>
            {
                if (i1.Value != i2.Value)
                {
                    i1.Value = i2.Value;
                    return(true);
                }

                return(false);
            };

            var itemCount  = 0;
            var eventCount = 0;
            var action     = NotifyCollectionChangedAction.Add;

            oc.CollectionChanged += (sender, args) =>
            {
                ++eventCount;
                itemCount = oc.Count;
                action    = args.Action;
            };

            oc.UpdateToMatch(toChange, i => i.Key, updateAction).Should().BeTrue();

            itemCount.Should().Be(3);
            eventCount.Should().Be(1);
            action.Should().Be(NotifyCollectionChangedAction.Reset);

            oc.Single(i => i.Key == "First").Value.Should().Be(1);
            oc.Single(i => i.Key == "Second").Value.Should().Be(200);
            oc.Single(i => i.Key == "Fourth").Value.Should().Be(4);
        }
Exemple #11
0
        public void UpdateToMatchReturnsTrueIfItemsAreAdded()
        {
            var oc = new ObservableCollectionEx <string>
            {
                "Existing",
                "Items"
            };

            var toAdd = new List <string>
            {
                "Existing",
                "Items",
                "Foo",
                "Bar"
            };

            oc.UpdateToMatch(toAdd, s => s).Should().BeTrue();
        }
Exemple #12
0
        public void UpdateToMatchRemovesMissingItems()
        {
            var oc = new ObservableCollectionEx <string>
            {
                "Existing",
                "Items"
            };

            var toRemove = new List <string>
            {
                "Items"
            };

            oc.UpdateToMatch(toRemove, s => s);

            oc.Should().ContainInOrder(toRemove);
            oc.Should().OnlyContain(s => toRemove.Contains(s));
            oc.Count.Should().Be(1);
        }
Exemple #13
0
        public void UpdateToMatchAddsNewItems()
        {
            var oc = new ObservableCollectionEx <string>
            {
                "Existing",
                "Items"
            };

            var toAdd = new List <string>
            {
                "Existing",
                "Items",
                "Foo",
                "Bar"
            };

            oc.UpdateToMatch(toAdd, s => s);

            oc.Should().ContainInOrder(toAdd);
            oc.Should().OnlyContain(s => toAdd.Contains(s));
            oc.Count.Should().Be(4);
        }
Exemple #14
0
        public void UpdateToMatchWithANullCollectionThrows()
        {
            var oc = new ObservableCollectionEx <string>();

            oc.UpdateToMatch(null, s => s);
        }
        public void UpdateToMatchReturnsTrueIfItemsAreUpdated()
        {
            var oc = new ObservableCollectionEx<CollectionItem>
            {
                new CollectionItem("First", 1),
                new CollectionItem("Second", 2),
                new CollectionItem("Third", 3)
            };

            var toChange = new List<CollectionItem>
            {
                new CollectionItem("First", 1),
                new CollectionItem("Second", 200),
                new CollectionItem("Third", 3)
            };

            Func<CollectionItem, CollectionItem, bool> updateAction = (i1, i2) =>
            {
                if (i1.Value != i2.Value)
                {
                    i1.Value = i2.Value;
                    return true;
                }

                return false;
            };

            oc.UpdateToMatch(toChange, i => i.Key, updateAction).Should().BeTrue();
        }
        public void UpdateToMatchAddsNewItems()
        {
            var oc = new ObservableCollectionEx<string>
            {
                "Existing",
                "Items"
            };

            var toAdd = new List<string>
            {
                "Existing",
                "Items",
                "Foo",
                "Bar"
            };

            oc.UpdateToMatch(toAdd, s => s);

            oc.Should().ContainInOrder(toAdd);
            oc.Should().OnlyContain(s => toAdd.Contains(s));
            oc.Count.Should().Be(4);
        }
        public void UpdateToMatchRaisesCollectionChangeOnlyOnceWithResetIfItemsAreRemoved()
        {
            var oc = new ObservableCollectionEx<string>
            {
                "Existing",
                "Items"
            };

            var toRemove = new List<string>
            {
                "Items"
            };

            var itemCount = 0;
            var eventCount = 0;
            var action = NotifyCollectionChangedAction.Add;

            oc.CollectionChanged += (sender, args) =>
            {
                ++eventCount;
                itemCount = oc.Count;
                action = args.Action;
            };

            oc.UpdateToMatch(toRemove, s => s);

            itemCount.Should().Be(1);
            eventCount.Should().Be(1);
            action.Should().Be(NotifyCollectionChangedAction.Reset);
        }
        public void UpdateToMatchRaisesCollectionChangeOnlyOnceWithResetIfItemsAreUpdated()
        {
            var oc = new ObservableCollectionEx<CollectionItem>
            {
                new CollectionItem("First", 1),
                new CollectionItem("Second", 2),
                new CollectionItem("Third", 3)
            };

            var toChange = new List<CollectionItem>
            {
                new CollectionItem("First", 1),
                new CollectionItem("Second", 200),
                new CollectionItem("Third", 3)
            };

            Func<CollectionItem, CollectionItem, bool> updateAction = (i1, i2) =>
            {
                if (i1.Value != i2.Value)
                {
                    i1.Value = i2.Value;
                    return true;
                }

                return false;
            };

            var itemCount = 0;
            var eventCount = 0;
            var action = NotifyCollectionChangedAction.Add;

            oc.CollectionChanged += (sender, args) =>
            {
                ++eventCount;
                itemCount = oc.Count;
                action = args.Action;
            };

            oc.UpdateToMatch(toChange, i => i.Key, updateAction).Should().BeTrue();

            itemCount.Should().Be(3);
            eventCount.Should().Be(1);
            action.Should().Be(NotifyCollectionChangedAction.Reset);
        }
        public void UpdateToMatchUpdatesChangedItems()
        {
            var oc = new ObservableCollectionEx<CollectionItem>
            {
                new CollectionItem("First", 1),
                new CollectionItem("Second", 2),
                new CollectionItem("Third", 3)
            };

            var toChange = new List<CollectionItem>
            {
                new CollectionItem("First", 100),
                new CollectionItem("Second", 200),
                new CollectionItem("Third", 3)
            };

            Func<CollectionItem, CollectionItem, bool> updateAction = (i1, i2) =>
            {
                if (i1.Value != i2.Value)
                {
                    i1.Value = i2.Value;
                    return true;
                }

                return false;
            };

            oc.UpdateToMatch(toChange, i => i.Key, updateAction);

            oc.Single(i => i.Key == "First").Value.Should().Be(100);
            oc.Single(i => i.Key == "Second").Value.Should().Be(200);
            oc.Single(i => i.Key == "Third").Value.Should().Be(3);
        }
        public void UpdateToMatchReturnsTrueIfItemsAreRemoved()
        {
            var oc = new ObservableCollectionEx<string>
            {
                "Existing",
                "Items"
            };

            var toRemove = new List<string>
            {
                "Items"
            };

            oc.UpdateToMatch(toRemove, s => s).Should().BeTrue();
        }
Exemple #21
0
        public async Task RefreshContainerAsync()
        {
            Guid token;

            lock (_tokenSyncObj)
            {
                token         = Guid.NewGuid();
                _refreshToken = token;
            }

            try
            {
                var container = await _restConnection.MakeRequestAsync <MediaContainer, string>(Method.Get,
                                                                                                ResponseType.Xml, PlexResources.MyPlexBaseUrl, PlexResources.MyPlexDevices,
                                                                                                headers : PlexHeaders.CreatePlexRequest(User));

                if (container != null && container.ResponseObject != null)
                {
                    bool updated;
                    lock (_deviceSyncObj)
                    {
                        if (token != _refreshToken)
                        {
                            return;
                        }

                        updated = _devices.UpdateToMatch(container.ResponseObject.Devices, d => d.ClientIdentifier, UpdateDevice);
                        _servers.UpdateToMatch(GetByProvides(container.ResponseObject, "server"), d => d.ClientIdentifier);
                        _players.UpdateToMatch(GetByProvides(container.ResponseObject, "player"), d => d.ClientIdentifier);
                    }

                    if (updated)
                    {
                        OnDevicesUpdated();
                    }
                }
            }
            catch
            {
                var updated = false;

                lock (_deviceSyncObj)
                {
                    if (token != _refreshToken)
                    {
                        return;
                    }

                    // lost connection, so clear everything
                    if (_devices.Any())
                    {
                        _devices.Clear();
                        _servers.Clear();
                        _players.Clear();

                        updated = true;
                    }
                }

                if (updated)
                {
                    OnDevicesUpdated();
                }
            }
        }
        public void UpdateToMatchRemovesMissingItems()
        {
            var oc = new ObservableCollectionEx<string>
            {
                "Existing",
                "Items"
            };

            var toRemove = new List<string>
            {
                "Items"
            };

            oc.UpdateToMatch(toRemove, s => s);

            oc.Should().ContainInOrder(toRemove);
            oc.Should().OnlyContain(s => toRemove.Contains(s));
            oc.Count.Should().Be(1);
        }
        public void UpdateToMatchWithANullKeyFuncThrows()
        {
            var oc = new ObservableCollectionEx<string>();
            var toAdd = new List<string>
            {
                "Existing",
                "Items",
                "Foo",
                "Bar"
            };

            oc.UpdateToMatch<string>(toAdd, null);
        }
 public void UpdateToMatchWithANullCollectionThrows()
 {
     var oc = new ObservableCollectionEx<string>();
     oc.UpdateToMatch(null, s => s);
 }
        public async Task RefreshContainerAsync()
        {
            Guid token;

            lock (_tokenSyncObj)
            {
                token         = Guid.NewGuid();
                _refreshToken = token;
            }

            try
            {
                var container = await _connectionHelper.MakeRequestAsync <MediaContainer>(Method.Get,
                                                                                          PlexResources.MyPlexBaseUrl, PlexResources.MyPlexDevices, user : User);

                bool updated;

                lock (_deviceSyncObj)
                {
                    if (token != _refreshToken)
                    {
                        return;
                    }

                    updated = _devices.UpdateToMatch(container.Devices, d => d.ClientIdentifier, UpdateDevice);
                    _servers.UpdateToMatch(GetByProvides(container, "server"), d => d.ClientIdentifier);
                    _players.UpdateToMatch(GetByProvides(container, "player"), d => d.ClientIdentifier);
                }

                if (updated)
                {
                    OnDevicesUpdated();
                }
            }
            catch
            {
                var updated = false;

                lock (_deviceSyncObj)
                {
                    if (token != _refreshToken)
                    {
                        return;
                    }

                    // lost connection, so clear everything
                    if (_devices.Any())
                    {
                        _devices.Clear();
                        _servers.Clear();
                        _players.Clear();

                        updated = true;
                    }
                }

                if (updated)
                {
                    OnDevicesUpdated();
                }
            }
        }
        public void UpdateToMatchReturnsTrueIfItemsAreAdded()
        {
            var oc = new ObservableCollectionEx<string>
            {
                "Existing",
                "Items"
            };

            var toAdd = new List<string>
            {
                "Existing",
                "Items",
                "Foo",
                "Bar"
            };

            oc.UpdateToMatch(toAdd, s => s).Should().BeTrue();
        }