Untitled

 avatar
unknown
plain_text
2 months ago
2.1 kB
2
Indexable
private async void OnTimeInClicked(object sender, EventArgs e)
{
    try
    {
        var openTimeEntry = await _firebaseHelper.GetOpenTimeEntryAsync(_currentInstructors.ID);
        if (openTimeEntry != null)
        {
            await DisplayAlert("Error", "You already have an active Time In.", "OK");
            return;
        }

        var timeEntry = new TimeEntry
        {
            instructorId = _currentInstructors.ID,
            FirstName = _currentInstructors.FirstName,
            LastName = _currentInstructors.LastName,
            TimeIn = DateTime.Now,
            SubjectName = SubjectNameLabel.Text,
            SubjectCode = SubjectCodeLabel.Text
        };

        _currentEntryKey = await _firebaseHelper.AddTimeInAsync(timeEntry);

        if (!string.IsNullOrEmpty(_currentEntryKey))
        {
            TimeInLabel.Text = $"Time In: {timeEntry.TimeIn:g}";
            TimeOutButton.IsEnabled = true;
            TimeInButton.IsEnabled = false;
        }
        else
        {
            await DisplayAlert("Error", "Failed to record Time In.", "OK");
        }
    }
    catch (Exception ex)
    {
        await DisplayAlert("Error", $"An error occurred: {ex.Message}", "OK");
    }
}

private async void OnTimeOutClicked(object sender, EventArgs e)
{
    try
    {
        if (string.IsNullOrEmpty(_currentEntryKey))
        {
            await DisplayAlert("Error", "No active Time In record found.", "OK");
            return;
        }

        var timeEntry = await _firebaseHelper.TimeOutAsync(_currentEntryKey);

        if (timeEntry?.TimeOut != null)
        {
            TimeOutLabel.Text = $"Time Out: {timeEntry.TimeOut.Value:g}";
            HoursWorkedLabel.Text = $"Hours Worked: {timeEntry.FormattedHoursWorked}";
            TimeOutButton.IsEnabled = false;
        }
        else
        {
            await DisplayAlert("Error", "Failed to record Time Out.", "OK");
        }
    }
    catch (Exception ex)
    {
        await DisplayAlert("Error", $"An error occurred: {ex.Message}", "OK");
    }
}
Leave a Comment