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 GenerateClass(this ViewModelBuilder vmBuilder, INamedTypeSymbol viewModelClassSymbol, INamedTypeSymbol viewModelBaseSymbol)
        {
            var inheritFromViewModelBaseClass = !InheritsFromViewModelBase(viewModelClassSymbol, viewModelBaseSymbol);

            vmBuilder.AppendLine($"partial class {viewModelClassSymbol.Name}" + (inheritFromViewModelBaseClass ? " : global::MvvmGen.ViewModels.ViewModelBase" : ""));
            vmBuilder.AppendLine("{");
            vmBuilder.IncreaseIndent();
        }
Exemplo n.º 3
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("}");
            }
        }
        internal static void GenerateNamespace(this ViewModelBuilder vmBuilder, INamedTypeSymbol viewModelClassSymbol)
        {
            vmBuilder.AppendLine();

            // Add namespace declaration
            if (viewModelClassSymbol.ContainingNamespace is null)
            {
                return;
                // TODO: Show an error here. ViewModel class must be top-level within a namespace
            }

            vmBuilder.AppendLine($"namespace {viewModelClassSymbol.ContainingNamespace}");
            vmBuilder.AppendLine("{");
            vmBuilder.IncreaseIndent();
        }
Exemplo n.º 5
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("}");
     }
 }
        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("}");
        }
    }
Exemplo n.º 7
0
        internal static void GenerateFactoryClass(this ViewModelBuilder vmBuilder, ViewModelToGenerate viewModelToGenerate)
        {
            if (viewModelToGenerate.ViewModelFactoryToGenerate is null)
            {
                return;
            }

            var factoryToGenerate  = viewModelToGenerate.ViewModelFactoryToGenerate;
            var viewModelClassName = viewModelToGenerate.ViewModelClassSymbol.Name;

            var injectionsToGenerate = viewModelToGenerate.InjectionsToGenerate ?? Enumerable.Empty <InjectionToGenerate>();

            var accessModifier = viewModelToGenerate.ViewModelClassSymbol.DeclaredAccessibility switch
            {
                Accessibility.Public => "public",
                Accessibility.Internal => "internal",
                _ => ""
            };

            vmBuilder.AppendLine();
            vmBuilder.AppendLine($"{accessModifier} interface {factoryToGenerate.InterfaceName} : IViewModelFactory<{viewModelClassName}> {{ }}");
            vmBuilder.AppendLine();
            vmBuilder.AppendLine($"{accessModifier} class {factoryToGenerate.ClassName} : {factoryToGenerate.InterfaceName}");
            vmBuilder.AppendLine("{");
            vmBuilder.IncreaseIndent();

            vmBuilder.Append($"public {factoryToGenerate.ClassName}(");
            var first = true;

            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()};");
            }

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

            InjectionPropertyGenerator.GenerateInjectionProperties(vmBuilder, injectionsToGenerate);

            vmBuilder.AppendLine();
            vmBuilder.Append($"public {viewModelClassName} Create() => new {viewModelClassName}(");
            first = true;
            foreach (var injectionToGenerate in injectionsToGenerate)
            {
                if (!first)
                {
                    vmBuilder.Append(", ");
                }
                first = false;
                vmBuilder.Append(injectionToGenerate.PropertyName);
            }

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