LCOV - code coverage report
Current view: top level - lib/themes - yamltheme.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 0 119 0.0 %
Date: 2024-02-26 20:09:01 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 chatReactionIconColor => getColor("chatReactionIconColor") ?? fallbackTheme.chatReactionIconColor;
     191           0 :   get messageFromMeBackgroundColor => getColor("messageFromMeBackgroundColor") ?? fallbackTheme.messageFromMeBackgroundColor;
     192           0 :   get messageFromMeTextColor => getColor("messageFromMeTextColor") ?? fallbackTheme.messageFromMeTextColor;
     193           0 :   get messageFromOtherBackgroundColor => getColor("messageFromOtherBackgroundColor") ?? fallbackTheme.messageFromOtherBackgroundColor;
     194           0 :   get messageFromOtherTextColor => getColor("messageFromOtherTextColor") ?? fallbackTheme.messageFromOtherTextColor;
     195           0 :   get messageSelectionColor => getColor("messageSelectionColor") ?? fallbackTheme.messageSelectionColor;
     196           0 :   get menuBackgroundColor => getColor("menuBackgroundColor") ?? fallbackTheme.menuBackgroundColor;
     197           0 :   get snackbarBackgroundColor => getColor("snackbarBackgroundColor") ?? fallbackTheme.snackbarBackgroundColor;
     198           0 :   get snackbarTextColor => getColor("snackbarTextColor") ?? fallbackTheme.snackbarTextColor;
     199             : 
     200             :   // Images
     201             : 
     202           0 :   get chatImageColor => getColor("chatImageColor") ?? fallbackTheme.chatImageColor;
     203           0 :   get chatImage => getImage("chatImage") ?? fallbackTheme.chatImage;
     204             : 
     205           0 :   ImageProvider loadImage(String key, {BuildContext? context}) {
     206           0 :     File f = File(key);
     207           0 :     if (f.existsSync()) {
     208           0 :       return FileImage(f);
     209             :     }
     210             : 
     211           0 :     final assetsDir = this.assetsDir;
     212             :     if (assetsDir != null) {
     213           0 :       File f = File(path.join(assetsDir, key));
     214           0 :       if (f.existsSync()) {
     215           0 :         return FileImage(f);
     216             :       }
     217             :     }
     218             : 
     219             :     if (context != null) {
     220           0 :       File af = File(path.join(Provider.of<FlwtchState>(context, listen: false).cwtch.getAssetsDir(), key));
     221           0 :       if (af.existsSync()) {
     222           0 :         return FileImage(af);
     223             :       }
     224             :     }
     225             : 
     226           0 :     return AssetImage(key);
     227             :   }
     228             : }

Generated by: LCOV version 1.14