Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 005119c

Browse files
committed
Using a singleton for caching data
1 parent 815a884 commit 005119c

File tree

9 files changed

+256
-48
lines changed

9 files changed

+256
-48
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Collections.Generic;
2+
3+
namespace GitHub.Unity
4+
{
5+
interface IBranchCache
6+
{
7+
List<GitBranch> LocalBranches { get; set; }
8+
List<GitBranch> RemoteBranches { get; set; }
9+
}
10+
}

src/GitHub.Api/Git/GitBranch.cs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,35 @@
1-
using GitHub.Unity;
1+
using System;
22

33
namespace GitHub.Unity
44
{
5-
struct GitBranch
5+
interface ITreeData
66
{
7-
public string Name { get; private set; }
8-
public string Tracking { get; private set; }
9-
public bool Active { get; private set; }
7+
string Name { get; }
8+
bool IsActive { get; }
9+
}
10+
11+
[Serializable]
12+
struct GitBranch : ITreeData
13+
{
14+
private string name;
15+
private string tracking;
16+
private bool active;
17+
public string Name { get { return name; } }
18+
public string Tracking { get { return tracking; } }
19+
public bool IsActive { get { return active; } }
1020

1121
public GitBranch(string name, string tracking, bool active)
1222
{
1323
Guard.ArgumentNotNullOrWhiteSpace(name, "name");
14-
Guard.ArgumentNotNull(tracking, "tracking");
1524

16-
Name = name;
17-
Tracking = tracking;
18-
Active = active;
25+
this.name = name;
26+
this.tracking = tracking;
27+
this.active = active;
28+
}
29+
30+
public override string ToString()
31+
{
32+
return $"{Name} Tracking? {Tracking} Active? {IsActive}";
1933
}
2034
}
2135
}

src/GitHub.Api/Git/GitRemote.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ enum GitRemoteFunction
1111
Both
1212
}
1313

14+
[Serializable]
1415
struct GitRemote
1516
{
1617
public string Name;

src/GitHub.Api/GitHub.Api.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
<Compile Include="Authentication\ILoginManager.cs" />
104104
<Compile Include="Application\ApplicationManagerBase.cs" />
105105
<Compile Include="Helpers\Constants.cs" />
106+
<Compile Include="Cache\IBranchCache.cs" />
106107
<Compile Include="Platform\DefaultEnvironment.cs" />
107108
<Compile Include="Extensions\EnvironmentExtensions.cs" />
108109
<Compile Include="Extensions\FileEventExtensions.cs" />

src/GitHub.Logging/Logging.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public static LogAdapterBase LogAdapter
5656

5757
private static ILogging instance;
5858

59-
private static ILogging Instance
59+
public static ILogging Instance
6060
{
6161
get {
6262
if (instance == null)
Lines changed: 94 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,121 @@
1-
using System;
2-
using System.IO;
3-
using System.Linq;
4-
using UnityEditorInternal;
1+
using System.Collections.Generic;
52
using UnityEngine;
63

74
namespace GitHub.Unity
85
{
9-
sealed class ApplicationCache : ScriptableObject
6+
sealed class ApplicationCache : ScriptObjectSingleton<ApplicationCache>
107
{
11-
private static ApplicationCache instance;
12-
private static string cachePath;
13-
148
[SerializeField] private bool firstRun = true;
159
public bool FirstRun
1610
{
17-
get { return firstRun; }
18-
private set
11+
get
1912
{
20-
firstRun = value;
21-
Flush();
13+
var val = firstRun;
14+
if (firstRun)
15+
{
16+
firstRun = false;
17+
Save(true);
18+
}
19+
return val;
2220
}
2321
}
2422

25-
public static ApplicationCache Instance
23+
[SerializeField] private string createdDate;
24+
public string CreatedDate
2625
{
27-
get { return instance ?? CreateApplicationCache(EntryPoint.Environment); }
26+
get { return createdDate; }
2827
}
28+
}
2929

30-
private static ApplicationCache CreateApplicationCache(IEnvironment environment)
30+
[Location("cache/branches.yaml", LocationAttribute.Location.UserFolder)]
31+
sealed class BranchCache : ScriptObjectSingleton<BranchCache>, IBranchCache
32+
{
33+
[SerializeField] private List<GitBranch> localBranches;
34+
[SerializeField] private List<GitBranch> remoteBranches;
35+
[SerializeField] private List<GitBranch> test;
36+
public BranchCache()
3137
{
32-
cachePath = environment.UnityProjectPath + "/Temp/github_cache.yaml";
38+
test = new List<GitBranch>() { new GitBranch("name", "tracking", false) };
39+
}
3340

34-
if (File.Exists(cachePath))
41+
public List<GitBranch> LocalBranches
42+
{
43+
get
3544
{
36-
var objects = InternalEditorUtility.LoadSerializedFileAndForget(cachePath);
37-
if (objects.Any())
38-
{
39-
instance = objects[0] as ApplicationCache;
40-
if (instance != null)
41-
{
42-
if (instance.FirstRun)
43-
{
44-
instance.FirstRun = false;
45-
}
46-
47-
return instance;
48-
}
49-
}
45+
if (localBranches == null)
46+
localBranches = new List<GitBranch>();
47+
return localBranches;
48+
}
49+
set
50+
{
51+
Logging.GetLogger().Debug("Saving branches {0}", value.Join(","));
52+
localBranches = value;
53+
Save(true);
54+
}
55+
}
56+
public List<GitBranch> RemoteBranches
57+
{
58+
get
59+
{
60+
if (remoteBranches == null)
61+
remoteBranches = new List<GitBranch>();
62+
return remoteBranches;
63+
}
64+
set
65+
{
66+
remoteBranches = value;
67+
Save(true);
5068
}
69+
}
70+
}
71+
72+
[Location("views/branches.yaml", LocationAttribute.Location.UserFolder)]
73+
sealed class Favourites : ScriptObjectSingleton<Favourites>
74+
{
75+
[SerializeField] private List<string> favouriteBranches;
76+
public List<string> FavouriteBranches
77+
{
78+
get
79+
{
80+
if (favouriteBranches == null)
81+
FavouriteBranches = new List<string>();
82+
return favouriteBranches;
83+
}
84+
set
85+
{
86+
favouriteBranches = value;
87+
Save(true);
88+
}
89+
}
90+
91+
public void SetFavourite(string branchName)
92+
{
93+
if (FavouriteBranches.Contains(branchName))
94+
return;
95+
FavouriteBranches.Add(branchName);
96+
Save(true);
97+
}
5198

52-
instance = CreateInstance<ApplicationCache>();
53-
instance.Flush();
99+
public void UnsetFavourite(string branchName)
100+
{
101+
if (!FavouriteBranches.Contains(branchName))
102+
return;
103+
FavouriteBranches.Remove(branchName);
104+
Save(true);
105+
}
54106

55-
return instance;
107+
public void ToggleFavourite(string branchName)
108+
{
109+
if (FavouriteBranches.Contains(branchName))
110+
FavouriteBranches.Remove(branchName);
111+
else
112+
FavouriteBranches.Add(branchName);
113+
Save(true);
56114
}
57115

58-
private void Flush()
116+
public bool IsFavourite(string branchName)
59117
{
60-
InternalEditorUtility.SaveToSerializedFileAndForget(new UnityEngine.Object[] { this }, cachePath, true);
118+
return FavouriteBranches.Contains(branchName);
61119
}
62120
}
63121
}

src/UnityExtension/Assets/Editor/GitHub.Unity/GitHub.Unity.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
</Compile>
8787
<Compile Include="Misc\RepositoryInitializer.cs" />
8888
<Compile Include="RunLocationShim.cs" />
89+
<Compile Include="ScriptObjectSingleton.cs" />
8990
<Compile Include="Threading\SingleThreadSynchronizationContext.cs" />
9091
<Compile Include="Properties\AssemblyInfo.cs" />
9192
<Compile Include="$(SolutionDir)common\SolutionInfo.cs">
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
using System;
2+
using System.Linq;
3+
using UnityEditorInternal;
4+
using UnityEngine;
5+
6+
namespace GitHub.Unity
7+
{
8+
[AttributeUsage(AttributeTargets.Class)]
9+
class LocationAttribute : Attribute
10+
{
11+
public enum Location { PreferencesFolder, ProjectFolder, UserFolder }
12+
public string filepath { get; set; }
13+
public LocationAttribute(string relativePath, Location location)
14+
{
15+
Guard.ArgumentNotNullOrWhiteSpace(relativePath, "relativePath");
16+
17+
if (relativePath[0] == '/')
18+
relativePath = relativePath.Substring(1);
19+
20+
if (location == Location.PreferencesFolder)
21+
filepath = InternalEditorUtility.unityPreferencesFolder + "/" + relativePath;
22+
else if (location == Location.UserFolder)
23+
filepath = EntryPoint.Environment.UserCachePath.Combine(relativePath).ToString(SlashMode.Forward);
24+
else
25+
filepath = relativePath;
26+
}
27+
}
28+
29+
30+
class ScriptObjectSingleton<T> : ScriptableObject where T : ScriptableObject
31+
{
32+
private string filePath = null;
33+
private NPath nFilePath = null;
34+
private NPath FilePath
35+
{
36+
get
37+
{
38+
if (nFilePath == null)
39+
{
40+
if (filePath == "")
41+
return null;
42+
if (filePath == null)
43+
filePath = GetFilePath();
44+
if (filePath == null)
45+
filePath = "";
46+
else
47+
nFilePath = filePath.ToNPath();
48+
}
49+
return nFilePath;
50+
}
51+
}
52+
53+
private static T instance;
54+
public static T Instance
55+
{
56+
get
57+
{
58+
if (instance == null)
59+
CreateAndLoad();
60+
return instance;
61+
}
62+
}
63+
64+
protected ScriptObjectSingleton()
65+
{
66+
if (instance != null)
67+
{
68+
Logging.Instance.Error("Singleton already exists!");
69+
}
70+
else
71+
{
72+
instance = this as T;
73+
System.Diagnostics.Debug.Assert(instance != null);
74+
}
75+
}
76+
77+
private static void CreateAndLoad()
78+
{
79+
System.Diagnostics.Debug.Assert(instance == null);
80+
81+
string filePath = GetFilePath();
82+
if (!string.IsNullOrEmpty(filePath))
83+
{
84+
InternalEditorUtility.LoadSerializedFileAndForget(filePath);
85+
}
86+
87+
if (instance == null)
88+
{
89+
var inst = CreateInstance<T>() as ScriptObjectSingleton<T>;
90+
inst.hideFlags = HideFlags.HideAndDontSave;
91+
inst.Save(true);
92+
}
93+
94+
System.Diagnostics.Debug.Assert(instance != null);
95+
}
96+
97+
protected virtual void Save(bool saveAsText)
98+
{
99+
if (instance == null)
100+
{
101+
Logging.Instance.Error("Cannot save singleton, no instance!");
102+
return;
103+
}
104+
105+
NPath filePath = GetFilePath();
106+
if (filePath != null)
107+
{
108+
filePath.Parent.EnsureDirectoryExists();
109+
InternalEditorUtility.SaveToSerializedFileAndForget(new[] { instance }, filePath, saveAsText);
110+
}
111+
}
112+
113+
private static NPath GetFilePath()
114+
{
115+
var attr = typeof(T).GetCustomAttributes(true)
116+
.Select(t => t as LocationAttribute)
117+
.FirstOrDefault(t => t != null);
118+
Logging.Instance.Debug("FilePath {0}", attr != null ? attr.filepath : null);
119+
120+
return attr != null ? attr.filepath.ToNPath() : null;
121+
}
122+
}
123+
}

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BranchesView.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,11 @@ private void BuildTree(IEnumerable<GitBranch> local, IEnumerable<GitBranch> remo
307307
for (var index = 0; index < localBranches.Count; ++index)
308308
{
309309
var branch = localBranches[index];
310-
var node = new BranchTreeNode(branch.Name, NodeType.LocalBranch, branch.Active);
310+
var node = new BranchTreeNode(branch.Name, NodeType.LocalBranch, branch.IsActive);
311311
localBranchNodes.Add(node);
312312

313313
// Keep active node for quick reference
314-
if (branch.Active)
314+
if (branch.IsActive)
315315
{
316316
activeBranchNode = node;
317317
}

0 commit comments

Comments
 (0)