-
Notifications
You must be signed in to change notification settings - Fork 207
/
App.xaml.cs
143 lines (129 loc) · 5.67 KB
/
App.xaml.cs
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
138
139
140
141
142
143
// ---------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE 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 SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// ---------------------------------------------------------------------------------
using System.IO;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media.Animation;
using Windows.ApplicationModel;
using Windows.Globalization;
using Windows.Storage;
using Contoso.App.ViewModels;
using Contoso.App.Views;
using Contoso.Repository;
using Contoso.Repository.Rest;
using Contoso.Repository.Sql;
namespace Contoso.App
{
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
public partial class App : Application
{
/// <summary>
/// Gets main App Window
/// </summary>
public static Window Window { get { return m_window; } }
private static Window m_window;
/// <summary>
/// Gets the app-wide MainViewModel singleton instance.
/// </summary>
public static MainViewModel ViewModel { get; } = new MainViewModel();
/// <summary>
/// Pipeline for interacting with backend service or database.
/// </summary>
public static IContosoRepository Repository { get; private set; }
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App() => InitializeComponent();
/// <summary>
/// Invoked when the application is launched normally by the end user.
/// </summary>
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
m_window = new MainWindow();
// Load the database.
if (ApplicationData.Current.LocalSettings.Values.TryGetValue(
"data_source", out object dataSource))
{
switch (dataSource.ToString())
{
case "Rest": if (!UseRest()) UseSqlite(); break;
default: UseSqlite(); break;
}
}
else
{
UseSqlite();
}
// Prepare the app shell and window content.
AppShell shell = m_window.Content as AppShell ?? new AppShell();
shell.Language = ApplicationLanguages.Languages[0];
m_window.Content = shell;
if (shell.AppFrame.Content == null)
{
// When the navigation stack isn't restored, navigate to the first page
// suppressing the initial entrance animation.
shell.AppFrame.Navigate(typeof(CustomerListPage), null,
new SuppressNavigationTransitionInfo());
}
m_window.Activate();
}
/// <summary>
/// Configures the app to use the Sqlite data source. If no existing Sqlite database exists,
/// loads a demo database filled with fake data so the app has content.
/// </summary>
public static void UseSqlite()
{
string demoDatabasePath = Package.Current.InstalledLocation.Path + @"\Assets\Contoso.db";
string databasePath = ApplicationData.Current.LocalFolder.Path + @"\Contoso.db";
if (!File.Exists(databasePath))
{
File.Copy(demoDatabasePath, databasePath);
}
var dbOptions = new DbContextOptionsBuilder<ContosoContext>().UseSqlite(
"Data Source=" + databasePath);
Repository = new SqlContosoRepository(dbOptions);
}
/// <summary>
/// Configures the app to use the REST data source. For convenience, a read-only source is provided.
/// You can also deploy your own copy of the REST service locally or to Azure. See the README for details.
/// </summary>
public static bool UseRest()
{
try
{
var accessToken = Task.Run(async () => await MsalHelper.GetTokenAsync(Constants.WebApiScopes)).Result;
Repository = new RestContosoRepository($"{Constants.ApiUrl}/api/", accessToken);
}
catch (System.Exception ex)
{
return false;
}
return true;
}
}
}