Exemplo n.º 1
0
        private static void Generate(ViewModelBuilder vmBuilder, string viewModelClassName,
                                     IEnumerable <InjectionToGenerate>?injectionsToGenerate,
                                     bool hasCommands, bool isEventSubscriber)
        {
            vmBuilder.AppendLineBeforeMember();
            vmBuilder.Append($"public {viewModelClassName}(");
            injectionsToGenerate ??= Enumerable.Empty <InjectionToGenerate>();

            var    first = true;
            string?eventAggregatorAccessForSubscription = null;

            if (isEventSubscriber)
            {
                var eventAggregatorInjection = injectionsToGenerate.FirstOrDefault(x => x.Type == "MvvmGen.Events.IEventAggregator");
                if (eventAggregatorInjection is not null)
                {
                    eventAggregatorAccessForSubscription = $"this.{eventAggregatorInjection.PropertyName}";
                }
                else
                {
                    eventAggregatorAccessForSubscription = "eventAggregator";
                    first = false;
                    vmBuilder.Append($"MvvmGen.Events.IEventAggregator {eventAggregatorAccessForSubscription}");
                }
            }

            foreach (var injectionToGenerate in injectionsToGenerate)
            {
                if (!first)
                {
                    vmBuilder.Append(", ");
                }
                first = false;
                vmBuilder.Append($"{injectionToGenerate.Type} {injectionToGenerate.PropertyName.ToCamelCase()}");
            }

            vmBuilder.AppendLine(")");
            vmBuilder.AppendLine("{");
            vmBuilder.IncreaseIndent();
            foreach (var injectionToGenerate in injectionsToGenerate)
            {
                vmBuilder.AppendLine($"this.{injectionToGenerate.PropertyName} = {injectionToGenerate.PropertyName.ToCamelCase()};");
            }

            if (isEventSubscriber)
            {
                vmBuilder.AppendLine($"{eventAggregatorAccessForSubscription}.RegisterSubscriber(this);");
            }

            if (hasCommands)
            {
                vmBuilder.AppendLine($"this.InitializeCommands();");
            }

            vmBuilder.AppendLine($"this.OnInitialize();");
            vmBuilder.DecreaseIndent();
            vmBuilder.AppendLine("}");
            vmBuilder.AppendLine();
            vmBuilder.AppendLine($"partial void OnInitialize();");
        }
Exemplo n.º 2
0
 internal static void GenerateCommandProperties(this ViewModelBuilder vmBuilder, IEnumerable <CommandToGenerate>?commandsToGenerate)
 {
     if (commandsToGenerate is not null)
     {
         foreach (var commandToGenerate in commandsToGenerate)
         {
             vmBuilder.AppendLineBeforeMember();
             vmBuilder.AppendLine($"public DelegateCommand {commandToGenerate.PropertyName} {{ get; private set; }}");
         }
     }
 }
 internal static void GenerateInjectionProperties(this ViewModelBuilder vmBuilder, IEnumerable <InjectionToGenerate>?injectionsToGenerate)
 {
     if (injectionsToGenerate is not null)
     {
         foreach (var injectionToGenerate in injectionsToGenerate)
         {
             vmBuilder.AppendLineBeforeMember();
             vmBuilder.AppendLine($"{injectionToGenerate.PropertyAccessModifier} {injectionToGenerate.Type} {injectionToGenerate.PropertyName} {{ get; {injectionToGenerate.SetterAccessModifier} set; }}");
         }
     }
 }
Exemplo n.º 4
0
 internal static void GenerateCommandInitializeMethod(this ViewModelBuilder vmBuilder, IEnumerable <CommandToGenerate>?commandsToGenerate)
 {
     if (commandsToGenerate is not null && commandsToGenerate.Any())
     {
         vmBuilder.AppendLineBeforeMember();
         vmBuilder.AppendLine("private void InitializeCommands()");
         vmBuilder.AppendLine("{");
         vmBuilder.IncreaseIndent();
         foreach (var commandToGenerate in commandsToGenerate)
         {
             vmBuilder.Append($"{commandToGenerate.PropertyName} = new DelegateCommand({GetMethodCall(commandToGenerate.ExecuteMethod)}");
             if (commandToGenerate.CanExecuteMethod.HasValue)
             {
                 vmBuilder.Append($", {GetMethodCall(commandToGenerate.CanExecuteMethod.Value)}");
             }
             vmBuilder.AppendLine(");");
         }
         vmBuilder.DecreaseIndent();
         vmBuilder.AppendLine("}");
     }
 }
Exemplo n.º 5
0
        internal static void GenerateInvalidateCommandsMethod(this ViewModelBuilder vmBuilder,
                                                              IDictionary <string, List <string> >?commandsByPropertyNames)
        {
            if (commandsByPropertyNames is not null && commandsByPropertyNames.Any())
            {
                vmBuilder.AppendLineBeforeMember();
                vmBuilder.AppendLine("protected override void InvalidateCommands(string? propertyName)");
                vmBuilder.AppendLine("{");
                vmBuilder.IncreaseIndent();
                vmBuilder.AppendLine("base.InvalidateCommands(propertyName);");

                var first       = true;
                var ifStatement = "if";

                foreach (var commandsByPropertyName in commandsByPropertyNames)
                {
                    vmBuilder.AppendLine(@$ "{ifStatement} (propertyName == " "{commandsByPropertyName.Key}" ")");
                    if (first)
                    {
                        first       = false;
                        ifStatement = "else if";
                    }
                    vmBuilder.AppendLine("{");
                    vmBuilder.IncreaseIndent();
                    foreach (var commandName in commandsByPropertyName.Value)
                    {
                        vmBuilder.AppendLine($"{commandName}.RaiseCanExecuteChanged();");
                    }

                    vmBuilder.DecreaseIndent();
                    vmBuilder.AppendLine("}");
                }

                vmBuilder.DecreaseIndent();
                vmBuilder.AppendLine("}");
            }
        }
        private static void GenerateProperty(ViewModelBuilder vmBuilder, PropertyToGenerate p)
        {
            vmBuilder.AppendLineBeforeMember();
            vmBuilder.Append($"public {p.PropertyType} {p.PropertyName}");

            if (p.IsReadOnly)
            {
                vmBuilder.AppendLine($" => {p.BackingField};");
                return;
            }
            else
            {
                vmBuilder.AppendLine();
            }

            vmBuilder.AppendLine("{");
            vmBuilder.IncreaseIndent();
            vmBuilder.AppendLine($"get => {p.BackingField};");
            vmBuilder.AppendLine("set");
            vmBuilder.AppendLine("{");
            vmBuilder.IncreaseIndent();
            vmBuilder.AppendLine($"if ({p.BackingField} != value)");
            vmBuilder.AppendLine("{");
            vmBuilder.IncreaseIndent();
            vmBuilder.AppendLine($"{p.BackingField} = value;");
            vmBuilder.AppendLine($"OnPropertyChanged(\"{p.PropertyName}\");");
            if (p.PropertiesToInvalidate is not null)
            {
                foreach (var propertyToInvalidate in p.PropertiesToInvalidate)
                {
                    vmBuilder.AppendLine($"OnPropertyChanged(\"{propertyToInvalidate}\");");
                }
            }
            if (p.EventsToPublish is not null)
            {
                foreach (var eventToPublish in p.EventsToPublish)
                {
                    var createPublishCondition = eventToPublish.PublishCondition is { Length : > 0 };
                    if (createPublishCondition)
                    {
                        vmBuilder.AppendLine($"if ({eventToPublish.PublishCondition})");
                        vmBuilder.AppendLine("{");
                        vmBuilder.IncreaseIndent();
                    }
                    vmBuilder.AppendLine($"{eventToPublish.EventAggregatorMemberName}.Publish(new {eventToPublish.EventType}({eventToPublish.EventConstructorArgs}));");

                    if (createPublishCondition)
                    {
                        vmBuilder.DecreaseIndent();
                        vmBuilder.AppendLine("}");
                    }
                }
            }
            if (p.MethodsToCall is not null)
            {
                foreach (var methodToCall in p.MethodsToCall)
                {
                    vmBuilder.AppendLine($"{methodToCall.MethodName}({methodToCall.MethodArgs});");
                }
            }
            vmBuilder.DecreaseIndent();
            vmBuilder.AppendLine("}");
            vmBuilder.DecreaseIndent();
            vmBuilder.AppendLine("}");
            vmBuilder.DecreaseIndent();
            vmBuilder.AppendLine("}");
        }
    }