using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Input;
namespace SimpleAttendance
{
public class MainPageModel : INotifyPropertyChanged
{
public MainPageModel()
{
// Set the command properties
LoadDatabaseCommand = new DelegateCommand(LoadDatabase, CanLoadDatabase);
OpenDayCommand = new DelegateCommand(OpenDay, CanOpenDay);
}
// Commands
#region LoadDatabaseCommand
public ICommand LoadDatabaseCommand { get; set; }
public void LoadDatabase(object param)
{
if ((!IsDataLoaded) && !(DesignerProperties.IsInDesignTool))
{
LoadData();
}
}
private bool CanLoadDatabase(object param)
{
if (param != null)
{
return !(bool)param;
}
else
{
return false;
}
}
#endregion
#region OpenDayCommand
public ICommand OpenDayCommand { get; set; }
public void OpenDay(object param)
{
try
{
// Set the AttendanceDate
// This will raise the PropertyChanged Behavior
// that will trigger the AddAttendanceToTabControl Behavior
// to add the AttendanceDay.xaml to the Tab control
AttendanceDate = Convert.ToDateTime(param);
this.NotifyPropertyChanged("AttendanceDate");
}
catch
{
// Bad date entered
}
}
private bool CanOpenDay(object param)
{
// Cheing for DesignerProperties.IsInDesignTool
// prevents us from seeing an error in Expression Blend
return (param != null && (!(DesignerProperties.IsInDesignTool)));
}
#endregion
// Operations
#region LoadData
public void LoadData()
{
bool hasKeys = false;
foreach (var item in SterlingService.Current.Database.Query<Student, int>())
{
hasKeys = true;
break;
}
if (!hasKeys)
{
SetupData();
}
}
#endregion
#region SetupData
private void SetupData()
{
// Students
var StudentData = new List<Student>()
{
new Student() { StudentId = 1 , Name = "Student One" },
new Student() { StudentId = 2 , Name = "Student Two" },
new Student() { StudentId = 3 , Name = "Student Three" },
new Student() { StudentId = 4 , Name = "Student Four" },
new Student() { StudentId = 5 , Name = "Student Five" },
new Student() { StudentId = 6 , Name = "Student Six" },
new Student() { StudentId = 7 , Name = "Student Seven" },
new Student() { StudentId = 8 , Name = "Student Eight" },
new Student() { StudentId = 9 , Name = "Student Nine" },
new Student() { StudentId = 10 , Name = "Student Ten" },
};
foreach (var item in StudentData)
{
SterlingService.Current.Database.Save(item);
}
// Enrollments
var EnrollmentData = new List<Enrollment>()
{
new Enrollment() {EnrollmentId = 1, StudentId = 1, StartDate = new DateTime(2010,1,1), StopDate = new DateTime(2010,2,1) },
new Enrollment() {EnrollmentId = 2, StudentId = 2, StartDate = new DateTime(2010,1,1), StopDate = new DateTime(2010,2,11) },
new Enrollment() {EnrollmentId = 3, StudentId = 3, StartDate = new DateTime(2010,1,3), StopDate = new DateTime(2010,2,1) },
new Enrollment() {EnrollmentId = 4, StudentId = 4, StartDate = new DateTime(2010,1,3), StopDate = new DateTime(2010,2,1) },
new Enrollment() {EnrollmentId = 5, StudentId = 5, StartDate = new DateTime(2010,1,10), StopDate = new DateTime(2010,2,15) },
new Enrollment() {EnrollmentId = 6, StudentId = 6, StartDate = new DateTime(2010,1,3), StopDate = new DateTime(2010,2,1) },
new Enrollment() {EnrollmentId = 7, StudentId = 7, StartDate = new DateTime(2010,1,22), StopDate = new DateTime(2010,2,1) },
new Enrollment() {EnrollmentId = 8, StudentId = 8, StartDate = new DateTime(2010,1,1), StopDate = new DateTime(2010,2,1) },
new Enrollment() {EnrollmentId = 9, StudentId = 9, StartDate = new DateTime(2010,1,1), StopDate = new DateTime(2010,2,1) },
new Enrollment() {EnrollmentId = 10, StudentId = 10, StartDate = new DateTime(2010,1,1), StopDate = new DateTime(2010,2,1) }
};
foreach (var item in EnrollmentData)
{
SterlingService.Current.Database.Save(item);
}
// Sample Attendance
SterlingService.Current.Database.Save(new Attendance()
{
AttendanceKey = String.Format("{0}-{1}", "1", new DateTime(2010, 1, 1).Ticks.ToString()),
EnrollmentId = 1,
AttendanceDate = new DateTime(2010, 1, 1),
AttendanceStatus = "P"
});
}
#endregion
// Properties
#region IsDataLoaded
private bool _IsDataLoaded = false;
public bool IsDataLoaded
{
get { return _IsDataLoaded; }
private set
{
if (IsDataLoaded == value)
{
return;
}
_IsDataLoaded = value;
this.NotifyPropertyChanged("IsDataLoaded");
}
}
#endregion
#region AttendanceDate
private DateTime? _AttendanceDate = null;
public DateTime? AttendanceDate
{
get { return _AttendanceDate; }
set
{
if (AttendanceDate == value)
{
return;
}
_AttendanceDate = value;
this.NotifyPropertyChanged("AttendanceDate");
}
}
#endregion
// Utility
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
#endregion
}
}