3
3
using Giantapp . LiveWallpaper . Engine ;
4
4
using LiveWallpaper . LocalServer . Models ;
5
5
using LiveWallpaper . LocalServer . Utils ;
6
+ using Microsoft . Toolkit . Uwp . Notifications ;
6
7
using System ;
7
8
using System . IO ;
8
9
using System . Reflection ;
10
+ using System . Threading ;
9
11
using System . Threading . Tasks ;
12
+ using Windows . ApplicationModel ;
13
+ using Windows . Storage ;
14
+ using Windows . UI . Notifications ;
10
15
11
16
namespace LiveWallpaper . LocalServer
12
17
{
13
18
public class AppManager
14
19
{
15
20
private static readonly NLog . Logger logger = NLog . LogManager . GetCurrentClassLogger ( ) ;
16
- private static readonly string _runningDataFilePath ;
17
- private static readonly string _userSettingFilePath ;
21
+ public static string RunningDataFilePath { get ; private set ; }
22
+ public static string UserSettingFilePath { get ; private set ; }
23
+ public static string CacheDir { get ; private set ; }
24
+ public static string ConfigDir { get ; private set ; }
25
+ public static string LogDir { get ; private set ; }
18
26
private static IStartupManager _startupManager = null ;
19
27
20
-
21
28
static AppManager ( )
22
29
{
23
- //MyDocuments这个路径不会虚拟化,方便从Dart端读取 (Flutter 方案已放弃,注释先留着)
24
- _runningDataFilePath = $ "{ Environment . GetFolderPath ( Environment . SpecialFolder . MyDocuments ) } \\ { AppName } \\ runningData.json";
25
- _userSettingFilePath = $ "{ Environment . GetFolderPath ( Environment . SpecialFolder . LocalApplicationData ) } \\ { AppName } \\ Config\\ userSetting.json";
30
+ //_runningDataFilePath = $"{Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)}\\{AppName}\\runningData.json";
31
+ //runningdata目录修改为和配置一个目录2021.9.30
32
+ DesktopBridge . Helpers helpers = new ( ) ;
33
+ if ( helpers . IsRunningAsUwp ( ) )
34
+ {
35
+ //uwp放在包里面,卸载时可以清理干净
36
+ ConfigDir = Path . Combine ( ApplicationData . Current . LocalCacheFolder . Path , $ "Local\\ { AppName } \\ ") ;
37
+ }
38
+ else
39
+ {
40
+ ConfigDir = $ "{ Environment . GetFolderPath ( Environment . SpecialFolder . LocalApplicationData ) } \\ { AppName } \\ ";
41
+ }
42
+ CacheDir = $ "{ ConfigDir } Cache\\ ";
43
+ RunningDataFilePath = $ "{ ConfigDir } Config\\ runningData.json";
44
+ UserSettingFilePath = $ "{ ConfigDir } Config\\ userSetting.json";
45
+ LogDir = $ "{ ConfigDir } /Logs";
46
+ ToastNotificationManagerCompat . OnActivated += ToastNotificationManagerCompat_OnActivated ;
47
+ }
48
+
49
+ private static async void ToastNotificationManagerCompat_OnActivated ( ToastNotificationActivatedEventArgsCompat e )
50
+ {
51
+ // Obtain the arguments from the notification
52
+ ToastArguments args = ToastArguments . Parse ( e . Argument ) ;
53
+ string actionString = "action" ;
54
+ if ( args . Contains ( actionString ) )
55
+ {
56
+ string action = args . Get ( actionString ) ;
57
+ switch ( action )
58
+ {
59
+ case "review" :
60
+ try
61
+ {
62
+ await OpenStoreReview ( ) ;
63
+ RunningData = await JsonHelper . JsonDeserializeFromFileAsync < RunningData > ( RunningDataFilePath ) ;
64
+ RunningData . CurrentVersionReviewed = true ;
65
+ await JsonHelper . JsonSerializeAsync ( RunningData , RunningDataFilePath ) ;
66
+ }
67
+ catch ( Exception ex )
68
+ {
69
+ System . Diagnostics . Debug . WriteLine ( ex ) ;
70
+ }
71
+ break ;
72
+ }
73
+ }
74
+ ToastNotificationManagerCompat . History . Clear ( ) ;
75
+
76
+ // Obtain any user input (text boxes, menu selections) from the notification
77
+ //ValueSet userInput = toastArgs.UserInput;
78
+
79
+ //// Need to dispatch to UI thread if performing UI operations
80
+ //Application.Current.Dispatcher.Invoke(delegate
81
+ //{
82
+ // // TODO: Show the corresponding content
83
+ // MessageBox.Show("Toast activated. Args: " + toastArgs.Argument);
84
+ //});
26
85
}
27
86
28
87
#region properties
@@ -74,26 +133,68 @@ public static FileDownloader PlayerDownloader
74
133
public static event EventHandler CultureChanged ;
75
134
#endregion
76
135
136
+ public static async Task < bool > OpenStoreReview ( )
137
+ {
138
+ try
139
+ {
140
+ //旧方法,不推荐的方式.但是推荐的方式获取不到ID
141
+ var pfn = Package . Current . Id . FamilyName ;
142
+ var uri = new Uri ( $ "ms-windows-store://review/?PFN={ pfn } ") ;
143
+ bool success = await Windows . System . Launcher . LaunchUriAsync ( uri ) ;
144
+ return success ;
145
+ //var id = Package.Current.Id.ProductId;
146
+ //bool ok = await Windows.System.Launcher.LaunchUriAsync(new Uri("ms-windows-store://review/?ProductId=9WZDNCRFHVJL"));
147
+ }
148
+ catch ( Exception ex )
149
+ {
150
+ System . Diagnostics . Debug . WriteLine ( ex ) ;
151
+ return false ;
152
+ }
153
+ }
154
+
77
155
public static async Task Initialize ( int hostPort )
78
156
{
79
157
try
80
158
{
81
159
//应用程序数据
82
- RunningData = await JsonHelper . JsonDeserializeFromFileAsync < RunningData > ( _runningDataFilePath ) ;
160
+ RunningData = await JsonHelper . JsonDeserializeFromFileAsync < RunningData > ( RunningDataFilePath ) ;
83
161
if ( RunningData == null )
84
162
{
85
163
//生成默认运行数据
86
164
RunningData = new RunningData ( ) ;
87
165
}
88
166
//更新端口号
89
167
RunningData . HostPort = hostPort ;
90
- await JsonHelper . JsonSerializeAsync ( RunningData , _runningDataFilePath ) ;
168
+ var version = Assembly . GetEntryAssembly ( ) . GetName ( ) . Version . ToString ( ) ;
169
+
170
+ if ( RunningData . CurrentVersion == null )
171
+ {
172
+ _ = ShowGuidToastAsync ( ) ; //第一次启动
173
+ }
174
+
175
+ if ( RunningData . CurrentVersion != version )
176
+ {
177
+ RunningData . CurrentVersion = version ;
178
+ RunningData . CurrentVersionLaunchedCount = 0 ;
179
+ RunningData . CurrentVersionReviewed = false ;
180
+ }
181
+ else
182
+ {
183
+ RunningData . CurrentVersionLaunchedCount ++ ;
184
+ }
185
+
186
+ await JsonHelper . JsonSerializeAsync ( RunningData , RunningDataFilePath ) ;
187
+
188
+ if ( ! RunningData . CurrentVersionReviewed && RunningData . CurrentVersionLaunchedCount > 0 && RunningData . CurrentVersionLaunchedCount % 30 == 0 )
189
+ {
190
+ ShowReviewToast ( ) ;
191
+ }
91
192
92
193
if ( UserSetting == null )
93
194
await LoadUserSetting ( ) ;
94
195
95
196
//开机启动
96
- DesktopBridge . Helpers helpers = new DesktopBridge . Helpers ( ) ;
197
+ DesktopBridge . Helpers helpers = new ( ) ;
97
198
if ( helpers . IsRunningAsUwp ( ) )
98
199
_startupManager = new DesktopBridgeStartupManager ( AppName ) ;
99
200
else
@@ -123,6 +224,71 @@ public static async Task Initialize(int hostPort)
123
224
}
124
225
}
125
226
227
+ public static async Task ShowGuidToastAsync ( )
228
+ {
229
+ string appDir = Path . GetDirectoryName ( Assembly . GetEntryAssembly ( ) . Location ) ;
230
+ string imgPath = Path . Combine ( appDir , "Assets\\ guide.gif" ) ;
231
+ new ToastContentBuilder ( )
232
+ . AddText ( await GetText ( "client.started" ) )
233
+ . AddHeroImage ( new Uri ( imgPath ) )
234
+ . AddButton ( new ToastButtonDismiss ( await GetText ( "common.ok" ) ) )
235
+ . Show ( ) ;
236
+ }
237
+
238
+ private static async void ShowReviewToast ( )
239
+ {
240
+ var toastContent = new ToastContent ( )
241
+ {
242
+ Visual = new ToastVisual ( )
243
+ {
244
+ BindingGeneric = new ToastBindingGeneric ( )
245
+ {
246
+ Children =
247
+ {
248
+ new AdaptiveText ( )
249
+ {
250
+ Text = await GetText ( "common.reviewTitle" )
251
+ } ,
252
+ new AdaptiveText ( )
253
+ {
254
+ Text = await GetText ( "common.reviewContent" )
255
+ }
256
+ }
257
+ }
258
+ } ,
259
+ Actions = new ToastActionsCustom ( )
260
+ {
261
+ Buttons =
262
+ {
263
+ new ToastButton ( await GetText ( "common.thumbUp" ) , "action=review" )
264
+ {
265
+ ActivationType = ToastActivationType . Background
266
+ } ,
267
+ new ToastButtonDismiss ( await GetText ( "common.close" ) )
268
+ }
269
+ } ,
270
+ Launch = "action=viewEvent&eventId=63851"
271
+ } ;
272
+
273
+ // Create the toast notification
274
+ var toastNotif = new ToastNotification ( toastContent . GetXml ( ) ) ;
275
+
276
+ // And send the notification
277
+ ToastNotificationManagerCompat . CreateToastNotifier ( ) . Show ( toastNotif ) ;
278
+ }
279
+
280
+ public static async Task < string > GetText ( string key )
281
+ {
282
+ if ( UserSetting == null )
283
+ {
284
+ await LoadUserSetting ( ) ;
285
+ }
286
+ string culture = UserSetting . General . CurrentLan ?? Thread . CurrentThread . CurrentCulture . Name ;
287
+ var r = await LanService . Instance . GetTextAsync ( key , culture ) ;
288
+ return r ;
289
+ }
290
+
291
+
126
292
public static async Task WaitInitialized ( )
127
293
{
128
294
while ( ! Initialized )
@@ -131,7 +297,7 @@ public static async Task WaitInitialized()
131
297
132
298
public static async Task LoadUserSetting ( )
133
299
{
134
- UserSetting = await JsonHelper . JsonDeserializeFromFileAsync < UserSetting > ( _userSettingFilePath ) ;
300
+ UserSetting = await JsonHelper . JsonDeserializeFromFileAsync < UserSetting > ( UserSettingFilePath ) ;
135
301
if ( UserSetting == null )
136
302
UserSetting = new UserSetting ( ) ;
137
303
UserSetting . Wallpaper . FixScreenOptions ( ) ;
@@ -147,7 +313,7 @@ internal static async Task<BaseApiResult> SaveUserSetting(UserSetting setting)
147
313
{
148
314
try
149
315
{
150
- await JsonHelper . JsonSerializeAsync ( setting , _userSettingFilePath ) ;
316
+ await JsonHelper . JsonSerializeAsync ( setting , UserSettingFilePath ) ;
151
317
152
318
bool lanChanged = false ;
153
319
if ( UserSetting . General . CurrentLan != setting . General . CurrentLan )
@@ -174,7 +340,7 @@ internal static async Task SaveRunningData(RunningData data)
174
340
{
175
341
try
176
342
{
177
- await JsonHelper . JsonSerializeAsync ( data , _runningDataFilePath ) ;
343
+ await JsonHelper . JsonSerializeAsync ( data , RunningDataFilePath ) ;
178
344
//更新内存对象
179
345
RunningData = data ;
180
346
0 commit comments