Ejemplo n.º 1
0
        /// <summary>
        /// Gets the list of the current users projects from the WakaTime API.
        /// </summary>
        static void GetUserProjects()
        {
            if (!ApiKeyValidated)
            {
                return;
            }

            s_RetrievingProjects = true;
            var www     = UnityWebRequest.Get(FormatApiUrl("users/current/projects"));
            var request = www.Send();

            // Wait until we've finished, but allow the user to cancel
            while (!request.isDone)
            {
                var cancelled = EditorUtility.DisplayCancelableProgressBar(
                    "WakaTime Api",
                    "Getting your projects from WakaTime...",
                    request.progress
                    );

                // Abort the operation and return if the user cancelled.
                if (cancelled)
                {
                    s_RetrievingProjects = false;
                    www.Abort();
                    EditorUtility.ClearProgressBar();
                    return;
                }
            }

            // Parse the result
            var result = JsonUtility.FromJson <ResponseSchema <ProjectSchema[]> >(www.downloadHandler.text);

            // If the result returned an error, the key is likely no good
            if (result.error != null)
            {
                UnityEngine.Debug.LogError("<WakaTime> Failed to get projects from WakaTime API.");
            }
            else
            {
                UnityEngine.Debug.Log("<WakaTime> Successfully retrieved project list from WakaTime API.");

                foreach (ProjectSchema project in result.data)
                {
                    if (Application.productName == project.name)
                    {
                        UnityEngine.Debug.Log("<WakaTime> Found a project with identical name to current Unity project; setting it to active.");
                        ActiveProject = project;
                    }
                }

                s_UserProjects = result.data;
            }

            s_RetrievingProjects = false;
            EditorUtility.ClearProgressBar();
        }
Ejemplo n.º 2
0
        static void WakaTimePreferencesView()
        {
            if (EditorApplication.isCompiling)
            {
                EditorGUILayout.HelpBox(
                    "Hold up!\n" +
                    "Unity is compiling right now, so to prevent catastrophic " +
                    "failure of something you'll have to try again once it's done.",
                    MessageType.Warning
                    );
                return;
            }

            // Plugin Meta
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.HelpBox(string.Format("v{0:0.0} by @bengsfort", Version), MessageType.Info);
            EditorGUILayout.BeginVertical();
            var githubButton = GUILayout.Button("Github");
            var updateButton = GUILayout.Button("Update");

            EditorGUILayout.EndVertical();
            EditorGUILayout.EndHorizontal();

            if (githubButton)
            {
                Application.OpenURL(GithubRepo);
            }

            if (updateButton)
            {
                UpdatePlugin();
            }

            EditorGUILayout.Separator();

            // Main integration settings
            EditorGUILayout.BeginVertical();
            EditorGUILayout.LabelField("Integration Settings", EditorStyles.boldLabel);

            // Don't show the rest of the items if its not even enabled
            Enabled = EditorGUILayout.BeginToggleGroup("Enable WakaTime", Enabled);

            EditorGUILayout.Separator();
            EditorGUILayout.Separator();

            // Should version control be enabled?
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.PrefixLabel("Enable Version Control");
            EnableVersionControl = EditorGUILayout.Toggle(EnableVersionControl);
            EditorGUILayout.EndHorizontal();

            // API Key field
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.PrefixLabel("API Key (" + (ApiKeyValidated ? "Validated" : "Invalid") + ")");
            EditorGUILayout.BeginVertical();
            var newApiKey         = EditorGUILayout.PasswordField(ApiKey);
            var validateKeyButton = GUILayout.Button("Validate key");

            EditorGUILayout.EndVertical();
            EditorGUILayout.EndHorizontal();

            // Project selection
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.PrefixLabel("Active Project");
            var projects         = GetProjectDropdownOptions();
            var projectSelection = EditorGUILayout.Popup(s_ActiveProjectIndex, projects);

            EditorGUILayout.EndHorizontal();

            EditorGUILayout.Separator();
            EditorGUILayout.Separator();

            // Current settings information
            EditorGUILayout.LabelField("Current Details", EditorStyles.boldLabel);
            // User information
            if (ApiKeyValidated)
            {
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Logged in as: " + User.display_name);
                EditorGUILayout.EndHorizontal();
            }
            // Project information
            if (ActiveProject.name != null)
            {
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Working on project: " + ActiveProject.name);
                EditorGUILayout.EndHorizontal();
            }

            // Git information
            if (Enabled && EnableVersionControl)
            {
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Currently on branch: " + GitHelper.branch);
                EditorGUILayout.EndHorizontal();
            }
            EditorGUILayout.EndToggleGroup();
            EditorGUILayout.EndVertical();

            // Handle any changed data
            if (GUI.changed)
            {
                // Has the active project changed?
                if (s_ActiveProjectIndex != projectSelection)
                {
                    ActiveProject = s_UserProjects[Mathf.Max(projectSelection - 1, 0)];
                }

                // If the Api Key has changed, reset the validation
                if (newApiKey != ApiKey)
                {
                    ApiKey          = newApiKey;
                    ApiKeyValidated = false;
                }

                if (validateKeyButton && !ApiKeyValidated)
                {
                    GetCurrentUser();
                }
            }
        }