2026-01-11 08:17:29 +00:00
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
import 'package:lucide_icons/lucide_icons.dart';
|
|
|
|
|
|
import '../../services/backup_service.dart';
|
2026-01-29 15:54:22 +00:00
|
|
|
|
import '../../theme/app_colors.dart';
|
2026-01-11 08:17:29 +00:00
|
|
|
|
|
|
|
|
|
|
class BackupSettingsSection extends StatefulWidget {
|
|
|
|
|
|
final String title;
|
|
|
|
|
|
|
|
|
|
|
|
const BackupSettingsSection({
|
|
|
|
|
|
super.key,
|
2026-01-13 09:13:23 +00:00
|
|
|
|
this.title = 'バックアップ・復元',
|
2026-01-11 08:17:29 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
State<BackupSettingsSection> createState() => _BackupSettingsSectionState();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
enum _BackupState { idle, signingIn, signingOut, backingUp, restoring }
|
|
|
|
|
|
|
|
|
|
|
|
class _BackupSettingsSectionState extends State<BackupSettingsSection> {
|
|
|
|
|
|
final BackupService _backupService = BackupService();
|
|
|
|
|
|
_BackupState _state = _BackupState.idle;
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
void initState() {
|
|
|
|
|
|
super.initState();
|
|
|
|
|
|
_initBackupService();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> _initBackupService() async {
|
|
|
|
|
|
await _backupService.init();
|
|
|
|
|
|
if (mounted) {
|
|
|
|
|
|
setState(() {});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> _signIn() async {
|
2026-01-29 15:54:22 +00:00
|
|
|
|
final messenger = ScaffoldMessenger.of(context);
|
2026-01-11 08:17:29 +00:00
|
|
|
|
setState(() => _state = _BackupState.signingIn);
|
|
|
|
|
|
final account = await _backupService.signIn();
|
|
|
|
|
|
if (mounted) {
|
|
|
|
|
|
setState(() => _state = _BackupState.idle);
|
|
|
|
|
|
if (account != null) {
|
2026-01-29 15:54:22 +00:00
|
|
|
|
messenger.showSnackBar(
|
2026-01-11 08:17:29 +00:00
|
|
|
|
SnackBar(content: Text('${account.email} で連携しました')),
|
|
|
|
|
|
);
|
|
|
|
|
|
} else {
|
2026-01-29 15:54:22 +00:00
|
|
|
|
messenger.showSnackBar(
|
2026-01-11 08:17:29 +00:00
|
|
|
|
const SnackBar(content: Text('連携がキャンセルされました')),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> _signOut() async {
|
2026-01-29 15:54:22 +00:00
|
|
|
|
final messenger = ScaffoldMessenger.of(context);
|
2026-01-11 08:17:29 +00:00
|
|
|
|
final confirmed = await showDialog<bool>(
|
|
|
|
|
|
context: context,
|
|
|
|
|
|
builder: (context) => AlertDialog(
|
|
|
|
|
|
title: const Text('連携解除'),
|
2026-01-13 10:00:16 +00:00
|
|
|
|
content: const Text('Googleアカウントとの連携を解除しますか?\n安全にログアウトできます。'),
|
2026-01-11 08:17:29 +00:00
|
|
|
|
actions: [
|
|
|
|
|
|
TextButton(
|
|
|
|
|
|
onPressed: () => Navigator.pop(context, false),
|
|
|
|
|
|
child: const Text('キャンセル'),
|
|
|
|
|
|
),
|
|
|
|
|
|
ElevatedButton(
|
|
|
|
|
|
onPressed: () => Navigator.pop(context, true),
|
|
|
|
|
|
child: const Text('解除'),
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
if (confirmed == true) {
|
|
|
|
|
|
setState(() => _state = _BackupState.signingOut);
|
|
|
|
|
|
await _backupService.signOut();
|
|
|
|
|
|
if (mounted) {
|
|
|
|
|
|
setState(() => _state = _BackupState.idle);
|
2026-01-29 15:54:22 +00:00
|
|
|
|
messenger.showSnackBar(
|
2026-01-11 08:17:29 +00:00
|
|
|
|
const SnackBar(content: Text('連携を解除しました')),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> _createBackup() async {
|
2026-01-29 15:54:22 +00:00
|
|
|
|
final messenger = ScaffoldMessenger.of(context);
|
2026-01-11 08:17:29 +00:00
|
|
|
|
setState(() => _state = _BackupState.backingUp);
|
|
|
|
|
|
final success = await _backupService.createBackup();
|
|
|
|
|
|
if (mounted) {
|
|
|
|
|
|
setState(() => _state = _BackupState.idle);
|
2026-01-29 15:54:22 +00:00
|
|
|
|
messenger.showSnackBar(
|
2026-01-11 08:17:29 +00:00
|
|
|
|
SnackBar(
|
|
|
|
|
|
content: Text(success ? 'バックアップが完了しました' : 'バックアップに失敗しました'),
|
2026-01-29 15:54:22 +00:00
|
|
|
|
// Snackbars can keep Green/Red for semantic clarity, or be neutral.
|
|
|
|
|
|
// User asked to remove Green/Red icons from the UI, but feedback (Snackbar) usually stays semantic.
|
|
|
|
|
|
// However, to be safe and "Washi", let's use Sumi (Black) for success?
|
|
|
|
|
|
// Or just leave snackbars as they are ephemeral. The request was likely about the visible static UI.
|
2026-01-11 08:17:29 +00:00
|
|
|
|
backgroundColor: success ? Colors.green : Colors.red,
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-13 10:00:16 +00:00
|
|
|
|
Future<void> _confirmBackup() async {
|
2026-01-29 15:54:22 +00:00
|
|
|
|
final appColors = Theme.of(context).extension<AppColors>()!;
|
2026-01-13 10:00:16 +00:00
|
|
|
|
final confirmed = await showDialog<bool>(
|
|
|
|
|
|
context: context,
|
|
|
|
|
|
builder: (context) => AlertDialog(
|
|
|
|
|
|
title: const Text('バックアップ'),
|
|
|
|
|
|
content: const Text('Google Driveに最新データのみ保存(過去分は上書き)されます。\n本当に続行しますか?'),
|
|
|
|
|
|
actions: [
|
|
|
|
|
|
TextButton(
|
|
|
|
|
|
onPressed: () => Navigator.pop(context, false),
|
|
|
|
|
|
child: const Text('キャンセル'),
|
|
|
|
|
|
),
|
|
|
|
|
|
ElevatedButton(
|
|
|
|
|
|
onPressed: () => Navigator.pop(context, true),
|
|
|
|
|
|
style: ElevatedButton.styleFrom(
|
2026-01-29 15:54:22 +00:00
|
|
|
|
backgroundColor: appColors.brandPrimary,
|
|
|
|
|
|
foregroundColor: appColors.surfaceSubtle,
|
2026-01-13 10:00:16 +00:00
|
|
|
|
),
|
|
|
|
|
|
child: const Text('バックアップ'),
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2026-01-29 15:54:22 +00:00
|
|
|
|
if (confirmed == true && mounted) {
|
2026-01-13 10:00:16 +00:00
|
|
|
|
await _createBackup();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-11 08:17:29 +00:00
|
|
|
|
Future<void> _restoreBackup() async {
|
2026-01-29 15:54:22 +00:00
|
|
|
|
final messenger = ScaffoldMessenger.of(context);
|
|
|
|
|
|
final appColors = Theme.of(context).extension<AppColors>()!;
|
|
|
|
|
|
// Note: hasBackup check is async
|
2026-01-11 08:17:29 +00:00
|
|
|
|
final hasBackup = await _backupService.hasBackupOnDrive();
|
2026-01-29 15:54:22 +00:00
|
|
|
|
if (!hasBackup) {
|
|
|
|
|
|
if (mounted) {
|
|
|
|
|
|
messenger.showSnackBar(
|
|
|
|
|
|
const SnackBar(content: Text('バックアップファイルが見つかりません')),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2026-01-11 08:17:29 +00:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-29 15:54:22 +00:00
|
|
|
|
if (!mounted) return;
|
|
|
|
|
|
|
2026-01-11 08:17:29 +00:00
|
|
|
|
final confirmed = await showDialog<bool>(
|
|
|
|
|
|
context: context,
|
|
|
|
|
|
builder: (context) => AlertDialog(
|
|
|
|
|
|
title: Row(
|
|
|
|
|
|
children: [
|
2026-01-29 15:54:22 +00:00
|
|
|
|
Icon(LucideIcons.alertTriangle, color: appColors.warning, size: 24),
|
2026-01-11 08:17:29 +00:00
|
|
|
|
const SizedBox(width: 8),
|
|
|
|
|
|
const Text('データ復元'),
|
|
|
|
|
|
],
|
|
|
|
|
|
),
|
|
|
|
|
|
content: const Text('現在のデータは上書きされます。\n削除されたデータは元に戻りません。\n\n本当に続行しますか?'),
|
|
|
|
|
|
actions: [
|
|
|
|
|
|
TextButton(
|
|
|
|
|
|
onPressed: () => Navigator.pop(context, false),
|
|
|
|
|
|
child: const Text('キャンセル'),
|
|
|
|
|
|
),
|
|
|
|
|
|
ElevatedButton(
|
|
|
|
|
|
onPressed: () => Navigator.pop(context, true),
|
|
|
|
|
|
style: ElevatedButton.styleFrom(
|
2026-01-29 15:54:22 +00:00
|
|
|
|
backgroundColor: appColors.warning,
|
|
|
|
|
|
foregroundColor: appColors.surfaceSubtle,
|
2026-01-11 08:17:29 +00:00
|
|
|
|
),
|
|
|
|
|
|
child: const Text('復元'),
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2026-01-29 15:54:22 +00:00
|
|
|
|
if (confirmed == true && mounted) {
|
2026-01-11 08:17:29 +00:00
|
|
|
|
setState(() => _state = _BackupState.restoring);
|
|
|
|
|
|
final success = await _backupService.restoreBackup();
|
|
|
|
|
|
if (mounted) {
|
|
|
|
|
|
setState(() => _state = _BackupState.idle);
|
2026-01-29 15:54:22 +00:00
|
|
|
|
messenger.showSnackBar(
|
2026-01-11 08:17:29 +00:00
|
|
|
|
SnackBar(
|
|
|
|
|
|
content: Text(success ? '復元が完了しました' : '復元に失敗しました'),
|
|
|
|
|
|
backgroundColor: success ? Colors.green : Colors.red,
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
|
final currentUser = _backupService.currentUser;
|
|
|
|
|
|
final isAnyProcessing = _state != _BackupState.idle;
|
2026-01-29 15:54:22 +00:00
|
|
|
|
final appColors = Theme.of(context).extension<AppColors>()!;
|
2026-01-11 08:17:29 +00:00
|
|
|
|
|
|
|
|
|
|
return Column(
|
|
|
|
|
|
children: [
|
|
|
|
|
|
_buildSectionHeader(context, widget.title, LucideIcons.cloud),
|
2026-01-13 00:57:18 +00:00
|
|
|
|
|
2026-01-29 15:54:22 +00:00
|
|
|
|
// Wi-Fi warning
|
2026-01-13 00:57:18 +00:00
|
|
|
|
Container(
|
|
|
|
|
|
margin: const EdgeInsets.only(bottom: 12),
|
|
|
|
|
|
padding: const EdgeInsets.all(12),
|
|
|
|
|
|
decoration: BoxDecoration(
|
2026-01-29 15:54:22 +00:00
|
|
|
|
color: appColors.info.withValues(alpha: 0.1),
|
2026-01-13 00:57:18 +00:00
|
|
|
|
borderRadius: BorderRadius.circular(8),
|
2026-01-29 15:54:22 +00:00
|
|
|
|
border: Border.all(color: appColors.info.withValues(alpha: 0.3)),
|
2026-01-13 00:57:18 +00:00
|
|
|
|
),
|
|
|
|
|
|
child: Row(
|
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
|
children: [
|
2026-01-29 15:54:22 +00:00
|
|
|
|
Icon(LucideIcons.wifi, color: appColors.info, size: 20),
|
2026-01-13 00:57:18 +00:00
|
|
|
|
const SizedBox(width: 12),
|
|
|
|
|
|
Expanded(
|
|
|
|
|
|
child: Text(
|
2026-01-29 15:54:22 +00:00
|
|
|
|
'Wi-Fi環境下での実行を推奨します\nデータ量が100MB以上になる可能性があります',
|
2026-01-13 00:57:18 +00:00
|
|
|
|
style: TextStyle(
|
|
|
|
|
|
fontSize: 12,
|
2026-01-29 15:54:22 +00:00
|
|
|
|
color: appColors.textSecondary,
|
|
|
|
|
|
fontWeight: FontWeight.w500,
|
2026-01-13 00:57:18 +00:00
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
|
2026-01-11 08:17:29 +00:00
|
|
|
|
Card(
|
2026-01-29 15:54:22 +00:00
|
|
|
|
color: appColors.surfaceSubtle,
|
|
|
|
|
|
elevation: 0,
|
|
|
|
|
|
margin: EdgeInsets.zero,
|
2026-01-11 08:17:29 +00:00
|
|
|
|
child: Column(
|
|
|
|
|
|
children: [
|
|
|
|
|
|
ListTile(
|
|
|
|
|
|
leading: Icon(
|
|
|
|
|
|
currentUser != null ? LucideIcons.checkCircle2 : LucideIcons.user,
|
2026-01-29 15:54:22 +00:00
|
|
|
|
color: currentUser != null ? appColors.success : appColors.iconSubtle,
|
2026-01-11 08:17:29 +00:00
|
|
|
|
),
|
2026-01-29 15:54:22 +00:00
|
|
|
|
title: Text(currentUser == null ? 'Googleアカウント連携' : currentUser.email,
|
|
|
|
|
|
style: TextStyle(color: appColors.textPrimary)),
|
|
|
|
|
|
subtitle: currentUser == null ? Text('Google Driveにバックアップ',
|
|
|
|
|
|
style: TextStyle(color: appColors.textSecondary)) : null,
|
2026-01-11 08:17:29 +00:00
|
|
|
|
trailing: (_state == _BackupState.signingIn || _state == _BackupState.signingOut)
|
|
|
|
|
|
? const SizedBox(width: 24, height: 24, child: CircularProgressIndicator(strokeWidth: 2))
|
|
|
|
|
|
: ElevatedButton(
|
|
|
|
|
|
style: ElevatedButton.styleFrom(
|
2026-01-29 15:54:22 +00:00
|
|
|
|
backgroundColor: currentUser == null ? appColors.brandPrimary : appColors.error,
|
|
|
|
|
|
foregroundColor: appColors.surfaceSubtle,
|
2026-01-11 08:17:29 +00:00
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 16),
|
|
|
|
|
|
),
|
2026-01-29 15:54:22 +00:00
|
|
|
|
onPressed: isAnyProcessing ? null : (currentUser == null ? _signIn : _signOut),
|
|
|
|
|
|
child: Text(
|
|
|
|
|
|
currentUser == null ? '連携' : '解除',
|
|
|
|
|
|
style: const TextStyle(fontWeight: FontWeight.bold),
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
2026-01-11 08:17:29 +00:00
|
|
|
|
),
|
|
|
|
|
|
if (currentUser != null) ...[
|
2026-01-29 15:54:22 +00:00
|
|
|
|
Divider(height: 1, color: appColors.divider),
|
2026-01-11 08:17:29 +00:00
|
|
|
|
ListTile(
|
2026-01-29 15:54:22 +00:00
|
|
|
|
leading: Icon(LucideIcons.uploadCloud, color: appColors.iconDefault),
|
|
|
|
|
|
title: Text('バックアップ', style: TextStyle(color: appColors.textPrimary)),
|
2026-01-11 08:17:29 +00:00
|
|
|
|
trailing: _state == _BackupState.backingUp
|
|
|
|
|
|
? const SizedBox(width: 24, height: 24, child: CircularProgressIndicator(strokeWidth: 2))
|
|
|
|
|
|
: null,
|
2026-01-13 10:00:16 +00:00
|
|
|
|
onTap: isAnyProcessing ? null : _confirmBackup,
|
2026-01-11 08:17:29 +00:00
|
|
|
|
),
|
2026-01-29 15:54:22 +00:00
|
|
|
|
Divider(height: 1, color: appColors.divider),
|
2026-01-11 08:17:29 +00:00
|
|
|
|
ListTile(
|
2026-01-29 15:54:22 +00:00
|
|
|
|
leading: Icon(LucideIcons.downloadCloud, color: appColors.iconDefault),
|
|
|
|
|
|
title: Text('データ復元', style: TextStyle(color: appColors.textPrimary)),
|
2026-01-11 08:17:29 +00:00
|
|
|
|
trailing: _state == _BackupState.restoring
|
|
|
|
|
|
? const SizedBox(width: 24, height: 24, child: CircularProgressIndicator(strokeWidth: 2))
|
|
|
|
|
|
: null,
|
|
|
|
|
|
onTap: isAnyProcessing ? null : _restoreBackup,
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
],
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Widget _buildSectionHeader(BuildContext context, String title, IconData icon) {
|
2026-01-29 15:54:22 +00:00
|
|
|
|
final appColors = Theme.of(context).extension<AppColors>()!;
|
2026-01-11 08:17:29 +00:00
|
|
|
|
return Padding(
|
|
|
|
|
|
padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 4),
|
|
|
|
|
|
child: Row(
|
|
|
|
|
|
children: [
|
2026-01-29 15:54:22 +00:00
|
|
|
|
Icon(icon, size: 20, color: appColors.iconDefault),
|
2026-01-11 08:17:29 +00:00
|
|
|
|
const SizedBox(width: 8),
|
|
|
|
|
|
Text(
|
|
|
|
|
|
title,
|
|
|
|
|
|
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
|
|
|
|
|
fontWeight: FontWeight.bold,
|
2026-01-29 15:54:22 +00:00
|
|
|
|
color: appColors.textPrimary,
|
2026-01-11 08:17:29 +00:00
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|