How do you Bind an Image to the HighlightedImage-Property of an ImageView? - xamarin.ios

I'm currently trying to bind two images to an iOS ImageView via MvvmCross.
One should be displayed when the ImageView is in 'default' state, the other one when the ImageView is highlighted.
By the following code I can bind the Image for the default state. But how do I bind the one for 'highlighted' state?
public CategoryCell(IntPtr handle): base(string.Empty, handle)
{
_imageViewLoader = new MvxImageViewLoader(() => this.imageView);
this.DelayBind(() =>
{
var set = this.CreateBindingSet<CategoryCell, MaterialCategory>();
set.Bind(titleLabel).To(materialCategory => materialCategory.Label);
set.Bind(_imageViewLoader).To(materialCategory => materialCategory.ImageActiveUri);
set.Bind(this).For(cell => cell.Selected).To(materialCategory => materialCategory.IsSelected);
set.Apply();
});
}

Another approach if you do not need image loading i.e. for lots of static UX.
You can set up as follows -
_imageView = new UIImageView(UIImage.FromFile("some/image/off.png"))
{
HighlightedImage = UIImage.FromFile("some/image/on.png")
};
And bind it e.g. an "Enabled" Property -
this.DelayBind(() =>
{
var set = this.CreateBindingSet<SomeView, SomeViewModel>();
set.Bind(_imageView).For(v => v.Highlighted).To(vm => vm.Enabled);
set.Apply();
});
And don't forget to add Highlighted to your LinkerPleaseInclude.cs.
Hope this helps

I think the best solution is to introduce an extra property ImageUri. In the setter of your IsSelected you set the ImageUri dependend on the selection state.
ViewModel:
public class MaterialCategory : MvxViewModel
{
//...
public string ImageActiveUri { ... } // call UpdateImageUri() here, too
public string ImageInactiveUri { ... } // call UpdateImageUri() here, too
public string ImageUri { ... }
public bool IsSelected
{
get { return _isSelected; }
set
{
_isSelected = value;
UpdateImageUri();
RaisePropertyChanged(() => IsSelected);
}
}
private void UpdateImageUri()
{
ImageUri = IsSelected ? ImageActiveUri : ImageInactiveUri;
}
}
Binding:
set.Bind(_imageViewLoader).To(materialCategory => materialCategory.ImageUri);
// instead of:
// set.Bind(_imageViewLoader).To(materialCategory => materialCategory.ImageActiveUri);

Related

Unit testing that the swagger doc is correct without starting a server

I'd like to test that the swagger document is correct for my application (mainly, because I've added a strategy to generate custom OperationIds and I want to ensure they are correctly unique)
However, the only solutions I found are all using a "real" server (cf https://stackoverflow.com/a/52521454/1545567), which is not an option for me since I do not have the database, message bus, etc... when I launch the unit tests in CI...
At the moment, I have the following but it always generate 0 paths and 0 models ...
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using SampleCheckIn;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Linq;
using Xunit;
using SampleCheckIn.Def;
using Service.Utils;
using Swashbuckle.AspNetCore.Swagger;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.FileProviders;
namespace D4Interop.Tests
{
public class TmpTest
{
[Fact]
public void Tmp()
{
var controllers = typeof(Startup).Assembly.GetTypes().Where(x => IsController(x)).ToList();
controllers.Any().Should().BeTrue();
var services = new ServiceCollection();
controllers.ForEach(c => services.AddScoped(c));
services.AddLogging(logging => logging.AddConsole());
services.AddControllers(); //here, I've also tried AddMvcCore and other ASP methods...
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("api", new OpenApiInfo { Title = Constants.SERVICE_NAME, Version = "_", Description = Constants.SERVICE_DESC });
//c.OperationFilter<SwaggerUniqueOperationId>(); //this is my filter that ensures the operationId is unique
c.CustomOperationIds(apiDesc =>
{
return apiDesc.TryGetMethodInfo(out var methodInfo) ? methodInfo.Name : null;
});
});
services.AddSingleton<IWebHostEnvironment>(new FakeWebHostEnvironment());
var serviceProvider = services.BuildServiceProvider();
var swaggerProvider = serviceProvider.GetRequiredService<ISwaggerProvider>();
var swagger = swaggerProvider.GetSwagger("api");
swagger.Should().NotBeNull();
swagger.Paths.Any().Should().BeTrue();
}
private bool IsController(Type x)
{
return typeof(Microsoft.AspNetCore.Mvc.ControllerBase).IsAssignableFrom(x);
}
}
internal class FakeWebHostEnvironment : IWebHostEnvironment
{
public FakeWebHostEnvironment()
{
}
public IFileProvider WebRootFileProvider { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public string WebRootPath { get => "/root"; set => throw new NotImplementedException(); }
public string EnvironmentName { get => "dev"; set => throw new NotImplementedException(); }
public string ApplicationName { get => "app"; set => throw new NotImplementedException(); }
public string ContentRootPath { get => "/"; set => throw new NotImplementedException(); }
public IFileProvider ContentRootFileProvider { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
}
}
Ok, I've finally found that I just need to mix the linked answer with my code :
[Fact]
public async Task TestSwagger()
{
var server = Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(options => { options.UseStartup<Startup>(); })
.Build();
var swagger = server.Services
.GetRequiredService<ISwaggerProvider>()
.GetSwagger("xxx"); //xxx should be the name of your API
swagger.Should().NotBeNull();
swagger.Paths.Any().Should().BeTrue();
swagger.Components.Schemas.Should().NotBeNull();
}

Shopware 6: Extend existing core DAL entities with a reference

I'm trying to create a connection between 2 existing entities PropertyGroup and CustomFieldSet. Use-case is irrelevant.
So I created an EntityExtension:
public function extendFields(FieldCollection $collection): void
{
$collection->add(
(new ManyToOneAssociationField('customFieldSet', 'custom_field_set', CustomFieldSetDefinition::class))
);
}
public function getDefinitionClass(): string
{
return PropertyGroupDefinition::class;
}
And override the administration component to also include this association when loading the entity:
Component.override('sw-property-detail', {
methods: {
loadEntityData() {
this.isLoading = true;
const criteria = this.defaultCriteria;
criteria.addAssociation('customFieldSet', new Criteria(1, 500));
this.propertyRepository.get(this.groupId, Shopware.Context.api, criteria)
.then((currentGroup) => {
this.propertyGroup = currentGroup;
this.isLoading = false;
}).catch(() => {
this.isLoading = false;
});
}
}
});
(I tried to override defaultCriteria but that didn't work because of this.$super being unable to access computed properties).
But it keeps saying FRAMEWORK__ASSOCIATION_NOT_FOUND. I debugged the EntityDefinition and it seems that this extension is not even loaded.
I checked if my EntityExtension is loaded in the Symfony container and it is, but it seems that it doesn't reach the entity definition.
The EntityExtension seems to be missing the addition of a FkField inside the function extendFields:
public function extendFields(FieldCollection $collection): void
{
$collection->add(
(new FkField('custom_field_set', 'customFieldSetId', CustomFieldSetDefinition::class)),
);
$collection->add(
(new ManyToOneAssociationField('customFieldSet', 'custom_field_set', CustomFieldSetDefinition::class))
);
}
A new use statement has to be added for the FkField:
use Shopware\Core\Framework\DataAbstractionLayer\Field\FkField;

sfChart reset data for graph

I have a graph in a 'detail' view. this is accessed when an item is chosen in a tableview. the first graph is correct, but when I pick another item in the table, it still shows the first graph. I enabled ListenPropertyChange = true and raispropertychanged it twice.
view(viewdidload):
SFChart chart = new SFChart();
chart.Frame = this.headerview.Frame;
//Adding Primary Axis for the Chart.
SFCategoryAxis primaryAxis = new SFCategoryAxis();
chart.PrimaryAxis = primaryAxis;
//Adding Secondary Axis for the Chart.
SFNumericalAxis secondaryAxis = new SFNumericalAxis();
chart.SecondaryAxis = secondaryAxis;
SFLineSeries series = new SFLineSeries()
{
XBindingPath = "timestamp",
YBindingPath = "price_btc",
ListenPropertyChange = true
};
series.EnableTooltip = true;
chart.Series.Add(series);
set.Bind(series).For(s => s.ItemsSource).To(vm => vm.CoinHistory);
set.Apply();
viewModel(init):
CoinHistory = new ObservableCollection<ChartDataModel>(_CoinHistoryGraph.Select(x => new ChartDataModel(float.Parse(x.price_btc), x.timestamp)));
RaisePropertyChanged(() => CoinHistory);
ViewModel properties:
private ObservableCollection<ChartDataModel> _CoinHistory;
public ObservableCollection<ChartDataModel> CoinHistory
{
get
{
return _CoinHistory;
}
set
{
_CoinHistory = value;
RaisePropertyChanged(() => CoinHistory);
}
}
How do you pass your model data to the details view? According to your descriptions, the first ViewModel should hold the whole data. When user click one cell, the tableView source's SelectionChangedCommand event will trigger. Normally we bind this command to the corresponding ViewModel's command, then we can configure the push and pass the parameters there.
Here is my bind in the first view which contains a UITableView:
var set = this.CreateBindingSet<FirstView, FirstViewModel>();
set.Bind(source).For(s => s.ItemsSource).To(vm => vm.ItemsGroup);
set.Bind(source).For(s => s.SelectionChangedCommand).To(vm => vm.ShowDetailsCommand);
set.Apply();
When user click one cell in the TableView, ShowDetailsCommand will fire in the FirstViewModel:
private readonly Lazy<IMvxNavigationService> _navigationService = new Lazy<IMvxNavigationService>(Mvx.Resolve<IMvxNavigationService>);
private MvxCommand<Item> showDetailsCommand;
public ICommand ShowDetailsCommand
{
get
{
return showDetailsCommand ?? (showDetailsCommand = new MvxCommand<Item>(showDetails));
}
}
async void showDetails(Item item)
{
// This item is bound to the ItemsSource through ItemsGroup
await _navigationService.Value.Navigate<SecondViewModel, Item>(item);
}
Then the second ViewModel can accept this item via:
public class SecondViewModel : MvxViewModel<Item>
{
private List<CoinHistoryModel> _CoinHistory;
public List<CoinHistoryModel> CoinHistory
{
get
{
return _CoinHistory;
}
set
{
_CoinHistory = value;
RaisePropertyChanged(() => CoinHistory);
}
}
public override void Prepare(Item parameter)
{
CoinHistory = parameter.SingleCoinHistory;
}
}
At last the details view will show the CoinHistory, if you have successfully bound it to the second view model.
Here is my demo for you referring to.
I was not able to replicate the issue you have stated here, instead I have prepared a sample based on your requirement and you can find the sample from the below link.
Sample
If this does not resolve your query, can you revert me back with more details.
Hope this helps
Note: I work for Syncfusion.

How to bind Map/Image in a List with Click event?

I want to bind map with a field and its click event as well, which will take it to a MvxCommand and show some MapViewModel.
[Register("HoursEntryCell")]
public class HoursEntryCell : MvxTableViewCell
{
public HoursEntryCell()
{
CreateLayout();
InitializeBindings();
}
public HoursEntryCell(IntPtr handle)
: base(handle)
{
CreateLayout();
InitializeBindings();
}
private UILabel hours;
private UIImageView imageView;
private UILabel jobName;
private MKMapView location;
private void CreateLayout()
{
jobName = new UILabel(new RectangleF(10, 10, 100, 30));
jobName.AdjustsFontSizeToFitWidth = true;
jobName.Lines = 0;
jobName.Font = jobName.Font.WithSize(16);
imageView = new UIImageView(UIImage.FromBundle("pencil.png"));
imageView.Frame = new RectangleF(270, 10,imageView.Image.CGImage.Width, imageView.Image.CGImage.Height);
Accessory = UITableViewCellAccessory.DisclosureIndicator;
location = new MKMapView(new RectangleF(15, 40, 280, 160));
location.AddAnnotation(new MKPointAnnotation()
{
Title = "My Loc",
Coordinate = new CLLocationCoordinate2D(23.0092509, 72.5061084)
});
location.UserInteractionEnabled = false;
salaryLable.Text = "Salary";
hours = new UILabel(new RectangleF(200,200,50,50));
ContentView.AddSubviews(jobName, location, hours,salaryLable, imageView);
}
private void InitializeBindings()
{
this.DelayBind(() =>
{
var set = this.CreateBindingSet<HoursEntryCell, ListViewModel>();
set.Bind(location).To(vm => vm.MyLocation);
set.Bind(hours).To(vm => vm.Salary);
set.Bind(jobName).To(vm => vm.EmployeeName);
set.Apply();
});
}
}
}
I want to achieve something like set.Bind(location).To(vm => vm.GoNextCommand); along with the map (set.Bind(location).To(vm => vm.MyLocation);)
Or How can I bind simple image button click event to a MvxCommand from the list?
How can I go for it?
Need Help.
You'll probably need to do a combination of things to get this to work properly...
1.) Do your binding from the view that has your list view. In the example above how does the cell actually get access to your view model? Look at the example here: https://github.com/MvvmCross/MvvmCross-Tutorials/blob/master/DailyDilbert/DailyDilbert.Touch/Views/ListView.cs#L16
2.) you'll need to probably create custom bindings to handle the map view or it might be similar to this example from Stuart: MvvmCross iOS: How to bind MapView Annotation to jump to another view?

Why use Automappers ValueResolver?

Why do this
Mapper.CreateMap<MyObject, AnotherObject>().
ForMember(x => x.DateAsString, m => m.ResolveUsing<StringToDateTimeFormatter>());
private class StringToDateTimeFormatter : ValueResolver<DateTime, string>
{
protected override string ResolveCore(DateTimesource)
{
return source.ToString("yyyy-MM-dd");
}
}
when you can do this
Mapper.CreateMap<MyObject, AnotherObject>().
ForMember(x => x.DateAsString, m => m.MapFrom(x => x.Date.ToString("yyy-MM-dd")));
???
Update
Here's an example on how to do more complex business logic
Mapper.CreateMap<MyObject, AnotherObject>().
ForMember(x => x.DateAsString, m => m.MapFrom(n => MyMethod(n.DateAsString)));
private object MyMethod(string dateTime)
{
if(!MyDomainObjectIsValid(dateTime))
{
throw new MyValidationException();
}
// do more stuff
}
I still don't see the need for a ValueResolver...
Obviously for your example it is more reasonable to use just MapFrom.
ValueResolvers are needed for more complicated cases. For example when you need to do some validation and throw exception accordingly.
EDIT
ValueResolvers provide access to the destination type and value. Here is small example.
public class FakeResolver : IValueResolver
{
public ResolutionResult Resolve(ResolutionResult source)
{
if (source.Context.DestinationType == typeof(string) && source.Context.DestinationValue == "test")
throw new Exception();
return source;
}
}

Resources