Exemple #1
0
        public async Task Sync()
        {
            await InitStorageState();

            Console.WriteLine("SyncTo");
            if (_storageState.Synced == true)
            {
                return;
            }

            VersionedData cachedRemoteData = await GetCachedRemoteData();

            VersionedData remoteData = await _remoteStorage.GetRemoteData() ??
                                       await SyncToRemoteStorage(cachedRemoteData.Version);

            if (remoteData.Version > cachedRemoteData.Version)
            {
                Console.WriteLine($"remote:{remoteData.Version}->local:{cachedRemoteData.Version}");
                await UpdateStorageFromRemoteData(remoteData);
            }
            else if (remoteData.Version == cachedRemoteData.Version)
            {
                await SyncToRemoteStorage(remoteData.Version + 1);
            }
            else
            {
                throw new InvalidOperationException(
                          $"Versions mismatch: local:{cachedRemoteData.Version} remote:{remoteData.Version}");
            }
        }
Exemple #2
0
        private async Task UpdateStorageFromRemoteData(VersionedData data)
        {
            Console.WriteLine($"Synced from remote:{data.Data}");
            await _storage.SetItemAsync("remote_data", data);

            await _storage.SetItemAsync("notions", data.Data);

            _storageState.Synced = true;
        }
        public async Task Upload(VersionedData data)
        {
            var storageToUpdate = new File {
                Name = (await _remoteStorageInfoRepository.GetRemoteStorageInfo()).Name
            };

            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data))))
            {
                FilesResource.UpdateMediaUpload updateRequest =
                    _service.Files.Update(storageToUpdate,
                                          (await _remoteStorageInfoRepository.GetRemoteStorageInfo()).Id, stream, DataFileContentType);
                await updateRequest.UploadAsync();
            }
        }
Exemple #4
0
        private async Task <VersionedData> SyncToRemoteStorage(int newVersion)
        {
            VersionedData newRemoteData = await GetVersionedDataFromLocalStorage(newVersion);

            await _storage.SetItemAsync("remote_data", newRemoteData);

            await _remoteStorage.Upload(newRemoteData);

            VersionedData remoteData = await _remoteStorage.GetRemoteData();

            await UpdateStorageFromRemoteData(remoteData);

            _storageState.Synced = true;

            return(remoteData);
        }