using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using UnityEditor; using UnityEngine; namespace Crowdin.Editor { using Plugin.Server_API; using Plugin.Server_API.Services; using Plugin.Helpers; using Plugin.Helpers.PluginLocalization; using Plugin.Models; using Plugin.Server_API.Helpers; using Plugin.Server_API.Serializable.Language; using Plugin.Server_API.Serializable.Project; public class ConnectToCrowdinPopup : BasePopupEditor { private string OrganizationDomain { get; set; } private string Token { get; set; } private int CurrentProjectId { get; set; } private ProjectType CurrentProjectType { get; set; } private Dictionary> TargetLanguages { get; } = new Dictionary>(); private string[] _options; private int[] _projectIds; private ProjectType[] _projectTypes; private int _index; private string[] _sourceLanguages; private string _currentSourceLanguage; private bool _isPlayMode; [MenuItem("Tools/Crowdin/Connect to Crowdin", false, 90)] private static void OpenAuthorizationSettings() { var window = InitPopup( LocalizationHelper.GetLocalStringByKey(LocalKeys.connect_crowdin_popup_title) ); window.Initialize(); } private void Initialize() { _isPlayMode = EditorApplication.isPlaying; FolderHelper.TryCreateFolders(); SettingsHelper.TryCreateSettingsObj(); SettingsHelper.TryLoadSettings(); AssetDatabase.Refresh(ImportAssetOptions.DontDownloadFromCacheServer); if (!SettingsHelper.IsLoadSettings()) return; Token = SettingsHelper.GetToken(); OrganizationDomain = SettingsHelper.GetOrganizationDomain(); CurrentProjectId = SettingsHelper.TryGetProjectId(); CurrentProjectType = SettingsHelper.GetProjectType(); if (!string.IsNullOrEmpty(Token) && (_options is null || _options.Length is 0)) { status.AppendLine(LocalizationHelper.GetLocalStringByKey(LocalKeys.get_projects_label)); GetCrowdinProjects(); } } private void OnGUI() { if ((!_isPlayMode || !EditorApplication.isPlaying) && (_isPlayMode || EditorApplication.isPlaying)) { Close(); return; } EditorGUIUtility.labelWidth = 200; EditorGUILayout.LabelField(LocalizationHelper.GetLocalStringByKey(LocalKeys.connect_crowdin_token)); Token = EditorGUILayout.TextField("", Token); EditorGUILayout.LabelField(LocalizationHelper.GetLocalStringByKey(LocalKeys.connect_crowdin_domain)); OrganizationDomain = EditorGUILayout.TextField("", OrganizationDomain); GUILayout.BeginHorizontal("box"); if (SettingsHelper.IsLoadSettings()) { SettingsHelper.TryCreateSettingsObj(); if (GUILayout.Button(LocalizationHelper.GetLocalStringByKey(LocalKeys.connect_crowdin_btn_title))) { status.Clear(); status.AppendLine(LocalizationHelper.GetLocalStringByKey(LocalKeys.get_projects_label)); SettingsHelper.TrySaveSettings(TargetLanguages, _currentSourceLanguage, Token, OrganizationDomain, CurrentProjectId, _index, CurrentProjectType); GetCrowdinProjects(); } } if (_options != null) { _index = EditorGUILayout.Popup(_index, _options); CurrentProjectId = _projectIds[_index]; CurrentProjectType = _projectTypes[_index]; _currentSourceLanguage = _sourceLanguages[_index]; if (CurrentProjectId != SettingsHelper.GetProjectId()) { SettingsHelper.TrySaveSettings(TargetLanguages, _currentSourceLanguage, Token, OrganizationDomain, CurrentProjectId, _index, CurrentProjectType); } } GUILayout.EndHorizontal(); OnGUIStatusLabel(); } private void GetCrowdinProjects() { GetCrowdinProjectsAsync().Forget(); } private async Task GetCrowdinProjectsAsync() { TargetLanguages.Clear(); List projects; try { var service = new ProjectsApiService(); projects = await service.ListProjects(lifetime.Token); } catch (OperationCanceledException) { return; } catch (CrowdinApiException ex) { Debug.LogException(ex); status.AppendLine(LocalizationHelper.GetLocalStringByKey(LocalKeys.get_projects_error_label)); _options = null; return; } if (projects.Count <= 0) { status.AppendLine(LocalizationHelper.GetLocalStringByKey(LocalKeys.connect_crowdin_get_failed)); return; } _options = new string[projects.Count]; _projectIds = new int[projects.Count]; _projectTypes = new ProjectType[projects.Count]; _sourceLanguages = new string[projects.Count]; _index = SettingsHelper.GetProjectIndex(); for (var i = 0; i < projects.Count; i++) { _options[i] = projects[i].name.Replace("/", " "); _projectIds[i] = projects[i].id; _sourceLanguages[i] = projects[i].sourceLanguageId; _projectTypes[i] = projects[i].type; TargetLanguages.Add(_projectIds[i], projects[i].targetLanguages); } CurrentProjectId = projects[0].id; CurrentProjectType = projects[0].type; _currentSourceLanguage = projects[0].sourceLanguageId; SettingsHelper.TrySaveSettings(TargetLanguages, _currentSourceLanguage, Token, OrganizationDomain, CurrentProjectId, _index, CurrentProjectType); status.AppendLine(LocalizationHelper.GetLocalStringByKey(LocalKeys.connect_crowdin_successful)); } protected override void OnDisable() { base.OnDisable(); SettingsHelper.TrySaveSettings(TargetLanguages, _currentSourceLanguage, Token, OrganizationDomain, CurrentProjectId, _index, CurrentProjectType); } } }