Skip to content
This repository has been archived by the owner on Jan 29, 2023. It is now read-only.

Commit

Permalink
- Some changes.
Browse files Browse the repository at this point in the history
- Add 3 `Ready Components`.
  • Loading branch information
CorrM committed Apr 25, 2021
1 parent ac687b1 commit 996ce91
Show file tree
Hide file tree
Showing 11 changed files with 315 additions and 76 deletions.
4 changes: 2 additions & 2 deletions MAU.Net/Attributes/MauContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace MAU.Attributes
{
[AttributeUsage(AttributeTargets.Class)]
public sealed class MauContainer : Attribute
public sealed class MauContainerAttribute : Attribute
{
public static bool HasAttribute(Type type) => Attribute.IsDefined(type, typeof(MauContainer));
public static bool HasAttribute(Type type) => Attribute.IsDefined(type, typeof(MauContainerAttribute));
}
}
22 changes: 11 additions & 11 deletions MAU.Net/Attributes/MauEnumMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace MAU.Attributes
{
[AttributeUsage(AttributeTargets.Field)]
public sealed class MauEnumMember : Attribute
public sealed class MauEnumMemberAttribute : Attribute
{
public enum EnumParser
{
Expand All @@ -17,24 +17,24 @@ public enum EnumParser
public long ValueAsNum { get; }
public EnumParser Parser { get; }

public MauEnumMember(string value)
public MauEnumMemberAttribute(string value)
{
ValueAsStr = value;
Parser = EnumParser.String;
}
public MauEnumMember(long value)
public MauEnumMemberAttribute(long value)
{
ValueAsNum = value;
Parser = EnumParser.Number;
}

public static bool HasAttribute(PropertyInfo pi) => Attribute.IsDefined(pi, typeof(MauEnumMember));
public static bool HasAttribute(FieldInfo fi) => Attribute.IsDefined(fi, typeof(MauEnumMember));
public static bool HasAttribute(PropertyInfo pi) => Attribute.IsDefined(pi, typeof(MauEnumMemberAttribute));
public static bool HasAttribute(FieldInfo fi) => Attribute.IsDefined(fi, typeof(MauEnumMemberAttribute));
public static bool HasAttribute(Enum enumValue)
{
Type enumType = enumValue.GetType();
string name = Enum.GetName(enumType, enumValue);
return Attribute.IsDefined(enumType.GetField(name), typeof(MauEnumMember));
return Attribute.IsDefined(enumType.GetField(name), typeof(MauEnumMemberAttribute));
}

public static bool HasNotSetValue(Type enumType)
Expand All @@ -48,15 +48,15 @@ public static bool GetValidEnumValue(Type valueType, ref object originalValue)
if (!valueType.IsEnum)
return false;

if (!MauEnumMember.HasNotSetValue(valueType))
if (!MauEnumMemberAttribute.HasNotSetValue(valueType))
throw new Exception($"'MauEnumMember.NotSet' must to be in any 'Enum' used as 'MauProperty' type or return of 'MauMethod'. {valueType.FullName}");

object o = originalValue;
string enumValName = valueType.GetFields()
.Where(MauEnumMember.HasAttribute)
.Where(MauEnumMemberAttribute.HasAttribute)
.FirstOrDefault(f =>
{
MauEnumMember f1 = f.GetCustomAttributes<MauEnumMember>(false)
var f1 = f.GetCustomAttributes<MauEnumMemberAttribute>(false)
.FirstOrDefault(memEnum => memEnum.IsEqual(o));
return f1?.IsEqual(o) == true;
Expand Down Expand Up @@ -89,13 +89,13 @@ public static object GetValue(Enum enumValue)
{
Type enumType = enumValue.GetType();
string name = Enum.GetName(enumType, enumValue);
MauEnumMember instance = enumType.GetField(name).GetCustomAttributes<MauEnumMember>(false).FirstOrDefault();
var instance = enumType.GetField(name).GetCustomAttributes<MauEnumMemberAttribute>(false).FirstOrDefault();
if (instance == null)
return null;

return instance.Parser == EnumParser.Number
? instance.ValueAsNum
: (object)instance.ValueAsStr;
: instance.ValueAsStr;
}
}
}
6 changes: 3 additions & 3 deletions MAU.Net/Attributes/MauEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
namespace MAU.Attributes
{
[AttributeUsage(AttributeTargets.Event)]
public sealed class MauEvent : Attribute
public sealed class MauEventAttribute : Attribute
{
public string EventName { get; }

public static bool HasAttribute(EventInfo eventInfo) => Attribute.IsDefined(eventInfo, typeof(MauEvent));
public static bool HasAttribute(EventInfo eventInfo) => Attribute.IsDefined(eventInfo, typeof(MauEventAttribute));

public MauEvent(string eventName)
public MauEventAttribute(string eventName)
{
EventName = eventName;
}
Expand Down
30 changes: 21 additions & 9 deletions MAU.Net/Attributes/MauMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using PostSharp.Extensibility;
using PostSharp.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace MAU.Attributes
Expand Down Expand Up @@ -51,38 +53,48 @@ public override void OnEntry(MethodExecutionArgs args)
}
public override void OnExit(MethodExecutionArgs args)
{
if (MethodCallType == MauMethodCallType.ExecuteFromAngular)
Type retType = ((MethodInfo)args.Method).ReturnType;

if (MethodCallType == MauMethodCallType.ExecuteFromAngular || !MyAngularUi.IsConnected)
{
base.OnExit(args);
return;
}

if (!MyAngularUi.IsConnected)
// Prepare Args
List<object> argsToSend = args.Arguments.ToList();
for (int i = 0; i < argsToSend.Count; i++)
{
base.OnExit(args);
return;
object param = argsToSend[i];
if (!param.GetType().IsEnum || !MauEnumMemberAttribute.HasAttribute((Enum) param))
continue;

argsToSend[i] = MauEnumMemberAttribute.GetValue((Enum)param);
}

// Send
var holder = (MauComponent)args.Instance;
var data = new JObject
{
{"methodType", (int)MethodType},
{"methodName", MethodName},
{"methodArgs", JArray.FromObject(args.Arguments.ToArray())}
{"methodArgs", JArray.FromObject(argsToSend)}
};

MyAngularUi.RequestState request = MyAngularUi.SendRequestAsync(holder.MauId, MyAngularUi.RequestType.CallMethod, data).GetAwaiter().GetResult();

// Wait return
// If its void function then just wait until execution finish
if (((MethodInfo)args.Method).ReturnType == typeof(void))
if (retType == typeof(void))
{
// Wait function
holder.GetMethodRetValue(request.RequestId);
return;
}

// Set return value of function
args.ReturnValue = holder.GetMethodRetValue(request.RequestId);
// Wait and set return value of function
object ret = holder.GetMethodRetValue(request.RequestId);
if (ret is not null)
args.ReturnValue = ret /*?? Activator.CreateInstance(retType)*/;
}
}
}
12 changes: 9 additions & 3 deletions MAU.Net/Attributes/MauProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public enum MauPropertyStatus
public MauPropertyStatus PropStatus { get; set; }
public bool Important { get; set; }

/// <summary>
/// Set property value even not exists
/// </summary>
public bool ForceSet { get; set; }

public MauProperty(string propertyName, MauPropertyType propType)
{
PropertyName = propertyName;
Expand Down Expand Up @@ -60,11 +65,11 @@ internal static void SendMauProp(MauComponent holder, string mauPropName)
}
else if (propType.IsEnum)
{
if (!MauEnumMember.HasNotSetValue(propValue.GetType()))
if (!MauEnumMemberAttribute.HasNotSetValue(propValue.GetType()))
throw new Exception($"NotSet must to be in any MauProperty value is 'Enum', {propValue.GetType().FullName}");

if (MauEnumMember.HasAttribute((Enum)propValue))
propValue = MauEnumMember.GetValue((Enum)propValue);
if (MauEnumMemberAttribute.HasAttribute((Enum)propValue))
propValue = MauEnumMemberAttribute.GetValue((Enum)propValue);

// If it's NotSet just ignore so the angular value will be set,
// Angular value will be in .Net side, so the value will be correct here.
Expand Down Expand Up @@ -96,6 +101,7 @@ internal static void SendMauProp(MauComponent holder, string mauPropName)
{
{"propType", (int)mauPropHolder.PropAttr.PropType},
{"propStatus", (int)mauPropHolder.PropAttr.PropStatus},
{"propForce", mauPropHolder.PropAttr.ForceSet},
{"propName", mauPropName},
{"propVal", MyAngularUi.ParseMauDataToFrontEnd(propType, propValue)}
};
Expand Down
16 changes: 9 additions & 7 deletions MAU.Net/Core/MauComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ public abstract class MauComponent

#region [ UI Methods ]

public void SetStyle(string styleName, string styleValue)
public void SetStyle(string styleName, string styleValue, string childQuerySelector = "")
{
var data = new JObject
{
{"styleName", styleName},
{"styleValue", styleValue},
{"childQuerySelector", childQuerySelector},
};

MyAngularUi.SendRequestAsync(MauId, MyAngularUi.RequestType.SetStyle, data);
Expand Down Expand Up @@ -149,9 +150,9 @@ private void Init()
// Events
{
EventInfo[] eventInfos = this.GetType().GetEvents(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
foreach (EventInfo eventInfo in eventInfos.Where(MauEvent.HasAttribute))
foreach (EventInfo eventInfo in eventInfos.Where(MauEventAttribute.HasAttribute))
{
var attr = eventInfo.GetCustomAttribute<MauEvent>();
var attr = eventInfo.GetCustomAttribute<MauEventAttribute>();
HandledEvents.Add(attr.EventName, eventInfo);
}
}
Expand Down Expand Up @@ -211,7 +212,7 @@ internal void FireEvent(string eventName, string eventType, JObject eventData)
var eventDelegate = (MulticastDelegate)fi.GetValue(this);

// There any subscriber .?
if (eventDelegate == null)
if (eventDelegate is null)
return;

// Invoke all subscribers
Expand Down Expand Up @@ -271,7 +272,8 @@ internal void RequestPropValue(string propName)
{
{"propName", propName},
{"propType", (int)mauProperty.PropAttr.PropType},
{"propStatus", (int)mauProperty.PropAttr.PropStatus} // Needed by `SetPropHandler`
{"propStatus", (int)mauProperty.PropAttr.PropStatus}, // Needed by `SetPropHandler`
{"propForce", mauProperty.PropAttr.ForceSet} // Needed by `SetPropHandler`
};

MyAngularUi.SendRequestAsync(MauId, MyAngularUi.RequestType.GetPropValue, data);
Expand All @@ -285,7 +287,7 @@ internal void SetPropValue(string propName, JToken propValueJson)
object propValue = MyAngularUi.ParseMauDataFromFrontEnd(propValType, propValueJson);

// Make valid enum value
MauEnumMember.GetValidEnumValue(propValType, ref propValue);
MauEnumMemberAttribute.GetValidEnumValue(propValType, ref propValue);

// Deadlock will not happen because of 'HandledProps[propName].HandleOnSet'
lock (HandledProps[propName])
Expand All @@ -301,7 +303,7 @@ internal void SetMethodRetValue(int callMethodRequestId, string methodName, JTok
object methodRet = MyAngularUi.ParseMauDataFromFrontEnd(methodRetType, methodRetValueJson);

// Make valid enum value
MauEnumMember.GetValidEnumValue(methodRetType, ref methodRet);
MauEnumMemberAttribute.GetValidEnumValue(methodRetType, ref methodRet);

MyAngularUi.OrdersResponse.TryAdd(callMethodRequestId, methodRet);
}
Expand Down
18 changes: 10 additions & 8 deletions MAU.Net/MyAngularUi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
Expand Down Expand Up @@ -259,8 +260,8 @@ public static async Task StartAsync(int webSocketPort = 2911)

Port = webSocketPort;
WebSocket = new MauWebSocket(Port);
WebSocket.OnOpen += () => Task.Run(OnOpen);
WebSocket.OnClose += () => Task.Run(OnClose);
WebSocket.OnOpen += () => Task.Run(OpenCallback);
WebSocket.OnClose += () => Task.Run(CloseCallback);
WebSocket.OnMessage += (message) => Task.Run(() => OnMessage(message));
WebSocket.Start();

Expand Down Expand Up @@ -289,11 +290,12 @@ private static void ReSyncMauComponents()
MauVariable.SendMauVariable(mauComponent, varName);

// Props
foreach ((string propName, _) in mauComponent.GetValidToSetHandledProps())
Dictionary<string, MauPropertyHolder> props = mauComponent.GetValidToSetHandledProps();
foreach ((string propName, _) in props)
MauProperty.SendMauProp(mauComponent, propName);

// Events
MauEvent.SendMauEventsAsync(mauComponent);
MauEventAttribute.SendMauEventsAsync(mauComponent);
}

if (_mauComponents.Count > 0)
Expand Down Expand Up @@ -457,13 +459,13 @@ internal static void OnMessage(string message)
}
}

internal static void OnOpen()
internal static void OpenCallback()
{
IsConnected = true;
ReSyncMauComponents();
}

internal static void OnClose()
internal static void CloseCallback()
{
IsConnected = false;
}
Expand Down Expand Up @@ -513,7 +515,7 @@ internal static void RegisterComponent(MauComponent mauComponent)
private static void BootStrapMau()
{
// Create instance of all 'MauParentComponent'
foreach (Type item in AppAssembly.GetTypes().Where(t => !t.IsAbstract && MauContainer.HasAttribute(t)))
foreach (Type item in AppAssembly.GetTypes().Where(t => !t.IsAbstract && MauContainerAttribute.HasAttribute(t)))
{
if (!item.IsSealed)
throw new Exception($"MauContainer must be 'Sealed', '{item.FullName}'.");
Expand All @@ -531,7 +533,7 @@ private static void BootStrapMau()
public static T GetMauContainer<T>() where T : class
{
Type t = typeof(T);
if (!MauContainer.HasAttribute(t))
if (!MauContainerAttribute.HasAttribute(t))
throw new Exception($"'{t.FullName}' not a MauContainer.");

string compName = typeof(T).FullName;
Expand Down

0 comments on commit 996ce91

Please sign in to comment.