<file_sep>﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using App1.Models;

namespace App1.Services
{
    class MockBeerDataStore : IDataStore<Beer>
    {
        List<Beer> beers;
        List<Beer> myBeers;

        public MockBeerDataStore()
        {
            myBeers = new List<Beer>();
            beers = new List<Beer>();
            var mockBeers = new List<Beer>
            {
                new Beer
                {
                    Name = "Tyskie",
                    Description =
                        "Gronie to wzorcowy przykład jasnego, pełnego piwa, które od lat cieszy się uznaniem nie tylko w Polsce, ale i na całym świecie."
                }
            };
            foreach (var beer in mockBeers)
            {
                beers.Add(beer);
            }
        }

        public async Task<bool> AddItemAsync(Beer item)
        {
            beers.Add(item);

            return await Task.FromResult(true);
        }

        public async Task<bool> UpdateItemAsync(Beer item)
        {
            var oldItem = beers.Where((Beer arg) => arg.Id == item.Id).FirstOrDefault();
            beers.Remove(oldItem);
            beers.Add(item);

            return await Task.FromResult(true);
        }

        public async Task<bool> DeleteItemAsync(int id)
        {
            var oldItem = beers.Where((Beer arg) => arg.Id == id).FirstOrDefault();
            beers.Remove(oldItem);

            return await Task.FromResult(true);
        }

        public async Task<Beer> GetItemAsync(int id)
        {
            return await Task.FromResult(beers.FirstOrDefault(s => s.Id == id));
        }

        public async Task<IEnumerable<Beer>> GetItemsAsync(bool forceRefresh = false)
        {
            return await Task.FromResult(beers);
        }


        public async Task<IEnumerable<Beer>> GetMyBeersAsync(bool forceRefresh = false)
        {
            return await Task.FromResult(myBeers);
        }


        public async Task<bool> AddMyBeer(Beer beer)
        {
            myBeers.Add(beer);

            return await Task.FromResult(true);
        }
    }
}<file_sep>﻿using System.IO;
using Android.App;
using Android.Content.PM;
using Android.OS;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Environment = System.Environment;

namespace App1.Droid
{
    [Activity(Label = "App1", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true,
        ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            string dbName = "app_db.sqlite";
            string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            string fullpath = Path.Combine(folderPath, dbName);

            base.OnCreate(savedInstanceState);
            Forms.Init(this, savedInstanceState);
            LoadApplication(new App(fullpath));
        }
    }
}<file_sep>﻿using System;
using App1.Models;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace App1.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class NewBeerPage : ContentPage
    {
        public Beer Beer { get; set; }

        public NewBeerPage()
        {
            InitializeComponent();

            Beer = new Beer
            {
                Name = "<NAME>",
                Description = "This is a beer description."
            };
            BindingContext = this;
        }

        async void Save_Clicked(object sender, EventArgs e)
        {
            MessagingCenter.Send(this, "AddItem", Beer);
            await Navigation.PopModalAsync();
        }
    }
}<file_sep>﻿using System;
using App1.Models;
using App1.ViewModels;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace App1.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class BeerDetailPage : ContentPage
    {
        BeerDetailViewModel viewModel;


        public Beer Beer { get; set; }

        public BeerDetailPage(BeerDetailViewModel viewModel)
        {
            InitializeComponent();

            BindingContext = this.viewModel = viewModel;
        }

        public BeerDetailPage()
        {
            InitializeComponent();

            var Beer = new Beer
            {
                Name = "<NAME>",
                Description = "This is an item description."
            };

            viewModel = new BeerDetailViewModel(Beer);
            BindingContext = viewModel;
        }

        async void Add_Clicked(object sender, EventArgs e)
        {
            MessagingCenter.Send(this, "AddToMyBeer", viewModel.Beer);
        }
    }
}<file_sep>﻿using SQLite;

namespace App1.Models
{
    public class Beer
    {
        [PrimaryKey, AutoIncrement] public int Id { get; set; }
        public string Name { get; set; }
        public string Text { get; set; }
        public string Description { get; set; }
        public string Style { get; set; }
        public Brewery Brewery { get; set; }
        public int Rate { get; set; }
    }
}<file_sep>using System;
using App1.Models;
using App1.ViewModels;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace App1.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MyBeersPage : ContentPage
    {
        MyBeerViewModel viewModel;

        public MyBeersPage()
        {
            InitializeComponent();

            BindingContext = viewModel = new MyBeerViewModel();
        }

        async void OnBeerSelected(object sender, SelectedItemChangedEventArgs args)
        {
            var item = args.SelectedItem as Beer;
            if (item == null)
                return;

            await Navigation.PushAsync(new MyBeerDetailPage(new BeerDetailViewModel(item)));

            // Manually deselect item.
            BeersListView.SelectedItem = null;
        }

        async void AddBeer_Clicked(object sender, EventArgs e)
        {
            await Navigation.PushModalAsync(new NavigationPage(new NewBeerPage()));
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();

            if (viewModel.MyBeers.Count == 0)
                viewModel.LoadItemsCommand.Execute(null);
        }
    }
}<file_sep>﻿using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Threading.Tasks;
using App1.Models;
using App1.Views;
using Xamarin.Forms;

namespace App1.ViewModels
{
    public class MyBeerViewModel : BaseViewModel
    {
        public Command LoadItemsCommand { get; set; }
        public ObservableCollection<Beer> MyBeers { get; set; }

        public MyBeerViewModel()
        {
            Title = "MyBeers";
            MyBeers = new ObservableCollection<Beer>();
            LoadItemsCommand = new Command(async () => await ExecuteLoadBeersCommand());

            MessagingCenter.Subscribe(this, "AddToMyBeer",
                (Action<BeerDetailPage, Beer>) (async (obj, item) =>
                {
                    var newItem = item;
                    MyBeers.Add(newItem);
                    await BeerStore.AddMyBeer(newItem);
                }));
        }

        async Task ExecuteLoadBeersCommand()
        {
            if (IsBusy)
            {
                return;
            }

            IsBusy = true;
            try
            {
                MyBeers.Clear();
                var beers = await BeerStore.GetMyBeersAsync(true);
                foreach (var beer in beers)
                {
                    MyBeers.Add(beer);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
            finally
            {
                IsBusy = false;
            }
        }
    }
}<file_sep>﻿using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Threading.Tasks;
using App1.Models;
using App1.Views;
using Xamarin.Forms;

namespace App1.ViewModels
{
    public class BeersViewModel : BaseViewModel
    {
        public ObservableCollection<Beer> Beers { get; set; }
        public Command LoadBeersCommand { get; set; }

        public BeersViewModel()
        {
            Title = "Browse";
            Beers = new ObservableCollection<Beer>();
            LoadBeersCommand = new Command(async () => await ExecuteLoadItemsCommand());

            MessagingCenter.Subscribe(this, "AddItem",
                (Action<NewBeerPage, Beer>) (async (obj, item) =>
                {
                    var newItem = item;
                    Beers.Add(newItem);
                    await BeerStore.AddItemAsync(newItem);
                }));
        }

        async Task ExecuteLoadItemsCommand()
        {
            if (IsBusy)
                return;

            IsBusy = true;

            try
            {
                Beers.Clear();
                var items = await BeerStore.GetItemsAsync(true);
                foreach (var item in items)
                {
                    Beers.Add(item);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
            finally
            {
                IsBusy = false;
            }
        }
    }
}<file_sep>﻿using System.Collections.Generic;
using SQLite;

namespace App1.Models
{
    public class Brewery
    {
        [PrimaryKey, AutoIncrement] public int Id { get; set; }
        public string Text { get; set; }
        public string Description { get; set; }
        public List<Beer> BeerList { get; set; }
    }
}<file_sep>﻿using SQLite;

namespace App1.Models
{
    public class Item
    {
        [PrimaryKey, AutoIncrement] public int Id { get; set; }
        public string Text { get; set; }
        public string Description { get; set; }
    }
}<file_sep>﻿using App1.Models;

namespace App1.ViewModels
{
    public class BeerDetailViewModel : BaseViewModel
    {
        public Beer Beer { get; set; }

        public BeerDetailViewModel(Beer item = null)
        {
            Title = item?.Name;
            Beer = item;
        }
    }
}<file_sep>using App1.ViewModels;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace App1.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MyBeerDetailPage : ContentPage
    {
        BeerDetailViewModel viewModel;


        public MyBeerDetailPage(BeerDetailViewModel viewModel)
        {
            InitializeComponent();
            BindingContext = this.viewModel = viewModel;
        }

//        public MyBeerDetailPage()
//        {
//            InitializeComponent();
//
//            var Beer = new Beer
//            {
//                Name = "<NAME>",
//                Description = "This is an item description."
//            };
//
//            viewModel = new BeerDetailViewModel(Beer);
//            BindingContext = viewModel;
//        }

//        async void Update(object sender, EventArgs e)
//        {
//            MessagingCenter.Send(this, "AddToMyBeer", viewModel.Beer);
//        }
    }
}<file_sep>﻿using System;
using App1.Models;
using App1.ViewModels;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace App1.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class BeersPage : ContentPage
    {
        BeersViewModel viewModel;

        public BeersPage()
        {
            InitializeComponent();

            BindingContext = viewModel = new BeersViewModel();
        }

        async void OnItemSelected(object sender, SelectedItemChangedEventArgs args)
        {
            var beers = args.SelectedItem as Beer;
            if (beers == null)
                return;

            await Navigation.PushAsync(new BeerDetailPage(new BeerDetailViewModel(beers)));

            // Manually deselect item.
            BeersListView.SelectedItem = null;
        }

        async void AddItem_Clicked(object sender, EventArgs e)
        {
            await Navigation.PushModalAsync(new NavigationPage(new NewBeerPage()));
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();

            if (viewModel.Beers.Count == 0)
                viewModel.LoadBeersCommand.Execute(null);
        }
    }
}