Skip to content

Commit

Permalink
Data is now loaded in another thread.
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisellgren committed Jun 24, 2012
1 parent 730cdab commit b540b59
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 28 deletions.
21 changes: 16 additions & 5 deletions Libraries/EnhancedObservableCollection.cs
Expand Up @@ -30,24 +30,35 @@ public void DisableNotifications()
disableNotifications = true;
}

public void EnableNotifications()
/// <summary>
/// Enables notifications back.
///
/// A reset notification is sent if the sendNotification parameter is set to true.
/// </summary>
/// <param name="sendNotification"></param>
public void EnableNotifications(bool sendNotification = false)
{
disableNotifications = false;

if (sendNotification)
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}

/// <summary>
/// Adds the elements of the specified collection to the end of the ObservableCollection(Of T).
/// </summary>
public void AddRange(IEnumerable<T> collection)
{
disableNotifications = true;
var temporarilyDisableNotifications = !disableNotifications;

if (temporarilyDisableNotifications)
disableNotifications = true;

foreach (T item in collection)
{
Add(item);
}

disableNotifications = false;
if (temporarilyDisableNotifications)
disableNotifications = false;

OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
Expand Down
3 changes: 2 additions & 1 deletion UserControls/StatusGrid.xaml.cs
Expand Up @@ -32,7 +32,8 @@ private void StatusGridElement_SelectionChanged(object sender, SelectionChangedE
var repositoryViewModel = repositoryTabs.SelectedItem as RepositoryViewModel;

// Tell repository view model to update status item diff.
repositoryViewModel.UpdateStatusItemDiff(StatusGridElement.SelectedItems);
if (repositoryViewModel is RepositoryViewModel)
repositoryViewModel.UpdateStatusItemDiff(StatusGridElement.SelectedItems);
}
}
}
90 changes: 68 additions & 22 deletions ViewModels/RepositoryViewModel.cs
Expand Up @@ -17,6 +17,7 @@
using GG.UserControls.Dialogs;
using System.Collections;
using System.Text.RegularExpressions;
using System.Threading;

namespace GG
{
Expand Down Expand Up @@ -111,9 +112,6 @@ public RepositoryViewModel()
StatusItemDiff = "";
}

/// <summary>
/// Commands.
/// </summary>
#region Commands.

public DelegateCommand ExportPatchCommand { get; private set; }
Expand Down Expand Up @@ -317,7 +315,6 @@ private void CommitChanges(object action)

// Reconstruct the repository.
LoadEntireRepository();
LoadRepositoryStatus();

// Clear the commit message box.
UIHelper.FindChild<TextBox>(Application.Current.MainWindow, "CommitMessageTextBox").Clear();
Expand Down Expand Up @@ -437,22 +434,27 @@ public bool Init()
/// </summary>
public void LoadEntireRepository()
{
using (var repo = new LibGit2Sharp.Repository(RepositoryFullPath))
ThreadStart start = delegate()
{
LoadTags(repo);
LoadBranchesAndCommits(repo);
LoadRepositoryStatus();
LoadRecentCommitMessages();
using (var repo = new LibGit2Sharp.Repository(RepositoryFullPath))
{
LoadTags(repo);
LoadBranchesAndCommits(repo);
LoadRepositoryStatus(repo);
LoadRecentCommitMessages();

ListenToDirectoryChanges();
}
ListenToDirectoryChanges();
}
};

new Thread(start).Start();
}

/// <summary>
/// Loads branches and commits.
/// </summary>
/// <param name="repo"></param>
private void LoadBranchesAndCommits(LibGit2Sharp.Repository repo)
private void LoadBranchesAndCommits(LibGit2Sharp.Repository repo = null)
{
var dispose = false;
if (repo == null)
Expand Down Expand Up @@ -518,8 +520,15 @@ private void LoadBranchesAndCommits(LibGit2Sharp.Repository repo)
RepoUtil.IncrementCommitTreeVisualPositionsRecursively(branch.Tip);
}

Commits.EnableNotifications();
Branches.EnableNotifications();
// Fire notifications for the collections on the UI thread.
Application.Current.Dispatcher.Invoke(
DispatcherPriority.Normal,
(Action) delegate()
{
Commits.EnableNotifications(true);
Branches.EnableNotifications(true);
}
);

if (dispose)
repo.Dispose();
Expand All @@ -528,7 +537,7 @@ private void LoadBranchesAndCommits(LibGit2Sharp.Repository repo)
/// <summary>
/// Loads the tags.
/// </summary>
private void LoadTags(LibGit2Sharp.Repository repo)
private void LoadTags(LibGit2Sharp.Repository repo = null)
{
var dispose = false;
if (repo == null)
Expand All @@ -551,18 +560,35 @@ private void LoadTags(LibGit2Sharp.Repository repo)
Tags.Add(t);
}

Tags.EnableNotifications();
// Fire notifications for the Tags collection on the UI thread.
Application.Current.Dispatcher.Invoke(
DispatcherPriority.Normal,
(Action) delegate()
{
Tags.EnableNotifications(true);
}
);

if (dispose)
repo.Dispose();
}

private Object thisLock = new Object();

/// <summary>
/// Loads the repository status (modified, added, removed).
/// </summary>
private void LoadRepositoryStatus()
private void LoadRepositoryStatus(LibGit2Sharp.Repository repo = null)
{
var repo = new LibGit2Sharp.Repository(RepositoryFullPath);
var dispose = false;
if (repo == null)
{
repo = new LibGit2Sharp.Repository(RepositoryFullPath);
dispose = true;
}

// A small performance boost.
StatusItems.DisableNotifications();

StatusItems.Clear();

Expand Down Expand Up @@ -596,20 +622,40 @@ private void LoadRepositoryStatus()

StatusItems.AddRange(itemList);

repo.Dispose();
if (dispose)
repo.Dispose();

// Fire notifications for the collection on the UI thread.
Application.Current.Dispatcher.Invoke(
DispatcherPriority.Normal,
(Action) delegate()
{
StatusItems.EnableNotifications(true);
}
);
}

/// <summary>
/// Stores the last RecentCommitMessageCount commit messages.
/// </summary>
private void LoadRecentCommitMessages()
{
// A small performance boost.
RecentCommitMessages.DisableNotifications();

RecentCommitMessages.Clear();

foreach (Commit commit in Commits.Take(RecentCommitMessageCount))
{
RecentCommitMessages.Add(new RecentCommitMessage(commit.ShortDescription));
}

// Fire notifications for the collection on the UI thread.
Application.Current.Dispatcher.Invoke(
DispatcherPriority.Normal,
(Action) delegate()
{
RecentCommitMessages.EnableNotifications(true);
}
);
}

#endregion
Expand Down Expand Up @@ -656,7 +702,7 @@ private void ListenToDirectoryChanges()

ReloadStatusDelegate reloadStatusDelegate = delegate(object sender, FileSystemEventArgs e)
{
Application.Current.Dispatcher.Invoke(
Application.Current.Dispatcher.BeginInvoke(
DispatcherPriority.Normal,
(Action) delegate()
{
Expand Down

0 comments on commit b540b59

Please sign in to comment.