Skip to content

Commit

Permalink
Remove role from view when removed from current item
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlang42 committed Aug 12, 2021
1 parent a3ae7ba commit f5640ae
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
4 changes: 2 additions & 2 deletions CarmenUI/ViewModels/ItemView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public ItemView(Item item, CastGroup[] cast_groups)
Item = item;
Roles = new ObservableCollection<RoleOnlyView>(item.Roles.InOrder().Select(r =>
{
var rv = new RoleOnlyView(r, cast_groups, item);
var rv = new RoleOnlyView(r, cast_groups, this);
rv.PropertyChanged += RoleView_PropertyChanged;
return rv;
}));
Expand Down Expand Up @@ -156,7 +156,7 @@ private void RoleView_PropertyChanged(object? sender, PropertyChangedEventArgs e

public RoleOnlyView AddRole()
{
var role_view = new RoleOnlyView(new Role(), castGroups, Item);
var role_view = new RoleOnlyView(new Role(), castGroups, this);
Roles.Add(role_view);
return role_view;
}
Expand Down
13 changes: 9 additions & 4 deletions CarmenUI/ViewModels/RoleOnlyView.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Carmen.ShowModel.Applicants;
using Carmen.ShowModel.Structure;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
Expand All @@ -13,7 +14,7 @@ namespace CarmenUI.ViewModels
{
public class RoleOnlyView : RoleView
{
private Item currentItem;
private ItemView itemView;

public CastGroup[] CastGroups { get; init; }

Expand All @@ -26,12 +27,12 @@ public class RoleOnlyView : RoleView

public string CommaSeparatedItems => string.Join(", ", Role.Items.Select(r => r.Name));

public string? CommaSeparatedOtherItems => Role.Items.Count < 2 ? null : string.Join(", ", Role.Items.Where(i => i != currentItem).Select(r => r.Name));
public string? CommaSeparatedOtherItems => Role.Items.Count < 2 ? null : string.Join(", ", Role.Items.Where(i => i != itemView.Item).Select(r => r.Name));

public RoleOnlyView(Role role, CastGroup[] cast_groups, Item current_item)
public RoleOnlyView(Role role, CastGroup[] cast_groups, ItemView item_view)
: base(role)
{
currentItem = current_item;
itemView = item_view;
CastGroups = cast_groups;
CountByGroups = new NullableCountByGroup[cast_groups.Length];
for (var i = 0; i < cast_groups.Length; i++)
Expand All @@ -47,6 +48,10 @@ private void Items_CollectionChanged(object? sender, NotifyCollectionChangedEven
{
OnPropertyChanged(nameof(CommaSeparatedItems));
OnPropertyChanged(nameof(CommaSeparatedOtherItems));
if (e.Action != NotifyCollectionChangedAction.Move
&& e.OldItems is IList removed_items
&& removed_items.Contains(itemView.Item))
itemView.RemoveRole(this);
}

private void CountByGroup_PropertyChanged(object? sender, PropertyChangedEventArgs e)
Expand Down

0 comments on commit f5640ae

Please sign in to comment.