Skip to content

Commit

Permalink
Finished SQLite Demo
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreKraemer committed May 5, 2017
1 parent 8fb47ea commit 9c50590
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@ public string GetDocumentsPath()
{
return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
}

public string GetDatabasePath()
{
return GetDocumentsPath();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using XamarinLocalDataAccessDemo.iOS.Servcies;
using XamarinLocalDataAccessDemo.Services;

Expand All @@ -11,5 +12,11 @@ public string GetDocumentsPath()
{
return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
}

public string GetDatabasePath()
{
var path = Path.Combine(GetDocumentsPath(), "..", "library");
return path;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<Button Clicked="LocalDataButton_OnClicked" Text="Read Application Base Data"></Button>
<Button Clicked="EditApplicationBaseDataButton_OnClicked" Text="Edit Application Base Data"></Button>
<Button Clicked="SecureDataButton_OnClicked" Text="Secure Data"></Button>
<Button Clicked="SqliteButton_OnClicked" Text="SQLite"></Button>
</StackLayout>

</ContentPage>
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,10 @@ private void SecureDataButton_OnClicked(object sender, EventArgs e)
{
Navigation.PushAsync(new SaveSecureDataDemoPage());
}

private void SqliteButton_OnClicked(object sender, EventArgs e)
{
Navigation.PushAsync(new SqliteDemoPage());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace XamarinLocalDataAccessDemo.Models
using System.ComponentModel.DataAnnotations;
namespace XamarinLocalDataAccessDemo.Models
{
public class Dish
{
[Key]
public int Id { get; set; }

public string Name { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using SQLite;

namespace XamarinLocalDataAccessDemo.Models
{
public class SqliteDataBase
{
private SQLiteAsyncConnection _database;

public SqliteDataBase(string path)
{
path = Path.Combine(path, "dishes.sqlite");
_database = new SQLiteAsyncConnection(path);
_database.CreateTableAsync<Dish>().Wait();
}

public Task<List<Dish>> GetDishesAsync()
{
return _database.Table<Dish>().ToListAsync();
}


public Task<Dish> GetDishAsync(int id)
{
return _database.Table<Dish>().Where(i => i.Id == id).FirstOrDefaultAsync();
}

public Task<int> SaveDishAsync(Dish item)
{
if (item.Id != 0)
{
return _database.UpdateAsync(item);
}
else
{
return _database.InsertAsync(item);
}
}

public Task<int> DeleteDishAsync(Dish item)
{
return _database.DeleteAsync(item);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XamarinLocalDataAccessDemo.Pages.SqliteDemoPage"
Title="ListView">
<ListView ItemsSource="{Binding Items}"
IsPullToRefreshEnabled="true"
CachingStrategy="RecycleElement"
IsRefreshing="{Binding IsBusy, Mode=OneWay}"
RefreshCommand="{Binding RefreshDataCommand}">
<ListView.Header>
<StackLayout Padding="10"
Orientation="Horizontal"
HorizontalOptions="FillAndExpand"
BackgroundColor="#dadada">
<Button Text="Add Entry" Command="{Binding AddCommand}"></Button>
</StackLayout>
</ListView.Header>
<!--Built in Cells-->
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Name}"
Detail="{Binding Description}"/>
</DataTemplate>
</ListView.ItemTemplate>


</ListView>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using XamarinLocalDataAccessDemo.Models;
using XamarinLocalDataAccessDemo.Services;

namespace XamarinLocalDataAccessDemo.Pages
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SqliteDemoPage : ContentPage
{
public SqliteDemoPage()
{
InitializeComponent ();
BindingContext = new SqliteDemoPageViewModel();
}

}



class SqliteDemoPageViewModel : INotifyPropertyChanged
{
public ObservableCollection<Dish> Items { get; }
bool _busy;
private SqliteDataBase _db;

public SqliteDemoPageViewModel()
{

var path = DependencyService.Get<IPathService>().GetDatabasePath();
_db = new SqliteDataBase(path);
RefreshDataCommand = new Command(
async () => await RefreshData());
AddCommand = new Command(
async () => await AddData());

var t = _db.GetDishesAsync();
t.Wait();
var dishes = t.Result;
Items = new ObservableCollection<Dish>(dishes);
}

public ICommand RefreshDataCommand { get; }
public ICommand AddCommand { get; }

async Task RefreshData()
{
IsBusy = true;
IsBusy = true;
var dishes = await _db.GetDishesAsync();

Items.Clear();
foreach (var dish in dishes)
{
Items.Add(dish);
}
IsBusy = false;
await Task.Delay(2000);

IsBusy = false;
}

async Task AddData()
{
IsBusy = true;
var dish = new Dish
{
Name = $"Ensalada Fantasia ({DateTime.Now:T})",
Description = "Salat aus allen Zutaten, die noch übrig waren",
Price = 1.99,
CategoryId = 1
};
await _db.SaveDishAsync(dish);
IsBusy = false;
}

public bool IsBusy
{
get { return _busy; }
set
{
_busy = value;
OnPropertyChanged();
((Command)RefreshDataCommand).ChangeCanExecute();
}
}


public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged([CallerMemberName]string propertyName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
public interface IPathService
{
string GetDocumentsPath();
string GetDatabasePath();
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard1.4</TargetFramework>
<TargetFramework>netstandard1.6</TargetFramework>
<PackageTargetFallback>portable-net45+win8+wpa81+wp8</PackageTargetFallback>
<DebugType>full</DebugType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="10.0.2" />
<PackageReference Include="sqlite-net-pcl" Version="1.3.1" />
<PackageReference Include="System.ComponentModel.Annotations" Version="4.3.0" />
<PackageReference Include="Xamarin.Forms" Version="2.3.4.231" />
</ItemGroup>

Expand Down

0 comments on commit 9c50590

Please sign in to comment.