-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathLocalStorageContext.cs
More file actions
137 lines (118 loc) · 3.93 KB
/
LocalStorageContext.cs
File metadata and controls
137 lines (118 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// ******************************************************************
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THE CODE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
// ******************************************************************
using System;
using System.Threading.Tasks;
using BuildCast.Helpers;
using Microsoft.EntityFrameworkCore;
using Windows.Storage;
namespace BuildCast.DataModel
{
// documnetation is here: https://docs.microsoft.com/en-us/ef/core/get-started/uwp/getting-started
// under package manager console, use Add-Migration <nameofchange> every time this class updated
public class LocalStorageContext : DbContext
{
private static AsyncInitilizer<LocalStorageContext> _initializer = new AsyncInitilizer<LocalStorageContext>();
private DbSet<Favorite> _favorites;
private DbSet<Episode> _episodeCache;
private DbSet<InkNote> _memes;
private DbSet<InkNoteData> _inkNotes;
private DbSet<EpisodePlaybackState> _playbackState;
static LocalStorageContext()
{
_initializer.InitializeWith(CheckForDatabase);
}
public DbSet<Favorite> Favorites
{
get
{
_initializer.CheckInitialized();
return _favorites;
}
set
{
_favorites = value;
}
}
public DbSet<Episode> EpisodeCache
{
get
{
_initializer.CheckInitialized();
return _episodeCache;
}
set
{
_episodeCache = value;
}
}
public DbSet<InkNote> Memes
{
get
{
_initializer.CheckInitialized();
return _memes;
}
set
{
_memes = value;
}
}
public DbSet<InkNoteData> MemeData
{
get
{
_initializer.CheckInitialized();
return _inkNotes;
}
set
{
_inkNotes = value;
}
}
public DbSet<EpisodePlaybackState> PlaybackState
{
get
{
_initializer.CheckInitialized();
return _playbackState;
}
set
{
_playbackState = value;
}
}
public static void CheckMigrations()
{
_initializer.CheckInitialized();
using (var db = new LocalStorageContext())
{
db.Database.Migrate();
}
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=buildcast.db");
}
private static async Task CheckForDatabase()
{
var mainDbFileName = "buildcast.db";
var mainDbAssetPath = $"ms-appx:///Assets/{mainDbFileName}";
var data = Windows.Storage.ApplicationData.Current.LocalFolder;
var exists = await data.TryGetItemAsync(mainDbFileName);
if (exists == null)
{
var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(mainDbAssetPath)).AsTask().ConfigureAwait(false);
var database = await file.CopyAsync(data).AsTask().ConfigureAwait(false);
}
}
}
}