Example #1
0
        public async void SavePerson(Model.Person person)
        {
            try
            {
                using (var db = new Model.PeopleDbContext(dbFilePath))
                {
                    db.People.Add(person);
                    var saveResult = await db.SaveChangesAsync();

                    if (saveResult > 0)
                    {
                        people.Add(person);
                        peopleAdapter.NotifyItemChanged(people.IndexOf(person));

                        Toast.MakeText(this, "Person added successfully.", ToastLength.Short).Show();
                    }
                }
            }
            catch (System.Exception exception)
            {
                throw;
            }
        }
Example #2
0
        protected override async void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);

            people             = new List <Model.Person>();
            firstNameEditText  = FindViewById <EditText>(Resource.Id.firstNameEditText);
            lastNameEditText   = FindViewById <EditText>(Resource.Id.lastNameEditText);
            saveButton         = FindViewById <Button>(Resource.Id.saveButton);
            peopleRecyclerView = FindViewById <RecyclerView>(Resource.Id.peopleRecyclerView);
            peopleRecyclerView.HasFixedSize = true;
            saveButton.SetOnClickListener(this);

            dbFilePath = System.IO.Path
                         .Combine($"{System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)}", "People.db");

            try
            {
                using (var db = new Model.PeopleDbContext(dbFilePath))
                {
                    await db.Database.MigrateAsync();

                    people.AddRange(await db.People.ToListAsync());

                    layoutManager = new LinearLayoutManager(this);
                    peopleRecyclerView.SetLayoutManager(layoutManager);

                    peopleAdapter = new PeopleAdapter(this);
                    peopleRecyclerView.SetAdapter(peopleAdapter);
                }
            }
            catch (System.Exception exception)
            {
            }
        }