-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleDocConnectorSettings.cs
More file actions
123 lines (103 loc) · 4 KB
/
GoogleDocConnectorSettings.cs
File metadata and controls
123 lines (103 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Collections.Generic;
using StansAssets.Plugins;
using UnityEngine;
namespace StansAssets.GoogleDoc
{
class GoogleDocConnectorSettings : PackageScriptableSettingsSingleton<GoogleDocConnectorSettings>, ISerializationCallbackReceiver
{
public const string SpreadsheetsResourcesSubFolder = "Spreadsheets";
public override string PackageName => "com.stansassets.google-doc-connector-pro";
public string CredentialsFolderPath => $"{PackagesConfig.SettingsPath}/{PackageName}";
public string SpreadsheetsFolderPath => $"{SettingsFolderPath}/{SpreadsheetsResourcesSubFolder}";
public string CredentialsPath => $"{CredentialsFolderPath}/credentials.json";
[SerializeField]
List<Spreadsheet> m_Spreadsheets = new List<Spreadsheet>();
public List<Spreadsheet> Spreadsheets => m_Spreadsheets;
readonly Dictionary<string, Spreadsheet> m_SpreadsheetsMap = new Dictionary<string, Spreadsheet>();
[SerializeField]
string m_LocalizationSpreadsheetId = string.Empty;
[SerializeField]
int m_LocalizationSheetId = -1;
internal string LocalizationSpreadsheetId => m_LocalizationSpreadsheetId;
internal int LocalizationSheetId => m_LocalizationSheetId;
internal Spreadsheet CreateSpreadsheet(string id)
{
if (m_SpreadsheetsMap.ContainsKey(id))
{
throw new ArgumentException($"Spreadsheet with Id:{id} already exists");
}
var spreadsheet = new Spreadsheet(id);
m_Spreadsheets.Add(spreadsheet);
m_SpreadsheetsMap.Add(id, spreadsheet);
Save();
return spreadsheet;
}
internal void LocalizationSpreadsheetIdSet(string newSpreadsheetId)
{
m_LocalizationSpreadsheetId = newSpreadsheetId;
Save();
}
internal void LocalizationSpreadsheetIdSet(string newSpreadsheetId, int sheetId)
{
m_LocalizationSheetId = sheetId;
m_LocalizationSpreadsheetId = newSpreadsheetId;
Save();
}
internal void RemoveSpreadsheet(string id)
{
var spreadsheet = GetSpreadsheet(id);
if (spreadsheet == null)
{
throw new KeyNotFoundException($"Spreadsheet with Id:{id} DOESN'T exist");
}
spreadsheet.CleanUpLocalCache();
m_Spreadsheets.Remove(spreadsheet);
m_SpreadsheetsMap.Remove(spreadsheet.Id);
if (spreadsheet.Id == m_LocalizationSpreadsheetId)
{
m_LocalizationSpreadsheetId = string.Empty;
m_LocalizationSheetId = -1;
}
}
internal bool HasSpreadsheet(string id)
{
var spreadsheet = GetSpreadsheet(id);
return spreadsheet != null;
}
internal Spreadsheet GetSpreadsheet(string id)
{
if (m_SpreadsheetsMap.TryGetValue(id, out var spreadsheet))
{
if (!spreadsheet.IsLoaded)
{
spreadsheet.InitFromCache();
}
return spreadsheet;
}
return null;
}
internal void ForceUpdateSpreadsheet(string id)
{
if (m_SpreadsheetsMap.TryGetValue(id, out var spreadsheet))
{
spreadsheet.InitFromCache();
spreadsheet.FinalizeDataUpdate();
}
}
public void OnBeforeSerialize()
{
//Nothing to do here. We just need OnAfterDeserialize to repopulate m_SpreadsheetsMap
//with serialized Spreadsheets data
}
public void OnAfterDeserialize()
{
m_SpreadsheetsMap.Clear();
foreach (var spreadsheet in m_Spreadsheets)
{
m_SpreadsheetsMap[spreadsheet.Id] = spreadsheet;
spreadsheet.IsLoaded = false;
}
}
}
}