public AsyncTask LoadActivityStates(string studentId, IEnumerable <string> activityIds)
        {
            return(new AsyncTask((prevResult, onCompleted, onError) =>
            {
                try
                {
                    Uri serverUri = new Uri(appSettings.ServerURI, UriKind.RelativeOrAbsolute);
                    Uri restUri = new Uri(serverUri, "rest/");

                    ActivityStateRepository repo = new ActivityStateRepository(restUri);
                    if (repo == null)
                    {
                        throw new Exception("ActivityStateRepository is not initialized.");
                    }

                    repo.GetActivityStates(studentId, activityIds, (ActivityStateRepository.Response response) =>
                    {
                        if (response.Success)
                        {
                            onCompleted(response.Items);
                        }
                        else
                        {
                            onError(new Exception(response.Error));
                        }
                    });
                }
                catch (Exception e)
                {
                    onError(e);
                }
            }));
        }
        public AsyncTask SaveActivityState(ActivityState activityState)
        {
            Contract.ArgumentNotNull("activityState", activityState);

            return(new AsyncTask((prevResult, onCompleted, onError) =>
            {
                try
                {
                    Uri serverUri = new Uri(appSettings.ServerURI, UriKind.RelativeOrAbsolute);
                    Uri restUri = new Uri(serverUri, "rest/");

                    ActivityStateRepository repo = new ActivityStateRepository(restUri);
                    if (repo == null)
                    {
                        throw new Exception("ActivityStateRepository is not initialized.");
                    }

                    // Load ActivityState (if it exists)
                    repo.Update(activityState, (ActivityStateRepository.Response response) =>
                    {
                        try
                        {
                            if (response.Success)
                            {
                                onCompleted(response.Item);
                            }
                            else
                            {
                                onError(new Exception(response.Error));
                            }
                        }
                        catch (Exception e)
                        {
                            onError(e);
                        }
                    });
                }
                catch (Exception e)
                {
                    onError(e);
                }
            }));
        }
        public AsyncTask LoadActivityState(string studentId, string activityId, bool createNewIfDoesNotExit = false)
        {
            return(new AsyncTask((prevResult, onCompleted, onError) =>
            {
                try
                {
                    Uri serverUri = new Uri(appSettings.ServerURI, UriKind.RelativeOrAbsolute);
                    Uri restUri = new Uri(serverUri, "rest/");

                    ActivityStateRepository repo = new ActivityStateRepository(restUri);
                    if (repo == null)
                    {
                        throw new Exception("ActivityStateRepository is not initialized.");
                    }

                    // Load ActivityState (if it exists)
                    repo.GetActivityStates(studentId, new string[] { activityId }, (ActivityStateRepository.Response loadResponse) =>
                    {
                        try
                        {
                            if (loadResponse.Success)
                            {
                                if (loadResponse.Items != null && loadResponse.Items.Count > 0)
                                {
                                    onCompleted(loadResponse.Items.OrderByDescending(s => s.ModifiedDate).First());
                                }
                                else
                                {
                                    // Create ActivityState
                                    ActivityState newState = new ActivityState()
                                    {
                                        ActivityId = activityId,
                                        StudentId = studentId
                                    };
                                    repo.Insert(newState, (ActivityStateRepository.Response insertResponse) =>
                                    {
                                        if (insertResponse.Success)
                                        {
                                            onCompleted(insertResponse.Item);
                                        }
                                        else
                                        {
                                            onError(new Exception(insertResponse.Error));
                                        }
                                    });
                                }
                            }
                            else
                            {
                                onError(new Exception(loadResponse.Error));
                            }
                        }
                        catch (Exception e)
                        {
                            onError(e);
                        }
                    });
                }
                catch (Exception e)
                {
                    onError(e);
                }
            }));
        }