From e9816c08927a2d9d34ffa21814cc0232406588c0 Mon Sep 17 00:00:00 2001 From: Yassin Date: Fri, 6 Dec 2024 11:56:43 +0200 Subject: [PATCH] feat(quran): Add moshaf type and version to update notification - Enhanced `UpdateAvailable` state to include `moshafType` (Hafs or Warsh) alongside version information. - Updated notifier logic to handle `moshafType` when checking and comparing Quran updates. - Localized new dialog content with placeholders for `moshafName` and `version` in `intl_en.arb`. - Refactored update dialog to display the moshaf type and version dynamically. - Streamlined `downloadQuran` functionality to pass the correct moshaf type to the notifier. --- lib/l10n/intl_en.arb | 14 +++++++++++ .../quran/widget/download_quran_popup.dart | 23 ++++++++----------- .../download_quran_notifier.dart | 15 +++++++++--- .../download_quran/download_quran_state.dart | 11 ++++++++- 4 files changed, 45 insertions(+), 18 deletions(-) diff --git a/lib/l10n/intl_en.arb b/lib/l10n/intl_en.arb index 237a0b3fd..aa41e2135 100644 --- a/lib/l10n/intl_en.arb +++ b/lib/l10n/intl_en.arb @@ -395,5 +395,19 @@ "example": "604" } } + }, + "quranUpdateDialogContent": "A new update for the {moshafName} Quran (version {version}) is available.", + "@quranUpdateDialogContent": { + "description": "Message to display in the update available dialog with Quran name and version.", + "placeholders": { + "moshafName": { + "type": "String", + "example": "Hafs" + }, + "version": { + "type": "String", + "example": "2.0" + } + } } } diff --git a/lib/src/pages/quran/widget/download_quran_popup.dart b/lib/src/pages/quran/widget/download_quran_popup.dart index bc6db8535..9900c2508 100644 --- a/lib/src/pages/quran/widget/download_quran_popup.dart +++ b/lib/src/pages/quran/widget/download_quran_popup.dart @@ -59,6 +59,7 @@ class _DownloadQuranDialogState extends ConsumerState { Extracting() => _buildExtractingDialog(context, state), Success() => _handleSuccess(context), CancelDownload() => const SizedBox(), + UpdateAvailable() => _buildUpdateAvailableDialog(context, state), _ => const SizedBox(), }; } @@ -87,8 +88,14 @@ class _DownloadQuranDialogState extends ConsumerState { } Widget _buildUpdateAvailableDialog(BuildContext context, UpdateAvailable state) { + final moshafName = switch (state.moshafType) { + MoshafType.warsh => S.of(context).warsh, + MoshafType.hafs => S.of(context).hafs, + }; + return AlertDialog( title: Text(S.of(context).updateAvailable), + content: Text(S.of(context).quranUpdateDialogContent(moshafName, state.version)), actions: [ TextButton( onPressed: () => Navigator.pop(context), @@ -97,8 +104,8 @@ class _DownloadQuranDialogState extends ConsumerState { TextButton( autofocus: true, onPressed: () { - // final notifier = ref.read(downloadQuranNotifierProvider.notifier); - // notifier.downloadQuran(notifier.selectedMoshafType); + final notifier = ref.read(downloadQuranNotifierProvider.notifier); + notifier.downloadQuran(state.moshafType); }, child: Text(S.of(context).download), ), @@ -171,18 +178,6 @@ class _DownloadQuranDialogState extends ConsumerState { ); } - Widget _buildNoUpdateDialog(BuildContext context, NoUpdate state) { - return AlertDialog( - title: Text(S.of(context).updatedQuran), - actions: [ - TextButton( - onPressed: () => Navigator.pop(context), - child: Text(S.of(context).ok), - ), - ], - ); - } - Widget _buildChooseDownloadMoshaf(BuildContext context) { return Focus( focusNode: _dialogFocusNode, diff --git a/lib/src/state_management/quran/download_quran/download_quran_notifier.dart b/lib/src/state_management/quran/download_quran/download_quran_notifier.dart index 46e535360..99fbb8cb6 100644 --- a/lib/src/state_management/quran/download_quran/download_quran_notifier.dart +++ b/lib/src/state_management/quran/download_quran/download_quran_notifier.dart @@ -81,7 +81,10 @@ class DownloadQuranNotifier extends AutoDisposeAsyncNotifier orElse: () async { final remoteVersion = await downloadQuranRepoImpl.getRemoteQuranVersion(moshafType: moshafType); return localVersionOption.fold( - () => UpdateAvailable(remoteVersion), + () => UpdateAvailable( + version: remoteVersion, + moshafType: moshafType, + ), (localVersion) => _compareVersions(moshafType, localVersion, remoteVersion), ); }, @@ -90,7 +93,10 @@ class DownloadQuranNotifier extends AutoDisposeAsyncNotifier final remoteVersion = await downloadQuranRepoImpl.getRemoteQuranVersion(moshafType: moshafType); return localVersionOption.fold( - () => UpdateAvailable(remoteVersion), + () => UpdateAvailable( + version: remoteVersion, + moshafType: moshafType, + ), (localVersion) => _compareVersions(moshafType, localVersion, remoteVersion), ); } else { @@ -115,7 +121,10 @@ class DownloadQuranNotifier extends AutoDisposeAsyncNotifier Future _compareVersions(MoshafType moshafType, String localVersion, String remoteVersion) async { if (VersionHelper.isNewer(remoteVersion, localVersion)) { - return UpdateAvailable(remoteVersion); + return UpdateAvailable( + version: remoteVersion, + moshafType: moshafType, + ); } else { final savePath = await getApplicationSupportDirectory(); final quranPathHelper = QuranPathHelper( diff --git a/lib/src/state_management/quran/download_quran/download_quran_state.dart b/lib/src/state_management/quran/download_quran/download_quran_state.dart index 81a52a95a..1619bee28 100644 --- a/lib/src/state_management/quran/download_quran/download_quran_state.dart +++ b/lib/src/state_management/quran/download_quran/download_quran_state.dart @@ -46,8 +46,17 @@ class NoUpdate extends DownloadQuranState with EquatableMixin { class UpdateAvailable extends DownloadQuranState { final String version; + final MoshafType moshafType; + + const UpdateAvailable({ + required this.version, + required this.moshafType, + }); - const UpdateAvailable(this.version); + @override + String toString() { + return 'UpdateAvailable: $version , $moshafType'; + } } class CancelDownload extends DownloadQuranState {