Skip to content

Commit b34ff21

Browse files
committed
feat: アップデートダイアログの初期化フラグを追加し、権限チェックの実装を改善
1 parent 1d4f14a commit b34ff21

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

lib/widgets/update_dialogs.dart

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:async';
2+
import 'dart:io';
23
import 'package:flutter/material.dart';
34
import '../services/enhanced_update_service.dart';
45
import '../services/update_manager.dart';
@@ -28,12 +29,21 @@ class _UpdateProgressDialogState extends State<UpdateProgressDialog> {
2829
bool _isDownloading = false;
2930
bool _isCompleted = false;
3031
bool _hasError = false;
32+
bool _isInitialized = false; // 初期化フラグを追加
3133
String? _downloadedFilePath;
32-
3334
@override
3435
void initState() {
3536
super.initState();
36-
_startDownload();
37+
// Theme.of(context)を使用する処理はdidChangeDependenciesに移動
38+
}
39+
@override
40+
void didChangeDependencies() {
41+
super.didChangeDependencies();
42+
// 初回実行時のみアップデートチェックを開始
43+
if (!_isInitialized && !_isDownloading && !_isCompleted && !_hasError) {
44+
_isInitialized = true;
45+
_startDownload();
46+
}
3747
}
3848

3949
@override
@@ -42,28 +52,41 @@ class _UpdateProgressDialogState extends State<UpdateProgressDialog> {
4252
_updateService.disposeProgressStream();
4353
super.dispose();
4454
}
45-
4655
void _startDownload() async {
4756
setState(() {
4857
_isDownloading = true;
4958
_hasError = false;
5059
});
5160

5261
// Check permissions for Android
53-
if (Theme.of(context).platform == TargetPlatform.android) {
54-
bool permissionGranted = await _updateService.requestStoragePermission();
55-
if (!permissionGranted) {
56-
setState(() {
57-
_hasError = true;
58-
_currentProgress = UpdateProgress(
59-
progress: 0.0,
60-
status: 'ストレージ権限が必要です',
61-
errorMessage: 'Storage permission denied',
62-
);
63-
_isDownloading = false;
64-
});
65-
return;
62+
try {
63+
// Use Platform.isAndroid instead of Theme.of(context).platform
64+
if (Platform.isAndroid) {
65+
bool permissionGranted = await _updateService.requestStoragePermission();
66+
if (!permissionGranted) {
67+
setState(() {
68+
_hasError = true;
69+
_currentProgress = UpdateProgress(
70+
progress: 0.0,
71+
status: 'ストレージ権限が必要です',
72+
errorMessage: 'Storage permission denied',
73+
);
74+
_isDownloading = false;
75+
});
76+
return;
77+
}
6678
}
79+
} catch (e) {
80+
setState(() {
81+
_hasError = true;
82+
_currentProgress = UpdateProgress(
83+
progress: 0.0,
84+
status: '権限チェックエラー',
85+
errorMessage: e.toString(),
86+
);
87+
_isDownloading = false;
88+
});
89+
return;
6790
}
6891

6992
// Listen to progress stream

0 commit comments

Comments
 (0)