Share via

Notes doosent read from firebase

Eduardo Gomez 4,316 Reputation points
2022-02-12T09:00:45.587+00:00

I took another approach, instead of initialize a Observable in the MainPage, I do it in the FirebaseServces

FirebaseServices

 public class FirebaseServices {  
  
        private ObservableCollection<NotebookNote> FireBaseNotebooks { get; set; }  
        private readonly FirebaseAuthProvider AuthProvider;  
        private readonly FirebaseClient Client;  
        const string BASE_URL = "https://duonotes-f2b77-default-rtdb.europe-west1.firebasedatabase.app/";  
        public FirebaseServices() {  
  
            FireBaseNotebooks = new ObservableCollection<NotebookNote>();  
            AuthProvider = new FirebaseAuthProvider(new FirebaseConfig(AppConstant.WEB_API_KEY));  
            Client = new FirebaseClient(BASE_URL);  
        }  

Read Method

    public async Task<ObservableCollection<NotebookNote>> ReadAsync(string ChildName, string NotebookId = "") {  
  
            var list = await Client.Child(ChildName)  
                 .OnceAsync<NotebookNote>();  
  
            var collection = new List<NotebookNote>();  
  
            foreach (var item in list) {  
                NotebookNote notebookNote = null;  
                notebookNote = Convert(ChildName, item);  
                collection.Add(notebookNote);  
            }  
  
            if (ChildName.Equals(AppConstant.Notes)) {  
                collection = collection.Where(n => ((Note)n).NotebookId == NotebookId).ToList();  
            } else {  
                collection = collection.Where(n => n.UserID == Preferences.Get(AppConstant.UserID, string.Empty)).ToList();  
            }  
            FireBaseNotebooks.Clear();  
            foreach (var element in collection) {  
                FireBaseNotebooks.Add(element);  
            }  
  
            return FireBaseNotebooks;  
        }  

Now if I select a notebook, this code will execute

MainPageModel

        public async virtual void SeletedItemActionAsync() {  
  
            if (SelectedNotebook != null) {  
  
                NotesPage notesPage = new NotesPage();  
                await Application.Current.MainPage.Navigation.PushAsync(notesPage);  
                var viewModel = notesPage.BindingContext as NotesPageModel;  
                viewModel.NotebookAction(SelectedNotebook.Id);  
                SelectedNotebook = null;  
            }  

NotesPage

  public Action<string> NotebookAction { get; set; }  
  
        public string NotebookId { get; set; }  
  
        public Note SeletedNote { get; set; }  
  
        public NotesPageModel() {  
            FabAnimationCommmand = new Command<Frame>(AnimateButtonCommand);  
  
            NotebookAction = async (id) => {  
  
                NotebookId = id;  
  
                if (!string.IsNullOrEmpty(NotebookId)) {  
  
                    Console.WriteLine($"this is te notebookID I am reciving:  {NotebookId} So I a supose to read");  
                   await App.FirebaseServices.ReadAsync(AppConstant.Notes, NotebookId);  
                }  
            };  
        }  

and every time I navigate to this page, I get the NoteookID, and I am suppose to read

174211-screenshot-2022-02-14-174941.png

https://github.com/eduardoagr/DuoNotes

Developer technologies | .NET | Xamarin
0 comments No comments

Answer accepted by question author

JarvanZhang 23,971 Reputation points
2022-02-14T06:16:07.707+00:00

Hello,​

Welcome to our Microsoft Q&A platform!

My suspicions are that the empty contractor doesn't initialize an ObservableCollection

Because the 'NotebookAction' will not be called automatically, the action is just initialized in the constructor method of 'NotesPageModel' class. To exectue the action, you need to call action.Invoke(...) command. You could try using 'PageAppearCommand' instead as the 'MainPage' did.

Here is the sample code, you could refer to it.

   public class NotesPageModel : MainPageModel  
   {  
       public ICommand PageAppearCommand { get; set; }  
     
       public NotesPageModel()  
       {  
           FabAnimationCommmand = new Command<Frame>(AnimateButtonCommand);  
     
           PageAppearCommand = new Command(NotePageAppearAction);  
       }  
       private void NotePageAppearAction(object obj)  
       {  
           App.FirebaseServices.ReadAsync(AppConstant.Notes, Application.Current.Properties["notebook_id"] as string);  
       }  
       ...  
   }  

NotesPage.xaml

   <ContentPage x:Class="DuoNotes.View.NotesPage" ...>  
     
       <ContentPage.BindingContext>  
           <vm:NotesPageModel />  
       </ContentPage.BindingContext>  
     
       <ContentPage.Behaviors>  
           <community:EventToCommandBehavior Command="{Binding PageAppearCommand}" EventName="Appearing" />  
       </ContentPage.Behaviors>  
       ...  
   </ContentPage>  

Best Regards,

Jarvan Zhang


If the response is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.