Skip to content

Commit 746383f

Browse files
authored
Merge pull request #24 from tsgcpp/feature/fix_cache_collections_in_editor
fix: Use CacheListConverter to return "StringTableCollection"s
2 parents 58c21c5 + 052dcd9 commit 746383f

File tree

9 files changed

+281
-23
lines changed

9 files changed

+281
-23
lines changed

Assets/Plugins/LocalizationExtension/Editor/Google/StringTableCollection/StringTableCollectionBundleEditor.cs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,16 @@ public sealed class StringTableCollectionBundleEditor : UnityEditor.Editor
1515

1616
public override void OnInspectorGUI()
1717
{
18-
using (var changeCheck = new EditorGUI.ChangeCheckScope())
19-
{
20-
DrawDefaultInspector();
21-
EditorGUILayout.Space(16);
18+
DrawDefaultInspector();
19+
EditorGUILayout.Space(16);
2220

23-
DrawToolsWithSheetsServiceProvider();
24-
EditorGUILayout.Space(8);
21+
DrawToolsWithSheetsServiceProvider();
22+
EditorGUILayout.Space(8);
2523

26-
DrawToolsWithServiceAccount();
27-
EditorGUILayout.Space(8);
24+
DrawToolsWithServiceAccount();
25+
EditorGUILayout.Space(8);
2826

29-
DrawStringTableCollections(changeCheck.changed);
30-
}
27+
DrawStringTableCollections();
3128
}
3229

3330
private void DrawToolsWithSheetsServiceProvider()
@@ -63,28 +60,20 @@ private void DrawToolsWithServiceAccount()
6360
}
6461

6562
private bool _showStringTableCollections = true;
66-
private IReadOnlyList<StringTableCollection> _stringTableCollectionsCache = new List<StringTableCollection>();
63+
private readonly CacheListConverter<DefaultAsset, StringTableCollection> _stringTableCollectionsConverter =
64+
new CacheListConverter<DefaultAsset, StringTableCollection>(
65+
actualConverter: new FolderToCollectionConverter());
6766

68-
private void DrawStringTableCollections(bool guiChanged)
67+
private void DrawStringTableCollections()
6968
{
7069
var foldoutStyle = new GUIStyle(EditorStyles.foldout)
7170
{
7271
fontStyle = FontStyle.Bold,
7372
};
7473

7574
_showStringTableCollections = EditorGUILayout.Foldout(_showStringTableCollections, "Target \"StringTableCollection\"s", foldoutStyle);
76-
if (!_showStringTableCollections)
77-
{
78-
_stringTableCollectionsCache = null;
79-
return;
80-
}
81-
82-
if (_stringTableCollectionsCache == null || guiChanged)
83-
{
84-
_stringTableCollectionsCache = Bundle.StringTableCollections;
85-
}
8675

87-
var stringTableCollections = Bundle.StringTableCollections;
76+
var stringTableCollections = _stringTableCollectionsConverter.Convert(Bundle.TargetFolders);
8877
using var h = new EditorGUILayout.VerticalScope(GUI.skin.box);
8978
using var g = new EditorGUI.DisabledGroupScope(true);
9079
foreach (var collection in stringTableCollections)
@@ -139,5 +128,13 @@ private ServiceAccountSheetsServiceProvider CreateServiceAccountSheetsServicePro
139128
applicationName: sheetsSesrvicesProvider.ApplicationName);
140129
return provider;
141130
}
131+
132+
private sealed class FolderToCollectionConverter : IListConverter<DefaultAsset, StringTableCollection>
133+
{
134+
public IReadOnlyList<StringTableCollection> Convert(IReadOnlyList<DefaultAsset> list)
135+
{
136+
return AssetFinding.FindAssetsInFolders<StringTableCollection>(list);
137+
}
138+
}
142139
}
143140
}

Assets/Plugins/LocalizationExtension/Editor/ListConverter.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace Tsgcpp.Localization.Extension.Editor
6+
{
7+
public sealed class CacheListConverter<T, TResult> : IListConverter<T, TResult>
8+
where T : class
9+
where TResult : class
10+
{
11+
private readonly IListConverter<T, TResult> _actualConverter;
12+
private readonly List<T> _listCache = new List<T>(capacity: 32);
13+
private IReadOnlyList<TResult> _resultListCache = null;
14+
15+
public CacheListConverter(IListConverter<T, TResult> actualConverter)
16+
{
17+
_actualConverter = actualConverter;
18+
}
19+
20+
public IReadOnlyList<TResult> Convert(IReadOnlyList<T> list)
21+
{
22+
if (list == null)
23+
{
24+
throw new NullReferenceException("list is null");
25+
}
26+
27+
if (_resultListCache != null && CompareArgListEquality(list))
28+
{
29+
return _resultListCache;
30+
}
31+
32+
return CreateAndHoldCache(list);
33+
}
34+
35+
private IReadOnlyList<TResult> CreateAndHoldCache(IReadOnlyList<T> list)
36+
{
37+
_listCache.Clear();
38+
_listCache.AddRange(list);
39+
_resultListCache = _actualConverter.Convert(list);
40+
return _resultListCache;
41+
}
42+
43+
private bool CompareArgListEquality(IReadOnlyList<T> list)
44+
{
45+
if (list.Count != _listCache.Count)
46+
{
47+
return false;
48+
}
49+
50+
return _listCache.SequenceEqual(list);
51+
}
52+
}
53+
}

Assets/Plugins/LocalizationExtension/Editor/ListConverter/CacheListConverter.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Collections.Generic;
2+
3+
namespace Tsgcpp.Localization.Extension.Editor
4+
{
5+
public interface IListConverter<T, TResult>
6+
where T : class
7+
where TResult : class
8+
{
9+
IReadOnlyList<TResult> Convert(IReadOnlyList<T> list);
10+
}
11+
}

Assets/Plugins/LocalizationExtension/Editor/ListConverter/IListConverter.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Tests/LocalizationExtension/Editor/ListConverter.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using NUnit.Framework;
5+
using UnityEngine;
6+
using UnityEngine.TestTools;
7+
using Tsgcpp.Localization.Extension.Editor;
8+
9+
namespace Tests.Tsgcpp.Localization.Extension.Editor
10+
{
11+
public class TestCacheListConverter
12+
{
13+
CacheListConverter<string, object> _target;
14+
ListConverterMock _listConverterMock;
15+
16+
[SetUp]
17+
public void SetUp()
18+
{
19+
_listConverterMock = new ListConverterMock();
20+
_target = new CacheListConverter<string, object>(_listConverterMock);
21+
}
22+
23+
[Test]
24+
public void Convert_ReturnsNewResultList_IfFirstCall()
25+
{
26+
// setup
27+
var list = new List<string> { "a", "b", "c" };
28+
29+
// when
30+
var actual = _target.Convert(list);
31+
32+
// then
33+
Assert.That(actual, Is.SameAs(_listConverterMock.ResultList));
34+
Assert.That(_listConverterMock.LastArg, Is.SameAs(list));
35+
Assert.That(_listConverterMock.ConvertCount, Is.EqualTo(1));
36+
}
37+
38+
[Test]
39+
public void Convert_ReturnsNewResultList_IfSecondCallWithAnother()
40+
{
41+
// setup
42+
var list = new List<string> { "a", "b", "c" };
43+
var list2 = new List<string> { "a", "c" };
44+
_target.Convert(list);
45+
46+
var resultList2 = new List<object> { 2, "b" };
47+
_listConverterMock.ResultList = resultList2;
48+
49+
// when
50+
var actual = _target.Convert(list2);
51+
52+
// then
53+
Assert.That(actual, Is.SameAs(resultList2));
54+
Assert.That(_listConverterMock.ConvertCount, Is.EqualTo(2));
55+
}
56+
57+
[Test]
58+
public void Convert_ReturnsCachedResultList_IfSecondCallWithTheSameList()
59+
{
60+
// setup
61+
var list = new List<string> { "a", "b", "c" };
62+
_target.Convert(list);
63+
64+
// when
65+
var actual = _target.Convert(list);
66+
67+
// then
68+
Assert.That(actual, Is.SameAs(_listConverterMock.ResultList));
69+
// Not call ListConverterMock.Convert() because the cache is already created.
70+
Assert.That(_listConverterMock.ConvertCount, Is.EqualTo(1));
71+
}
72+
73+
[Test]
74+
public void Convert_ReturnsCachedResultList_IfSecondCallWithTheSameValues()
75+
{
76+
// setup
77+
var list = new List<string> { "a", "b", "c" };
78+
var list2 = new List<string> { "a", "b", "c" };
79+
_target.Convert(list);
80+
81+
// when
82+
var actual = _target.Convert(list2);
83+
84+
// then
85+
Assert.That(actual, Is.SameAs(_listConverterMock.ResultList));
86+
// Not call ListConverterMock.Convert() because the cache is already created.
87+
Assert.That(_listConverterMock.ConvertCount, Is.EqualTo(1));
88+
}
89+
90+
[Test]
91+
public void Convert_ReturnsNewResultList_IfSecondCallWithAnotherValuesOfSameCount()
92+
{
93+
// setup
94+
var list = new List<string> { "a", "b", "c" };
95+
var list2 = new List<string> { "a", "c", "d" };
96+
_target.Convert(list);
97+
98+
var resultList2 = new List<object> { 2, "b" };
99+
_listConverterMock.ResultList = resultList2;
100+
101+
// when
102+
var actual = _target.Convert(list2);
103+
104+
// then
105+
Assert.That(actual, Is.SameAs(resultList2));
106+
Assert.That(_listConverterMock.ConvertCount, Is.EqualTo(2));
107+
}
108+
109+
[Test]
110+
public void Convert_ReturnsNewResultList_IfSecondCallWithAnotherValuesOfDifferentCount()
111+
{
112+
// setup
113+
var list = new List<string> { "a", "b", "c" };
114+
var list2 = new List<string> { "a", "b" };
115+
_target.Convert(list);
116+
117+
var resultList2 = new List<object> { 2, "b" };
118+
_listConverterMock.ResultList = resultList2;
119+
120+
// when
121+
var actual = _target.Convert(list2);
122+
123+
// then
124+
Assert.That(actual, Is.SameAs(resultList2));
125+
Assert.That(_listConverterMock.ConvertCount, Is.EqualTo(2));
126+
}
127+
128+
[Test]
129+
public void Convert_ThrowsNullReferenceException_IfListIsNull()
130+
{
131+
Assert.Throws<NullReferenceException>(() => _target.Convert(null));
132+
}
133+
134+
public class ListConverterMock : IListConverter<string, object>
135+
{
136+
public IReadOnlyList<object> ResultList { get; set; } = new List<object> { 1, "a", null };
137+
public IReadOnlyList<string> LastArg { get; set; } = null;
138+
public int ConvertCount { get; set; } = 0;
139+
140+
public IReadOnlyList<object> Convert(IReadOnlyList<string> list)
141+
{
142+
LastArg = list;
143+
ConvertCount += 1;
144+
return ResultList;
145+
}
146+
}
147+
}
148+
}

Assets/Tests/LocalizationExtension/Editor/ListConverter/TestCacheListConverter.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)