public static string Convert(ResourceCandidate resourceCandidate, string defaultLanguage) { try { ValidatePlatform(resourceCandidate); var language = GetLanguage(resourceCandidate); var directory = Path.GetDirectoryName(resourceCandidate.LogicalPath); var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(resourceCandidate.LogicalPath); var scale = GetScale(resourceCandidate); var extension = Path.GetExtension(resourceCandidate.LogicalPath).ToLowerInvariant(); return(Path.Combine(language, directory, $"{fileNameWithoutExtension}{scale}{extension}")); } #if HAS_UNO catch (Exception ex) { ex.Log().Info($"Couldn't convert {resourceCandidate.ValueAsString} to an iOS resource path.", ex); #else catch (Exception) { #endif return(null); } }
private static void ValidatePlatform(ResourceCandidate resourceCandidate) { var custom = resourceCandidate.GetQualifierValue("custom"); if (custom != null && custom != "ios") { throw new NotSupportedException($"Custom qualifier of value {custom} is not supported on iOS."); } }
private static void ValidateResourceName(ResourceCandidate resourceCandidate) { var resourceName = Path.GetFileName(resourceCandidate.LogicalPath).Split(new char[] { '.' })[0]; if (string.IsNullOrWhiteSpace(resourceName) || !ValidResourceName.IsMatch(resourceName)) { throw new NotSupportedException($"Resource name {resourceName} is not supported on Android."); } }
private static string GetLanguage(ResourceCandidate resourceCandidate) { var language = resourceCandidate.GetQualifierValue("language"); if (language == null) { return(""); } if (language.Contains("-")) { language = language.Replace("-", "_"); } return($"{language}.lproj"); }
public static string?Convert(ResourceCandidate resourceCandidate, string defaultLanguage) { try { ValidatePlatform(resourceCandidate); var language = GetLanguage(resourceCandidate.GetQualifierValue("language"), defaultLanguage); var dpi = GetDpi(resourceCandidate.GetQualifierValue("scale")); var fileName = AndroidResourceNameEncoder.Encode(Path.GetFileNameWithoutExtension(resourceCandidate.LogicalPath)) + Path.GetExtension(resourceCandidate.LogicalPath); return(Path.Combine($"drawable{language}{dpi}", fileName)); } catch (Exception ex) { ex.Log().Info($"Couldn't convert {resourceCandidate.ValueAsString} to an Android resource path.", ex); return(null); } }
private static string GetScale(ResourceCandidate resourceCandidate) { var scale = resourceCandidate.GetQualifierValue("scale"); switch (scale) { case null: case "100": return(""); case "200": return("@2x"); case "300": return("@3x"); default: throw new NotSupportedException($"Scale qualifier of value {scale} is not supported on iOS."); } }
public static string?Convert(ResourceCandidate resourceCandidate, string defaultLanguage) { try { ValidatePlatform(resourceCandidate); var language = GetLanguage(resourceCandidate.GetQualifierValue("language"), defaultLanguage); var dpi = GetDpi(resourceCandidate.GetQualifierValue("scale")); var theme = GetTheme(resourceCandidate.GetQualifierValue("theme")); var fileName = AndroidResourceNameEncoder.EncodeDrawablePath(resourceCandidate.LogicalPath); return(Path.Combine($"drawable{language}{theme}{dpi}", fileName)); } #if HAS_UNO catch (Exception ex) { ex.Log().Info($"Couldn't convert {resourceCandidate.ValueAsString} to an Android resource path.", ex); #else catch (Exception) { #endif return(null); } }