protected override void OnAppearing() { base.OnAppearing(); cts = new CancellationTokenSource(); foreach (string profileId in bookmarkController.ProfileIds) { BookmarkViewModel viewModel = null; if (!_bookmarks.TryGetValue(profileId, out viewModel)) { viewModel = new BookmarkViewModel(profileId); viewModel.Label = profileId; _bookmarks.Add(profileId, viewModel); Bookmarks.Add(viewModel); } profileApi.Get(profileId).Subscribe(model => { viewModel.Label = model.Name; void setImage(ImageModel image) { viewModel.Image = ImageSource.FromStream(() => new MemoryStream(image.Bytes)); }; imageApi.Get(model.ImageId).Subscribe(setImage, cts.Token); }, cts.Token); } }
public async Task GetFileNull() { var preprocessor = Substitute.For<IPreprocessor>(); var store = Substitute.For<IDataStore>(); var api = new ImageApi(preprocessor, store); var response = await api.Get(null); Assert.IsNotNull(response); Assert.AreEqual(HttpStatusCode.PreconditionFailed, response.StatusCode); }
public async Task GetFileNull() { var preprocessor = Substitute.For <IPreprocessor>(); var store = Substitute.For <IDataStore>(); var api = new ImageApi(preprocessor, store); var response = await api.Get(null); Assert.IsNotNull(response); Assert.AreEqual(HttpStatusCode.PreconditionFailed, response.StatusCode); }
public async Task Resize() { var bytes = File.ReadAllBytes(Environment.CurrentDirectory + @"\icon.png"); var file = Guid.NewGuid().ToString() + "_.png"; await this.container.Save(file, bytes, "image/png"); var api = new ImageApi(connectionString); var data = await api.Get(file, 10); Assert.IsNotNull(data); var resized = await data.Content.ReadAsByteArrayAsync(); Assert.IsTrue(bytes.LongLength > resized.LongLength); Assert.AreEqual("image/jpeg", data.Content.Headers.ContentType.MediaType); }
public async Task Get() { var random = new Random(); var bytes = new byte[128]; random.NextBytes(bytes); var file = Guid.NewGuid().ToString(); await this.container.Save(file, bytes, "image/jpeg"); var api = new ImageApi(connectionString); var data = await api.Get(file); Assert.IsNotNull(data); Assert.AreEqual(bytes, await data.Content.ReadAsByteArrayAsync()); Assert.AreEqual("image/jpeg", data.Content.Headers.ContentType.MediaType); }
public async Task Resize() { var dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase); var f = dir.Substring(6, dir.Length - 6) + @"\icon.png"; var bytes = File.ReadAllBytes(f); var file = Guid.NewGuid().ToString() + "_.png"; await this.container.Save(file, bytes, "image/png"); var api = new ImageApi(connectionString); var data = await api.Get(file, 10); Assert.IsNotNull(data); var resized = await data.Content.ReadAsByteArrayAsync(); Assert.IsTrue(bytes.LongLength > resized.LongLength); Assert.AreEqual("image/jpeg", data.Content.Headers.ContentType.MediaType); }
void SetModel(ProfileModel profile) { model = profile; viewModel.DioceseList = structureController.Structure?.Dioceses?.Values.ToList(); viewModel.FunctionList = structureController.Structure?.Functions?.Values.ToList(); if (profile == null) { return; } viewModel.Firstname = profile.Firstname; viewModel.Lastname = profile.Lastname; viewModel.Residence = profile.Residence; viewModel.Phone = profile.Phone; viewModel.Mail = profile.Mail; viewModel.WoodbadgeCount = profile.WoodbadgeCount; viewModel.FavouriteStage = profile.FavouriteStage; viewModel.RelationshipStatus = profile.RelationshipStatus; viewModel.Association = profile.Association; viewModel.IsPriest = profile.IsPriest; if (profile.GeorgesPoints < 0) { viewModel.GeorgesPoints = ""; } else { viewModel.GeorgesPoints = profile.GeorgesPoints.ToString(); } if (!String.IsNullOrEmpty(profile.DioceseId)) { Diocese diocese = structureController.Structure.Dioceses[profile.DioceseId]; viewModel.SelectedDiocese = diocese; if (diocese.HasRegions) { if (!String.IsNullOrEmpty(profile.RegionId)) { Region region = diocese?.Regions[profile.RegionId]; viewModel.SelectedRegion = region; if (!String.IsNullOrEmpty(profile.TribeId)) { Tribe tribe = region?.Tribes[profile.TribeId]; viewModel.SelectedTribe = tribe; } } } else { viewModel.SelectedRegion = null; if (!String.IsNullOrEmpty(profile.TribeId)) { Tribe tribe = diocese?.Tribes[profile.TribeId]; viewModel.SelectedTribe = tribe; } } } if (!String.IsNullOrEmpty(profile.FunctionId)) { viewModel.SelectedFunction = structureController.Structure.Functions[profile.FunctionId]; } if (String.IsNullOrEmpty(profile.ImageId)) { viewModel.Image = ImageSource.FromFile("avatar.jpg"); } else { imageApi.Get(profile.ImageId).Subscribe(image => { viewModel.Image = ImageSource.FromStream(() => new MemoryStream(image.Bytes)); }, cts.Token); } }
private void SetModel(ProfileModel model) { profile = model; if (profile == null) { return; } if (ctsSecondary != null) { ctsSecondary.Cancel(); } ctsSecondary = new CancellationTokenSource(); viewModel.IsDataLoaded = true; viewModel.IsMyProfile = (model.Id == authController.AuthToken.UserId); viewModel.IsBookmarked = bookmarkController.IsBookmarked(model.Id); viewModel.Name = model.Name; viewModel.Firstname = model.Firstname; viewModel.Lastname = model.Lastname; viewModel.Residence = model.Residence; viewModel.Phone = model.Phone; viewModel.Mail = model.Mail; viewModel.WoodbadgeCount = model.WoodbadgeCount; viewModel.FavouriteStage = model.FavouriteStage; viewModel.RelationshipStatus = model.RelationshipStatus; viewModel.ShowContact = !(string.IsNullOrEmpty(viewModel.Phone) && string.IsNullOrEmpty(viewModel.Mail)); if (model.GeorgesPoints < 0) { viewModel.GeorgesPoints = ""; } else { viewModel.GeorgesPoints = model.GeorgesPoints.ToString(); } if (!String.IsNullOrEmpty(model.DioceseId)) { Diocese diocese = structureController.Structure.Dioceses[model.DioceseId]; viewModel.Diocese = diocese; if (diocese.HasRegions) { if (!String.IsNullOrEmpty(model.RegionId)) { Region region = diocese?.Regions[model.RegionId]; viewModel.Region = region; if (!String.IsNullOrEmpty(model.TribeId)) { Tribe tribe = region?.Tribes[model.TribeId]; viewModel.Tribe = tribe; } } } else { viewModel.Region = null; if (!String.IsNullOrEmpty(model.TribeId)) { Tribe tribe = diocese?.Tribes[model.TribeId]; viewModel.Tribe = tribe; } } } if (!String.IsNullOrEmpty(model.FunctionId)) { viewModel.Function = structureController.Structure.Functions[model.FunctionId]; } if (!String.IsNullOrEmpty(model.ImageId)) { imageApi.Get(model.ImageId).Subscribe(image => { viewModel.Image = ImageSource.FromStream(() => new MemoryStream(image.Bytes)); }, ctsSecondary.Token); } adventureApi.Get(profile.Id).Subscribe(adventures => { if (ctsAdventureProfiles != null) { ctsAdventureProfiles.Cancel(); } ctsAdventureProfiles = new CancellationTokenSource(); viewModel.ShowAdventures = true; viewModel.Adventures.Clear(); float adventurePoints = 0; foreach (AdventureModel adventure in adventures) { if (adventure.WithUserId == authController.AuthToken.UserId && adventure.WithConfirmed) { viewModel.ShowAdventureButton = false; } if (adventure.Confirmed && adventure.WithConfirmed) { // adventure confirmed from both users => show on profile page AdventureViewModel adventureViewModel = new AdventureViewModel(); adventureViewModel.Label = adventure.WithUserId; adventureViewModel.Points = adventure.Points.ToString(); viewModel.Adventures.Add(adventureViewModel); adventurePoints += adventure.Points; profileApi.Get(adventure.WithUserId).Subscribe(withProfile => { adventureViewModel.Label = withProfile.Name; if (!string.IsNullOrEmpty(withProfile.ImageId)) { imageApi.Get(withProfile.ImageId).Subscribe(image => { adventureViewModel.Image = ImageSource.FromStream(() => new MemoryStream(image.Bytes)); }, ctsAdventureProfiles.Token); } }, ctsAdventureProfiles.Token); } } int adventureCount = viewModel.Adventures.Count; FormattedString pointsText = new FormattedString(); pointsText.Spans.Add(new Span { Text = profile.Firstname, FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)) }); pointsText.Spans.Add(new Span { Text = " hat insgesamt ", FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)) }); pointsText.Spans.Add(new Span { Text = adventurePoints.ToString(), FontSize = 60, FontAttributes = FontAttributes.Bold, TextColor = Color.SteelBlue }); pointsText.Spans.Add(new Span { Text = " Punkte bei " + adventureCount.ToString() + " Abenteuer" + (adventureCount > 1 ? "n" : "") + " gesammelt", FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)) }); viewModel.AdventurePoints = pointsText; if (viewModel.Adventures.Count > 0) { viewModel.ShowAdventures = true; // resize the listview to match content adventuresList.HeightRequest = adventuresList.RowHeight * Math.Min(viewModel.Adventures.Count, MAX_ADVENTURES); } }, ctsSecondary.Token); profileApi.Get(authController.AuthToken.UserId).Subscribe(async myProfile => { this.myProfile = myProfile; float possiblePoints = await CalculatePointsAsync(myProfile, profile); FormattedString hint = new FormattedString(); hint.Spans.Add(new Span { Text = possiblePoints.ToString(), FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)), FontAttributes = FontAttributes.Bold }); hint.Spans.Add(new Span { Text = " Punkte bekommst du für ein Abenteuer mit " }); hint.Spans.Add(new Span { Text = model.Firstname }); viewModel.PossiblePoints = hint; }, ctsSecondary.Token); }