@@ -5,7 +5,7 @@ import 'package:chrono_sheet/category/model/category_representation.dart';
55import 'package:chrono_sheet/file/state/file_state.dart' ;
66import 'package:chrono_sheet/generated/app_localizations.dart' ;
77import 'package:chrono_sheet/log/util/log_util.dart' ;
8- import 'package:chrono_sheet/category/service/category_manager .dart' ;
8+ import 'package:chrono_sheet/category/service/shared_category_data_manager .dart' ;
99import 'package:chrono_sheet/sheet/model/sheet_model.dart' ;
1010import 'package:chrono_sheet/sheet/parser/sheet_parser.dart' ;
1111import 'package:chrono_sheet/util/regexp_util.dart' ;
@@ -91,18 +91,18 @@ class CategoriesStateManager extends _$CategoryStateManager {
9191 if (selectedFile == null ) {
9292 return CategoriesState .empty;
9393 }
94- final cached = await _readCachedCategoryState (selectedFile);
95- if (cached == null ) {
94+ final local = await _readLocalCategoryState (selectedFile);
95+ if (local == null ) {
9696 final result = await _parseAndApplyCategoriesFromSheet (selectedFile);
9797 return result.match ((error) => CategoriesState (), (newState) => newState);
9898 } else {
99- _logger.info ("found cached category state: $cached " );
99+ _logger.info ("found local category state: $local " );
100100 _parseAndApplyCategoriesFromSheet (selectedFile).then ((result) {
101101 result.map ((newState) {
102102 state = AsyncValue .data (newState);
103103 });
104104 });
105- return cached ;
105+ return local ;
106106 }
107107 }
108108
@@ -117,11 +117,11 @@ class CategoriesStateManager extends _$CategoryStateManager {
117117 (categoryNamesFromFile) async {
118118 _logger.info ("fetched remote categories: $categoryNamesFromFile " );
119119 final currentState = state;
120- CategoriesState ? cached ;
120+ CategoriesState ? local ;
121121 if (currentState is AsyncData ) {
122- cached = currentState.value;
122+ local = currentState.value;
123123 }
124- final newCategoryState = await _merge (cached , categoryNamesFromFile);
124+ final newCategoryState = await _merge (local , categoryNamesFromFile);
125125 await _cacheCategoryState (newCategoryState, file);
126126 return Either .right (newCategoryState);
127127 },
@@ -140,7 +140,7 @@ class CategoriesStateManager extends _$CategoryStateManager {
140140 );
141141 }
142142
143- Future <CategoriesState ?> _readCachedCategoryState (GoogleFile file) async {
143+ Future <CategoriesState ?> _readLocalCategoryState (GoogleFile file) async {
144144 List <Category > categories = [];
145145 Set <String > usedCategories = {};
146146 for (int i = 0 ; ; i++ ) {
@@ -156,7 +156,7 @@ class CategoriesStateManager extends _$CategoryStateManager {
156156 categories.add (category);
157157 }
158158 _logger.info (
159- "found ${categories .length } cached categories for file ${file .id } (${file .name }): \n ${categories .join ("\n " )}" ,
159+ "found ${categories .length } local category(ies) for file ${file .id } (${file .name }): \n ${categories .join ("\n " )}" ,
160160 );
161161 if (categories.isEmpty) {
162162 return null ;
@@ -264,12 +264,12 @@ class CategoriesStateManager extends _$CategoryStateManager {
264264 return {s1: _getDefaultRepresentationText (s1), s2: _getDefaultRepresentationText (s2)};
265265 }
266266
267- Future <CategoriesState > _merge (CategoriesState ? cached , List <String > currentCategoryNames) async {
268- _logger.info ("merging cached categories state (\n $cached \n ) and current categories ($currentCategoryNames )" );
269- if (currentCategoryNames.isEmpty && (cached ? .categories.all ((c) => c.persistedInGoogle) ?? true )) {
267+ Future <CategoriesState > _merge (CategoriesState ? local , List <String > currentCategoryNames) async {
268+ _logger.info ("merging local categories state (\n $local \n ) and current categories (\n $currentCategoryNames \n )" );
269+ if (currentCategoryNames.isEmpty && (local ? .categories.all ((c) => c.persistedInGoogle) ?? true )) {
270270 return CategoriesState .empty;
271271 }
272- if (cached == null ) {
272+ if (local == null ) {
273273 List <Category > categories = await Future .wait (
274274 currentCategoryNames.map ((name) async {
275275 return await _getCategoryByName (name);
@@ -279,12 +279,12 @@ class CategoriesStateManager extends _$CategoryStateManager {
279279 final selected = categories.first;
280280 return CategoriesState (selected: selected, categories: categories);
281281 }
282- List <Category > sortedCategories = List .of (cached .categories);
282+ List <Category > sortedCategories = List .of (local .categories);
283283 sortedCategories.removeWhere (
284284 (category) => ! currentCategoryNames.contains (category.name) && category.persistedInGoogle,
285285 );
286- final Set <String > cachedCategoryNames = Set .of (cached .categories.map ((c) => c.name));
287- final categoryNamesToAdd = currentCategoryNames.where ((name) => ! cachedCategoryNames .contains (name));
286+ final Set <String > localCategoryNames = Set .of (local .categories.map ((c) => c.name));
287+ final categoryNamesToAdd = currentCategoryNames.where ((name) => ! localCategoryNames .contains (name));
288288 final categoriesToAdd = await Future .wait (
289289 categoryNamesToAdd.map ((name) async {
290290 return await _getCategoryByName (name);
@@ -294,7 +294,7 @@ class CategoriesStateManager extends _$CategoryStateManager {
294294
295295 sortedCategories = ensureNoDuplicateTextRepresentations (sortedCategories);
296296
297- Category ? selected = cached .selected;
297+ Category ? selected = local .selected;
298298 if (selected == null || ! currentCategoryNames.contains (selected.name)) {
299299 selected = sortedCategories.first;
300300 }
@@ -334,7 +334,7 @@ class CategoriesStateManager extends _$CategoryStateManager {
334334 for (int i = 0 ; i < categories.length; i++ ) {
335335 await _prefs.setString (_Key .getFileCategoryPrefix (fileToUse, i), categories[i].name);
336336 }
337- _logger.info ("cached categories state for file ${fileToUse .id } (${fileToUse .name }): $state " );
337+ _logger.info ("local categories state for file ${fileToUse .id } (${fileToUse .name }): $state " );
338338 return ManageCategoryResult .success;
339339 }
340340
@@ -360,6 +360,7 @@ class CategoriesStateManager extends _$CategoryStateManager {
360360 }
361361
362362 Future <ManageCategoryResult > addNewCategory (Category category) async {
363+ _logger.info ("got a request to add category $category " );
363364 final currentState = await future;
364365 final withConflictingName = _findCategoryWithName (currentState, category.name);
365366 if (withConflictingName != null ) {
@@ -374,7 +375,7 @@ class CategoriesStateManager extends _$CategoryStateManager {
374375 state = AsyncValue .data (newState);
375376 final result = await _cacheCategoryState (newState);
376377 if (result == ManageCategoryResult .success) {
377- _logger.info ("categories state after adding new category ' $category ': $ newState " );
378+ _logger.info ("successfully added category $category " );
378379 }
379380
380381 ref.read (categoryManagerProvider).onNewCategory (category);
0 commit comments