public async Task <SynchronizationResult> UpdateRemoteFromLocal(string plainAccountsBeforeChange, IEnumerable <Account> currentAccounts)
        {
            SynchronizationResult result = new SynchronizationResult()
            {
                Successful = false
            };

            try
            {
                await AuthenticateAsync();
                await GetFileFromOneDrive();

                if (_isInitialSetup)
                {
                    // File was removed
                    throw new RemovedSynchronizationException();
                }

                bool stale = false;

                stale = !Comparer.AreEqual(plainAccountsBeforeChange, decrypted);

                if (stale)
                {
                    Account[] accounts = null;

                    try
                    {
                        accounts = JsonConvert.DeserializeObject <Account[]>(decrypted);
                    }
                    catch (Exception)
                    {
                        accounts = null;
                    }

                    throw new StaleException(accounts);
                }

                if (encrypter != null && encrypter.IsInitialized)
                {
                    string plainAccounts = JsonConvert.SerializeObject(currentAccounts);
                    string encrypted     = encrypter.Encrypt(plainAccounts);

                    Stream stream = GenerateStreamFromString(encrypted);

                    var item = await client.Drive.Special.AppRoot
                               .ItemWithPath(FILENAME)
                               .Content.Request()
                               .PutAsync <Item>(stream);

                    result.Successful = true;
                }
            }
            catch (OneDriveException)
            {
                throw new NetworkException();
            }

            return(result);
        }
        public async Task <SynchronizationResult> Synchronize(IEnumerable <Account> localAccounts)
        {
            SynchronizationResult result = new SynchronizationResult()
            {
                HasChanges = false,
                Successful = false
            };

            if (encrypter != null && encrypter.IsInitialized)
            {
                await GetFileFromOneDrive();

                List <Account> mergedAccounts = new List <Account>();
                Account[]      remoteAccounts = new Account[0];

                if (!string.IsNullOrWhiteSpace(decrypted))
                {
                    remoteAccounts = JsonConvert.DeserializeObject <Account[]>(decrypted);
                }

                mergedAccounts.AddRange(localAccounts);

                foreach (Account account in remoteAccounts)
                {
                    if (!mergedAccounts.Contains(account))
                    {
                        mergedAccounts.Add(account);
                    }
                }

                string plainContents = JsonConvert.SerializeObject(mergedAccounts);
                string encrypted     = encrypter.Encrypt(plainContents);

                Stream stream = GenerateStreamFromString(encrypted);

                var item = await client.Drive.Special.AppRoot
                           .ItemWithPath(FILENAME)
                           .Content.Request()
                           .PutAsync <Item>(stream);

                result.Accounts   = mergedAccounts.ToArray();
                result.Successful = true;
            }

            return(result);
        }
        public async Task <SynchronizationResult> UpdateLocalFromRemote(string plainAccounts)
        {
            SynchronizationResult result = new SynchronizationResult();

            await GetFileFromOneDrive();

            if (_isInitialSetup)
            {
                // File was removed
                throw new RemovedSynchronizationException();
            }

            if (!string.IsNullOrWhiteSpace(decrypted))
            {
                result.Accounts   = JsonConvert.DeserializeObject <Account[]>(decrypted);
                result.HasChanges = !Comparer.AreEqual(plainAccounts, decrypted);
                result.Successful = true;
            }

            return(result);
        }