internal void RollbackState()
        {
            if (previousState != null)
            {
                TotalItemCount = previousState.TotalItemCount;
                IsLoaded       = previousState.IsLoaded;
                var fetchedKeys = FetchedKeys;

                InitializeFetchedKeys();
                InitializeDifferenceCollections();

                foreach (var fetchedKey in fetchedKeys)
                {
                    FetchedKeys.Add(fetchedKey);
                }

                foreach (var addedKey in previousState.AddedKeys)
                {
                    if (fetchedKeys.ContainsKey(addedKey))
                    {
                        FetchedKeys.Remove(addedKey);
                    }
                    addedKeys.Add(addedKey, addedKey);
                }
                foreach (var removedKey in previousState.RemovedKeys)
                {
                    if (!FetchedKeys.ContainsKey(removedKey))
                    {
                        FetchedKeys.Add(removedKey);
                    }
                    removedKeys.Add(removedKey, removedKey);
                }
            }
        }
        /// <summary>
        /// Applies all changes to state.
        /// </summary>
        public bool ApplyChanges()
        {
            if (HasChanges)
            {
                EnsureFetchedKeysIsNotNull();
                BackupState();
                var currentFetchedKeys = FetchedKeys;
                InitializeFetchedKeys();

                foreach (var currentFetchedKey in currentFetchedKeys)
                {
                    if (!removedKeys.ContainsKey(currentFetchedKey))
                    {
                        FetchedKeys.Add(currentFetchedKey);
                    }
                }
                foreach (var addedKey in addedKeys)
                {
                    FetchedKeys.Add(addedKey.Value);
                }
                InitializeDifferenceCollections();
                Rebind();
                return(true);
            }
            return(false);
        }
 /// <summary>
 /// Sets cached keys to <paramref name="keys"/>.
 /// </summary>
 /// <param name="keys">The keys.</param>
 public void Update(IEnumerable <Key> keys, long?count)
 {
     FetchedKeys.Clear();
     TotalItemCount = count;
     foreach (var key in keys)
     {
         FetchedKeys.Add(key);
     }
     Rebind();
 }
        private void UpdateStateDisconnected(IEnumerable <Key> syncronizedKeys, long?count)
        {
            FetchedKeys.Clear();
            var countExceptRemoved = 0;

            foreach (var key in syncronizedKeys)
            {
                FetchedKeys.Add(key);
                if (!removedKeys.ContainsKey(key))
                {
                    countExceptRemoved++;
                }
            }
            TotalItemCount = count.HasValue
        ? countExceptRemoved + AddedItemsCount
        : count;
            Rebind();
        }
 /// <summary>
 /// Registers the specified fetched key in cached state.
 /// </summary>
 /// <param name="key">The key to register.</param>
 public void Register(Key key)
 {
     FetchedKeys.Add(key);
 }