Skip to content

Commit

Permalink
feat: update Quran configuration file URL to tv_config.json (#1465)
Browse files Browse the repository at this point in the history
* feat: update Quran configuration file URL to tv_config.json

* 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.

* fix: solve the overlapping issue when updating the Quran version

* fix formatting
  • Loading branch information
YassinNouh21 authored Dec 10, 2024
1 parent 6422442 commit 90eaa39
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 22 deletions.
14 changes: 14 additions & 0 deletions lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
}
2 changes: 1 addition & 1 deletion lib/src/const/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ abstract class QuranConstant {
static const String kQuranModePref = 'quran_mode';
static const String kSavedCurrentPage = 'saved_current_page';
static const String kFavoriteReciterBox = 'favorite_reciter_box';
static const String quranMoshafConfigJsonUrl = 'https://cdn.mawaqit.net/quran/config.json';
static const String quranMoshafConfigJsonUrl = 'https://cdn.mawaqit.net/quran/tv_config.json';
static const String kIsFirstTime = 'is_first_time_quran';
static const String kQuranReciterImagesBaseUrl = 'https://cdn.mawaqit.net/quran/reciters-pictures/';
}
Expand Down
20 changes: 20 additions & 0 deletions lib/src/data/repository/quran/quran_download_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class QuranDownloadRepositoryImpl implements QuranDownloadRepository {
String? filePath,
}) async {
try {
// Clean up existing files before downloading
await _cleanupExistingFiles();

await remoteDataSource.downloadQuranWithProgress(
version: version,
moshafType: moshafType,
Expand Down Expand Up @@ -75,6 +78,23 @@ class QuranDownloadRepositoryImpl implements QuranDownloadRepository {
}
}

Future<void> _cleanupExistingFiles() async {
try {
// Delete the quran directory if it exists
if (await Directory(quranPathHelper.quranDirectoryPath).exists()) {
await Directory(quranPathHelper.quranDirectoryPath).delete(recursive: true);
}

// Delete the zip directory if it exists
if (await Directory(quranPathHelper.quranZipDirectoryPath).exists()) {
await Directory(quranPathHelper.quranZipDirectoryPath).delete(recursive: true);
}
} catch (e) {
// Log error but don't throw, as this is cleanup
print('Error during cleanup: $e');
}
}

Future<void> _cleanupAfterCancellation(String version) async {
await DirectoryHelper.deleteDirectories([
quranPathHelper.quranZipDirectoryPath,
Expand Down
23 changes: 9 additions & 14 deletions lib/src/pages/quran/widget/download_quran_popup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class _DownloadQuranDialogState extends ConsumerState<DownloadQuranDialog> {
Extracting() => _buildExtractingDialog(context, state),
Success() => _handleSuccess(context),
CancelDownload() => const SizedBox(),
UpdateAvailable() => _buildUpdateAvailableDialog(context, state),
_ => const SizedBox(),
};
}
Expand Down Expand Up @@ -87,8 +88,14 @@ class _DownloadQuranDialogState extends ConsumerState<DownloadQuranDialog> {
}

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),
Expand All @@ -97,8 +104,8 @@ class _DownloadQuranDialogState extends ConsumerState<DownloadQuranDialog> {
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),
),
Expand Down Expand Up @@ -171,18 +178,6 @@ class _DownloadQuranDialogState extends ConsumerState<DownloadQuranDialog> {
);
}

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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ class DownloadQuranNotifier extends AutoDisposeAsyncNotifier<DownloadQuranState>
orElse: () async {
final remoteVersion = await downloadQuranRepoImpl.getRemoteQuranVersion(moshafType: moshafType);
return localVersionOption.fold(
() => UpdateAvailable(remoteVersion),
() => UpdateAvailable(
version: remoteVersion,
moshafType: moshafType,
),
(localVersion) => _compareVersions(moshafType, localVersion, remoteVersion),
);
},
Expand All @@ -90,7 +93,10 @@ class DownloadQuranNotifier extends AutoDisposeAsyncNotifier<DownloadQuranState>
final remoteVersion = await downloadQuranRepoImpl.getRemoteQuranVersion(moshafType: moshafType);

return localVersionOption.fold(
() => UpdateAvailable(remoteVersion),
() => UpdateAvailable(
version: remoteVersion,
moshafType: moshafType,
),
(localVersion) => _compareVersions(moshafType, localVersion, remoteVersion),
);
} else {
Expand All @@ -115,7 +121,10 @@ class DownloadQuranNotifier extends AutoDisposeAsyncNotifier<DownloadQuranState>

Future<DownloadQuranState> _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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 22 additions & 3 deletions lib/src/state_management/quran/reading/quran_reading_notifer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,13 @@ class QuranReadingNotifier extends AutoDisposeAsyncNotifier<QuranReadingState> {
final currentState = state.value!;
if (page >= 0 && page < currentState.totalPages) {
await _saveLastReadPage(page);
!isPortairt
? currentState.pageController.jumpToPage((page / 2).floor())
: currentState.pageController.jumpToPage(page); // Jump to the selected page

if (currentState.pageController.hasClients) {
!isPortairt
? currentState.pageController.jumpToPage((page / 2).floor())
: currentState.pageController.jumpToPage(page);
}

final newSurahName = _getCurrentSurahName(page, currentState.suwar);

return currentState.copyWith(
Expand Down Expand Up @@ -133,6 +137,10 @@ class QuranReadingNotifier extends AutoDisposeAsyncNotifier<QuranReadingState> {
}

state = AsyncLoading();

// Clear any existing SVGs in memory
await _clearSvgCache();

final svgs = await _loadSvgs(moshafType: moshafType);

if (svgs.isEmpty) {
Expand All @@ -157,6 +165,17 @@ class QuranReadingNotifier extends AutoDisposeAsyncNotifier<QuranReadingState> {
}
}

Future<void> _clearSvgCache() async {
// Clear any existing state
state = AsyncLoading();
state = await AsyncValue.guard(() async {
return state.value!.copyWith(
svgs: [],
pageController: PageController(),
);
});
}

Future<void> _saveLastReadPage(int index) async {
try {
final quranRepository = await ref.read(quranReadingRepositoryProvider.future);
Expand Down

0 comments on commit 90eaa39

Please sign in to comment.