loadIntricateThemes static method

Future<void> loadIntricateThemes()

Implementation

static Future<void> loadIntricateThemes() async {
  // get all .json files in the assets/themes folder
  // for each file, load the json and create a new AppTheme
  _intricateThemes = <AppTheme>[];
  dynamic rule = loadYaml(
      await rootBundle.loadString("assets/rules/themes.yml"));
  int i = 0;
  int f = 0;
  if (rule.runtimeType == YamlList && rule.isNotEmpty) {
    await Future.forEach(rule, (dynamic x) async {
      if (x is YamlMap) {
        try {
          ThemeData? theme = ThemeDecoder.decodeThemeData(
              jsonDecode(
                  await rootBundle.loadString(x['location'])));
          if (theme == null) {
            Debug().ohno("Failed to DECODE theme $x, skipping");
            f++;
          } else {
            // this might look like idiot proof code, but we have to add our own touch to mapping icons from static attributes to enums, just look at flutter_icons and community_material_icons D: it is very sad
            ThemeData better = theme.copyWith(
              textTheme: theme.textTheme
                  .apply(fontFamily: Shared.FONT_FAMILY_SANS),
              primaryTextTheme: theme.primaryTextTheme
                  .apply(fontFamily: Shared.FONT_FAMILY_SANS),
            );
            List<String> iconDescriptor = x['icon'].split(",");
            if (iconDescriptor.length == 2 &&
                GenericUtils.matchesOfAny(iconDescriptor[0],
                    <String>["community", "flutter"])) {
              IconData? icon = Icons.palette_rounded;
              if (iconDescriptor[0] == "community") {
                for (CommunityMaterialIconsEnumMapper v
                    in CommunityMaterialIconsEnumMapper.values) {
                  if (v.name == iconDescriptor[1]) {
                    icon = v.data;
                  }
                }
              } else if (iconDescriptor[0] == "flutter") {
                for (FlutterIconEnumMapper v
                    in FlutterIconEnumMapper.values) {
                  if (v.name == iconDescriptor[1]) {
                    icon = v.data;
                  }
                }
              } else {
                throw "'${iconDescriptor[0]}' is not a valid store to look for icons. Expecting 'community' or 'flutter'";
              }
              if (icon == null) {
                throw "Failed to find the icon ${iconDescriptor[1]} under ${iconDescriptor[0]}! Make sure ${iconDescriptor[0] == "community" ? "lib/extern/community_material_icons.dart" : "lib/extern/flutter_icons.dart"} is properly updated!";
              }
              _intricateThemes.add(AppTheme(
                  id: x['id'],
                  data: better,
                  description:
                      "A ${better.brightness.name} theme"));
              AvailableTheme.export.add(AvailableTheme(
                  x['canonical_name'],
                  x['id'],
                  icon,
                  x['author'],
                  better.brightness == Brightness.dark));
              Debug().info(
                  "Registered: ${x['id']} to the intricate theme list!");
              i++;
            } else {
              Debug().ohno(
                  "Failed to load theme ${x['id']} because the 'icon' field does not follow '[community/flutter],[icon_name]' format!");
            }
          }
        } catch (e) {
          Debug().ohno("Failed to load a theme $x because of $e");
          f++;
        }
      } else {
        Debug().ohno(
            "Cannot proceed with $x because typeof($x) != YamlMap. Instead it is ${x.runtimeType}.");
      }
    });
  }
  Debug().info("Loaded $i themes. Failed $f themes");
}