fetchProfile function

Future<Map> fetchProfile(
  1. XRInputSource xrInputSource,
  2. String basePath, [
  3. dynamic defaultProfile,
  4. bool getAssetPath = true,
])

Implementation

Future<Map> fetchProfile(XRInputSource xrInputSource, String basePath, [defaultProfile, bool getAssetPath = true]) async{
  // Get the list of profiles
  final supportedProfilesList = await fetchProfilesList(basePath);

  // Find the relative path to the first requested profile that is recognized
  Map? match;
  (xrInputSource.profiles.dartify() as List).any((profileId){
    final supportedProfile = supportedProfilesList[profileId] as Map<String,dynamic>?;
    if (supportedProfile != null) {
      match = {
        'profileId': profileId,
        'profilePath': '$basePath/${supportedProfile['path']}',
        'deprecated': supportedProfile['deprecated'] != null && supportedProfile['deprecated'] != false
      };
    }
    return match != null;
  });

  if (match == null) {
    if (!defaultProfile) {
      throw('No matching profile name found');
    }

    final supportedProfile = supportedProfilesList[defaultProfile];
    if (supportedProfile == null) {
      throw ('No matching profile name found and default profile "$defaultProfile" missing.');
    }

    match = {
      'profileId': defaultProfile,
      'profilePath': '$basePath/${supportedProfile['path']}',
      'deprecated': supportedProfile['deprecated'] != null && supportedProfile['deprecated'] != false
    };
  }

  final profile = await fetchJsonFile(match!['profilePath']);

  String? assetPath;
  if (getAssetPath) {
    dynamic layout;
    if (xrInputSource.handedness != null) {
      final map = profile['layouts']?.keys?.toList();
      layout = profile['layouts'][map[0]];
    }
    else {
      layout = profile['layouts'][xrInputSource.handedness];
    }

    if (layout == null) {
      throw(
        'No matching handedness, ${xrInputSource.handedness}, in profile ${match!['profileId']}'
      );
    }

    if (layout['assetPath'] != null) {
      assetPath = (match!['profilePath'] as String?)?.replaceAll('profile.json', layout['assetPath']);
    }
  }

  return {
    'profile': profile,
    'assetPath': assetPath
  };
}