updateDarkStylesXml function

Future<void> updateDarkStylesXml({
  1. YamlMap? android12AndAbove,
  2. String? color,
})

Updates the values-night/styles.xml file for the splash screen setup.

Implementation

Future<void> updateDarkStylesXml({
  YamlMap? android12AndAbove,
  String? color,
}) async {
  const androidValuesFolder = CmdStrings.androidDarkValuesDirectory;

  if (android12AndAbove != null &&
      (android12AndAbove[YamlKeys.colorKey] != null ||
          android12AndAbove[YamlKeys.imageKey] != null)) {
    const v31 = CmdStrings.androidValuesV31Directory;
    if (!await Directory(v31).exists()) {
      Directory(v31).create();
    }
    const style = '$v31/${AndroidStrings.stylesXml}';
    if (await File(style).exists()) {
      File(style).delete();
    }
    final styleFile = File(style);

    createAndroid12Styles(
      styleFile: styleFile,
      color: android12AndAbove[YamlKeys.colorKey],
      imageSource: android12AndAbove[YamlKeys.imageKey],
    );
  }
  final xml = File('$androidValuesFolder/${AndroidStrings.stylesXml}');
  final xmlExists = await xml.exists();
  if (!xmlExists) {
    log("values-night/styles.xml updated.");
    return;
  }
  final xmlDoc = XmlDocument.parse(xml.readAsStringSync());
  xmlDoc.findAllElements(AndroidStrings.itemElement).where((itemElement) {
    return itemElement.getAttribute(AndroidStrings.nameAttr) ==
        AndroidStrings.itemNameAttrValue;
  }).forEach((itemElement) {
    itemElement.setAttribute(
      AndroidStrings.nameAttr,
      AndroidStrings.itemNameAttrValue,
    );
    itemElement.innerText = AndroidStrings.androidDarkDrawable;
  });

  await xml.writeAsString(xmlDoc.toXmlString(pretty: true));
  log('values-night/styles.xml updated.');
}