switchFontType method

Future<void> switchFontType({
  1. required int fontIndex,
})

تبديل نوع الخط وتحميله إذا لم يكن محملاً من قبل

Switch font type and download it if not already downloaded

Implementation

Future<void> switchFontType({required int fontIndex}) async {
  // إعادة التحقق من حالة التحميل من التخزين
  // Re-check download status from storage
  final storageValue =
      GetStorage().read<bool>(_StorageConstants().isDownloadedCodeV2Fonts);
  state.isDownloadedV2Fonts.value = storageValue ?? false;

  // التحقق مما إذا كان الخط المطلوب هو نفس الخط الحالي
  // Check if the requested font is the same as the current font
  if (state.fontsSelected.value == fontIndex &&
      (fontIndex == 0 || state.isDownloadedV2Fonts.value)) {
    log('Font is already selected', name: 'QuranGetters');
    return;
  }

  // إذا كان الخط هو الخط الافتراضي (0)، فقط قم بتعيينه
  // If the font is the default font (0), just set it
  if (fontIndex == 0) {
    state.fontsSelected.value = fontIndex;
    GetStorage().write(_StorageConstants().fontsSelected, fontIndex);
    Get.forceAppUpdate();
    log('Default font selected', name: 'QuranGetters');
    return;
  }

  // إذا كان الخط محملاً بالفعل، قم بتعيينه
  // If the font is already downloaded, just set it
  if (state.isDownloadedV2Fonts.value) {
    state.fontsSelected.value = fontIndex;
    GetStorage().write(_StorageConstants().fontsSelected, fontIndex);
    update(['fontsSelected']);
    Get.forceAppUpdate();
    log('Downloaded font selected', name: 'QuranGetters');
    return;
  }

  // إذا كان الخط غير محمل، قم بتحميله أولاً ثم تعيينه
  // If the font is not downloaded, download it first then set it
  try {
    log('Starting font download', name: 'QuranGetters');
    state.isPreparingDownload.value = true;
    state.isDownloadingFonts.value = true;
    // تهيئة قيمة تقدم التحميل
    // Initialize download progress value
    state.fontsDownloadProgress.value = 0.0;
    update(['fontsDownloadingProgress']);

    await downloadAllFontsZipFile(fontIndex);

    state.fontsSelected.value = fontIndex;
    GetStorage().write(_StorageConstants().fontsSelected, fontIndex);
    update(['fontsSelected', 'fontsDownloadingProgress']);
    Get.forceAppUpdate();
    log('Font downloaded and selected', name: 'QuranGetters');
  } catch (e) {
    state.isDownloadingFonts.value = false;
    state.isPreparingDownload.value = false;
    update(['fontsDownloadingProgress']);
    log('Error downloading font: $e', name: 'QuranGetters');
  }
}