Beispiel #1
0
        public async Task ImportSnapshotsAsync(Location locationToImport,
                                               Location importedLocation,
                                               CancellationToken cancellationToken)
        {
            var snapshotsToImport = await _snapshotService.GetSnapshotsByLocationIdAsync(locationToImport.Id);

            var remoteSnapshots = await _snapshotServiceProxy.GetSnapshotsByLocationIdAsync(importedLocation.Id);

            foreach (var snapshotToImport in snapshotsToImport)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }

                if (remoteSnapshots.Any(_ => _.PictureFileName == snapshotToImport.PictureFileName))
                {
                    continue;
                }

                var pictureToImport = await _pictureService.GetSnapshotContentAsync(snapshotToImport);

                if (pictureToImport.Length == 0)
                {
                    continue;
                }
                snapshotToImport.LocationId = importedLocation.Id;
                await _snapshotServiceProxy.AddSnapshotAsync(snapshotToImport);

                await _pictureServiceProxy.SaveSnapshotContentAsync(snapshotToImport, pictureToImport);
            }
        }
        public async Task OnLoaded()
        {
            IsBusy = true;
            var navParam = (SnapshotsViewNavParams)NavigationParam;

            _groupByCriteria = navParam.GroupByCriteria;
            var payload = navParam.SnapshotsIdsource;

            Parent = payload;
            IEnumerable <LocationSnapshot> snapshots = null;

            RaisePropertyChanged(nameof(CanAddSnapshot));

            if (payload is Location location)
            {
                snapshots = await _locationSnapshotDataService.GetSnapshotsByLocationIdAsync(location.Id);
            }
            else if (payload is SnapshotGroup group)
            {
                snapshots = await _locationSnapshotDataService.GetSnapshotsByIdsAsync(group.SnapshotIds);
            }

            var miniatures = await _pictureService.GetSnapshotMiniaturesAsync(snapshots);

            foreach (var miniature in miniatures)
            {
                var thumbnail = await _bitmapConverter.GetBitmapAsync(miniature.Data);

                SnapshotThumbnails.Add(new SnapshotThumbnail {
                    Snapshot = miniature.Snapshot, Thumbnail = thumbnail
                });
            }
            IsBusy = false;
        }
        public async void RemoveSelectedLocations_ShouldSucceed()
        {
            // Arrange
            SetUp();
            LocationsViewModel sit = null;
            var snapshotsToDelete  = new List <LocationSnapshot>
            {
                new LocationSnapshot {
                    Id = 1
                },
                new LocationSnapshot {
                    Id = 2
                }
            };

            _snapshotDataService.GetSnapshotsByLocationIdAsync(Arg.Any <int>())
            .Returns(_ =>
            {
                var tcs = new TaskCompletionSource <IEnumerable <LocationSnapshot> >();
                tcs.SetResult(snapshotsToDelete);
                return(tcs.Task);
            });
            _snapshotDataService.RemoveSnapshotAsync(Arg.Any <int>())
            .Returns(_ =>
            {
                var tcs = new TaskCompletionSource <LocationSnapshot>();
                tcs.SetResult(new LocationSnapshot());
                return(tcs.Task);
            });
            _pictureService.RemoveSnapshotContentAsync(Arg.Any <LocationSnapshot>())
            .Returns(_ => Task.CompletedTask);
            _locationDataService.RemoveLocationAsync(Arg.Any <int>())
            .Returns(_ =>
            {
                var tcs = new TaskCompletionSource <Location>();
                tcs.SetResult(sit.Locations[1]);
                return(tcs.Task);
            });

            // Act
            sit = CreateViewModel();
            await sit.OnLoaded();

            sit.SelectedLocations = new List <Location> {
                sit.Locations[1]
            };
            await sit.RemoveSelectedLocations();

            // Assert
            Assert.Equal(2, sit.Locations.Count);
            Assert.DoesNotContain(sit.Locations, _ => _.Name == "Prague");
            await _snapshotDataService.Received().RemoveSnapshotAsync(snapshotsToDelete[0].Id);

            await _snapshotDataService.Received().RemoveSnapshotAsync(snapshotsToDelete[1].Id);

            await _pictureService.Received().RemoveSnapshotContentAsync(snapshotsToDelete[0]);

            await _pictureService.Received().RemoveSnapshotContentAsync(snapshotsToDelete[1]);
        }
Beispiel #4
0
        public async Task <IActionResult> GetSnapshotsByLocationId(int locationId)
        {
            var snapshots = await _locationSnapshotDataService.GetSnapshotsByLocationIdAsync(locationId);

            var result = Mapper.Map <IEnumerable <LocationSnapshot>, IEnumerable <LocationSnapshotDto> >(snapshots);

            return(Ok(result));
        }
        public async void ImportAsync_ShouldImportNewLocation()
        {
            // Arrange
            SetUp();
            _locationServiceProxy.GetAllLocationsAsync()
            .Returns(_ =>
            {
                var tcs = new TaskCompletionSource <IEnumerable <Location> >();
                tcs.SetResult(new List <Location>());
                return(tcs.Task);
            });
            _locationServiceProxy.AddLocationAsync(Arg.Any <Location>())
            .Returns(_ =>
            {
                var tcs = new TaskCompletionSource <Location>();
                tcs.SetResult(_locationToImport);
                return(tcs.Task);
            });
            _snapshotServiceProxy.GetSnapshotsByLocationIdAsync(Arg.Any <int>())
            .Returns(_ =>
            {
                var tcs = new TaskCompletionSource <IEnumerable <LocationSnapshot> >();
                tcs.SetResult(new List <LocationSnapshot>());
                return(tcs.Task);
            });

            // Act
            var sit = new LocationDataImporter(_dataServiceFactory);
            await sit.ImportAsync(new List <Location> {
                _locationToImport
            }, new CancellationTokenSource().Token);

            // Assert
            await _locationServiceProxy.Received().AddLocationAsync(_locationToImport);

            await _snapshotServiceProxy.Received().AddSnapshotAsync(_snapshotsToImport[0]);

            await _snapshotServiceProxy.Received().AddSnapshotAsync(_snapshotsToImport[1]);

            await _pictureServiceProxy.Received().SaveSnapshotContentAsync(_snapshotsToImport[0], _pictureData);

            await _pictureServiceProxy.Received().SaveSnapshotContentAsync(_snapshotsToImport[1], _pictureData);
        }
        private void SetUp()
        {
            _dataServiceFactory   = Substitute.For <IDataServiceFactory>();
            _locationServiceProxy = Substitute.For <ILocationDataService>();
            _snapshotServiceProxy = Substitute.For <ILocationSnapshotDataService>();
            _snapshotService      = Substitute.For <ILocationSnapshotDataService>();
            _pictureServiceProxy  = Substitute.For <IPictureService>();
            _pictureService       = Substitute.For <IPictureService>();

            _dataServiceFactory.CreateLocationDataService(DataSourceType.Remote).Returns(_locationServiceProxy);
            _dataServiceFactory.CreateLocationSnapshotDataService(DataSourceType.Remote).Returns(_snapshotServiceProxy);
            _dataServiceFactory.CreateLocationSnapshotDataService(DataSourceType.Local).Returns(_snapshotService);
            _dataServiceFactory.CreatePictureService(DataSourceType.Remote).Returns(_pictureServiceProxy);
            _dataServiceFactory.CreatePictureService(DataSourceType.Local).Returns(_pictureService);

            _locationToImport = new Location
            {
                Id   = 1,
                Name = "Barcelona"
            };
            _snapshotsToImport = new List <LocationSnapshot>
            {
                new LocationSnapshot
                {
                    Id              = 1,
                    LocationId      = 1,
                    PictureFileName = "Barcelona1.jpg"
                },
                new LocationSnapshot
                {
                    Id              = 2,
                    LocationId      = 1,
                    PictureFileName = "Barcelona2.jpg"
                },
            };

            _snapshotService.GetSnapshotsByLocationIdAsync(Arg.Any <int>())
            .Returns(_ =>
            {
                var tcs = new TaskCompletionSource <IEnumerable <LocationSnapshot> >();
                tcs.SetResult(_snapshotsToImport);
                return(tcs.Task);
            });

            _pictureData = new byte[] { 1, 2, 3, 4 };
            _pictureService.GetSnapshotContentAsync(Arg.Any <LocationSnapshot>())
            .Returns(_ =>
            {
                var tcs = new TaskCompletionSource <byte[]>();
                tcs.SetResult(_pictureData);
                return(tcs.Task);
            });
        }
Beispiel #7
0
        public async Task RemoveSelectedLocations()
        {
            if (SelectedLocations.Count == 0)
            {
                await _dialogService.ShowAsync("Please select at least one location.");

                return;
            }

            var response = await _dialogService.ShowConfirmationAsync("Do you really want to remove the selected locations?");

            if (response == ConfirmationAnswer.Cancel)
            {
                return;
            }

            IsBusy = true;
            var selectedLocations = SelectedLocations.ToList();

            foreach (var selLoc in selectedLocations)
            {
                var snapshots = await _locationSnapshotDataService.GetSnapshotsByLocationIdAsync(selLoc.Id);

                var deletionTasks = snapshots.Select(async _ =>
                {
                    await _locationSnapshotDataService.RemoveSnapshotAsync(_.Id);
                    await _pictureService.RemoveSnapshotContentAsync(_);
                });
                await Task.WhenAll(deletionTasks);

                var removedLocation = await _locationDataService.RemoveLocationAsync(selLoc.Id);

                Locations.Remove(Locations.First(_ => _.Id == removedLocation.Id));
            }
            ;
            SetDefaultView();
            IsBusy = false;
        }
 private void SetUp()
 {
     _snapshots = new List <LocationSnapshot>
     {
         new LocationSnapshot {
             Id = 1, PictureFileName = "Barcelona_1.jpg"
         },
         new LocationSnapshot {
             Id = 2, PictureFileName = "Barcelona_2.jpg"
         },
         new LocationSnapshot {
             Id = 3, PictureFileName = "Barcelona_3.jpg"
         }
     };
     _miniatures = new List <SnapshotMiniature>
     {
         new SnapshotMiniature {
             Snapshot = _snapshots[0]
         },
         new SnapshotMiniature {
             Snapshot = _snapshots[1]
         },
         new SnapshotMiniature {
             Snapshot = _snapshots[2]
         }
     };
     _thumbnail           = new object();
     _snapshotDataService = Substitute.For <ILocationSnapshotDataService>();
     _snapshotDataService.GetSnapshotsByLocationIdAsync(Arg.Any <int>())
     .Returns(_ =>
     {
         var tcs = new TaskCompletionSource <IEnumerable <LocationSnapshot> >();
         tcs.SetResult(_snapshots);
         return(tcs.Task);
     });
     _snapshotDataService.GetSnapshotsByIdsAsync(Arg.Any <IEnumerable <int> >())
     .Returns(_ =>
     {
         var tcs = new TaskCompletionSource <IEnumerable <LocationSnapshot> >();
         tcs.SetResult(_snapshots);
         return(tcs.Task);
     });
     _pictureService = Substitute.For <IPictureService>();
     _pictureService.GetSnapshotMiniaturesAsync(Arg.Any <IEnumerable <LocationSnapshot> >())
     .Returns(_ =>
     {
         var tcs = new TaskCompletionSource <IEnumerable <SnapshotMiniature> >();
         tcs.SetResult(_miniatures);
         return(tcs.Task);
     });
     _bitmapConverter = Substitute.For <IBitmapConverter>();
     _bitmapConverter.GetBitmapAsync(Arg.Any <byte[]>())
     .Returns(_ =>
     {
         var tcs = new TaskCompletionSource <object>();
         tcs.SetResult(_thumbnail);
         return(tcs.Task);
     });
     _dialogService = Substitute.For <IDialogService>();
     _dialogService.ShowConfirmationAsync(Arg.Any <string>())
     .Returns(_ =>
     {
         var tcs = new TaskCompletionSource <ConfirmationAnswer>();
         tcs.SetResult(ConfirmationAnswer.OK);
         return(tcs.Task);
     });
     _navigationService       = Substitute.For <INavigationService>();
     _platformSpecificActions = Substitute.For <IPlatformSpecificActions>();
 }