prepareFonts method

Future<void> prepareFonts(
  1. int pageIndex, {
  2. bool isFontsLocal = false,
})

الدالة المعدلة: تحضير الخطوط للصفحة الحالية والصفحات المجاورة

Implementation

Future<void> prepareFonts(int pageIndex, {bool isFontsLocal = false}) async {
  if (state.fontsSelected.value == 1) {
    // إذا كان محمّلًا بالفعل محليًا لا نعيد الإرسال
    if (state.loadedFontPages.contains(pageIndex) || isFontsLocal) return;

    final currentGeneration = ++state._fontPreloadGeneration;
    if (!kIsWeb) {
      _sendFontLoadRequest(pageIndex, isFontsLocal, currentGeneration);
    } else {
      await loadFont(pageIndex);
    }

    // جدولة الصفحات المجاورة بعد تأخير بسيط
    state._debounceTimer?.cancel();
    state._debounceTimer = Timer(const Duration(milliseconds: 300), () {
      final gen = ++state._fontPreloadGeneration;
      final neighbors = [-2, -1, 1, 2, 3, 4];
      final candidates = neighbors
          .map((o) => pageIndex + o)
          .where((i) => i >= 0 && i < 604)
          .toList();
      if (!kIsWeb && state._fontPreloadGeneration != gen) return;
      for (final i in candidates) {
        if (state.loadedFontPages.contains(i)) continue;
        if (kIsWeb) {
          loadFont(i);
        } else {
          _sendFontLoadRequest(i, isFontsLocal, gen);
        }
      }
    });
  }
}