|
1 | 1 | import 'dart:convert'; |
2 | 2 | import 'dart:developer'; |
3 | 3 | import 'dart:io'; |
| 4 | +import 'package:flutter/material.dart'; |
4 | 5 | import 'package:Bloomee/core/models/exported.dart'; |
| 6 | +import 'package:Bloomee/core/theme/app_theme.dart'; |
5 | 7 | import 'package:Bloomee/screens/widgets/snackbar.dart'; |
6 | 8 | import 'package:Bloomee/services/db/global_db.dart'; |
7 | 9 | import 'package:Bloomee/services/db/db_provider.dart'; |
8 | 10 | import 'package:Bloomee/services/db/dao/playlist_dao.dart'; |
9 | 11 | import 'package:Bloomee/services/db/legacy/legacy_media_id_mapper.dart'; |
10 | 12 | import 'package:Bloomee/services/db/dao/track_dao.dart'; |
11 | 13 | import 'package:Bloomee/services/m3u_processor.dart'; |
| 14 | +import 'package:Bloomee/services/storage_backup_service.dart'; |
12 | 15 | import 'package:package_info_plus/package_info_plus.dart'; |
13 | 16 | import 'package:path_provider/path_provider.dart'; |
| 17 | +import 'package:Bloomee/l10n/app_localizations.dart'; |
14 | 18 |
|
15 | 19 | /// Service for importing and exporting playlists and tracks. |
16 | 20 | /// |
@@ -293,6 +297,9 @@ class ImportExportService { |
293 | 297 | } else if (data.containsKey('title') && |
294 | 298 | (data.containsKey('mediaId') || data.containsKey('duration'))) { |
295 | 299 | return await importMediaItem(filePath); |
| 300 | + } else if (data.containsKey('playlists') && data.containsKey('media_items')) { |
| 301 | + final result = await DBProvider.restoreLegacyJsonBackup(filePath); |
| 302 | + return result['success'] == true; |
296 | 303 | } else { |
297 | 304 | throw const FormatException("Unrecognized JSON structure."); |
298 | 305 | } |
@@ -426,4 +433,226 @@ class ImportExportService { |
426 | 433 | return null; |
427 | 434 | } |
428 | 435 | } |
| 436 | + |
| 437 | + /// Production-level entry point to handle any shared or picked import/restore file. |
| 438 | + /// Handles: Isar snapshots (.isar/.db), legacy full JSON backups, and playlist/song JSON exports. |
| 439 | + static Future<void> handleImportOrRestore(BuildContext context, String filePath) async { |
| 440 | + final l10n = AppLocalizations.of(context)!; |
| 441 | + try { |
| 442 | + final file = File(filePath); |
| 443 | + if (!await file.exists()) { |
| 444 | + SnackbarService.showMessage(l10n.storageRestoreNoFile); |
| 445 | + return; |
| 446 | + } |
| 447 | + |
| 448 | + final payloadType = await StorageBackupService.detectPayloadType(file); |
| 449 | + |
| 450 | + switch (payloadType) { |
| 451 | + case RestorePayloadType.isarSnapshot: |
| 452 | + if (!context.mounted) return; |
| 453 | + final confirm = await showDialog<bool>( |
| 454 | + context: context, |
| 455 | + builder: (ctx) => AlertDialog( |
| 456 | + backgroundColor: const Color.fromARGB(255, 24, 24, 24), |
| 457 | + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)), |
| 458 | + title: Text( |
| 459 | + l10n.storageRestoreConfirmTitle, |
| 460 | + style: const TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 18), |
| 461 | + ), |
| 462 | + content: Text( |
| 463 | + "${l10n.storageRestoreConfirmPrefix}\n\n${l10n.storageRestoreMediaBullet}\n${l10n.storageRestoreHistoryBullet}\n\n${l10n.storageRestoreConfirmSuffix}", |
| 464 | + style: const TextStyle(color: Colors.white70, fontSize: 14, height: 1.5), |
| 465 | + ), |
| 466 | + actions: [ |
| 467 | + TextButton( |
| 468 | + onPressed: () => Navigator.pop(ctx, false), |
| 469 | + child: Text(l10n.storageRestoreNo, style: const TextStyle(color: Colors.white70)), |
| 470 | + ), |
| 471 | + FilledButton( |
| 472 | + style: FilledButton.styleFrom( |
| 473 | + backgroundColor: Default_Theme.accentColor2, |
| 474 | + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)), |
| 475 | + ), |
| 476 | + onPressed: () => Navigator.pop(ctx, true), |
| 477 | + child: Text(l10n.storageRestoreYes, style: const TextStyle(color: Colors.white, fontWeight: FontWeight.bold)), |
| 478 | + ), |
| 479 | + ], |
| 480 | + ), |
| 481 | + ); |
| 482 | + |
| 483 | + if (confirm != true) return; |
| 484 | + if (!context.mounted) return; |
| 485 | + |
| 486 | + // Show progress dialog |
| 487 | + showDialog( |
| 488 | + context: context, |
| 489 | + barrierDismissible: false, |
| 490 | + builder: (ctx) => WillPopScope( |
| 491 | + onWillPop: () async => false, |
| 492 | + child: Center( |
| 493 | + child: Container( |
| 494 | + padding: const EdgeInsets.all(24), |
| 495 | + decoration: BoxDecoration( |
| 496 | + color: const Color.fromARGB(255, 24, 24, 24), |
| 497 | + borderRadius: BorderRadius.circular(16), |
| 498 | + ), |
| 499 | + child: Column( |
| 500 | + mainAxisSize: MainAxisSize.min, |
| 501 | + children: [ |
| 502 | + const CircularProgressIndicator(color: Default_Theme.accentColor2), |
| 503 | + const SizedBox(height: 16), |
| 504 | + Text(l10n.storageRestoring, style: const TextStyle(color: Colors.white, decoration: TextDecoration.none, fontSize: 14), textAlign: TextAlign.center), |
| 505 | + ], |
| 506 | + ), |
| 507 | + ), |
| 508 | + ), |
| 509 | + ), |
| 510 | + ); |
| 511 | + |
| 512 | + final result = await StorageBackupService.restoreBackup(filePath); |
| 513 | + if (!context.mounted) return; |
| 514 | + Navigator.pop(context); // Dismiss progress dialog |
| 515 | + |
| 516 | + final success = result['success'] == true; |
| 517 | + showDialog( |
| 518 | + context: context, |
| 519 | + builder: (ctx) => AlertDialog( |
| 520 | + backgroundColor: const Color.fromARGB(255, 24, 24, 24), |
| 521 | + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)), |
| 522 | + title: Text( |
| 523 | + success ? l10n.storageRestoreCompleted : l10n.storageRestoreFailedTitle, |
| 524 | + style: const TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 18), |
| 525 | + ), |
| 526 | + content: Text( |
| 527 | + success |
| 528 | + ? l10n.storageRestoreSuccessMessage |
| 529 | + : "${l10n.storageRestoreFailedMessage}\n- ${result['error'] ?? l10n.storageRestoreUnknownError}", |
| 530 | + style: const TextStyle(color: Colors.white70, fontSize: 14, height: 1.5), |
| 531 | + ), |
| 532 | + actions: [ |
| 533 | + FilledButton( |
| 534 | + style: FilledButton.styleFrom( |
| 535 | + backgroundColor: Default_Theme.accentColor2, |
| 536 | + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)), |
| 537 | + ), |
| 538 | + onPressed: () => Navigator.pop(ctx), |
| 539 | + child: Text(l10n.buttonOk, style: const TextStyle(color: Colors.white, fontWeight: FontWeight.bold)), |
| 540 | + ), |
| 541 | + ], |
| 542 | + ), |
| 543 | + ); |
| 544 | + break; |
| 545 | + |
| 546 | + case RestorePayloadType.legacyFullJson: |
| 547 | + if (!context.mounted) return; |
| 548 | + final confirm = await showDialog<bool>( |
| 549 | + context: context, |
| 550 | + builder: (ctx) => AlertDialog( |
| 551 | + backgroundColor: const Color.fromARGB(255, 24, 24, 24), |
| 552 | + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)), |
| 553 | + title: Text( |
| 554 | + l10n.storageRestoreConfirmTitle, |
| 555 | + style: const TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 18), |
| 556 | + ), |
| 557 | + content: Text( |
| 558 | + "${l10n.storageRestoreConfirmPrefix}\n\n${l10n.storageRestoreMediaBullet}\n\n${l10n.storageRestoreConfirmSuffix}", |
| 559 | + style: const TextStyle(color: Colors.white70, fontSize: 14, height: 1.5), |
| 560 | + ), |
| 561 | + actions: [ |
| 562 | + TextButton( |
| 563 | + onPressed: () => Navigator.pop(ctx, false), |
| 564 | + child: Text(l10n.storageRestoreNo, style: const TextStyle(color: Colors.white70)), |
| 565 | + ), |
| 566 | + FilledButton( |
| 567 | + style: FilledButton.styleFrom( |
| 568 | + backgroundColor: Default_Theme.accentColor2, |
| 569 | + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)), |
| 570 | + ), |
| 571 | + onPressed: () => Navigator.pop(ctx, true), |
| 572 | + child: Text(l10n.storageRestoreYes, style: const TextStyle(color: Colors.white, fontWeight: FontWeight.bold)), |
| 573 | + ), |
| 574 | + ], |
| 575 | + ), |
| 576 | + ); |
| 577 | + |
| 578 | + if (confirm != true) return; |
| 579 | + if (!context.mounted) return; |
| 580 | + |
| 581 | + // Show progress dialog |
| 582 | + showDialog( |
| 583 | + context: context, |
| 584 | + barrierDismissible: false, |
| 585 | + builder: (ctx) => WillPopScope( |
| 586 | + onWillPop: () async => false, |
| 587 | + child: Center( |
| 588 | + child: Container( |
| 589 | + padding: const EdgeInsets.all(24), |
| 590 | + decoration: BoxDecoration( |
| 591 | + color: const Color.fromARGB(255, 24, 24, 24), |
| 592 | + borderRadius: BorderRadius.circular(16), |
| 593 | + ), |
| 594 | + child: Column( |
| 595 | + mainAxisSize: MainAxisSize.min, |
| 596 | + children: [ |
| 597 | + const CircularProgressIndicator(color: Default_Theme.accentColor2), |
| 598 | + const SizedBox(height: 16), |
| 599 | + Text(l10n.storageRestoring, style: const TextStyle(color: Colors.white, decoration: TextDecoration.none, fontSize: 14), textAlign: TextAlign.center), |
| 600 | + ], |
| 601 | + ), |
| 602 | + ), |
| 603 | + ), |
| 604 | + ), |
| 605 | + ); |
| 606 | + |
| 607 | + final result = await DBProvider.restoreLegacyJsonBackup(filePath); |
| 608 | + if (!context.mounted) return; |
| 609 | + Navigator.pop(context); // Dismiss progress dialog |
| 610 | + |
| 611 | + final success = result['success'] == true; |
| 612 | + showDialog( |
| 613 | + context: context, |
| 614 | + builder: (ctx) => AlertDialog( |
| 615 | + backgroundColor: const Color.fromARGB(255, 24, 24, 24), |
| 616 | + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)), |
| 617 | + title: Text( |
| 618 | + success ? l10n.storageRestoreCompleted : l10n.storageRestoreFailedTitle, |
| 619 | + style: const TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 18), |
| 620 | + ), |
| 621 | + content: Text( |
| 622 | + success |
| 623 | + ? l10n.storageRestoreSuccessMessage |
| 624 | + : "${l10n.storageRestoreFailedMessage}\n- ${result['error'] ?? l10n.storageRestoreUnknownError}", |
| 625 | + style: const TextStyle(color: Colors.white70, fontSize: 14, height: 1.5), |
| 626 | + ), |
| 627 | + actions: [ |
| 628 | + FilledButton( |
| 629 | + style: FilledButton.styleFrom( |
| 630 | + backgroundColor: Default_Theme.accentColor2, |
| 631 | + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)), |
| 632 | + ), |
| 633 | + onPressed: () => Navigator.pop(ctx), |
| 634 | + child: Text(l10n.buttonOk, style: const TextStyle(color: Colors.white, fontWeight: FontWeight.bold)), |
| 635 | + ), |
| 636 | + ], |
| 637 | + ), |
| 638 | + ); |
| 639 | + break; |
| 640 | + |
| 641 | + case RestorePayloadType.playlistOrTrackJson: |
| 642 | + SnackbarService.showMessage(l10n.snackbarImportingMedia); |
| 643 | + final res = await importJSON(filePath); |
| 644 | + if (res) { |
| 645 | + SnackbarService.showMessage(l10n.snackbarImportCompleted); |
| 646 | + } |
| 647 | + break; |
| 648 | + |
| 649 | + case RestorePayloadType.unsupported: |
| 650 | + SnackbarService.showMessage(l10n.snackbarInvalidFileFormat); |
| 651 | + break; |
| 652 | + } |
| 653 | + } catch (e) { |
| 654 | + log("Error during import/restore: $e", name: "ImportExportService"); |
| 655 | + SnackbarService.showMessage(l10n.storageUnexpectedError); |
| 656 | + } |
| 657 | + } |
429 | 658 | } |
0 commit comments