void LoadNotes()
        {
            TableView.UserInteractionEnabled = false;
            indicatorView.StartAnimating();
            btnNewNote.Enabled = false;

            UpdateNotesCountLabel();
            Task.Factory.StartNew(LoadNotesAsync);

            async Task LoadNotesAsync()
            {
                InitializeFirestoreReferences();

                try {
                    await GetNotes();
                } catch (NSErrorException ex) {
                    InvokeOnMainThread(() => {
                        UIAlertHelper.ShowMessage("An error has ocurred...", ex.Error.LocalizedDescription, NavigationController, "Ok");
                    });
                } catch (Exception ex) {
                    Console.WriteLine(ex.Message);
                } finally {
                    InvokeOnMainThread(() => {
                        indicatorView.StopAnimating();
                        refreshControl.EndRefreshing();
                        btnNewNote.Enabled = true;
                        TableView.UserInteractionEnabled = true;
                    });
                }
            }
        }
Exemplo n.º 2
0
        async Task <bool> SignIn()
        {
            try {
                User user = await auth.SignInAnonymouslyAsync();

                AppDelegate.UserUid = user.Uid;
                return(true);
            } catch (NSErrorException ex) {
                AuthErrorCode errorCode;
                if (IntPtr.Size == 8)                 // 64 bits devices
                {
                    errorCode = (AuthErrorCode)((long)ex.Error.Code);
                }
                else                 // 32 bits devices
                {
                    errorCode = (AuthErrorCode)((int)ex.Error.Code);
                }

                var title       = "Anonymous sign-in failed!";
                var buttonTitle = "Ok";
                var message     = new StringBuilder();

                // Posible error codes that SignInAnonymously method could throw
                // Visit https://firebase.google.com/docs/auth/ios/errors for more information
                switch (errorCode)
                {
                case AuthErrorCode.OperationNotAllowed:
                    message.AppendLine("The app uses Anonymous sign-in to work correctly and seems to be disabled…");
                    message.AppendLine("Please, go to Firebase console and enable Anonymous login.");
                    message.Append("Open «Application Output» in Xamarin Studio for more information.");
                    buttonTitle = "Ok, let me enable it!";
                    Console.WriteLine("Anonymous sign-in seems to be disabled. Please, visit the following link to know how " +
                                      "to enable it: https://firebase.google.com/docs/auth/ios/anonymous-auth#before-you-begin");
                    break;

                case AuthErrorCode.NetworkError:
                    message.AppendLine("Seems to be the first time that you run the sample and you don't have access to internet.");
                    message.Append("The sample needs to run with internet at least once so you can create an Anonymous user.");
                    break;

                default:
                    message.Append("An unknown error has ocurred…");
                    break;
                }

                InvokeOnMainThread(() => {
                    indicatorView.StopAnimating();
                    UIAlertHelper.ShowMessage(title, message.ToString(), NavigationController, buttonTitle);
                });

                return(false);
            }
        }
        void DataSavedOnFirestore(DocumentSnapshot snapshot, NSError error)
        {
            if (error != null)
            {
                UIAlertHelper.ShowMessage("An error has occurred…", error.LocalizedDescription, NavigationController, "Ok");
                TableView.ReloadData();
                indicatorView.StopAnimating();
                return;
            }

            LoadNotes();
        }
Exemplo n.º 4
0
        // Gets the folder name and verifies that folder name is written correctly
        void btnNewFolder_Clicked(object sender, EventArgs e)
        {
            indicatorView.StartAnimating();

            UIAlertHelper.ShowMessage("Create a new folder",
                                      "Type a folder name.\nName can contain letters, numbers, spaces, dashes and underscores only",
                                      NavigationController,
                                      "Create",
                                      new [] { "Folder name" },
                                      HandleUIAlertControllerTextFieldResult);

            void HandleUIAlertControllerTextFieldResult(bool alertCanceled, string [] textfieldInputs)
            {
                if (alertCanceled)
                {
                    indicatorView.StopAnimating();
                    return;
                }

                var folderName = textfieldInputs [0].Trim();

                if (string.IsNullOrWhiteSpace(folderName))
                {
                    UIAlertHelper.ShowMessage("Empty name!", "A folder name cannot be a space or an empty name.", NavigationController, "Ok");
                    return;
                }

                if (!VerifyFolderName(folderName))
                {
                    UIAlertHelper.ShowMessage("Bad folder name!", "A name can contain letters, numbers, spaces, dashes and underscores only.", NavigationController, "Ok");
                    return;
                }

                if (VerifyIfFolderExists(folderName))
                {
                    UIAlertHelper.ShowMessage("Folder exists!", "A folder with that name already exists.", NavigationController, "Ok");
                    return;
                }

                Task.Factory.StartNew(async() => {
                    try {
                        await CreateFolder(folderName);
                    } catch (NSErrorException ex) {
                        InvokeOnMainThread(() => {
                            UIAlertHelper.ShowMessage("Error while creating new folder", ex.Error.LocalizedDescription, NavigationController, "Ok");
                        });
                    } finally {
                        InvokeOnMainThread(() => indicatorView.StopAnimating());
                    }
                });
            }
        }
Exemplo n.º 5
0
        public override void CommitEditingStyle(UITableView tableView, UITableViewCellEditingStyle editingStyle, NSIndexPath indexPath)
        {
            indicatorView.StartAnimating();

            if (editingStyle != UITableViewCellEditingStyle.Delete)
            {
                return;
            }

            // Delete the folder from the TableView so user cannot get access to it while deleting
            var folder = folders [indexPath.Row];

            folders.Remove(folder);

            if (folders.Count == 0)
            {
                tableView.ReloadRows(new [] { indexPath }, UITableViewRowAnimation.Automatic);
            }
            else
            {
                tableView.DeleteRows(new [] { indexPath }, UITableViewRowAnimation.Automatic);
            }

            Task.Factory.StartNew(async() => {
                try {
                    // Remove the folder from the Cloud Firestore
                    await DeleteFolder(folder);
                } catch (NSErrorException ex) {
                    // If something fails while deleting the folder, add it again to the TableView
                    folders.Insert(indexPath.Row, folder);
                    InvokeOnMainThread(() => {
                        UIAlertHelper.ShowMessage("Error while deleting the folder", ex.Error.LocalizedDescription, NavigationController, "Ok", () => {
                            // Due subcollections (in this case, the folder's notes) must be manually deleted,
                            // some of them could have been deleted when we were trying to delete the folder
                            UIAlertHelper.ShowMessage("Error while deleting the folder", "Some notes could have been deleted during the process.\nTry deleting the folder again to finish the process.", NavigationController, "Ok");
                        });

                        if (folders.Count == 1)
                        {
                            tableView.ReloadRows(new [] { indexPath }, UITableViewRowAnimation.Automatic);
                        }
                        else
                        {
                            tableView.InsertRows(new [] { indexPath }, UITableViewRowAnimation.Automatic);
                        }
                    });
                } finally {
                    folder = null;
                    InvokeOnMainThread(() => indicatorView.StopAnimating());
                }
            });
        }
        public override void CommitEditingStyle(UITableView tableView, UITableViewCellEditingStyle editingStyle, NSIndexPath indexPath)
        {
            indicatorView.StartAnimating();

            if (editingStyle != UITableViewCellEditingStyle.Delete)
            {
                return;
            }

            // Delete the note from the TableView so user cannot get access to it while deleting
            var note = notes [indexPath.Row];

            notes.Remove(note);

            if (notes.Count == 0)
            {
                tableView.ReloadRows(new [] { indexPath }, UITableViewRowAnimation.Automatic);
            }
            else
            {
                tableView.DeleteRows(new [] { indexPath }, UITableViewRowAnimation.Automatic);
            }

            UpdateNotesCountLabel();

            Task.Factory.StartNew(async() => {
                try {
                    // Remove the note from the Cloud Firestore
                    await DeleteNote(note);
                } catch (NSErrorException ex) {
                    // If something fails while deleting the note, add it again to the TableView
                    notes.Insert(indexPath.Row, note);
                    InvokeOnMainThread(() => {
                        UIAlertHelper.ShowMessage("Error while deleting the note", ex.Error.LocalizedDescription, NavigationController, "Ok");

                        if (notes.Count == 1)
                        {
                            tableView.ReloadRows(new [] { indexPath }, UITableViewRowAnimation.Automatic);
                        }
                        else
                        {
                            tableView.InsertRows(new [] { indexPath }, UITableViewRowAnimation.Automatic);
                        }

                        UpdateNotesCountLabel();
                    });
                } finally {
                    note = null;
                    InvokeOnMainThread(() => indicatorView.StopAnimating());
                }
            });
        }
Exemplo n.º 7
0
        void LoadFolders()
        {
            indicatorView.StartAnimating();
            btnNewFolder.Enabled = false;
            Task.Factory.StartNew(LoadFoldersAsync);

            async Task LoadFoldersAsync()
            {
                // Verify if you have already logged in to Firebase Anonymously
                if (string.IsNullOrWhiteSpace(AppDelegate.UserUid))
                {
                    if (!await SignIn())
                    {
                        return;
                    }
                }

                InitializeFirestoreReferences();

                try {
                    await AddUserToFirestoreIfNeeded();
                    await GetFolders();
                } catch (NSErrorException ex) {
                    InvokeOnMainThread(() => {
                        UIAlertHelper.ShowMessage("An error has ocurred...", ex.Error.LocalizedDescription, NavigationController, "Ok");
                    });
                } catch (Exception ex) {
                    Console.WriteLine(ex.Message);
                } finally {
                    InvokeOnMainThread(() => {
                        indicatorView.StopAnimating();
                        refreshControl.EndRefreshing();
                        btnNewFolder.Enabled             = true;
                        TableView.UserInteractionEnabled = true;
                    });
                }
            }
        }