initialiseFeatureFolders function

Future<void> initialiseFeatureFolders({
  1. required BuildContext context,
  2. required void onProgress(
    1. bool
    ),
  3. required void onComplete(),
})

Initialises required feature folders in the user's POD.

This function checks for the existence of essential feature folders and creates them if they don't exist. Currently handles 'bp' and 'pathology' folders. Returns a Future<void> that completes when all folders are verified/created.

Parameters:

  • context: The BuildContext for showing progress indicators and error messages
  • onProgress: Optional callback to track initialisation progress
  • onComplete: Optional callback triggered when initialisation is complete

Implementation

Future<void> initialiseFeatureFolders({
  required BuildContext context,
  required void Function(bool) onProgress,
  required void Function() onComplete,
}) async {
  try {
    onProgress.call(true);

    // List of required feature folders.

    final requiredFolders = ['bp', 'pathology'];

    // Check current resources.

    final dirUrl = await getDirUrl('healthpod/data');
    final resources = await getResourcesInContainer(dirUrl);

    // Create each missing folder.

    for (final folder in requiredFolders) {
      if (!resources.subDirs.contains(folder)) {
        if (!context.mounted) return;

        final result = await createFeatureFolder(
          featureName: folder,
          context: context,
          onProgressChange: (inProgress) {
            // Only propagate progress changes if the callback is provided.

            onProgress.call(inProgress);
          },
          onSuccess: () {
            debugPrint('Successfully created $folder folder');
          },
        );

        if (result != SolidFunctionCallStatus.success) {
          debugPrint('Failed to create $folder folder');
          // Continue with other folders even if one fails.
        }
      }
    }

    onComplete.call();
  } catch (e) {
    debugPrint('Error initializing feature folders: $e');
  } finally {
    onProgress.call(false);
  }
}