Skip to content
This repository has been archived by the owner on Dec 5, 2019. It is now read-only.

Commit

Permalink
DataStore abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
wojta committed Mar 14, 2018
1 parent 5bdb183 commit 2deafa2
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 92 deletions.
8 changes: 8 additions & 0 deletions Example/Example/Models/HasId.cs
@@ -0,0 +1,8 @@
using System;
namespace Example.Models
{
public interface HasId
{
string Id { get; set; }
}
}
4 changes: 2 additions & 2 deletions Example/Example/Models/NavItem.cs
Expand Up @@ -2,9 +2,9 @@

namespace Example.Models
{
public class Item
public class NavItem:HasId
{
public string Id { get; set; }
public string Id { get; set; }
public string Text { get; set; }
public string Description { get; set; }
}
Expand Down
50 changes: 50 additions & 0 deletions Example/Example/Services/BaseDataStore.cs
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;

namespace Example.Services
{
public partial class BaseDataStore<T> : IDataStore<T> where T : Models.HasId
{
protected List<T> items;

public BaseDataStore()
{
}

public async Task<bool> AddItemAsync(T item)
{
items.Add(item);
return await Task.FromResult(true);
}

public async Task<bool> DeleteItemAsync(T item)
{
var _item = items.AsQueryable().Where((T arg) => arg.Id == item.Id).FirstOrDefault();
items.Remove(_item);

return await Task.FromResult(true);
}

public async Task<T> GetItemAsync(string id)
{
return await Task.FromResult(items.FirstOrDefault(s => s.Id == id));
}

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

public async Task<bool> UpdateItemAsync(T item)
{
var _item = items.Where<T>((T arg) => arg.Id == item.Id).FirstOrDefault();
items.Remove(_item);
items.Add(item);

return await Task.FromResult(true);
}
}
}
68 changes: 0 additions & 68 deletions Example/Example/Services/MockDataStore.cs

This file was deleted.

30 changes: 30 additions & 0 deletions Example/Example/Services/NavigationDataStore.cs
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using Example.Models;

[assembly: Xamarin.Forms.Dependency(typeof(Example.Services.NavigationDataStore))]
namespace Example.Services
{
public class NavigationDataStore : BaseDataStore<NavItem>
{

public NavigationDataStore()
{
items = new List<NavItem>();
var mockItems = new List<NavItem>
{
new NavItem { Id = Guid.NewGuid().ToString(), Text = "First item", Description="This is an item description." },
new NavItem { Id = Guid.NewGuid().ToString(), Text = "Second item", Description="This is an item description." },
new NavItem { Id = Guid.NewGuid().ToString(), Text = "Third item", Description="This is an item description." },
new NavItem { Id = Guid.NewGuid().ToString(), Text = "Fourth item", Description="This is an item description." },
new NavItem { Id = Guid.NewGuid().ToString(), Text = "Fifth item", Description="This is an item description." },
new NavItem { Id = Guid.NewGuid().ToString(), Text = "Sixth item", Description="This is an item description." },
};

mockItems.ForEach(item => items.Add(item));
}
}
}
2 changes: 1 addition & 1 deletion Example/Example/ViewModels/AboutViewModel.cs
Expand Up @@ -5,7 +5,7 @@

namespace Example.ViewModels
{
public class AboutViewModel : BaseViewModel
public class AboutViewModel : BaseViewModel<Object>
{
public AboutViewModel()
{
Expand Down
4 changes: 2 additions & 2 deletions Example/Example/ViewModels/BaseViewModel.cs
Expand Up @@ -10,9 +10,9 @@

namespace Example.ViewModels
{
public class BaseViewModel : INotifyPropertyChanged
public class BaseViewModel<It> : INotifyPropertyChanged
{
public IDataStore<Item> DataStore => DependencyService.Get<IDataStore<Item>>() ?? new MockDataStore();
public IDataStore<It> DataStore => DependencyService.Get<IDataStore<It>>();

bool isBusy = false;
public bool IsBusy
Expand Down
6 changes: 3 additions & 3 deletions Example/Example/ViewModels/NavItemDetailVM.cs
Expand Up @@ -4,10 +4,10 @@

namespace Example.ViewModels
{
public class ItemDetailViewModel : BaseViewModel
public class NavItemDetailVM : BaseViewModel<NavItem>
{
public Item Item { get; set; }
public ItemDetailViewModel(Item item = null)
public NavItem Item { get; set; }
public NavItemDetailVM(NavItem item = null)
{
Title = item?.Text;
Item = item;
Expand Down
12 changes: 6 additions & 6 deletions Example/Example/ViewModels/NavItemVM.cs
Expand Up @@ -10,20 +10,20 @@

namespace Example.ViewModels
{
public class ItemsViewModel : BaseViewModel
public class NavItemVM : BaseViewModel<NavItem>
{
public ObservableCollection<Item> Items { get; set; }
public ObservableCollection<NavItem> Items { get; set; }
public Command LoadItemsCommand { get; set; }

public ItemsViewModel()
public NavItemVM()
{
Title = "Browse";
Items = new ObservableCollection<Item>();
Items = new ObservableCollection<NavItem>();
LoadItemsCommand = new Command(async () => await ExecuteLoadItemsCommand());

MessagingCenter.Subscribe<NewItemPage, Item>(this, "AddItem", async (obj, item) =>
MessagingCenter.Subscribe<NewItemPage, NavItem>(this, "AddItem", async (obj, item) =>
{
var _item = item as Item;
var _item = item as NavItem;
Items.Add(_item);
await DataStore.AddItemAsync(_item);
});
Expand Down
8 changes: 4 additions & 4 deletions Example/Example/Views/ItemDetailPage.xaml.cs
Expand Up @@ -11,9 +11,9 @@ namespace Example.Views
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ItemDetailPage : ContentPage
{
ItemDetailViewModel viewModel;
NavItemDetailVM viewModel;

public ItemDetailPage(ItemDetailViewModel viewModel)
public ItemDetailPage(NavItemDetailVM viewModel)
{
InitializeComponent();

Expand All @@ -24,13 +24,13 @@ public ItemDetailPage()
{
InitializeComponent();

var item = new Item
var item = new NavItem
{
Text = "Item 1",
Description = "This is an item description."
};

viewModel = new ItemDetailViewModel(item);
viewModel = new NavItemDetailVM(item);
BindingContext = viewModel;
}
}
Expand Down
8 changes: 4 additions & 4 deletions Example/Example/Views/ItemsPage.xaml.cs
Expand Up @@ -16,22 +16,22 @@ namespace Example.Views
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ItemsPage : ContentPage
{
ItemsViewModel viewModel;
NavItemVM viewModel;

public ItemsPage()
{
InitializeComponent();

BindingContext = viewModel = new ItemsViewModel();
BindingContext = viewModel = new NavItemVM();
}

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

await Navigation.PushAsync(new ItemDetailPage(new ItemDetailViewModel(item)));
await Navigation.PushAsync(new ItemDetailPage(new NavItemDetailVM(item)));

// Manually deselect item.
ItemsListView.SelectedItem = null;
Expand Down
4 changes: 2 additions & 2 deletions Example/Example/Views/NewItemPage.xaml.cs
Expand Up @@ -11,13 +11,13 @@ namespace Example.Views
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class NewItemPage : ContentPage
{
public Item Item { get; set; }
public NavItem Item { get; set; }

public NewItemPage()
{
InitializeComponent();

Item = new Item
Item = new NavItem
{
Text = "Item name",
Description = "This is an item description."
Expand Down

0 comments on commit 2deafa2

Please sign in to comment.