getUserProfile method

Future<Map<String, dynamic>?> getUserProfile()

Gets the user profile data.

Implementation

Future<Map<String, dynamic>?> getUserProfile() async {
  try {
    // First check cache.

    if (_cachedProfile != null) {
      return _cachedProfile;
    }

    final loggedIn = await isLoggedIn();
    if (!loggedIn) return null;

    // Try to read profile from POD.

    if (!_context.mounted) return null;
    try {
      final readPath = await getReadPath('profile/profile.ttl');
      if (!_context.mounted) return null;

      final result = await readPod(readPath, _context, _child);

      if (result.isNotEmpty) {
        // Parse the profile data properly, including MovieList IDs.

        final webId = await getCurrentUserWebId();
        if (webId != null) {
          // Extract MovieList IDs from profile TTL.

          final movieListMatches =
              RegExp(r'moviestar-data:MovieList-([a-zA-Z0-9]+)')
                  .allMatches(result);
          final movieListIds =
              movieListMatches.map((m) => m.group(1)!).toList();

          _cachedProfile = {
            'webId': webId,
            'apiKey': null,
            'dob': null,
            'gender': null,
            'movieListIds': movieListIds,
          };
          return _cachedProfile;
        }
      }
    } catch (e) {
      debugPrint('❌ Failed to read profile from POD: $e');
    }

    return null;
  } catch (e) {
    debugPrint('❌ Failed to get user profile: $e');
    return null;
  }
}