firebase database xaamarin forms trigger code example

Example: firebase database xaamarin forms trigger

public class FirebaseHelper
    {
        public ObservableCollection<ViewProgramModel> _returnedEvents { get; set; }
        public FirebaseHelper()
        {
            _returnedEvents = new ObservableCollection<ViewProgramModel>();
         }

      //  internal ViewProgramModel MyViewProgramModel { get; set; }
        FirebaseClient firebase = new FirebaseClient("https://xxxxxxxxxx.firebaseio.com/");
        private readonly string ChildProgram = "ControllerData/xxxxxx_Pirate_1/Program";
        public static IDisposable returnedEvents;
        public async Task AddViewProgramModel()
        {
            //new ViewProgramModel() { MODE="test", TARGET=122, TRIGGER="122", TRIGGERVALUE=333,  }
            await firebase
              .Child(ChildProgram)
              .PostAsync( new ViewProgramModel() { MODE = "test", TARGET = 122, TRIGGER = "122", TRIGGERVALUE = 333, });
            GetAllData();
        }

        public async Task UpdateViewProgramModel(ViewProgramModel viewProgramModel , string oldValue)
        {
            var toUpdatePerson = (await firebase
                .Child(ChildProgram)
                .OnceAsync<ViewProgramModel>()).FirstOrDefault(a => a.Object.TARGET == Convert.ToInt32( oldValue));

            await firebase
                .Child(ChildProgram)
                .Child(toUpdatePerson.Key)
                .PutAsync(viewProgramModel);
            GetAllData();
        }
        public async Task DeleteViewProgramModel(string mode)
        {
            var toDeletePerson = (await firebase
               .Child(ChildProgram)
               .OnceAsync<ViewProgramModel>()).FirstOrDefault(a => a.Object.MODE == mode);
            await firebase.Child(ChildProgram).Child(toDeletePerson.Key).DeleteAsync();
            GetAllData();

        }

        public async void GetAllData()
        {
            _returnedEvents.Clear();

            var programs = await firebase.Child(ChildProgram).OnceAsync<ViewProgramModel>();
            for (int i = 0; i < programs.Count; i++)
            {
                _returnedEvents.Add(programs.ElementAt(i).Object);
            }
        }
        public async void listenForEvents()
        {
            _returnedEvents.Clear();

            var programs = await firebase.Child(ChildProgram).OnceAsync<ViewProgramModel>();
            for (int i = 0; i < programs.Count; i++)
            {
                _returnedEvents.Add(programs.ElementAt(i).Object);
            }

            //returnedEvents = firebase.Child(ChildProgram).OrderByKey().AsObservable<ViewProgramModel>()
            //    .Subscribe(eventReceived =>
            //    {
            //    if (eventReceived.EventType == Firebase.Database.Streaming.FirebaseEventType.InsertOrUpdate)
            //    {

            //        var found = _returnedEvents.FirstOrDefault(i => i._KEY == eventReceived.Key);

            //        if (found == null)
            //        {
            //            // not in observable collection, add it
            //            _returnedEvents.Add(eventReceived.Object);
            //        }
            //        else
            //        {
            //            // event was updated
            //            int tempIndex = _returnedEvents.IndexOf(found);
            //            _returnedEvents[tempIndex] = eventReceived.Object;
            //        }
            //    }
            //});
        }
    }
}

Tags:

Php Example