Ejemplo n.º 1
0
        /// <summary>
        /// Subscribes to a quiz given the ID
        /// </summary>
        /// <param name="dbId">the ID of the quiz to sub to</param>
        /// <param name="quizzesSearched">the quizzes currently displayed (used to get info about the quiz from the dbid)</param>
        /// <returns>If the subscription was successful</returns>
        public static async Task <OperationReturnMessage> SubscribeToQuizAsync(string dbId, List <QuizInfo> quizzesSearched)
        {
            if (QuizRosterDatabase.GetQuizInfo(dbId) == null) // make sure it isn't in yet
            {
                OperationReturnMessage returnMessage = await Task.Run(async() => await ServerOperations.SubscribeToQuiz(dbId));

                if (returnMessage == OperationReturnMessage.True)
                {
                    QuizInfo quiz             = quizzesSearched.Where(quizInfo => quizInfo.DBId == dbId).First();
                    string   lastModifiedDate = await Task.Run(() => ServerOperations.GetLastModifiedDate(dbId));

                    QuizInfo newInfo = new QuizInfo
                    {
                        DBId             = quiz.DBId,
                        AuthorName       = quiz.AuthorName,
                        QuizName         = quiz.QuizName,
                        Category         = quiz.Category,
                        LastModifiedDate = lastModifiedDate,
                        SyncStatus       = (int)SyncStatusEnum.NotDownloadedAndNeedDownload // 4 to represent not present in local directory and need download
                    };
                    QuizRosterDatabase.SaveQuizInfo(newInfo);
                    return(returnMessage);
                }
                else if (returnMessage == OperationReturnMessage.FalseInvalidCredentials)
                {
                    CredentialManager.IsLoggedIn = false;
                    return(returnMessage);
                }
                else
                {
                    return(returnMessage);
                }
            }
            else
            {
                return(OperationReturnMessage.False);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Updates the local database by syncing with the server
        /// </summary>
        /// <returns>an awaitable task for the completion of syncing</returns>
        public static async Task UpdateLocalDatabaseAsync()
        {
            if (App.Path != null && App.UserPath.Length > 2)
            {
                RealmConfiguration threadConfig = App.realmConfiguration(RosterPath);
                Realm threadInstance            = Realm.GetInstance(threadConfig);

                List <QuizInfo> QuizInfos = new List <QuizInfo>(threadInstance.All <QuizInfo>());
                if (CrossConnectivity.Current.IsConnected)
                {
                    for (int i = 0; i < QuizInfos.Count(); i++)
                    {
                        if (CredentialManager.IsLoggedIn)
                        {
                            if (QuizInfos[i].IsDeletedLocally)
                            {
                                if (await ServerOperations.DeleteQuiz(QuizInfos[i].DBId) == OperationReturnMessage.True)
                                {
                                    string toDeleteDBId = QuizInfos[i].DBId;
                                    QuizInfos.Remove(QuizInfos[i]);
                                    DeleteQuizInfo(toDeleteDBId);
                                }
                            }
                            else if (QuizInfos[i].SyncStatus != (int)SyncStatusEnum.NotDownloadedAndNeedDownload)
                            {
                                string lastModifiedDate = ServerOperations.GetLastModifiedDate(QuizInfos[i].DBId);
                                if (lastModifiedDate == "") // returns empty string could not reach server
                                {
                                    QuizInfo copy = new QuizInfo(QuizInfos[i])
                                    {
                                        SyncStatus = (int)SyncStatusEnum.Offline // 3 represents offline
                                    };
                                    EditQuizInfo(copy, threadInstance);
                                }
                                else
                                {
                                    // Server returns "false" if quiz is not already on the server
                                    if (lastModifiedDate == "false" || lastModifiedDate == null)
                                    {
                                        QuizInfo copy = new QuizInfo(QuizInfos[i])
                                        {
                                            SyncStatus = (int)SyncStatusEnum.NeedUpload // 1 represents need upload
                                        };
                                        EditQuizInfo(copy, threadInstance);
                                    }
                                    else
                                    {
                                        DateTime localModifiedDateTime  = Convert.ToDateTime(QuizInfos[i].LastModifiedDate);
                                        DateTime serverModifiedDateTime = Convert.ToDateTime(lastModifiedDate);
                                        if (localModifiedDateTime > serverModifiedDateTime)
                                        {
                                            QuizInfo copy = new QuizInfo(QuizInfos[i])
                                            {
                                                SyncStatus = (int)SyncStatusEnum.NeedUpload // 1 represents need upload
                                            };
                                            EditQuizInfo(copy, threadInstance);
                                        }
                                        else if (localModifiedDateTime < serverModifiedDateTime)
                                        {
                                            QuizInfo copy = new QuizInfo(QuizInfos[i])
                                            {
                                                SyncStatus = (int)SyncStatusEnum.NeedDownload // 0 represents needs download
                                            };
                                            EditQuizInfo(copy, threadInstance);
                                        }
                                        else if (localModifiedDateTime == serverModifiedDateTime)
                                        {
                                            QuizInfo copy = new QuizInfo(QuizInfos[i])
                                            {
                                                SyncStatus = (int)SyncStatusEnum.Synced // 2 represents in sync
                                            };
                                            EditQuizInfo(copy, threadInstance);
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            QuizInfo copy = new QuizInfo(QuizInfos[i])
                            {
                                SyncStatus = (int)SyncStatusEnum.Synced // 2 represents in sync
                            };
                            EditQuizInfo(copy, threadInstance);
                        }
                    }

                    string[] dbIds = new string[QuizInfos.Count];
                    for (int i = 0; i < dbIds.Length; i++)
                    {
                        dbIds[i] = QuizInfos[i].DBId;
                    }

                    List <string[]> missingQuizs = ServerOperations.GetMissingQuizzesByAuthorName(CredentialManager.Username, dbIds);
                    foreach (string[] missingQuiz in missingQuizs)
                    {
                        QuizInfo info = new QuizInfo
                        {
                            DBId             = missingQuiz[0],
                            AuthorName       = missingQuiz[1],
                            QuizName         = missingQuiz[2],
                            Category         = missingQuiz[3],
                            LastModifiedDate = missingQuiz[4],
                            SubscriberCount  = int.Parse(missingQuiz[5]),
                            SyncStatus       = (int)SyncStatusEnum.NotDownloadedAndNeedDownload
                        };
                        threadInstance.Write(() =>
                        {
                            threadInstance.Add(info);
                        });
                    }
                }
                else
                {
                    for (int i = 0; i < QuizInfos.Count; i++)
                    {
                        QuizInfo copy = new QuizInfo(QuizInfos[i])
                        {
                            SyncStatus = (int)SyncStatusEnum.Offline // 3 represents offline
                        };
                        EditQuizInfo(copy, threadInstance);
                    }
                }
            }
        }