NotificationServiceMap.cs 9.9 KB

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