public void LocalRefresh(bool refreshLeagues = true) { if (Athlete != null) { Athlete.LocalRefresh(); } if (League != null && refreshLeagues) { League.LocalRefresh(); } }
/// <summary> /// This app uses Azure as the backend which utilizes Notifications hubs /// </summary> /// <returns>The athlete notification hub registration.</returns> public Task UpdateAthleteNotificationHubRegistration(Athlete athlete, bool forceSave = false, bool sendTestPush = false) { return(new Task(() => { if (athlete == null) { throw new ArgumentNullException("athlete"); } if (athlete.Id == null || athlete.DeviceToken == null) { return; } var tags = new List <string> { App.CurrentAthlete.Id, "All", }; App.CurrentAthlete.LocalRefresh(); App.CurrentAthlete.Memberships.Select(m => m.LeagueId).ToList().ForEach(tags.Add); athlete.DevicePlatform = Xamarin.Forms.Device.OS.ToString(); var reg = new DeviceRegistration { Handle = athlete.DeviceToken, Platform = athlete.DevicePlatform, Tags = tags.ToArray() }; var registrationId = Client.InvokeApiAsync <DeviceRegistration, string>("registerWithHub", reg, HttpMethod.Put, null).Result; athlete.NotificationRegistrationId = registrationId; //Used to verify the device is successfully registered with the backend if (sendTestPush) { var qs = new Dictionary <string, string>(); qs.Add("athleteId", athlete.Id); Client.InvokeApiAsync("sendTestPushNotification", null, HttpMethod.Get, qs).Wait(); } if (athlete.IsDirty || forceSave) { var task = SaveAthlete(athlete); task.Start(); task.Wait(); } })); }
/// <summary> /// Registers an athlete with the backend and returns the new athlete profile /// </summary> async Task <Athlete> RegisterAthlete(UserProfile profile) { AuthenticationStatus = "Registering athlete"; var athlete = new Athlete(profile); var task = AzureService.Instance.SaveAthlete(athlete); await RunSafe(task); if (task.IsCompleted && task.IsFaulted) { return(null); } "You're now an officially registered athlete!".ToToast(); return(athlete); }
public static string GetChallengeConflictReason(this Membership membership, Athlete athlete) { if (!membership.League.HasStarted) { return("The league hasn't started yet"); } if (membership.Athlete == null || athlete.Id == membership.Athlete.Id) { return("You cannot challenge yourself"); } //Check to see if they are part of the same league var otherMembership = athlete.Memberships.SingleOrDefault(m => m.LeagueId == membership.LeagueId); if (otherMembership != null) { //Ensure they are within range and lower in rank than the challengee var diff = otherMembership.CurrentRank - membership.CurrentRank; if (diff <= 0 || diff > membership.League.MaxChallengeRange) { return("{0} is not within a valid range of being challenged".Fmt(membership.Athlete.Alias)); } } else { return("{0} is not a member of the {1} league".Fmt(membership.Athlete.Alias, membership.League.Name)); } var challenge = membership.GetOngoingChallenge(membership.Athlete); if (challenge != null) { return("{0} already has an ongoing challenge with {1}".Fmt(membership.Athlete.Alias, challenge.Opponent(membership.Athlete.Id).Alias)); } //Athlete is within range but let's make sure there aren't already challenges out there challenge = membership.GetOngoingChallenge(athlete); if (challenge != null) { var player = athlete.Id == App.CurrentAthlete.Id ? "You already have" : athlete.Alias + " already has"; return("{0} an ongoing challenge with {1}".Fmt(player, challenge.Opponent(athlete.Id).Alias)); } return(null); }
/// <summary> /// Gets the athlete's profile from the Azure backend /// </summary> async Task <Athlete> GetAthletesProfile() { Athlete athlete = null; //Let's try to load based on email address if (athlete == null && AuthUserProfile != null && !AuthUserProfile.Email.IsEmpty()) { var task = AzureService.Instance.GetAthleteByEmail(AuthUserProfile.Email); await RunSafe(task); if (task.IsCompleted && !task.IsFaulted) { athlete = task.Result; } } return(athlete); }
public Task UnregisterAthleteForPush(Athlete athlete) { return(new Task(() => { if (athlete == null || athlete.NotificationRegistrationId == null) { return; } var values = new Dictionary <string, string> { { "id", athlete.NotificationRegistrationId } }; var registrationId = Client.InvokeApiAsync <string>("unregister", HttpMethod.Delete, values).Result; })); }
public Task SaveAthlete(Athlete athlete) { return(new Task(() => { athlete.UserId = AzureService.Instance.Client.CurrentUser.UserId; if (athlete.Id == null) { Client.GetTable <Athlete>().InsertAsync(athlete).Wait(); } else { Client.GetTable <Athlete>().UpdateAsync(athlete).Wait(); } DataManager.Instance.Athletes.AddOrUpdate(athlete); })); }
async public Task GetLeagues(bool forceRefresh = false) { if (_hasLoadedLeaguesBefore && !forceRefresh) { Athlete.LocalRefresh(); return; } using (new Busy(this)) { await AthleteViewModel.GetLeagues(forceRefresh); LocalRefresh(); //Settings.Instance.LeagueColors.Clear(); DataManager.Instance.Leagues.Values.ToList().EnsureLeaguesThemed(); } _hasLoadedLeaguesBefore = true; }
public Task SaveAthlete(Athlete athlete) { return(new Task(() => { if (athlete.Id == null) { if (athlete.Email == "*****@*****.**") { athlete.IsAdmin = true; } Client.GetTable <Athlete>().InsertAsync(athlete).Wait(); } else { Client.GetTable <Athlete>().UpdateAsync(athlete).Wait(); } DataManager.Instance.Athletes.AddOrUpdate(athlete); })); }
public Task <Athlete> GetAthleteById(string id, bool force = false) { return(new Task <Athlete>(() => { Athlete a = null; if (!force) { DataManager.Instance.Athletes.TryGetValue(id, out a); } a = a ?? Client.GetTable <Athlete>().LookupAsync(id).Result; if (a != null) { a.IsDirty = false; DataManager.Instance.Athletes.AddOrUpdate(a); } return a; })); }
public void CreateChallenge(Athlete challenger, Athlete challengee, League league) { var time = TimeSpan.FromTicks(DateTime.Now.AddMinutes(60).Subtract(DateTime.Today).Ticks); if (time.Ticks > TimeSpan.TicksPerDay) { time = time.Subtract(TimeSpan.FromTicks(TimeSpan.TicksPerDay)); } SelectedTime = time; SelectedDate = DateTime.Today; var membership = league.Memberships.SingleOrDefault(m => m.AthleteId == challengee.Id); Challenge = new Challenge { BattleForRank = membership.CurrentRank, ChallengerAthleteId = challenger.Id, ChallengeeAthleteId = challengee.Id, ProposedTime = SelectedDateTime, LeagueId = league.Id, }; }
public bool HasExistingChallengeWithAthlete(Athlete athlete) { return(GetOngoingChallenge(athlete) != null); }
public AthleteEditViewModel(Athlete athlete = null) { Athlete = athlete ?? new Athlete(); }
public AthleteEditViewModel() { Athlete = new Athlete(); }
public override void NotifyPropertiesChanged() { _athlete = null; base.NotifyPropertiesChanged(); }
public static bool CanChallengeAthlete(this Membership membership, Athlete athlete) { return(membership.GetChallengeConflictReason(athlete) == null); }