@@ -82,22 +82,22 @@ class EnhancedUpdateService {
82
82
}
83
83
/// プログレス更新
84
84
void _updateProgress (UpdateProgress progress) {
85
- developer.log (
86
- 'Progress Update: ${progress .status } - ${progress .formattedProgress }' ,
87
- name: 'EnhancedUpdateService'
88
- );
85
+ // developer.log(
86
+ // 'Progress Update: ${progress.status} - ${progress.formattedProgress}',
87
+ // name: 'EnhancedUpdateService'
88
+ // );
89
89
90
90
if (_progressController != null && ! _progressController! .isClosed) {
91
91
_progressController! .add (progress);
92
- developer.log ('Progress sent to stream successfully' , name: 'EnhancedUpdateService' );
92
+ // developer.log('Progress sent to stream successfully', name: 'EnhancedUpdateService');
93
93
} else {
94
94
developer.log ('Warning: Progress controller is null or closed' , name: 'EnhancedUpdateService' );
95
95
// 緊急時はStreamControllerを再初期化
96
96
if (_progressController == null ) {
97
97
initializeProgressStream ();
98
98
if (_progressController != null && ! _progressController! .isClosed) {
99
99
_progressController! .add (progress);
100
- developer.log ('Progress sent after emergency re-initialization' , name: 'EnhancedUpdateService' );
100
+ // developer.log('Progress sent after emergency re-initialization', name: 'EnhancedUpdateService');
101
101
}
102
102
}
103
103
}
@@ -288,30 +288,27 @@ class EnhancedUpdateService {
288
288
initializeProgressStream ();
289
289
}
290
290
291
- _resetDownloadState (); // ダウンロード開始前に状態をリセット
291
+ _resetDownloadState ();
292
292
293
293
_updateProgress (UpdateProgress (
294
294
progress: 0.0 ,
295
295
status: 'ダウンロード準備中...' ,
296
296
));
297
297
298
298
final client = http.Client ();
299
- // http.StreamedResponse? response; // Removed as it's declared later
300
- // StreamSubscription? subscription; // Removed as await for is used
301
- // final fileBytes = <int>[]; // Removed as data is written directly to sink
302
299
String ? filePath;
303
300
304
301
try {
305
302
final request = http.Request ('GET' , Uri .parse (releaseInfo.downloadUrl! ));
306
- final http.StreamedResponse response = await client.send (request); // Ensure response is typed
303
+ final http.StreamedResponse response = await client.send (request);
307
304
308
305
if (response.statusCode != 200 ) {
309
306
_updateProgress (UpdateProgress (
310
307
progress: 0.0 ,
311
308
status: 'ダウンロードエラー: ${response .statusCode }' ,
312
309
errorMessage: 'HTTP ${response .statusCode }: ${response .reasonPhrase }' ,
313
310
));
314
- client.close (); // Close client on error
311
+ client.close ();
315
312
return null ;
316
313
}
317
314
@@ -335,19 +332,19 @@ class EnhancedUpdateService {
335
332
if (_cancelDownload) {
336
333
developer.log ('Download cancelled by user.' , name: 'EnhancedUpdateService' );
337
334
_updateProgress (UpdateProgress (
338
- progress: totalBytes > 0 ? downloadedBytes / totalBytes : 0.0 , // キャンセル時点の進捗
335
+ progress: totalBytes > 0 ? downloadedBytes / totalBytes : 0.0 ,
339
336
status: 'ダウンロードがキャンセルされました' ,
340
337
bytesDownloaded: downloadedBytes,
341
338
totalBytes: totalBytes,
342
339
errorMessage: 'User cancelled download' ,
343
340
));
344
341
await sink.close ();
345
- if (await file.exists ()) { // Check if file exists before deleting
342
+ if (await file.exists ()) {
346
343
await file.delete ();
347
344
}
348
345
filePath = null ;
349
- client.close (); // Close client on cancellation
350
- return null ; // Return null as download was cancelled
346
+ client.close ();
347
+ return null ;
351
348
}
352
349
353
350
sink.add (chunk);
@@ -384,15 +381,13 @@ class EnhancedUpdateService {
384
381
await sink.close ();
385
382
386
383
if (_cancelDownload) {
387
- // This case should ideally be handled within the loop, but as a safeguard:
388
- if (filePath != null && await File (filePath).exists ()) { // Check filePath and existence
384
+ if (filePath != null && await File (filePath).exists ()) {
389
385
await File (filePath).delete ();
390
386
}
391
- client.close (); // Ensure client is closed
387
+ client.close ();
392
388
return null ;
393
389
}
394
390
395
- // Check if file exists after download attempt (excluding cancellation)
396
391
if (! await file.exists ()){
397
392
developer.log ('Error: File does not exist after download attempt (not due to cancellation). Path: ${file .path }' , name: 'EnhancedUpdateService' );
398
393
_updateProgress (UpdateProgress (
@@ -407,17 +402,16 @@ class EnhancedUpdateService {
407
402
return null ;
408
403
}
409
404
410
-
411
405
if (totalBytes == 0 || downloadedBytes == totalBytes) {
412
406
_updateProgress (UpdateProgress (
413
407
progress: 1.0 ,
414
- status: 'ダウンロード完了' ,
408
+ status: 'ダウンロード完了!インストールを開始します... ' ,
415
409
bytesDownloaded: downloadedBytes,
416
410
totalBytes: totalBytes,
417
- isCompleted: true ,
411
+ isCompleted: false , // まだ完了していない(インストールが残っている)
418
412
));
419
413
developer.log ('Download completed: ${file .path }' , name: 'EnhancedUpdateService' );
420
- client.close (); // Close client on success
414
+ client.close ();
421
415
return file.path;
422
416
} else if (downloadedBytes < totalBytes && totalBytes > 0 ) {
423
417
_updateProgress (UpdateProgress (
@@ -431,7 +425,7 @@ class EnhancedUpdateService {
431
425
if (await file.exists ()) {
432
426
await file.delete ();
433
427
}
434
- client.close (); // Close client on incomplete download
428
+ client.close ();
435
429
return null ;
436
430
} else {
437
431
_updateProgress (UpdateProgress (
@@ -445,12 +439,12 @@ class EnhancedUpdateService {
445
439
if (await file.exists ()) {
446
440
await file.delete ();
447
441
}
448
- client.close (); // Close client on unexpected issue
442
+ client.close ();
449
443
return null ;
450
444
}
451
445
452
- } catch (e, s) { // Added stack trace
453
- developer.log ('Download error: $e \\ nStackTrace: $s ' , name: 'EnhancedUpdateService' ); // Log stack trace
446
+ } catch (e, s) {
447
+ developer.log ('Download error: $e \\ nStackTrace: $s ' , name: 'EnhancedUpdateService' );
454
448
_updateProgress (UpdateProgress (
455
449
progress: 0.0 ,
456
450
status: 'ダウンロードエラー' ,
@@ -464,13 +458,11 @@ class EnhancedUpdateService {
464
458
developer.log ('Error deleting incomplete file: $deleteError ' , name: 'EnhancedUpdateService' );
465
459
}
466
460
}
467
- client.close (); // Ensure client is closed on catch
461
+ client.close ();
468
462
return null ;
469
- } finally {
470
- // client.close(); // Moved to specific exit points (success, error, cancellation)
471
463
}
472
464
}
473
-
465
+
474
466
/// 起動時のアップデートチェック(サイレント)
475
467
Future <EnhancedAppUpdateInfo ?> checkForUpdateOnStartup (String currentVersion, String owner, String repo) async {
476
468
try {
@@ -705,4 +697,42 @@ class EnhancedUpdateService {
705
697
));
706
698
}
707
699
}
700
+
701
+ /// ダウンロードとインストールを連続して実行
702
+ Future <void > downloadAndInstallUpdate (EnhancedAppUpdateInfo releaseInfo, BuildContext context) async {
703
+ try {
704
+ // バージョンを記録
705
+ await markUpdateAttempt (releaseInfo.version);
706
+
707
+ // ダウンロード実行
708
+ final downloadedPath = await downloadUpdateWithProgress (releaseInfo);
709
+
710
+ if (downloadedPath != null && ! _cancelDownload) {
711
+ // ダウンロード成功後、インストール開始
712
+ await applyUpdate (downloadedPath, context);
713
+ } else if (_cancelDownload) {
714
+ _updateProgress (UpdateProgress (
715
+ progress: 0.0 ,
716
+ status: 'アップデートがキャンセルされました' ,
717
+ isCompleted: true ,
718
+ errorMessage: 'Update cancelled by user' ,
719
+ ));
720
+ } else {
721
+ _updateProgress (UpdateProgress (
722
+ progress: 0.0 ,
723
+ status: 'ダウンロードに失敗しました' ,
724
+ isCompleted: true ,
725
+ errorMessage: 'Download failed' ,
726
+ ));
727
+ }
728
+ } catch (e) {
729
+ developer.log ('Error in downloadAndInstallUpdate: $e ' , name: 'EnhancedUpdateService' );
730
+ _updateProgress (UpdateProgress (
731
+ progress: 0.0 ,
732
+ status: 'アップデート処理中にエラーが発生しました' ,
733
+ isCompleted: true ,
734
+ errorMessage: e.toString (),
735
+ ));
736
+ }
737
+ }
708
738
}
0 commit comments