NotificationServiceMap.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.Json;
  6. namespace FlutterCodeGenerator
  7. {
  8. internal class NotificationServiceMap : IServiceMap
  9. {
  10. private List<Type> _typeList;
  11. private string _serviceName;
  12. private EnumModelType _notificationEnumModelType;
  13. private List<ModelType> _notificationModelTypeList;
  14. public List<ComplexModelType> UsedComplexModelTypeList { get; }
  15. public NotificationServiceMap(Type notificationEnumType, List<Type> types)
  16. {
  17. _typeList = types;
  18. UsedComplexModelTypeList = new List<ComplexModelType>();
  19. GenerateDataCache.Instance.SetCurrentServiceMap(this);
  20. _notificationModelTypeList = new List<ModelType>();
  21. _notificationEnumModelType = (EnumModelType)ModelTypeGenerator.Create(notificationEnumType, notificationEnumType.Name, true);
  22. _serviceName = "NotificationService";
  23. foreach (var type in _typeList)
  24. {
  25. try
  26. {
  27. _notificationModelTypeList.Add(ModelTypeGenerator.Create(type, type.Name, true));
  28. }
  29. catch (Exception ex)
  30. {
  31. Console.WriteLine($"Type is {type.FullName},Error:{ex}");
  32. throw;
  33. }
  34. }
  35. }
  36. public string GetServiceDartString()
  37. {
  38. return null;
  39. }
  40. public string GetServiceModelDartString()
  41. {
  42. var dartString = new StringBuilder();
  43. var importServiceList = new List<string>();
  44. var alreadyGeneratedList = GenerateDataCache.Instance.AlreadyGeneratedList;
  45. foreach (var modelType in UsedComplexModelTypeList)
  46. {
  47. if (!alreadyGeneratedList.Any(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace))
  48. {
  49. alreadyGeneratedList.Add(modelType, _serviceName);
  50. dartString.AppendLine(modelType.GetDartString());
  51. }
  52. else
  53. {
  54. var importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value;
  55. if (importService == null)
  56. {
  57. throw new Exception("Import Service is null");
  58. }
  59. if (importService != _serviceName && !importServiceList.Contains(importService))
  60. {
  61. importServiceList.Add(importService);
  62. }
  63. }
  64. }
  65. if (string.IsNullOrWhiteSpace(dartString.ToString()))
  66. {
  67. return null;
  68. }
  69. var serviceModelDartString = new StringBuilder();
  70. foreach (var importService in importServiceList)
  71. {
  72. serviceModelDartString.AppendLine($"import '{LetterConverterHelper.FirstCharToLower(importService[0..^7])}.m.dart';");
  73. }
  74. if (importServiceList.Count > 0)
  75. {
  76. serviceModelDartString.AppendLine();
  77. }
  78. var dartStringInfo = dartString.ToString();
  79. if (dartStringInfo.Contains("JsonRpcUtils"))
  80. {
  81. serviceModelDartString.AppendLine(CodeGenerator.StringUtils);
  82. serviceModelDartString.AppendLine();
  83. }
  84. if (dartStringInfo.Contains("FJsonConvert"))
  85. {
  86. serviceModelDartString.AppendLine(CodeGenerator.StringJsonConvert);
  87. serviceModelDartString.AppendLine();
  88. }
  89. serviceModelDartString.AppendLine(dartStringInfo);
  90. return serviceModelDartString.ToString();
  91. }
  92. public string GetDecodeDartString()
  93. {
  94. var dartString = new StringBuilder();
  95. var importServiceList = new List<string>();
  96. var alreadyGeneratedList = GenerateDataCache.Instance.AlreadyGeneratedList;
  97. dartString.AppendLine("typedef _ModelBuilder<T> = T Function(Map<String, dynamic> map);");
  98. dartString.AppendLine("class NotificationDecoder {");
  99. dartString.AppendLine("\tNotificationDecoder._();");
  100. dartString.AppendLine("");
  101. dartString.AppendLine("\tstatic final _builders = _ModelBuilderCollection();");
  102. dartString.AppendLine("");
  103. dartString.AppendLine("\tstatic T decode<T>(Map<String, dynamic> map) {");
  104. dartString.AppendLine("\t\tfinal type = decodeType(map);");
  105. dartString.AppendLine("\t\tfinal builder = _builders.find(type);");
  106. dartString.AppendLine("\t\tfinal model = builder.call(map) as T;");
  107. dartString.AppendLine("\t\treturn model;");
  108. dartString.AppendLine("\t}");
  109. dartString.AppendLine($"\tstatic {_notificationEnumModelType.Name_Upper} decodeType(Map<String, dynamic> map) {{");
  110. dartString.AppendLine("\t\tfinal typeInt = map['type'] as int;");
  111. dartString.AppendLine($"\t\treturn {_notificationEnumModelType.Name_Upper}.values[typeInt];");
  112. dartString.AppendLine("\t}");
  113. dartString.AppendLine("");
  114. dartString.AppendLine("\tstatic void setup() {");
  115. dartString.AppendLine("\t\t/** Register notification model builders here */");
  116. if (alreadyGeneratedList.Any(x => x.Key.ParameterType.Name == _notificationEnumModelType.ParameterType.Name && x.Key.ParameterType.Namespace == _notificationEnumModelType.ParameterType.Namespace))
  117. {
  118. var importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == _notificationEnumModelType.ParameterType.Name && x.Key.ParameterType.Namespace == _notificationEnumModelType.ParameterType.Namespace).Value;
  119. if (importService == null)
  120. {
  121. throw new Exception("Import Service is null");
  122. }
  123. if (!importServiceList.Contains(importService))
  124. {
  125. importServiceList.Add(importService);
  126. }
  127. }
  128. foreach (var notificationModelType in _notificationModelTypeList)
  129. {
  130. if (alreadyGeneratedList.Any(x => x.Key.ParameterType.Name == notificationModelType.ParameterType.Name && x.Key.ParameterType.Namespace == notificationModelType.ParameterType.Namespace))
  131. {
  132. var importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == notificationModelType.ParameterType.Name && x.Key.ParameterType.Namespace == notificationModelType.ParameterType.Namespace).Value;
  133. if (importService == null)
  134. {
  135. throw new Exception("Import Service is null");
  136. }
  137. if (!importServiceList.Contains(importService))
  138. {
  139. importServiceList.Add(importService);
  140. }
  141. }
  142. var instance = Activator.CreateInstance(notificationModelType.ParameterType);
  143. var instanceString = JsonSerializer.Serialize(instance);
  144. try
  145. {
  146. var notificationType = JsonSerializer.Deserialize<VirtualNotification>(instanceString);
  147. if (notificationType != null)
  148. {
  149. var value = _notificationEnumModelType.UserDefinedEnumDictionary[notificationType.NotificationType];
  150. dartString.AppendLine($"\t\t_builders.add({_notificationEnumModelType.Name_Upper}.{value},");
  151. dartString.AppendLine($"\t\t\t\t(map) => {notificationModelType.Name_Upper}.fromJson(map));");
  152. dartString.AppendLine("");
  153. }
  154. }
  155. catch (Exception ex)
  156. {
  157. Console.WriteLine($"Notification Deserialize Error:{ex}");
  158. continue;
  159. }
  160. }
  161. dartString.AppendLine("\t}");
  162. dartString.AppendLine("}");
  163. dartString.AppendLine("");
  164. dartString.AppendLine("class _ModelBuilderCollection {");
  165. dartString.AppendLine($"\tfinal Map<{_notificationEnumModelType.Name_Upper}, _ModelBuilder> _source = {{}};");
  166. dartString.AppendLine("");
  167. dartString.AppendLine($"\tvoid add({_notificationEnumModelType.Name_Upper} type, _ModelBuilder builder) {{");
  168. dartString.AppendLine("\t\t_source[type] = builder;");
  169. dartString.AppendLine("\t}");
  170. dartString.AppendLine("");
  171. dartString.AppendLine($"\t_ModelBuilder find({_notificationEnumModelType.Name_Upper} type) {{");
  172. dartString.AppendLine("\t\tfinal builder = _source[type];");
  173. dartString.AppendLine("\t\treturn builder!;");
  174. dartString.AppendLine("\t}");
  175. dartString.AppendLine("}");
  176. var decodeDartString = new StringBuilder();
  177. foreach (var importService in importServiceList)
  178. {
  179. decodeDartString.AppendLine($"import '{LetterConverterHelper.FirstCharToLower(importService[0..^7])}.m.dart';");
  180. }
  181. if (importServiceList.Count > 0)
  182. {
  183. decodeDartString.AppendLine();
  184. }
  185. decodeDartString.AppendLine(dartString.ToString());
  186. return decodeDartString.ToString();
  187. }
  188. }
  189. }