Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public PullRequestListViewModelDesigner()
SelectedAuthor = Authors.ElementAt(1);
}

public ObservableCollection<IPullRequestModel> PullRequests { get; set; }
public ITrackingCollection<IPullRequestModel> PullRequests { get; set; }
public IPullRequestModel SelectedPullRequest { get; set; }
public ICommand OpenPullRequest { get; set; }

Expand Down
38 changes: 17 additions & 21 deletions src/GitHub.App/ViewModels/PullRequestListViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
using GitHub.Exports;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GitHub.Models;
using System.Collections.ObjectModel;
using ReactiveUI;
using NullGuard;
using System.ComponentModel.Composition;
using GitHub.Services;
using System.Linq;
using System.Reactive.Linq;
using GitHub.Extensions.Reactive;
using System.Windows.Data;
using GitHub.Collections;
using System.Windows.Input;
using GitHub.UI;
using System.Windows.Media.Imaging;
using GitHub.Collections;
using GitHub.Exports;
using GitHub.Models;
using GitHub.Services;
using GitHub.UI;
using NullGuard;
using ReactiveUI;

namespace GitHub.ViewModels
{
Expand Down Expand Up @@ -59,11 +55,11 @@ public PullRequestListViewModel(IRepositoryHost repositoryHost, ISimpleRepositor
.Subscribe(s => UpdateFilter(s, SelectedAssignee, SelectedAuthor));

this.WhenAny(x => x.SelectedAssignee, x => x.Value)
.Where(x => PullRequests != null)
.Where(x => PullRequests != null && x != EmptyUser)
.Subscribe(a => UpdateFilter(SelectedState, a, SelectedAuthor));

this.WhenAny(x => x.SelectedAuthor, x => x.Value)
.Where(x => PullRequests != null)
.Where(x => PullRequests != null && x != EmptyUser)
.Subscribe(a => UpdateFilter(SelectedState, SelectedAssignee, a));

trackingAuthors = new TrackingCollection<IAccount>(Observable.Empty<IAccount>(),
Expand All @@ -73,8 +69,8 @@ public PullRequestListViewModel(IRepositoryHost repositoryHost, ISimpleRepositor
trackingAuthors.Subscribe();
trackingAssignees.Subscribe();

Authors = trackingAuthors.CreateListenerCollection(new List<IAccount> { EmptyUser });
Assignees = trackingAssignees.CreateListenerCollection(new List<IAccount> { EmptyUser });
Authors = trackingAuthors.CreateListenerCollection(EmptyUser, this.WhenAnyValue(x => x.SelectedAuthor));
Assignees = trackingAssignees.CreateListenerCollection(EmptyUser, this.WhenAnyValue(x => x.SelectedAssignee));

PullRequests = new TrackingCollection<IPullRequestModel>();
pullRequests.Comparer = OrderedComparer<IPullRequestModel>.OrderByDescending(x => x.UpdatedAt).Compare;
Expand All @@ -86,7 +82,7 @@ public override void Initialize([AllowNull] ViewWithData data)
{
base.Initialize(data);

PullRequests = repositoryHost.ModelService.GetPullRequests(repository, pullRequests) as TrackingCollection<IPullRequestModel>;
PullRequests = repositoryHost.ModelService.GetPullRequests(repository, pullRequests);
pullRequests.Subscribe(pr =>
{
trackingAssignees.AddItem(pr.Assignee);
Expand All @@ -104,12 +100,12 @@ void UpdateFilter(PullRequestState state, [AllowNull]IAccount ass, [AllowNull]IA
(aut == null || aut.Equals(pr.Author));
}

TrackingCollection<IPullRequestModel> pullRequests;
public ObservableCollection<IPullRequestModel> PullRequests
ITrackingCollection<IPullRequestModel> pullRequests;
public ITrackingCollection<IPullRequestModel> PullRequests
{
[return: AllowNull]
get { return pullRequests; }
private set { this.RaiseAndSetIfChanged(ref pullRequests, (TrackingCollection<IPullRequestModel>)value); }
private set { this.RaiseAndSetIfChanged(ref pullRequests, value); }
}

IPullRequestModel selectedPullRequest;
Expand Down
129 changes: 92 additions & 37 deletions src/GitHub.Exports.Reactive/Collections/TrackingCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.Reactive.Subjects;
using System.Threading;
using System.Linq;
using System.Collections.Specialized;

namespace GitHub.Collections
{
Expand Down Expand Up @@ -47,53 +48,107 @@ public static ObservableCollection<T> CreateListenerCollection<T>(this ITracking
IList<T> stickieItemsOnTop = null)
where T : class, ICopyable<T>
{
var col = new ObservableCollection<T>(stickieItemsOnTop ?? Enumerable.Empty<T>());
tcol.CollectionChanged += (s, e) =>
if (stickieItemsOnTop == null)
{
var offset = 0;
if (stickieItemsOnTop != null)
{
foreach (var item in stickieItemsOnTop)
{
if (col.Contains(item))
offset++;
}
}
stickieItemsOnTop = new T[0];
}

var col = new ObservableCollection<T>(stickieItemsOnTop.Concat(tcol));
tcol.CollectionChanged += (_, e) => UpdateStickieItems(col, e, stickieItemsOnTop);
return col;
}

/// <summary>
/// Creates an observable collection that tracks an <see cref="ITrackingCollection{T}"/>
/// and adds a sticky item to the top of the collection when a related selection is null.
/// </summary>
/// <typeparam name="T">The type of items in the collection.</typeparam>
/// <param name="tcol">The source tracking collection</param>
/// <param name="stickieItemOnTop">The sticky item to add to the top of the collection.</param>
/// <param name="selection">
/// The current selection. If null or equal to the sticky item then the sticky item will be
/// added to the collection.
/// </param>
/// <returns>An <see cref="ObservableCollection{T}"/>.</returns>
public static ObservableCollection<T> CreateListenerCollection<T>(this ITrackingCollection<T> tcol,
T stickieItemOnTop,
IObservable<T> selection)
where T : class, ICopyable<T>
{
Debug.Assert(stickieItemOnTop != null, "stickieItemOnTop may not be null in CreateListenerCollection");
Debug.Assert(selection != null, "selection may not be null in CreateListenerCollection");

var stickieItems = new[] { stickieItemOnTop };
var result = new ObservableCollection<T>(tcol);
var hasSelection = false;

tcol.CollectionChanged += (_, e) =>
{
UpdateStickieItems(result, e, hasSelection ? stickieItems : null);
};

if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Move)
selection.Subscribe(x =>
{
hasSelection = x != null && !object.Equals(x, stickieItemOnTop);
var hasStickie = result.FirstOrDefault() == stickieItemOnTop;

if (hasSelection && !hasStickie)
{
for (int i = 0, oldIdx = e.OldStartingIndex, newIdx = e.NewStartingIndex;
i < e.OldItems.Count; i++, oldIdx++, newIdx++)
{
col.Move(oldIdx + offset, newIdx + offset);
}
result.Insert(0, stickieItemOnTop);
}
else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
else if (hasStickie)
{
foreach (T item in e.NewItems)
col.Add(item);
result.Remove(stickieItemOnTop);
}
else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
{
foreach (T item in e.OldItems)
col.Remove(item);
}
else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Replace)
});

return result;
}

static void UpdateStickieItems<T>(
ObservableCollection<T> col,
NotifyCollectionChangedEventArgs e,
IList<T> stickieItemsOnTop)
{
var offset = 0;
if (stickieItemsOnTop != null)
{
if (object.Equals(col.FirstOrDefault(), stickieItemsOnTop.FirstOrDefault()))
offset = stickieItemsOnTop.Count;
}

if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Move)
{
for (int i = 0, oldIdx = e.OldStartingIndex, newIdx = e.NewStartingIndex;
i < e.OldItems.Count; i++, oldIdx++, newIdx++)
{
for (int i = 0, idx = e.OldStartingIndex; i < e.OldItems.Count; i++, idx++)
col[idx + offset] = (T)e.NewItems[i];
col.Move(oldIdx + offset, newIdx + offset);
}
else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Reset)
}
else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
{
foreach (T item in e.NewItems)
col.Add(item);
}
else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
{
foreach (T item in e.OldItems)
col.Remove(item);
}
else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Replace)
{
for (int i = 0, idx = e.OldStartingIndex; i < e.OldItems.Count; i++, idx++)
col[idx + offset] = (T)e.NewItems[i];
}
else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Reset)
{
col.Clear();
if (stickieItemsOnTop != null)
{
col.Clear();
if (stickieItemsOnTop != null)
{
foreach (var item in stickieItemsOnTop)
col.Add(item);
}
foreach (var item in stickieItemsOnTop)
col.Add(item);
}
};
return col;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public override string ToString()

public interface IPullRequestListViewModel : IViewModel
{
ObservableCollection<IPullRequestModel> PullRequests { get; }
ITrackingCollection<IPullRequestModel> PullRequests { get; }
IPullRequestModel SelectedPullRequest { get; }
ICommand OpenPullRequest { get; }
IReadOnlyList<PullRequestState> States { get; set; }
Expand Down
107 changes: 0 additions & 107 deletions src/GitHub.UI/Behaviours/AddEmptyItemToList.cs

This file was deleted.

1 change: 0 additions & 1 deletion src/GitHub.UI/GitHub.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="Behaviours\AddEmptyItemToList.cs" />
<Compile Include="Behaviours\ClosePopupAction.cs" />
<Compile Include="Behaviours\OpenPopupAction.cs" />
<Compile Include="Controls\Octicons\OcticonPaths.Designer.cs">
Expand Down
Loading