Skip to content

Commit

Permalink
chore(project): upgrade to UIComponents v1.0.0-beta.6 (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonisavo committed Sep 22, 2023
1 parent 99d4a2f commit fddd516
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 46 deletions.
19 changes: 2 additions & 17 deletions Core/Services/Service.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,17 @@
using System.Collections.Generic;
using UIComponents;
using UIComponents;
using UIComponents.DependencyInjection;
using ILogger = UIComponents.ILogger;

namespace UnityGit.Core.Services
{
[Dependency(typeof(ILogger), provide: typeof(DebugLogger))]
public abstract class Service : IDependencyConsumer
public abstract class Service : DependencyConsumer
{
protected readonly ILogger Logger;

private readonly DependencyInjector _dependencyInjector;

protected Service()
{
DiContext.Current.RegisterConsumer(this);
_dependencyInjector = DiContext.Current.GetInjector(GetType());
Logger = Provide<ILogger>();
UIC_PopulateProvideFields();
}

protected T Provide<T>() where T : class
{
return _dependencyInjector.Provide<T>();
}

public abstract IEnumerable<IDependency> GetDependencies();

protected abstract void UIC_PopulateProvideFields();
}
}
28 changes: 10 additions & 18 deletions Editor/Toolbar/BranchToolbarButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,15 @@ static Styles()
};
}
}

private static readonly IStatusService StatusService;
private static readonly ICheckoutService CheckoutService;
private static readonly IBranchService BranchService;

private static readonly BranchToolbarButtonServices Services;

private static readonly GUIContent BranchButtonContent;

static BranchToolbarButton()
{
ToolbarExtender.LeftToolbarGUI.Add(DoBranchToolbarItem);
var injector = DiContext.Current.GetInjector(typeof(BranchToolbarButton));
var services = new BranchToolbarButtonServices();
DiContext.Current.RegisterConsumer(services);
injector.SetConsumer(services);
StatusService = injector.Provide<IStatusService>();
CheckoutService = injector.Provide<ICheckoutService>();
BranchService = injector.Provide<IBranchService>();
Services = new BranchToolbarButtonServices();

var texture = Icons.GetIcon(Icons.Name.Merge);

Expand All @@ -62,20 +54,20 @@ private static void DoBranchToolbarItem()

GUILayout.Space(6);

using (new EditorGUI.DisabledScope(!StatusService.HasProjectRepository()))
using (new EditorGUI.DisabledScope(!Services.StatusService.HasProjectRepository()))
DoBranchToolbarButton();
}

private static string GetToolbarButtonText()
{
var hasRepo = StatusService.HasProjectRepository();
var hasRepo = Services.StatusService.HasProjectRepository();

if (!hasRepo)
return "No repository";

var head = StatusService.ProjectRepository.Head;
var head = Services.StatusService.ProjectRepository.Head;

return BranchService.GetBranchName(head);
return Services.BranchService.GetBranchName(head);
}

private static void AddBranchButtonsToMenu(GenericMenu menu, IEnumerable<Branch> branches)
Expand All @@ -92,9 +84,9 @@ private static void AddBranchButtonsToMenu(GenericMenu menu, IEnumerable<Branch>
if (itemAsBranch == null || itemAsBranch.IsCurrentRepositoryHead)
return;
var repository = StatusService.ProjectRepository;
var repository = Services.StatusService.ProjectRepository;
CheckoutService.CheckoutBranch(repository, itemAsBranch);
Services.CheckoutService.CheckoutBranch(repository, itemAsBranch);
}, branch);
}
}
Expand All @@ -106,7 +98,7 @@ private static void DoBranchToolbarButton()

var menu = new GenericMenu();
var branchPartitions =
StatusService.ProjectRepository.Branches.GroupBy((branch) => branch.IsRemote);
Services.StatusService.ProjectRepository.Branches.GroupBy((branch) => branch.IsRemote);

foreach (var partition in branchPartitions)
{
Expand Down
24 changes: 13 additions & 11 deletions Editor/Toolbar/BranchToolbarButtonServices.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
using System.Collections.Generic;
using UIComponents;
using UIComponents.DependencyInjection;
using UnityGit.Core.Services;

namespace UnityGit.Editor.Toolbar
{
internal class BranchToolbarButtonServices : IDependencyConsumer
[Dependency(typeof(IStatusService), provide: typeof(StatusService))]
[Dependency(typeof(ICheckoutService), provide: typeof(CheckoutService))]
[Dependency(typeof(IBranchService), provide: typeof(BranchService))]
internal partial class BranchToolbarButtonServices : DependencyConsumer
{
public IEnumerable<IDependency> GetDependencies()
{
return new IDependency[]
{
Dependency.SingletonFor<IStatusService, StatusService>(),
Dependency.SingletonFor<ICheckoutService, CheckoutService>(),
Dependency.SingletonFor<IBranchService, BranchService>()
};
}
[Provide]
public IStatusService StatusService;

[Provide]
public ICheckoutService CheckoutService;

[Provide]
public IBranchService BranchService;
}

}

0 comments on commit fddd516

Please sign in to comment.