public Birthdays() { InitializeComponent(); // Connect to the database and instantiate data context. birthdayDB = new BirthdayBumperContext(BirthdayBumperContext.DBConnectionString); // Data context and observable collection are children of the main page. this.DataContext = this; this.Loaded += Birthdays_Loaded; }
/// <summary> /// Constructor for the Application object. /// </summary> public App() { // Global handler for uncaught exceptions. UnhandledException += Application_UnhandledException; // Standard XAML initialization InitializeComponent(); // Phone-specific initialization InitializePhoneApplication(); // Language display initialization InitializeLanguage(); // Show graphics profiling information while debugging. if (Debugger.IsAttached) { // Display the current frame rate counters. Application.Current.Host.Settings.EnableFrameRateCounter = true; // Show the areas of the app that are being redrawn in each frame. //Application.Current.Host.Settings.EnableRedrawRegions = true; // Enable non-production analysis visualization mode, // which shows areas of a page that are handed off to GPU with a colored overlay. //Application.Current.Host.Settings.EnableCacheVisualization = true; // Prevent the screen from turning off while under the debugger by disabling // the application's idle detection. // Caution:- Use this under debug mode only. Application that disables user idle detection will continue to run // and consume battery power when the user is not using the phone. PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled; } // Create the database if it does not exist. using (BirthdayBumperContext db = new BirthdayBumperContext(BirthdayBumperContext.DBConnectionString)) { if (db.DatabaseExists() == false) { //Create the database db.CreateDatabase(); } } }
private void LoadBirthdays() { BirthdaysList.DataContext = null; UpcomingBirthdays.DataContext = null; FriendData.IsLoading = true; Upcoming.IsLoading = true; using (birthdayDB = new BirthdayBumperContext(BirthdayBumperContext.DBConnectionString)) { string m = DateTime.Now.ToString("MMMM"); string d = DateTime.Now.Day.ToString(); string nextday = DateTime.Now.AddDays(1.0).Day.ToString(); // Define the query to gather all of the to-do items. var friendsInDB = from Friend friend in birthdayDB.Friends where friend.Day == d && friend.Month == m select friend; var upcoming = from Friend friend in birthdayDB.Friends where friend.Day == nextday && friend.Month == m select friend; // Execute the query and place the results into a collection. FriendData.Friends = new ObservableCollection<Friend>(friendsInDB); Upcoming.Friends = new ObservableCollection<Friend>(upcoming); BirthdaysList.DataContext = FriendData.Friends; UpcomingBirthdays.DataContext = Upcoming.Friends; } FriendData.IsLoading = false; Upcoming.IsLoading = false; CheckZeroBirthdays(); }
// Retrieve Birthday information from social networks / contacts and store it in local database. private async Task<bool> GetAndStoreBirthdays() { // Check for Network Connectivity. If not available, then show message and exit. if (!NetworkInterface.GetIsNetworkAvailable()) { MessageBox.Show("No Network Connectivity." + Environment.NewLine + "Please check if you are connected to the Internet."); return false; } List<Friend> friends = new List<Friend>(); #region Google Friends if (GoogleAccount.IsConnected) { List<Friend> g = await FriendData.GetGoogleBirthdays(); if (g != null) friends = friends.Concat(g).ToList<Friend>(); } #endregion #region Facebook Friends if (FacebookAccount.IsConnected) { List<Friend> f = await FriendData.GetFacebookBirthdays(); if (f != null) friends = friends.Concat(f).ToList<Friend>(); } #endregion using (birthdayDB = new BirthdayBumperContext(BirthdayBumperContext.DBConnectionString)) { // Define the query to gather all of the to-do items. var friendsInDB = from Friend friend in birthdayDB.Friends select friend; // Execute the query and place the results into a collection. var dbFriends = new ObservableCollection<Friend>(friendsInDB); birthdayDB.Friends.DeleteAllOnSubmit(dbFriends); birthdayDB.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict); birthdayDB.Friends.InsertAllOnSubmit(friends); birthdayDB.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict); } //bool contactSearch = false; //while(!contactSearch) // contactSearch = await FriendData.GetContactsBirthdays(); return true; }
/// <summary> /// Get Birthdays of Phone Contacts /// </summary> public Task<bool> GetContactsBirthdays() { TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>(); ObservableCollection<Friend> ContactFriends = new ObservableCollection<Friend>(); Contacts cons = new Contacts(); cons.SearchCompleted += (sender, e) => { tcs.SetResult(false); List<Contact> contacts = new List<Contact>(e.Results); if (contacts.Count == 0) { tcs.TrySetResult(true); } using (BirthdayBumperContext db = new BirthdayBumperContext(BirthdayBumperContext.DBConnectionString)) { foreach (var c in contacts) { DateTime d = DateTime.Now.AddDays(-1.0); List<DateTime> birthdays = new List<DateTime>(c.Birthdays); if (birthdays.Count > 0) d = birthdays.First<DateTime>(); try { if (d.Date.Equals(DateTime.Today.Date)) { //BitmapImage img = new BitmapImage(); //img.SetSource(c.GetPicture()); ContactFriend f = new ContactFriend( //c.GetHashCode().ToString(), c.PhoneNumbers.First() != null ? c.PhoneNumbers.First().ToString() : c.GetHashCode().ToString(), c.CompleteName.ToString(), d.Day.ToString(), d.Month.ToString(), d.Year.ToString(), "ContactImage" ); ContactFriends.Add(f); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } } db.Friends.InsertAllOnSubmit(ContactFriends); } tcs.TrySetResult(true); }; cons.SearchAsync(String.Empty, FilterKind.None, "Contacts Sync"); return tcs.Task; }