This commit is contained in:
2026-06-29 20:43:13 +03:00
parent 19db4fe688
commit 64a3f4e34e
24 changed files with 1154 additions and 152 deletions
+360
View File
@@ -0,0 +1,360 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import '../../core/l10n/app_strings.dart';
import '../../core/l10n/locale_provider.dart';
import '../../core/services/haptic_service.dart';
import '../providers/backup_provider.dart';
import '../providers/google_drive_provider.dart';
import '../providers/premium_provider.dart';
import 'error_snackbar.dart';
import '../services/backup_service.dart';
class BackupScreen extends ConsumerStatefulWidget {
const BackupScreen({super.key});
@override
ConsumerState<BackupScreen> createState() => _BackupScreenState();
}
class _BackupScreenState extends ConsumerState<BackupScreen> {
bool _backingUp = false;
bool _restoring = false;
DateTime? _lastBackupTime;
@override
void initState() {
super.initState();
_loadLastBackupTime();
}
Future<void> _loadLastBackupTime() async {
final service = ref.read(googleDriveServiceProvider);
final time = await service.getLastBackupTime();
if (mounted) {
setState(() => _lastBackupTime = time);
}
}
Future<void> _handleBackup() async {
final s = ref.read(stringsProvider);
final driveService = ref.read(googleDriveServiceProvider);
if (driveService.currentUser == null) {
showErrorSnackbar(context, s.backupRequiresSignIn);
return;
}
HapticService.light();
setState(() => _backingUp = true);
try {
final backupService = ref.read(backupServiceProvider);
final payload = <String, dynamic>{
'version': 1,
'exported_at': DateTime.now().toIso8601String(),
};
final data = backupService.createBackup(payload);
final result = await driveService.uploadBackup(data);
if (result.success) {
setState(() => _lastBackupTime = result.modifiedTime);
HapticService.medium();
if (mounted) {
showSuccessSnackbar(context, s.backupSuccess);
}
} else {
if (mounted) {
showErrorSnackbar(context, result.error ?? s.backupRestoreFailed);
}
}
} catch (e) {
if (mounted) {
showErrorSnackbar(context, e.toString());
}
} finally {
if (mounted) setState(() => _backingUp = false);
}
}
Future<void> _handleRestore() async {
final s = ref.read(stringsProvider);
final driveService = ref.read(googleDriveServiceProvider);
if (driveService.currentUser == null) {
showErrorSnackbar(context, s.backupRequiresSignIn);
return;
}
HapticService.light();
setState(() => _restoring = true);
try {
final raw = await driveService.downloadBackup();
if (raw == null) {
if (mounted) {
showWarningSnackbar(context, s.backupNoFileFound);
}
return;
}
final backupService = ref.read(backupServiceProvider);
final (result, data) = backupService.verifyAndParse(raw);
switch (result) {
case BackupVerifyResult.ok:
HapticService.medium();
if (mounted) {
showSuccessSnackbar(context, s.backupRestoreSuccess);
}
case BackupVerifyResult.tokenMismatch:
if (mounted) {
showErrorSnackbar(context, s.backupTokenMismatch);
}
case BackupVerifyResult.noToken:
if (mounted) {
showErrorSnackbar(context, s.backupNoToken);
}
case BackupVerifyResult.invalidFormat:
if (mounted) {
showErrorSnackbar(context, s.backupInvalidFormat);
}
}
} catch (e) {
if (mounted) {
showErrorSnackbar(context, e.toString());
}
} finally {
if (mounted) setState(() => _restoring = false);
}
}
@override
Widget build(BuildContext context) {
final s = ref.watch(stringsProvider);
final isPremium = ref.watch(isPremiumProvider);
final driveUserAsync = ref.watch(googleDriveUserProvider);
final driveUser = driveUserAsync.value;
final colorScheme = Theme.of(context).colorScheme;
return Scaffold(
body: SafeArea(
child: Column(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(8, 4, 16, 0),
child: Row(
children: [
IconButton(
icon: const Icon(Icons.arrow_back_rounded),
onPressed: () => context.pop(),
),
Text(
s.backupTitle,
style: Theme.of(context).textTheme.titleLarge?.copyWith(
fontWeight: FontWeight.w800,
),
),
],
),
),
Expanded(
child: SingleChildScrollView(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (!isPremium) ...[
_buildLockedState(context, s, colorScheme),
] else ...[
_buildSyncStatus(context, s, colorScheme, driveUser),
const SizedBox(height: 20),
_buildBackupActions(context, s, colorScheme, driveUser),
const SizedBox(height: 20),
_buildLastBackup(context, s, colorScheme),
],
],
),
),
),
],
),
),
);
}
Widget _buildLockedState(
BuildContext context,
AppStrings s,
ColorScheme colorScheme,
) {
return Container(
width: double.infinity,
padding: const EdgeInsets.all(24),
decoration: BoxDecoration(
color: colorScheme.surface,
borderRadius: BorderRadius.circular(16),
border: Border.all(color: colorScheme.outlineVariant),
),
child: Column(
children: [
Icon(Icons.lock_outline_rounded, size: 48, color: colorScheme.primary),
const SizedBox(height: 16),
Text(
s.backupRequiresPremium,
style: Theme.of(context).textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.w600,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 16),
FilledButton(
onPressed: () => context.push('/pro'),
child: Text(s.proBuy),
),
],
),
);
}
Widget _buildSyncStatus(
BuildContext context,
AppStrings s,
ColorScheme colorScheme,
dynamic driveUser,
) {
return Container(
width: double.infinity,
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: colorScheme.surface,
borderRadius: BorderRadius.circular(16),
border: Border.all(color: colorScheme.outlineVariant),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Icon(
driveUser != null
? Icons.cloud_done_rounded
: Icons.cloud_off_rounded,
color: driveUser != null ? colorScheme.primary : colorScheme.onSurface.withOpacity(0.4),
size: 24,
),
const SizedBox(width: 12),
Expanded(
child: Text(
driveUser != null
? s.proSyncEnabled
: s.backupRequiresSignIn,
style: Theme.of(context).textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.w600,
),
),
),
],
),
if (driveUser != null) ...[
const SizedBox(height: 8),
Text(
driveUser.email,
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color: colorScheme.onSurface.withOpacity(0.6),
),
),
],
],
),
);
}
Widget _buildBackupActions(
BuildContext context,
AppStrings s,
ColorScheme colorScheme,
dynamic driveUser,
) {
final disabled = driveUser == null;
return Column(
children: [
SizedBox(
width: double.infinity,
child: FilledButton.icon(
onPressed: (disabled || _backingUp) ? null : _handleBackup,
icon: _backingUp
? SizedBox(
width: 18,
height: 18,
child: CircularProgressIndicator(
strokeWidth: 2,
color: colorScheme.onPrimary,
),
)
: const Icon(Icons.backup_outlined),
label: Text(_backingUp ? s.backupCreating : s.backupCreate),
style: FilledButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 14),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
),
),
const SizedBox(height: 12),
SizedBox(
width: double.infinity,
child: OutlinedButton.icon(
onPressed: (disabled || _restoring) ? null : _handleRestore,
icon: _restoring
? SizedBox(
width: 18,
height: 18,
child: CircularProgressIndicator(
strokeWidth: 2,
color: colorScheme.primary,
),
)
: const Icon(Icons.restore_rounded),
label: Text(_restoring ? s.backupRestoring : s.backupRestore),
style: OutlinedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 14),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
),
),
],
);
}
Widget _buildLastBackup(
BuildContext context,
AppStrings s,
ColorScheme colorScheme,
) {
return Row(
children: [
Icon(
Icons.schedule_rounded,
size: 18,
color: colorScheme.onSurface.withOpacity(0.5),
),
const SizedBox(width: 8),
Text(
'${s.backupLastBackup}: ${_lastBackupTime != null ? _formatDate(_lastBackupTime!) : s.backupNever}',
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color: colorScheme.onSurface.withOpacity(0.5),
),
),
],
);
}
String _formatDate(DateTime dt) {
final d = dt.toLocal();
return '${d.day.toString().padLeft(2, '0')}.${d.month.toString().padLeft(2, '0')}.${d.year} ${d.hour.toString().padLeft(2, '0')}:${d.minute.toString().padLeft(2, '0')}';
}
}
+20 -23
View File
@@ -4,11 +4,9 @@ import 'package:go_router/go_router.dart';
import '../../core/l10n/app_strings.dart';
import '../../core/l10n/locale_provider.dart';
import '../../core/services/haptic_service.dart';
import '../feature_flags/feature_flags_provider.dart';
import '../models/user_model.dart';
import '../providers/billing_provider.dart';
import '../providers/current_user_provider.dart';
import '../providers/google_auth_provider.dart';
import '../providers/google_drive_provider.dart';
import '../providers/premium_provider.dart';
import 'error_snackbar.dart';
class ProScreen extends ConsumerStatefulWidget {
@@ -68,10 +66,9 @@ class _ProScreenState extends ConsumerState<ProScreen>
@override
Widget build(BuildContext context) {
final s = ref.watch(stringsProvider);
final flags = ref.watch(featureFlagsProvider);
final isVip = flags.maxAccounts != 3;
final googleUserAsync = ref.watch(googleCurrentUserProvider);
final googleUser = googleUserAsync.value;
final isPremium = ref.watch(isPremiumProvider);
final driveUserAsync = ref.watch(googleDriveUserProvider);
final driveUser = driveUserAsync.value;
final colorScheme = Theme.of(context).colorScheme;
return Stack(
@@ -88,18 +85,18 @@ class _ProScreenState extends ConsumerState<ProScreen>
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(height: 4),
_buildHeroBanner(context, s, colorScheme, isVip),
_buildHeroBanner(context, s, colorScheme, isPremium),
const SizedBox(height: 20),
_buildFeatureList(context, s, colorScheme),
const SizedBox(height: 16),
if (isVip) ...[
_buildProActiveSection(context, s, colorScheme, googleUser),
if (isPremium) ...[
_buildProActiveSection(context, s, colorScheme, driveUser),
],
],
),
),
),
_buildBottomBar(context, s, colorScheme, isVip),
_buildBottomBar(context, s, colorScheme, isPremium),
],
),
),
@@ -476,17 +473,17 @@ class _ProScreenState extends ConsumerState<ProScreen>
HapticService.light();
setState(() => _purchasing = true);
try {
final billing = ref.read(billingServiceProvider);
final success = await billing.purchasePro();
if (success) {
await ref.read(currentUserProvider.notifier).setPlan(UserPlan.vip);
final manager = ref.read(premiumManagerProvider);
final result = await manager.purchase();
if (result.success) {
await ref.read(currentUserProvider.notifier).refreshFromPremium();
if (mounted) {
_showSuccessOverlay();
HapticService.medium();
}
} else {
if (mounted) {
showErrorSnackbar(context, ref.read(stringsProvider).proPurchaseFailed);
showErrorSnackbar(context, result.error ?? ref.read(stringsProvider).proPurchaseFailed);
}
}
} catch (e) {
@@ -502,10 +499,10 @@ class _ProScreenState extends ConsumerState<ProScreen>
HapticService.light();
setState(() => _restoring = true);
try {
final billing = ref.read(billingServiceProvider);
final success = await billing.restorePurchases();
if (success) {
await ref.read(currentUserProvider.notifier).setPlan(UserPlan.vip);
final manager = ref.read(premiumManagerProvider);
final result = await manager.restore();
if (result.success) {
await ref.read(currentUserProvider.notifier).refreshFromPremium();
if (mounted) {
_showSuccessOverlay();
HapticService.medium();
@@ -527,7 +524,7 @@ class _ProScreenState extends ConsumerState<ProScreen>
Future<void> _handleGoogleSignIn() async {
HapticService.light();
try {
final service = ref.read(googleAuthProvider);
final service = ref.read(googleDriveServiceProvider);
await service.signIn();
} catch (e) {
if (mounted) {
@@ -539,7 +536,7 @@ class _ProScreenState extends ConsumerState<ProScreen>
Future<void> _handleGoogleSignOut() async {
HapticService.light();
try {
final service = ref.read(googleAuthProvider);
final service = ref.read(googleDriveServiceProvider);
await service.signOut();
} catch (e) {
if (mounted) {
+94 -62
View File
@@ -3,8 +3,8 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import '../../core/l10n/locale_provider.dart';
import '../../core/services/haptic_service.dart';
import '../feature_flags/feature_flags_provider.dart';
import '../providers/google_auth_provider.dart';
import '../providers/google_drive_provider.dart';
import '../providers/premium_provider.dart';
class ProSubscriptionCard extends ConsumerWidget {
const ProSubscriptionCard({super.key});
@@ -18,10 +18,9 @@ class ProSubscriptionCard extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final s = ref.watch(stringsProvider);
final flags = ref.watch(featureFlagsProvider);
final isVip = flags.maxAccounts != 3;
final googleUserAsync = ref.watch(googleCurrentUserProvider);
final googleUser = googleUserAsync.value;
final isPremium = ref.watch(isPremiumProvider);
final driveUserAsync = ref.watch(googleDriveUserProvider);
final driveUser = driveUserAsync.value;
return Container(
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 24),
@@ -45,7 +44,9 @@ class ProSubscriptionCard extends ConsumerWidget {
borderRadius: BorderRadius.circular(12),
),
child: Icon(
isVip ? Icons.verified_rounded : Icons.workspace_premium_rounded,
isPremium
? Icons.verified_rounded
: Icons.workspace_premium_rounded,
color: Colors.white,
size: 28,
),
@@ -57,80 +58,111 @@ class ProSubscriptionCard extends ConsumerWidget {
children: [
Text(
s.proTitle,
style: Theme.of(context).textTheme.titleLarge?.copyWith(
fontWeight: FontWeight.w900,
color: Colors.white,
),
style:
Theme.of(context).textTheme.titleLarge?.copyWith(
fontWeight: FontWeight.w900,
color: Colors.white,
),
),
const SizedBox(height: 4),
Text(
isPremium ? s.proActive : s.proSubtitle,
style: isPremium
? Theme.of(context).textTheme.bodyMedium?.copyWith(
color: Colors.white.withOpacity(0.9),
fontWeight: FontWeight.w600,
)
: Theme.of(context).textTheme.bodySmall?.copyWith(
color: Colors.white.withOpacity(0.7),
),
),
if (isVip) ...[
const SizedBox(height: 4),
Text(
s.proActive,
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
color: Colors.white.withOpacity(0.9),
fontWeight: FontWeight.w600,
),
),
] else ...[
const SizedBox(height: 4),
Text(
s.proSubtitle,
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color: Colors.white.withOpacity(0.7),
),
),
],
],
),
),
],
),
const SizedBox(height: 16),
Row(
children: [
Expanded(
child: OutlinedButton(
if (!isPremium) ...[
Row(
children: [
Expanded(
child: OutlinedButton(
onPressed: () {
HapticService.light();
context.push('/pro');
},
style: OutlinedButton.styleFrom(
foregroundColor: Colors.white,
side: BorderSide(
color: Colors.white.withOpacity(0.4), width: 1.5),
padding: const EdgeInsets.symmetric(vertical: 12),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
child: Text(
s.proAboutPro,
style: const TextStyle(
fontWeight: FontWeight.w700, fontSize: 15),
),
),
),
],
),
] else ...[
Row(
children: [
Expanded(
child: OutlinedButton.icon(
onPressed: () {
HapticService.light();
context.push('/backup');
},
icon: Icon(
driveUser != null
? Icons.cloud_done_rounded
: Icons.cloud_sync_rounded,
color: Colors.white,
size: 20,
),
label: Text(s.backupSyncWithDrive),
style: OutlinedButton.styleFrom(
foregroundColor: Colors.white,
side: BorderSide(
color: Colors.white.withOpacity(0.4), width: 1.5),
padding: const EdgeInsets.symmetric(vertical: 12),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
),
),
],
),
if (driveUser == null) ...[
const SizedBox(height: 10),
SizedBox(
width: double.infinity,
child: OutlinedButton.icon(
onPressed: () {
HapticService.light();
context.push('/pro');
ref.read(googleDriveServiceProvider).signIn();
},
icon: const Icon(Icons.login_rounded,
color: Colors.white, size: 18),
label: Text(s.proSignInGoogle),
style: OutlinedButton.styleFrom(
foregroundColor: Colors.white,
side: BorderSide(color: Colors.white.withOpacity(0.4), width: 1.5),
padding: const EdgeInsets.symmetric(vertical: 12),
side: BorderSide(
color: Colors.white.withOpacity(0.3), width: 1),
padding: const EdgeInsets.symmetric(vertical: 10),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
child: Text(
s.proAboutPro,
style: const TextStyle(fontWeight: FontWeight.w700, fontSize: 15),
),
),
),
],
),
if (isVip && googleUser == null) ...[
const SizedBox(height: 10),
SizedBox(
width: double.infinity,
child: OutlinedButton.icon(
onPressed: () {
HapticService.light();
ref.read(googleAuthProvider).signIn();
},
icon: const Icon(Icons.login_rounded, color: Colors.white, size: 18),
label: Text(s.proSignInGoogle),
style: OutlinedButton.styleFrom(
foregroundColor: Colors.white,
side: BorderSide(color: Colors.white.withOpacity(0.3), width: 1),
padding: const EdgeInsets.symmetric(vertical: 10),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
),
),
],
],
),