createFeatureFolder function
- required String featureName,
- required BuildContext context,
- bool createInitFile = true,
- required void onProgressChange(),
- required void onSuccess(),
Creates or verifies a feature folder in POD. Creates a feature folder in POD with initialisation file.
Returns a Future<SolidFunctionCallStatus> indicating the creation result.
The featureName
parameter specifies which feature folder to create (e.g. 'bp').
If createInitFile
is true, creates an initialisation file in the folder.
Implementation
/// Creates a feature folder in POD with initialisation file.
///
/// Returns a [Future<SolidFunctionCallStatus>] indicating the creation result.
/// The [featureName] parameter specifies which feature folder to create (e.g. 'bp').
/// If [createInitFile] is true, creates an initialisation file in the folder.
Future<SolidFunctionCallStatus> createFeatureFolder({
required String featureName,
required BuildContext context,
bool createInitFile = true,
required void Function(bool) onProgressChange,
required void Function() onSuccess,
}) async {
try {
onProgressChange.call(true);
// Check current resources.
final dirUrl = await getDirUrl('healthpod/data');
final resources = await getResourcesInContainer(dirUrl);
// Check if exists as directory.
bool existsAsDir = resources.subDirs.contains(featureName);
if (existsAsDir) {
debugPrint('Feature folder $featureName already exists as directory');
onSuccess.call();
return SolidFunctionCallStatus.success;
}
// Check if exists as file and delete if necessary.
bool existsAsFile = resources.files.contains(featureName);
if (existsAsFile) {
debugPrint(
'Removing existing file $featureName before creating directory');
if (!context.mounted) return SolidFunctionCallStatus.fail;
// Full path for deletion needs to include healthpod/data.
await deleteFile(
'healthpod/data/$featureName',
);
}
if (!context.mounted) {
debugPrint('Widget is no longer mounted, skipping folder creation.');
return SolidFunctionCallStatus.fail;
}
// Create the feature folder structure.
final result = await writePod(
'$featureName/.init',
'',
context,
const Text('Creating folder'),
encrypted: false,
);
// If folder creation was successful and initialization file is requested.
if (result == SolidFunctionCallStatus.success && createInitFile) {
final initContent = '''
{
"feature": "$featureName",
"created": "${DateTime.now().toIso8601String()}",
"version": "1.0"
}''';
if (!context.mounted) return result;
final initResult = await writePod(
'$featureName/init.json',
initContent,
context,
const Text('Creating initialization file'),
encrypted: true,
);
if (initResult == SolidFunctionCallStatus.success) {
onSuccess.call();
return SolidFunctionCallStatus.success;
}
}
return result;
} catch (e) {
debugPrint('Error creating feature folder: $e');
return SolidFunctionCallStatus.fail;
} finally {
onProgressChange.call(false);
}
}