-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8fb47ea
commit 9c50590
Showing
10 changed files
with
204 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
XamarinLocalDataAccessDemo/XamarinLocalDataAccessDemo/Models/Dish.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
XamarinLocalDataAccessDemo/XamarinLocalDataAccessDemo/Models/SqliteDataBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
XamarinLocalDataAccessDemo/XamarinLocalDataAccessDemo/Pages/SqliteDemoPage.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
103 changes: 103 additions & 0 deletions
103
XamarinLocalDataAccessDemo/XamarinLocalDataAccessDemo/Pages/SqliteDemoPage.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,6 @@ | |
public interface IPathService | ||
{ | ||
string GetDocumentsPath(); | ||
string GetDatabasePath(); | ||
} | ||
} |
4 changes: 3 additions & 1 deletion
4
XamarinLocalDataAccessDemo/XamarinLocalDataAccessDemo/XamarinLocalDataAccessDemo.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters