Skip to content

Commit

Permalink
Improve the way to print the debug log
Browse files Browse the repository at this point in the history
  • Loading branch information
lamnhan066 committed Jun 19, 2024
1 parent 9b895e3 commit e8b5375
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 41 deletions.
10 changes: 5 additions & 5 deletions lib/src/language_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class _LanguageBuilderState extends State<LanguageBuilder> with UpdateLanguage {
if ((widget.forceRebuild == true ||
(widget.forceRebuild == null && _languageHelper._forceRebuild))) {
if (_languageHelper._states.add(this)) {
printDebug(
printDebug(() =>
'Added $this to the states because the `forceRebuild` is `true`');
}
} else {
Expand All @@ -63,17 +63,17 @@ class _LanguageBuilderState extends State<LanguageBuilder> with UpdateLanguage {
// so all the posible `root` widgets have definitely been added to the list
// of the states. So we just need to add the state that its' parent is null.
if (getRoot == null && _languageHelper._states.add(this)) {
printDebug('Added $this to the states');
printDebug(() => 'Added $this to the states');
} else {
printDebug('$this was already contained in the states');
printDebug(() => '$this was already contained in the states');
}
}
printDebug('Length of the states: ${_languageHelper._states.length}');
printDebug(() => 'Length of the states: ${_languageHelper._states.length}');
}

@override
void dispose() {
printDebug('Removed $this from the states');
printDebug(() => 'Removed $this from the states');
_languageHelper._states.remove(this);
super.dispose();
}
Expand Down
46 changes: 23 additions & 23 deletions lib/src/language_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ class LanguageHelper {

// When the `data` is empty, a temporary data will be added.
if (_dataProviders.isEmpty) {
printDebug(
printDebug(() =>
'The `data` is empty, we will use a temporary `data` for the developing state');
_dataProviders = [
LanguageDataProvider.data({LanguageCodes.en: {}})
Expand Down Expand Up @@ -299,7 +299,7 @@ class LanguageHelper {
// Sync with device only track the changing of the device language,
// so it will not use the device language for the app at the first time.
prefs.setString(_deviceCodeKey, currentCode.code);
printDebug(
printDebug(() =>
'Sync with device saved the current language to local database.');
} else {
// We only consider to change the app language when the device language
Expand All @@ -308,9 +308,9 @@ class LanguageHelper {
if (currentCode != prefCode) {
finalCode = currentCode;
prefs.setString(_deviceCodeKey, currentCode.code);
printDebug('Sync with device applied the new device language');
printDebug(() => 'Sync with device applied the new device language');
} else {
printDebug('Sync with device used the current app language');
printDebug(() => 'Sync with device used the current app language');
}
}
}
Expand All @@ -320,7 +320,7 @@ class LanguageHelper {
if (isOptionalCountryCode && finalCode.locale.countryCode != null) {
// Try to use the `languageCode` only if the `languageCode_countryCode`
// is not available
printDebug(
printDebug(() =>
'language does not contain the $finalCode => Try to use the `languageCode` only..');
try {
tempCode = LanguageCodes.fromCode(finalCode.locale.languageCode);
Expand All @@ -331,17 +331,17 @@ class LanguageHelper {
}

if (tempCode == null) {
printDebug(
printDebug(() =>
'Unable to use the `languageCode` only => Change the code to ${codes.first}');
} else {
printDebug(
printDebug(() =>
'Able to use the `languageCode` only => Change the code to $tempCode');
}

finalCode = tempCode ?? codes.first;
}

printDebug('Set `currentCode` to $finalCode');
printDebug(() => 'Set `currentCode` to $finalCode');
_currentCode = finalCode;

_data.addAll(await _dataProvider.getData(code));
Expand Down Expand Up @@ -377,7 +377,7 @@ class LanguageHelper {
_addData(data: getData, database: _data, overwrite: overwrite);
_codes.addAll(await data.getSupportedCodes());
if (activate) change(code);
printDebug(
printDebug(() =>
'The new `data` is added and activated with overwrite is $overwrite');
}

Expand All @@ -397,7 +397,7 @@ class LanguageHelper {
_addData(data: getData, database: _dataOverrides, overwrite: overwrite);
_codesOverrides.addAll(await dataOverrides.getSupportedCodes());
if (activate) change(code);
printDebug(
printDebug(() =>
'The new `dataOverrides` is added and activated with overwrite is $overwrite');
}

Expand All @@ -421,14 +421,14 @@ class LanguageHelper {
final stringParams = params.map((key, value) => MapEntry(key, '$value'));

if (!codes.contains(toCode) && !codesOverrides.contains(toCode)) {
printDebug(
printDebug(() =>
'Cannot translate this text because $toCode is not available in `data` and `dataOverrides` ($text)');
return _replaceParams(text, stringParams);
}

final translated = _dataOverrides[toCode]?[text] ?? _data[toCode]![text];
if (translated == null) {
printDebug('This text is not contained in current $toCode ($text)');
printDebug(() => 'This text is not contained in current $toCode ($text)');
return _replaceParams(text, stringParams);
}

Expand All @@ -445,25 +445,25 @@ class LanguageHelper {
/// Change the language to this [code]
Future<void> change(LanguageCodes toCode) async {
if (!codes.contains(toCode)) {
printDebug('$toCode is not available in `data` or `dataOverrides`');
printDebug(() => '$toCode is not available in `data` or `dataOverrides`');

if (!_useInitialCodeWhenUnavailable) {
printDebug(
printDebug(() =>
'Does not allow using the initial code => Cannot change the language.');
return;
} else {
if (codes.contains(_initialCode)) {
printDebug(
printDebug(() =>
'`useInitialCodeWhenUnavailable` is true => Change the language to $_initialCode');
_currentCode = _initialCode;
} else {
printDebug(
printDebug(() =>
'`useInitialCodeWhenUnavailable` is true but the `initialCode` is not available in `data` or `dataOverrides` => Cannot change the language');
return;
}
}
} else {
printDebug('Set currentCode to $toCode');
printDebug(() => 'Set currentCode to $toCode');
_currentCode = toCode;
}

Expand All @@ -478,7 +478,7 @@ class LanguageHelper {
_dataOverrides.addAll(await _dataOverridesProvider.getData(code));
}

printDebug('Change language to $toCode for ${_states.length} states');
printDebug(() => 'Change language to $toCode for ${_states.length} states');
for (var state in _states) {
state.updateLanguage();
}
Expand All @@ -490,13 +490,13 @@ class LanguageHelper {

// Save to local memory
if (_isAutoSave) {
printDebug('Save this $toCode to local memory');
printDebug(() => 'Save this $toCode to local memory');
SharedPreferences.getInstance().then((pref) {
pref.setString(_autoSaveCodeKey, toCode.code);
});
}

printDebug('Changing completed!');
printDebug(() => 'Changing completed!');
}

/// Change the [useInitialCodeWhenUnavailable] value
Expand Down Expand Up @@ -589,7 +589,7 @@ class LanguageHelper {
buffer.write('==================================================');
buffer.write('\n');

printDebug(buffer.toString());
printDebug(() => buffer.toString());

return buffer.toString();
}
Expand Down Expand Up @@ -665,7 +665,7 @@ class LanguageHelper {
String fallback,
) {
if (!params.containsKey(translateCondition.param)) {
printDebug(
printDebug(() =>
'The params does not contain the condition param: ${translateCondition.param}');
return _replaceParams(fallback, params);
}
Expand All @@ -676,7 +676,7 @@ class LanguageHelper {
conditions[param] ?? conditions['default'] ?? conditions['_'];

if (translated == null) {
printDebug(
printDebug(() =>
'There is no result for key $param of condition ${translateCondition.param}');
return _replaceParams(fallback, params);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/models/language_data_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class LanguageDataProvider {
try {
return await rootBundle.loadString(path);
} catch (_) {
printDebug('The $path does not exist in the assets');
printDebug(() => () => 'The $path does not exist in the assets');
}
return Future.value('');
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/utils/print_debug.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import 'package:flutter/foundation.dart';
import 'package:language_helper/src/language_helper.dart';

/// Internal function, print debug log
void printDebug(Object? object) => LanguageHelper.instance.isDebug
? debugPrint('[Language Helper] $object')
void printDebug(Object? Function() object) => LanguageHelper.instance.isDebug
? debugPrint('[Language Helper] ${object()}')
: null;
18 changes: 10 additions & 8 deletions lib/src/utils/serializer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,18 @@ Map<String, dynamic> languageDataValuesFromMap(Map<String, dynamic> map) {
/// | | | |- en.json
/// | | | |- vi.json
void exportJson(LanguageData data, String path) {
printDebug('===========================================================');
printDebug('Exporting Json...');
printDebug(
() => '===========================================================');
printDebug(() => 'Exporting Json...');
_exportJsonCodes(data, path);
_exportJsonLanguages(data, path);
printDebug('Exported Json');
printDebug('===========================================================');
printDebug(() => 'Exported Json');
printDebug(
() => '===========================================================');
}

void _exportJsonCodes(LanguageData data, String path) {
printDebug('Creating codes.json...');
printDebug(() => 'Creating codes.json...');

JsonEncoder encoder = const JsonEncoder.withIndent(' ');

Expand All @@ -66,11 +68,11 @@ void _exportJsonCodes(LanguageData data, String path) {
final codes = data.keys.map((e) => e.code).toList();
desFile.writeAsStringSync(encoder.convert(codes));

printDebug('Created codes.json');
printDebug(() => 'Created codes.json');
}

void _exportJsonLanguages(LanguageData data, String path) {
printDebug('Creating languages json files...');
printDebug(() => 'Creating languages json files...');

JsonEncoder encoder = const JsonEncoder.withIndent(' ');

Expand All @@ -83,5 +85,5 @@ void _exportJsonLanguages(LanguageData data, String path) {
desFile.writeAsStringSync(data);
}

printDebug('Created languages json files');
printDebug(() => 'Created languages json files');
}
4 changes: 2 additions & 2 deletions lib/src/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ class Utils {
if (response.statusCode == 200) {
return utf8.decode(response.bodyBytes);
} else {
printDebug(
printDebug(() =>
'Failed to load data from URL. Status code: ${response.statusCode}');
}
} catch (e) {
printDebug('Error fetching data from URL: $e');
printDebug(() => 'Error fetching data from URL: $e');
}

return '';
Expand Down

0 comments on commit e8b5375

Please sign in to comment.