private async void ClockInWindow_ContentRendered1(object sender, EventArgs e)
        {
            this.Activate();

            try
            {
                Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
                var api       = new Clockify(_client);
                var timeEntry = await api.ClockIn(_opts.APIKey, _opts.Workspace);

                if (timeEntry == null)
                {
                    throw new ApplicationException("Failed to clock in for an unknown reason.");
                }
            }
            catch (Exception ex)
            {
                Mouse.OverrideCursor = System.Windows.Input.Cursors.Arrow;
                MessageBox.Show("ERROR: " + ex.Message);
                return;
            }
            finally
            {
                Mouse.OverrideCursor = System.Windows.Input.Cursors.Arrow;
                this.Close();
            }
        }
        private async void ClockOutButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
                this.IsEnabled       = false;

                if (string.IsNullOrWhiteSpace(DescriptionTextBox.Text))
                {
                    throw new ApplicationException("You must provide a Description!");
                }

                if (ProjectResultsListBox.SelectedItem == null)
                {
                    throw new ApplicationException("You must select a Project!");
                }

                if (TagComboBox.SelectedItems.Count == 0)
                {
                    throw new ApplicationException("You must select at least one tag!");
                }

                var selectedProject = ((KeyValuePair <string, Project>)ProjectResultsListBox.SelectedItem);
                var selectedTags    = new List <Tag>();
                foreach (var t in TagComboBox.SelectedItems)
                {
                    selectedTags.Add((Tag)t);
                }

                var api = new Clockify(_client);

                var input = new Clockify.ClockOutDti();
                input.start       = _timeEntry.TimeInterval.Start;
                input.end         = DateTime.UtcNow.ToString("o", CultureInfo.InvariantCulture);
                input.description = DescriptionTextBox.Text;
                input.billable    = (BillableCheckbox.IsChecked ??= false) ? "true" : "false";
                input.projectId   = selectedProject.Value.Id;
                input.tagIds      = selectedTags.Select(x => x.Id).ToList();

                var result = await api.ClockOut(_opts.APIKey, _opts.Workspace, _timeEntry.Id, input);

                if (result == null)
                {
                    throw new ApplicationException("Unknown response from Clockify when trying to Clock Out! Please use the Website to check your timesheet!");
                }

                this.Close();
            }
            catch (Exception ex)
            {
                Mouse.OverrideCursor = System.Windows.Input.Cursors.Arrow;
                MessageBox.Show("ERROR: " + ex.Message);
                return;
            }
            finally
            {
                Mouse.OverrideCursor = System.Windows.Input.Cursors.Arrow;
                this.IsEnabled       = true;
            }
        }
Example #3
0
        private async void ValidateAPIKeyButton_Click(object sender, RoutedEventArgs e)
        {
            resetForm();

            // Check Input
            if (string.IsNullOrWhiteSpace(APIKeyTextBox.Text))
            {
                MessageBox.Show("Please enter an API Key!");
                return;
            }

            try
            {
                Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;

                // API object for this request
                var api = new Clockify(_client);

                // Get User
                _user = await api.GetGeneric <User>(APIKeyTextBox.Text, "user");

                if (_user == null)
                {
                    throw new ApplicationException("Failed to get the user for an unknown reason.");
                }

                UsernameTextBox.Text = _user.Email;

                // Save the API Key for next time this settings dialog is opened
                Properties.Settings.Default.LastConfiguredAPIKey = APIKeyTextBox.Text;
                Properties.Settings.Default.Save();

                // Get Workspaces
                _workspaces = await api.GetGeneric <List <Workspace> >(APIKeyTextBox.Text, "workspaces");

                if (_workspaces == null)
                {
                    throw new ApplicationException("Failed to get the workspaces for an unknown reason.");
                }

                WorkspaceComboBox.Items.Clear();
                WorkspaceComboBox.ItemsSource       = _workspaces;
                WorkspaceComboBox.DisplayMemberPath = "Name";
                WorkspaceComboBox.Items.Refresh();

                WorkspaceComboBox.IsEnabled     = true;
                WorkspaceComboBox.IsReadOnly    = false;
                SelectWorkspaceButton.IsEnabled = true;
            }
            catch (Exception ex)
            {
                Mouse.OverrideCursor = System.Windows.Input.Cursors.Arrow;
                MessageBox.Show("ERROR: " + ex.Message);
                return;
            }
            finally
            {
                Mouse.OverrideCursor = System.Windows.Input.Cursors.Arrow;
            }
        }
        private async void ClockOutWindow_ContentRendered(object sender, EventArgs e)
        {
            try
            {
                Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
                this.IsEnabled       = false;

                var api = new Clockify(_client);

                _user = await api.GetGeneric <User>(_opts.APIKey, "user");

                if (_user == null)
                {
                    throw new ApplicationException("Failed to get the user for an unknown reason.");
                }
                UsernameTextBox.Text = _user.Email;

                _workspaces = await api.GetGeneric <List <Workspace> >(_opts.APIKey, "workspaces");

                if (_workspaces == null)
                {
                    throw new ApplicationException("Failed to get the workspaces for an unknown reason");
                }

                var workspace = _workspaces.FirstOrDefault(x => x.Id == _opts.Workspace);
                if (workspace == null)
                {
                    throw new ApplicationException("You do not have access to a workspace with that ID");
                }
                WorkspaceTextBox.Text = workspace.Name;

                var timeEntries = await api.GetGeneric <List <TimeEntry> >(_opts.APIKey, "workspaces/" + _opts.Workspace + "/user/" + _user.Id + "/time-entries?in-progress=true");

                if (timeEntries == null)
                {
                    throw new ApplicationException("Failed to get the time entries for an unknown reason");
                }
                if (timeEntries.Count == 0)
                {
                    throw new ApplicationException("You are not clocked in right now!");
                }
                else if (timeEntries.Count > 1)
                {
                    throw new ApplicationException("You are clocked in multiple times right now in the same workspace. I don\'t know how to handle that!");
                }
                _timeEntry = timeEntries.First();
                ClockStartedTextBox.Text = _timeEntry.TimeInterval.Start;

                _projects = await api.GetGeneric <List <Project> >(_opts.APIKey, "workspaces/" + _opts.Workspace + "/projects?archived=false");

                if (_projects == null)
                {
                    throw new ApplicationException("Failed to get the projects for an unknown reason");
                }
                _filteredProjects = new List <KeyValuePair <string, Project> >();
                foreach (var p in _projects)
                {
                    _filteredProjects.Add(new KeyValuePair <string, Project>(p.Name + " - " + p.ClientName, p));
                }
                _filteredProjects = _filteredProjects.OrderBy(x => x.Value.ClientName).ThenBy(x => x.Value.Name).ToList();
                ProjectResultsListBox.ItemsSource       = _filteredProjects;
                ProjectResultsListBox.DisplayMemberPath = "Key";
                ProjectResultsListBox.Items.Refresh();

                _tags = await api.GetGeneric <List <Tag> >(_opts.APIKey, "workspaces/" + _opts.Workspace + "/tags?archived=false");

                if (_tags == null)
                {
                    throw new ApplicationException("Failed to get the tags for any unknown reason");
                }
                _tags = _tags.OrderBy(x => x.Name).ToList();
                TagComboBox.ItemsSource       = _tags;
                TagComboBox.DisplayMemberPath = "Name";
                TagComboBox.Items.Refresh();
            }
            catch (Exception ex)
            {
                Mouse.OverrideCursor = System.Windows.Input.Cursors.Arrow;
                MessageBox.Show("ERROR: " + ex.Message);
                this.Close();
            }
            finally
            {
                Mouse.OverrideCursor = System.Windows.Input.Cursors.Arrow;
                this.IsEnabled       = true;
                DescriptionTextBox.Focus();
            }
        }