LCOV - code coverage report
Current view: top level - lib/themes - yamltheme.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 0 120 0.0 %
Date: 2024-09-10 17:47:43 Functions: 0 0 -

          Line data    Source code
       1             : import 'dart:convert';
       2             : import 'dart:io';
       3             : 
       4             : import 'package:cwtch/themes/cwtch.dart';
       5             : import 'package:cwtch/themes/opaque.dart';
       6             : import 'package:flutter/material.dart';
       7             : import 'package:flutter/services.dart';
       8             : import 'package:provider/provider.dart';
       9             : import 'package:yaml/yaml.dart';
      10             : import 'package:path/path.dart' as path;
      11             : 
      12             : import '../main.dart';
      13             : 
      14           0 : Future<Map<String, Map<String, OpaqueThemeType>>> loadBuiltinThemes() async {
      15           0 :   final manifestJson = await rootBundle.loadString('AssetManifest.json');
      16           0 :   final themesList = json.decode(manifestJson).keys.where((String key) => key.startsWith('assets/themes'));
      17             : 
      18           0 :   Map<String, Map<String, OpaqueThemeType>> themes = Map();
      19             : 
      20           0 :   for (String themefile in themesList) {
      21           0 :     if (themefile.substring(themefile.length - 4) != ".yml") {
      22             :       continue;
      23             :     }
      24             : 
      25             :     try {
      26           0 :       var data = await loadAssetYamlTheme(themefile);
      27             : 
      28             :       if (data != null) {
      29             :         // remove "assets/themes" and "theme.yml" from name
      30           0 :         var themename = themefile.substring(14, themefile.lastIndexOf("/"));
      31           0 :         themes[themename] = data;
      32             :       }
      33             :     } catch (e) {
      34           0 :       print("Failed to load theme: $themefile with exception: $e");
      35             :     }
      36             :   }
      37             :   return themes;
      38             : }
      39             : 
      40           0 : Future<Map<String, Map<String, OpaqueThemeType>>> loadCustomThemes(String themesDirPath) async {
      41           0 :   Map<String, Map<String, OpaqueThemeType>> themes = Map();
      42             : 
      43           0 :   Directory themesDir = Directory(themesDirPath);
      44           0 :   if (!themesDir.existsSync()) {
      45           0 :     themesDir = await themesDir.create();
      46             :   }
      47           0 :   await themesDir.list(recursive: false).forEach((themeDir) async {
      48             :     //final themeDir = Directory(path.join(themesDir, themedir));
      49           0 :     File themefile = File(path.join(themeDir.path, "theme.yml"));
      50           0 :     if (themefile.existsSync()) {
      51             :       try {
      52           0 :         var data = await loadFileYamlTheme(themefile.path, themeDir.path);
      53             : 
      54             :         if (data != null) {
      55           0 :           themes[path.basename(themeDir.path)] = data;
      56             :         }
      57             :       } catch (e) {
      58           0 :         print("Failed to load theme: $themefile with exception: $e");
      59             :       }
      60             :     }
      61             :   });
      62             : 
      63             :   return themes;
      64             : }
      65             : 
      66           0 : Future<YamlMap?> readAssetYamlTheme(String themefile) async {
      67           0 :   final contents = await rootBundle.loadString(themefile);
      68           0 :   return loadYaml(contents);
      69             : }
      70             : 
      71           0 : Future<Map<String, OpaqueThemeType>?> loadAssetYamlTheme(String themefile) async {
      72           0 :   final yml = await readAssetYamlTheme(themefile);
      73             : 
      74             :   if (yml == null) {
      75           0 :     print("Error: failed to load theme: $themefile");
      76             :     return null;
      77             :   }
      78           0 :   return loadYamlTheme(yml);
      79             : }
      80             : 
      81           0 : Future<Map<String, OpaqueThemeType>?> loadFileYamlTheme(String themefile, String assetsDir) async {
      82           0 :   final file = File(themefile);
      83           0 :   if (!file.existsSync()) {
      84           0 :     print("Error: theme file: $themefile does not exist");
      85             :     return null;
      86             :   }
      87           0 :   final contents = file.readAsStringSync();
      88           0 :   final yml = await loadYaml(contents);
      89             :   if (yml == null) {
      90           0 :     print("Error: failed to load theme: $themefile");
      91             :     return null;
      92             :   }
      93           0 :   return loadYamlTheme(yml, assetsDir);
      94             : }
      95             : 
      96           0 : Future<Map<String, OpaqueThemeType>?> loadYamlTheme(YamlMap yml, [String? assetsDir]) async {
      97           0 :   Map<String, OpaqueThemeType> subthemes = Map();
      98           0 :   if ((yml["themes"] as YamlMap).containsKey(mode_dark)) {
      99           0 :     subthemes[mode_dark] = YmlTheme(yml, yml["themes"]["name"], mode_dark, assetsDir);
     100             :   }
     101           0 :   if ((yml["themes"] as YamlMap).containsKey(mode_light)) {
     102           0 :     subthemes[mode_light] = YmlTheme(yml, yml["themes"]["name"], mode_light, assetsDir);
     103             :   }
     104             :   return subthemes;
     105             : }
     106             : 
     107             : class YmlTheme extends OpaqueThemeType {
     108             :   late YamlMap yml;
     109             :   late String mode;
     110             :   late String theme;
     111             :   late String? assetsDir;
     112             :   late OpaqueThemeType fallbackTheme;
     113             : 
     114           0 :   YmlTheme(YamlMap yml, theme, mode, assetsDir) {
     115           0 :     this.yml = yml;
     116           0 :     this.theme = theme;
     117           0 :     this.mode = mode;
     118           0 :     this.assetsDir = assetsDir;
     119           0 :     if (mode == mode_dark) {
     120           0 :       fallbackTheme = CwtchDark();
     121             :     } else {
     122           0 :       fallbackTheme = CwtchLight();
     123             :     }
     124             :   }
     125             : 
     126           0 :   Color? getColor(String name) {
     127           0 :     var val = yml["themes"][mode]["theme"][name];
     128           0 :     if (!(val is int)) {
     129           0 :       val = yml["themes"][mode]["theme"][val] ?? val;
     130             :     }
     131           0 :     if (!(val is int)) {
     132           0 :       val = yml["themes"][mode]?["colors"]?[val] ?? val;
     133             :     }
     134           0 :     if (!(val is int)) {
     135           0 :       val = yml["colors"]?[val];
     136             :     }
     137           0 :     if (!(val is int)) {
     138             :       return null;
     139             :     }
     140             : 
     141           0 :     if (val <= 0xFFFFFF) {
     142           0 :       return Color(0xFF000000 + val);
     143             :     } else {
     144           0 :       return Color(val);
     145             :     }
     146             :   }
     147             : 
     148           0 :   String? getImage(String name) {
     149           0 :     var val = yml["themes"][mode]["theme"]?[name];
     150             :     if (val != null) {
     151           0 :       if (assetsDir == null) {
     152           0 :         return path.join("assets", "themes", yml["themes"]["name"], val);
     153             :       } else {
     154             :         return val;
     155             :       }
     156             :     }
     157             :     return null;
     158             :   }
     159             : 
     160           0 :   get backgroundMainColor => getColor("backgroundMainColor") ?? fallbackTheme.backgroundMainColor;
     161           0 :   get backgroundPaneColor => getColor("backgroundPaneColor") ?? fallbackTheme.backgroundPaneColor;
     162           0 :   get topbarColor => getColor("topbarColor") ?? fallbackTheme.topbarColor;
     163           0 :   get mainTextColor => getColor("mainTextColor") ?? fallbackTheme.mainTextColor;
     164           0 :   get hilightElementColor => getColor("hilightElementColor") ?? fallbackTheme.hilightElementColor;
     165           0 :   get backgroundHilightElementColor => getColor("backgroundHilightElementColor") ?? fallbackTheme.backgroundHilightElementColor;
     166           0 :   get sendHintTextColor => getColor("sendHintTextColor") ?? fallbackTheme.sendHintTextColor;
     167           0 :   get defaultButtonColor => getColor("defaultButtonColor") ?? fallbackTheme.defaultButtonColor;
     168           0 :   get defaultButtonActiveColor => /*mode == mode_light ? darken(defaultButtonColor) :*/ lighten(getColor("defaultButtonColor") ?? fallbackTheme.defaultButtonColor);
     169           0 :   get defaultButtonTextColor => getColor("defaultButtonTextColor") ?? fallbackTheme.defaultButtonTextColor;
     170           0 :   get defaultButtonDisabledColor => getColor("defaultButtonDisabledColor") ?? fallbackTheme.defaultButtonDisabledColor;
     171           0 :   get textfieldBackgroundColor => getColor("textfieldBackgroundColor") ?? fallbackTheme.textfieldBackgroundColor;
     172           0 :   get textfieldBorderColor => getColor("textfieldBorderColor") ?? fallbackTheme.textfieldBorderColor;
     173           0 :   get textfieldHintColor => getColor("textfieldHintColor") ?? fallbackTheme.textfieldHintColor;
     174           0 :   get textfieldErrorColor => getColor("textfieldErrorColor") ?? fallbackTheme.textfieldErrorColor;
     175           0 :   get textfieldSelectionColor => getColor("textfieldSelectionColor") ?? fallbackTheme.textfieldSelectionColor;
     176           0 :   get scrollbarDefaultColor => getColor("scrollbarDefaultColor") ?? fallbackTheme.scrollbarDefaultColor;
     177           0 :   get portraitBackgroundColor => getColor("portraitBackgroundColor") ?? fallbackTheme.portraitBackgroundColor;
     178           0 :   get portraitOnlineBorderColor => getColor("portraitOnlineBorderColor") ?? fallbackTheme.portraitOnlineBorderColor;
     179           0 :   get portraitOfflineBorderColor => getColor("portraitOfflineBorderColor") ?? fallbackTheme.portraitOfflineBorderColor;
     180           0 :   get portraitBlockedBorderColor => getColor("portraitBlockedBorderColor") ?? fallbackTheme.portraitBlockedBorderColor;
     181           0 :   get portraitBlockedTextColor => getColor("portraitBlockedTextColor") ?? fallbackTheme.portraitBlockedTextColor;
     182           0 :   get portraitContactBadgeColor => getColor("portraitContactBadgeColor") ?? fallbackTheme.portraitContactBadgeColor;
     183           0 :   get portraitContactBadgeTextColor => getColor("portraitContactBadgeTextColor") ?? fallbackTheme.portraitContactBadgeTextColor;
     184           0 :   get portraitProfileBadgeColor => getColor("portraitProfileBadgeColor") ?? fallbackTheme.portraitProfileBadgeColor;
     185           0 :   get portraitProfileBadgeTextColor => getColor("portraitProfileBadgeTextColor") ?? fallbackTheme.portraitProfileBadgeTextColor;
     186           0 :   get portraitOnlineAwayColor => getColor("portraitOnlineAwayColor") ?? fallbackTheme.portraitOnlineAwayColor;
     187           0 :   get portraitOnlineBusyColor => getColor("portraitOnlineBusyColor") ?? fallbackTheme.portraitOnlineBusyColor;
     188           0 :   get dropShadowColor => getColor("dropShadowColor") ?? fallbackTheme.dropShadowColor;
     189           0 :   get toolbarIconColor => getColor("toolbarIconColor") ?? fallbackTheme.toolbarIconColor;
     190           0 :   get toolbarBackgroundColor => getColor("toolbarBackgroundColor") ?? fallbackTheme.toolbarBackgroundColor;
     191           0 :   get chatReactionIconColor => getColor("chatReactionIconColor") ?? fallbackTheme.chatReactionIconColor;
     192           0 :   get messageFromMeBackgroundColor => getColor("messageFromMeBackgroundColor") ?? fallbackTheme.messageFromMeBackgroundColor;
     193           0 :   get messageFromMeTextColor => getColor("messageFromMeTextColor") ?? fallbackTheme.messageFromMeTextColor;
     194           0 :   get messageFromOtherBackgroundColor => getColor("messageFromOtherBackgroundColor") ?? fallbackTheme.messageFromOtherBackgroundColor;
     195           0 :   get messageFromOtherTextColor => getColor("messageFromOtherTextColor") ?? fallbackTheme.messageFromOtherTextColor;
     196           0 :   get messageSelectionColor => getColor("messageSelectionColor") ?? fallbackTheme.messageSelectionColor;
     197           0 :   get menuBackgroundColor => getColor("menuBackgroundColor") ?? fallbackTheme.menuBackgroundColor;
     198           0 :   get snackbarBackgroundColor => getColor("snackbarBackgroundColor") ?? fallbackTheme.snackbarBackgroundColor;
     199           0 :   get snackbarTextColor => getColor("snackbarTextColor") ?? fallbackTheme.snackbarTextColor;
     200             : 
     201             :   // Images
     202             : 
     203           0 :   get chatImageColor => getColor("chatImageColor") ?? fallbackTheme.chatImageColor;
     204           0 :   get chatImage => getImage("chatImage") ?? fallbackTheme.chatImage;
     205             : 
     206           0 :   ImageProvider loadImage(String key, {BuildContext? context}) {
     207           0 :     File f = File(key);
     208           0 :     if (f.existsSync()) {
     209           0 :       return FileImage(f);
     210             :     }
     211             : 
     212           0 :     final assetsDir = this.assetsDir;
     213             :     if (assetsDir != null) {
     214           0 :       File f = File(path.join(assetsDir, key));
     215           0 :       if (f.existsSync()) {
     216           0 :         return FileImage(f);
     217             :       }
     218             :     }
     219             : 
     220             :     if (context != null) {
     221           0 :       File af = File(path.join(Provider.of<FlwtchState>(context, listen: false).cwtch.getAssetsDir(), key));
     222           0 :       if (af.existsSync()) {
     223           0 :         return FileImage(af);
     224             :       }
     225             :     }
     226             : 
     227           0 :     return AssetImage(key);
     228             :   }
     229             : }

Generated by: LCOV version 1.14