create static method

Future<LocalDataContainer> create(
  1. DocumentType documentType,
  2. String fileName, [
  3. LocalDataContainerDi? localDataContainerDi
])

fileName is expected to already contain the path joined into a string.

TODO: this operation calls async functions every construction. Either cache this in LocalValueImpl, or somehow cache the async calls for performance.

Implementation

// ignore: todo
/// TODO: this operation calls async functions every construction. Either cache this in
/// LocalValueImpl, or somehow cache the async calls for performance.
static Future<LocalDataContainer> create(
    DocumentType documentType, String fileName,
    [LocalDataContainerDi? localDataContainerDi]) async {
  final dataContainerDi = localDataContainerDi ?? LocalDataContainerDi();

  if (dataContainerDi.isWeb) {
    return dataContainerDi.buildSharedPrefDataContainer(fileName);
  }

  Future<Directory> dir;
  switch (documentType) {
    case DocumentType.document:
      dir = dataContainerDi.diGetApplicationDocumentsDirectory();
      break;
    case DocumentType.support:
      dir = dataContainerDi.diGetApplicationSupportDirectory();
      break;
    case DocumentType.temporary:
      dir = dataContainerDi.diGetTemporaryDirectory();
      break;
    case DocumentType.prefs:
      return dataContainerDi.buildSharedPrefDataContainer(fileName);
    case DocumentType.secure:
      return dataContainerDi.diGetSecureDataContainer(fileName);
  }

  final directory = await dir;
  return dataContainerDi
      .buildFileDataContainer('${directory.path}/$fileName');
}