loadProfiles method

  1. @override
Future<void> loadProfiles({
  1. bool includeSelf = false,
  2. bool loadAll = false,
})
override

Implementation

@override
Future<void> loadProfiles({bool includeSelf = false, bool loadAll = false}) async {
  AppConfig.logger.d("loadProfiles loadAll: $loadAll, current: ${_profiles.length}");
  try {
    if(_profiles.isEmpty || loadAll) {

      if(loadAll) {
        _profiles.value = await ProfileFirestore().retrieveAllProfiles();
        AppConfig.logger.d("Loaded ${_profiles.length} profiles (loadAll)");
      } else {
        // Collect all unique profile IDs we need
        final Set<String> neededProfileIds = {};

        if (profile.followers?.isNotEmpty ?? false) {
          neededProfileIds.addAll(profile.followers!);
        }
        if (profile.following?.isNotEmpty ?? false) {
          neededProfileIds.addAll(profile.following!);
        }
        if (profile.itemmates?.isNotEmpty ?? false) {
          neededProfileIds.addAll(profile.itemmates!);
        }

        // Only fetch needed profiles instead of ALL profiles
        if (neededProfileIds.isNotEmpty) {
          _profiles.value = await ProfileFirestore().retrieveFromList(neededProfileIds.toList());
          AppConfig.logger.d("Loaded ${_profiles.length} needed profiles (instead of all)");
        }
      }

    }

    if(!includeSelf) _profiles.remove(profile.id);

    if((profile.followers?.isNotEmpty ?? false) && _profiles.isNotEmpty) {
      _followerProfiles.value = _profiles.entries
          .where((entry) => profile.followers!.contains(entry.key))
          .fold(<String, AppProfile>{}, (map, entry) {
        map[entry.key] = entry.value;
        return map;
      });
    }

    if((profile.following?.isNotEmpty ?? false) && _profiles.isNotEmpty) {
      _followingProfiles.value = _profiles.entries
          .where((entry) => profile.following!.contains(entry.key))
          .fold(<String, AppProfile>{}, (map, entry) {
        map[entry.key] = entry.value;
        return map;
      });
    }

    if((profile.itemmates?.isNotEmpty ?? false )&& _profiles.isNotEmpty) {
      _mates.value = _profiles.entries
          .where((entry) => profile.following!.contains(entry.key))
          .fold(<String, AppProfile>{}, (map, entry) {
        map[entry.key] = entry.value;
        return map;
      });
    }

  } catch (e, st) {
    NeomErrorLogger.recordError(e, st, module: 'neom_core', operation: 'loadProfiles');
  }

  isLoading.value = false;
  _totalProfiles.value = _profiles.value;
  AppConfig.logger.d("${_totalProfiles.length} profiles found ");
  update();
}