Skip to content

Commit

Permalink
Scaffold app switch method
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmendie committed Feb 12, 2024
1 parent f6c5819 commit e62fc8d
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 9 deletions.
87 changes: 85 additions & 2 deletions Mendi.Blazor.DynamicNavigation.CLI/Commands/GetPageRoutes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ private async Task GenerateRoutes(IEnumerable<string> routeFilePaths, string pat
await CreateOnBackToPreviousPageClicked(basePath);
await CreateOnMapNavMenuClicked(basePath);
await CreateOnMapPageItemClicked(basePath);
await CreateOnMapAppSwitchClicked(basePath);
SaveGeneratedRoutes(sb.ToString(), basePath);

#endregion
Expand Down Expand Up @@ -437,6 +438,88 @@ private protected static async Task CreateOnMapPageItemClicked(string filePath)

}

private protected static async Task CreateOnMapAppSwitchClicked(string filePath)
{
var sort = filePath;
filePath = filePath.Replace('/', '\\');

if (File.Exists(filePath))
{
string[] lines = File.ReadAllLines(filePath);

// Find the starting line of the GetPageRoutes method
int startIndex = -1;
for (int i = 0; i < lines.Length; i++)
{
if (lines[i].Contains("OnMapAppSwitchClicked(int appId)"))
{
startIndex = i;
break;
}
}

bool proceed = true;
if (startIndex != -1) proceed = false;
if (startIndex != -1)
{
// Find the real ending brace of the GetPageRoutes method
int endIndex = UtilsHelper.FindMatchingClosingBrace(lines, startIndex);

// Remove existing GetPageRoutes method
if (endIndex != -1)
{
lines = lines.Take(startIndex).Concat(lines.Skip(endIndex + 1)).ToArray();
}
}

if (proceed)
{
//read file content
var fileContent = File.ReadAllText(sort);

// Assuming .razor.cs file contains a class definition, extract its type.
var className = await ComponentHelper.ExtractComponentClassName(fileContent);

if (className == null)
{
Console.WriteLine($">>> Failed to extract component type from the file content. Path: {filePath}");
return;
}

// Find the index where the GetPageRoutes method was removed
int insertIndex = startIndex;

// Generate the new method code
StringBuilder newMethodCode = new StringBuilder();
newMethodCode.AppendLine("public async Task OnMapAppSwitchClicked(int appId)");
newMethodCode.AppendLine("{");
newMethodCode.AppendLine("//add code logic before or after this line depending on your use case");
newMethodCode.AppendLine($" _ = await OnSwitchPageCliked(appId, PageRouteContainer, PageRouteRegistry, typeof({className}));");
newMethodCode.AppendLine("}");

if (startIndex != -1)
{
// Insert the new method code
lines = lines.Take(insertIndex).Concat(newMethodCode.ToString().Split('\n')).Concat(lines.Skip(insertIndex)).ToArray();
}
else
{
lines = lines.Take(lines.Length - 1).Concat(newMethodCode.ToString().Split('\n')).Concat(new[] { "}" }).ToArray();
}

// Write the modified content back to the file
File.WriteAllLines(filePath, lines);

FormatCode(filePath);
}
}
else
{
Console.WriteLine($"File not found: {filePath}");
}

}

private protected static async Task CreateOnBackToPreviousPageClicked(string filePath)
{
var sort = filePath;
Expand Down Expand Up @@ -596,7 +679,7 @@ private protected static void CreateSinglePageRoute(string filePath)
int startIndex = -1;
for (int i = 0; i < lines.Length; i++)
{
if (lines[i].Contains("DynamicNavigatorRoute SinglePageRoute { get; set; }"))
if (lines[i].Contains("DynamicNavigatorRoute? SinglePageRoute { get; set; }"))
{
startIndex = i;
break;
Expand Down Expand Up @@ -624,7 +707,7 @@ private protected static void CreateSinglePageRoute(string filePath)

// Generate the new method code
StringBuilder newMethodCode = new StringBuilder();
newMethodCode.AppendLine("public DynamicNavigatorRoute SinglePageRoute { get; set; } = null!;");
newMethodCode.AppendLine("public DynamicNavigatorRoute? SinglePageRoute { get; set; }");

if (startIndex != -1)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<RepositoryUrl>https://github.com/danielmendie/Mendi.Blazor.DynamicNavigation</RepositoryUrl>
<PackageTags>CLI</PackageTags>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Version>1.0.7</Version>
<Version>1.0.8</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,34 @@ public virtual Task BuildPageRoutes()
/// <summary>
/// Method invoked to switch between multiple app routes base on appId type
/// </summary>
/// <param name="page">App Id of the component app to switch to</param>
/// <param name="appId">App Id of the component app to switch to</param>
/// <param name="registry">Dynamic navigator registry</param>
/// <param name="container">Dynamic navigation container</param>
/// <param name="callingComponent">Type of calling component or class</param>
/// <param name="ignoreIndexOpt">Ignore persisting the dynamic navigation route data in IndexDB. Defaults to false </param>
/// <exception cref="NotImplementedException"></exception>
public virtual async Task OnSwitchPageCliked(int page, DynamicNavigatorRegistry registry, bool ignoreIndexOpt = false)
public virtual async Task<DynamicNavigatorRouteResult> OnSwitchPageCliked(int appId, DynamicNavigatorContainer container, DynamicNavigatorRegistry registry, Type callingComponent, bool ignoreIndexOpt = false)
{
DynamicNavigatorRouteResult result = new DynamicNavigatorRouteResult { NavigatorContainer = container };
try
{
if (registry.DefaultsRoutes is not null)
{
var component = registry.DefaultsRoutes[page];
var component = registry.DefaultsRoutes[appId];
var pageRoute = registry.ApplicationRoutes.FirstOrDefault(v => v.Value.ComponentPath.Equals(component));

var data = new DynamicNavigatorRoute
var SinglePageRoute = new DynamicNavigatorRoute
{
AppId = pageRoute.Value.AppId,
AppName = pageRoute.Value.AppName,
Component = nameof(pageRoute.Value.ComponentName)
};

result.NavigatorContainer = new DynamicNavigatorContainer { CurrentPageRoute = callingComponent.Assembly.GetType(pageRoute.Value.ComponentPath) };
result.NavigatorRoute = SinglePageRoute;

if (!ignoreIndexOpt)
await DynamicNavigatorIndexDbAddValue(DynamicNavigatorIndexDbKeyTypes.Page, data);
await DynamicNavigatorIndexDbAddValue(DynamicNavigatorIndexDbKeyTypes.Page, SinglePageRoute);

NavigationManager.NavigateTo("/", forceLoad: true);
}
Expand All @@ -69,6 +75,8 @@ public virtual async Task OnSwitchPageCliked(int page, DynamicNavigatorRegistry
{
Console.WriteLine(ex.ToString());
}

return result;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<RepositoryUrl>https://github.com/danielmendie/Mendi.Blazor.DynamicNavigation</RepositoryUrl>
<PackageTags>Web Assembly</PackageTags>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Version>1.0.7</Version>
<Version>1.0.8</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit e62fc8d

Please sign in to comment.