예제 #1
0
파일: BBooks.cs 프로젝트: xpem/Bookshelf
        public void UpdateBookSituation(string Key, Situation situation, int rate, string comment)
        {
            Book book = new BBooks().GetBook(Key);

            book.LastUpdate     = DateTime.Now;
            book.Situation      = situation;
            book.Rating.Rate    = rate;
            book.Rating.Comment = comment;

            localBooks.UpdateBook(book, User.Id);

            if (CrossConnectivity.Current.IsConnected)
            {
                _ = booksService.UpdateBook(book, User);
            }
        }
예제 #2
0
        /// <summary>
        /// Load local database from user
        /// </summary>
        /// <returns></returns>
        public async static void UpdateLocalDb()
        {
            try
            {
                BUser        bUser        = new BUser();
                BooksService booksService = new BooksService();
                LocalBooks   localBooks   = new LocalBooks();
                BBooks       bBooks       = new BBooks();

                //todo - get user by signin email
                User user             = bUser.GetUserLocal();
                bool ContinuosProcess = true;

                while (ContinuosProcess)
                {
                    //User not logged
                    if (user == null)
                    {
                        break;
                    }

                    Synchronizing = true;

                    if (CrossConnectivity.Current.IsConnected)
                    {
                        DateTime LastUptade = user.LastUpdate;

                        List <Books.Book> booksList = await localBooks.GetBooksLocalByLastUpdate(user.Id, user.LastUpdate);

                        //update fb database
                        foreach (Books.Book book in booksList)
                        {
                            //if the book has a local temporary Guid key, register it in the firebase
                            if (Guid.TryParse(book.BookKey, out Guid localBookId))
                            {
                                //define the key has a null for register the book in firebase
                                book.BookKey = null;

                                (bool success, string res) = await booksService.AddBook(book, user);

                                if (success)
                                {
                                    localBooks.UpdateLocalBookKey(localBookId, res, user.Id);
                                }
                                else
                                {
                                    throw (new Exception($"Não foi possivel sincronizar o livro {book.BookKey}, res: {res}"));
                                }
                            }
                            else
                            {
                                localBooks.UpdateBook(book, user.Id);
                            }
                        }

                        foreach (Books.Book book in await booksService.GetBooksByLastUpdate(user))
                        {
                            localBooks.SyncUpdateBook(book, user.Id);

                            if (LastUptade < book.LastUpdate)
                            {
                                LastUptade = book.LastUpdate;
                            }
                        }
                        bUser.UpdateUserLastUpdateLocal(user.Id, LastUptade);
                    }

                    Synchronizing = false;
                    //in a interval of thre minutes check updates
                    await Task.Delay(180000);
                }
            }
            catch (Exception ex) { throw ex; }
        }