I'm trying to use ReactiveRecyclerViewAdapter to update my RecyclerView based on the SourceList on my ViewModel, but there's not much documentation around this, but I should not be the only person struggling with this.
ViewModel:
public class PassportScreenViewModel : ReactiveObject
{
readonly IAccountService _accountService;
public PassportScreenViewModel(IAccountService accountService = null)
{
_accountService = accountService ?? Locator.Current.GetService<IAccountService>();
SchoolChoices = new SourceList<SchoolDataModel>();
SchoolChoices.Add(new SchoolDataModel(new SchoolChoice() { Name = "My simple test" }));
SchoolChoices.Add(new SchoolDataModel(new SchoolChoice() { Name = "My simple tes 2" }));
AddSchoolCommand = ReactiveCommand.Create(() => {
SchoolChoices.Edit(x => x.Add(new SchoolDataModel(new SchoolChoice() { Name = "My simple test " + SchoolChoices.Count })));
});
}
[Reactive] public string Headline { get; set; }
[Reactive] public string Title { get; set; }
[Reactive] public string Company { get; set; }
[Reactive] public SourceList<SchoolDataModel> SchoolChoices { get; set; }
public ReactiveCommand AddSchoolCommand { get; set; }
}
My Fragment:
[WireUpResource("recyclerView_passport_schools")]
public RecyclerView SchoolsRecycler { get; private set; }
[WireUpResource("textview_passport_addschool")]
public Button AddSchoolButton { get; private set; }
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate(Resource.Layout.fragment_passportscreen, container, false);
this.WireUpControls(view);
this.ViewModel = new PassportScreenViewModel();
Photo.Click += Photo_Click;
this.BindCommand(ViewModel, viewModel => viewModel.AddSchoolCommand, myView => myView.AddSchoolButton);
var adapter = new SchoolReactiveAdapter(ViewModel.SchoolChoices.Connect());
SchoolsRecycler.HasFixedSize = true;
SchoolsRecycler.SetLayoutManager(new LinearLayoutManager(Activity));
SchoolsRecycler.SetAdapter(adapter);
ViewModel.SchoolChoices.Connect().Subscribe(x =>
{
//adapter = new SchoolReactiveAdapter(ViewModel.SchoolChoices.Connect());
//SchoolsRecycler.SwapAdapter(adapter, true);
});
return view;
}
And my Adapter:
public class SchoolReactiveAdapter : ReactiveRecyclerViewAdapter<SchoolDataModel>
{
public SchoolReactiveAdapter(IObservable<IChangeSet<SchoolDataModel>> backingList) : base(backingList)
{
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
var itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.item_school, parent, false);
return new MySchoolViewHolder(itemView);
}
public class MySchoolViewHolder : ReactiveRecyclerViewViewHolder<SchoolDataModel>
{
[WireUpResource("textview_schoolitem_name")]
public TextView SchoolName { get; private set; }
public MySchoolViewHolder(View view) : base(view)
{
this.WireUpControls();
this.Bind(ViewModel, vm => vm.Name, v => v.SchoolName.Text);
}
}
}
If I uncomment this:
ViewModel.SchoolChoices.Connect().Subscribe(x =>
{
//adapter = new SchoolReactiveAdapter(ViewModel.SchoolChoices.Connect());
//SchoolsRecycler.SwapAdapter(adapter, true);
});
this will swap the adapter and the new Item will show up, but I think the Adapter should react to the ChangeSet from the ViewModel when I do Connect();
var adapter = new SchoolReactiveAdapter(ViewModel.SchoolChoices.Connect());
And even if List get's a new item, RecyclerView doesn't get updated. There's no documentation about how to fully set up a bound RecyclerView with ReactiveUI at least I couldn't find it, any of you can help me with this?
Thanks