using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace FlutterCodeGenerator { internal class OtherServiceMap : IServiceMap { private List _typeList; private string _serviceName; public List UsedComplexModelTypeList { get; } public OtherServiceMap(List types) { _typeList = types; UsedComplexModelTypeList = new List(); GenerateDataCache.Instance.SetCurrentServiceMap(this); _serviceName = "OtherService"; foreach (var type in _typeList) { try { var parameterModelType = ModelTypeGenerator.Create(type, type.Name, true); } catch (Exception ex) { Console.WriteLine($"Type is {type.FullName},Error:{ex}"); throw; } } } public string GetServiceDartString() { return null; } public string GetServiceModelDartString() { var dartString = new StringBuilder(); var importServiceList = new List(); var alreadyGeneratedList = GenerateDataCache.Instance.AlreadyGeneratedList; foreach (var modelType in UsedComplexModelTypeList) { if (!alreadyGeneratedList.Any(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace)) { alreadyGeneratedList.Add(modelType, _serviceName); dartString.AppendLine(modelType.GetDartString()); } else { var importService = alreadyGeneratedList.FirstOrDefault(x => x.Key.ParameterType.Name == modelType.ParameterType.Name && x.Key.ParameterType.Namespace == modelType.ParameterType.Namespace).Value; if (importService == null) { throw new Exception("Import Service is null"); } if (importService != _serviceName && !importServiceList.Contains(importService)) { importServiceList.Add(importService); } } } if (string.IsNullOrWhiteSpace(dartString.ToString())) { return null; } var serviceModelDartString = new StringBuilder(); foreach (var importService in importServiceList) { serviceModelDartString.AppendLine($"import '{LetterConverterHelper.FirstCharToLower(importService[0..^7])}.m.dart';"); } if (importServiceList.Count > 0) { serviceModelDartString.AppendLine(); } var dartStringInfo = dartString.ToString(); if (dartStringInfo.Contains("JsonRpcUtils")) { serviceModelDartString.AppendLine(CodeGenerator.StringUtils); serviceModelDartString.AppendLine(); } if (dartStringInfo.Contains("FJsonConvert")) { serviceModelDartString.AppendLine(CodeGenerator.StringJsonConvert); serviceModelDartString.AppendLine(); } serviceModelDartString.AppendLine(dartStringInfo); return serviceModelDartString.ToString(); } } }