Skip to content

Commit 7529fbe

Browse files
Update TimeClock Page and PageModel to finish page functionality
1 parent 019774b commit 7529fbe

File tree

5 files changed

+34
-8
lines changed

5 files changed

+34
-8
lines changed

TimeTrackerTutorial/PageModels/Base/PageModelLocator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static PageModelLocator()
2929

3030
// Register Services (registerd as Singletons by default)
3131
_container.Register<INavigationService, NavigationService>();
32-
_container.Register<IAccountService, AccountService>();
32+
_container.Register<IAccountService, MockAccountService>();
3333
}
3434

3535
/// <summary>

TimeTrackerTutorial/PageModels/TimeClockPageModel.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
using System.Collections.ObjectModel;
44
using System.Text;
55
using System.Threading.Tasks;
6+
using System.Timers;
67
using TimeTrackerTutorial.Models;
78
using TimeTrackerTutorial.PageModels.Base;
9+
using TimeTrackerTutorial.Services.Account;
810
using TimeTrackerTutorial.ViewModels.Buttons;
911

1012
namespace TimeTrackerTutorial.PageModels
@@ -32,7 +34,6 @@ public DateTime CurrentStartTime
3234
set => SetProperty(ref _currentStartTime, value);
3335
}
3436

35-
ObservableCollection<WorkItem> _workItems;
3637
public ObservableCollection<WorkItem> WorkItems
3738
{
3839
get => _workItems;
@@ -53,22 +54,41 @@ public ButtonModel ClockInOutButtonModel
5354
set => SetProperty(ref _clockInOutButtonModel, value);
5455
}
5556

56-
public TimeClockPageModel()
57+
private Timer _timer;
58+
private ObservableCollection<WorkItem> _workItems;
59+
private IAccountService _accountService;
60+
private double _hourlyRate;
61+
62+
public TimeClockPageModel(IAccountService accountService)
5763
{
64+
_accountService = accountService;
5865
WorkItems = new ObservableCollection<WorkItem>();
5966
ClockInOutButtonModel = new ButtonModel("Clock In", OnClockInOutAction);
67+
_timer = new Timer();
68+
_timer.Interval = 1000;
69+
_timer.Enabled = false;
70+
_timer.Elapsed += _timer_Elapsed;
71+
}
72+
73+
private void _timer_Elapsed(object sender, ElapsedEventArgs e)
74+
{
75+
RunningTotal += TimeSpan.FromSeconds(1);
6076
}
6177

62-
public override Task InitializeAsync(object navigationData)
78+
public override async Task InitializeAsync(object navigationData)
6379
{
6480
RunningTotal = new TimeSpan();
65-
return base.InitializeAsync(navigationData);
81+
_hourlyRate = await _accountService.GetCurrentPayRateAsync();
82+
await base.InitializeAsync(navigationData);
6683
}
6784

6885
private void OnClockInOutAction()
6986
{
7087
if (IsClockedIn)
7188
{
89+
_timer.Enabled = false;
90+
TodaysEarnings += _hourlyRate * RunningTotal.TotalHours;
91+
RunningTotal = TimeSpan.Zero;
7292
ClockInOutButtonModel.Text = "Clock In";
7393
WorkItems.Insert(0, new WorkItem
7494
{
@@ -79,6 +99,7 @@ private void OnClockInOutAction()
7999
else
80100
{
81101
CurrentStartTime = DateTime.Now;
102+
_timer.Enabled = true;
82103
ClockInOutButtonModel.Text = "Clock Out";
83104
}
84105
IsClockedIn = !IsClockedIn;

TimeTrackerTutorial/Pages/TimeClockPage.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252
<StackLayout BackgroundColor="{StaticResource PrimaryHeaderColor}"
5353
Padding="10">
54-
<Label Text="{Binding TodaysEarnings, StringFormat='Earnings Today: {0}'}"
54+
<Label Text="{Binding TodaysEarnings, StringFormat='Earnings Today: {0:C}'}"
5555
HorizontalOptions="Center" TextColor="White" />
5656
</StackLayout>
5757
</StackLayout>

TimeTrackerTutorial/Services/Account/IAccountService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ namespace TimeTrackerTutorial.Services.Account
88
public interface IAccountService
99
{
1010
Task<bool> LoginAsync(string username, string password);
11-
11+
Task<double> GetCurrentPayRateAsync();
1212
}
1313
}

TimeTrackerTutorial/Services/Account/AccountService.cs renamed to TimeTrackerTutorial/Services/Account/MockAccountService.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55

66
namespace TimeTrackerTutorial.Services.Account
77
{
8-
public class AccountService : IAccountService
8+
public class MockAccountService : IAccountService
99
{
10+
public Task<double> GetCurrentPayRateAsync()
11+
{
12+
return Task.FromResult(10.0);
13+
}
14+
1015
public Task<bool> LoginAsync(string username, string password)
1116
{
1217
if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))

0 commit comments

Comments
 (0)