Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add quickstart models and sorting #155

Merged
merged 4 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
215 changes: 193 additions & 22 deletions package/Editor/Models/Models.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ public class Models : EditorWindow

#region Public Fields

public static List<ModelData> modelsQuickStart = new();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add some comments to your newly added Lists

public static List<ModelData> modelsPrivate = new();
public static List<ModelData> modelsPublic = new();

public static List<TexturePair> texturesQuickStart = new();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add some comments to your newly added Lists

public static List<TexturePair> texturesPrivate = new();
public static List<TexturePair> texturesPublic = new();

public static string privacyQuickStart = "quickstart";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add some comments to your newly added Lists

public static string privacyPrivate = "private";
public static string privacyPublic = "public";

Expand Down Expand Up @@ -71,44 +74,94 @@ public static void SetTab(string privacySetting)
{
CurrentPrivacy = privacySetting;
isProcessing = false;
if (privacySetting == privacyPrivate)
switch (privacySetting)
{
PopulatePrivateModels();
}
else
{
PopulatePublicModels();
case string str when str.Equals(privacyQuickStart):
PopulateQuickStartModels();
break;

case string str when str.Equals(privacyPrivate):
PopulatePrivateModels();
break;

case string str when str.Equals(privacyPublic):
PopulatePublicModels();
break;

default:
break;
}
}

/// <summary>
/// Check which tab is selected, is it Private one ?
/// </summary>
/// <returns> True or False </returns>
public static bool IsPrivateTab()
{
return CurrentPrivacy == privacyPrivate;
}

/// <summary>
/// Check which tab is selected, is it Quickstart one ?
/// </summary>
/// <returns> True or False </returns>
public static bool IsQuickStartTab()
{
return CurrentPrivacy == privacyQuickStart;
}

/// <summary>
/// Depending from the tab selected returned correct models list.
/// </summary>
/// <returns> Models list depending from the tab </returns>
public static List<ModelData> GetModels()
{
return (IsPrivateTab()) ? modelsPrivate : modelsPublic;
if (IsQuickStartTab())
{
return modelsQuickStart;
}
else
{
return (IsPrivateTab()) ? modelsPrivate : modelsPublic;
}
}

/// <summary>
/// Depending from the tab selected returned correct textures list.
/// </summary>
/// <returns> Textures list depending from the tab </returns>
public static List<TexturePair> GetTextures()
{
return (IsPrivateTab()) ? texturesPrivate : texturesPublic;
if (IsQuickStartTab())
{
return texturesQuickStart;
}
else
{
return (IsPrivateTab()) ? texturesPrivate : texturesPublic;
}
}

public static string CurrentPrivacy
{
get => EditorPrefs.GetString("privacy", privacyPrivate);
get => EditorPrefs.GetString("privacy", privacyQuickStart);
set => EditorPrefs.SetString("privacy", value);
}

public static string PagniationTokenPrivate
public static string PaginationTokenQuickStart
{
get => EditorPrefs.GetString("paginationTokenQuickStart", "");
set => EditorPrefs.SetString("paginationTokenQuickStart", value);
}

public static string PaginationTokenPrivate
{
get => EditorPrefs.GetString("paginationTokenPrivate", "");
set => EditorPrefs.SetString("paginationTokenPrivate", value);
}

public static string PagniationTokenPublic
public static string PaginationTokenPublic
{
get => EditorPrefs.GetString("paginationTokenPublic", "");
set => EditorPrefs.SetString("paginationTokenPublic", value);
Expand All @@ -129,6 +182,7 @@ private static async void PopulatePublicModels()
modelsPublic.Clear();
await FetchAllPublicModels();
FetchAllPublicTextures();

modelsUI.RedrawPage(0);
}

Expand All @@ -137,9 +191,62 @@ private static async void PopulatePrivateModels()
modelsPrivate.Clear();
await FetchAllPrivateModels();
FetchAllPrivateTextures();

modelsUI.RedrawPage(0);
}

/// <summary>
/// On Selected quickstart models tab, launch a request and get all quickstart models
/// </summary>
private static async void PopulateQuickStartModels()
{
modelsQuickStart.Clear();

await FetchAllQuickStartModels();
FetchAllQuickStartTextures();
modelsUI.RedrawPage(0);
}

/// <summary>
/// Processing to get all quickstart textures
/// </summary>
private static void FetchAllQuickStartTextures()
{
foreach (var item in modelsQuickStart)
{
string downloadUrl = null;

if (item.thumbnail != null && !string.IsNullOrEmpty(item.thumbnail.url))
{
downloadUrl = item.thumbnail.url;
}
else if (item.trainingImages != null && item.trainingImages.Count > 0)
{
downloadUrl = item.trainingImages[0].downloadUrl;
}

if (string.IsNullOrEmpty(downloadUrl)) continue;

var texturePair = new TexturePair()
{
name = item.name,
texture = null,
};

texturesQuickStart.Add(texturePair);

CommonUtils.FetchTextureFromURL(downloadUrl, texture =>
{
texturePair.texture = texture;
});

if (window != null) { window.Repaint(); }
}
}

/// <summary>
/// Processing to get all private textures
/// </summary>
private static void FetchAllPrivateTextures()
{
foreach (var item in modelsPrivate)
Expand Down Expand Up @@ -174,6 +281,9 @@ private static void FetchAllPrivateTextures()
}
}

/// <summary>
/// Processing to get all public textures.
/// </summary>
private static void FetchAllPublicTextures()
{

Expand Down Expand Up @@ -209,19 +319,75 @@ private static void FetchAllPublicTextures()
}
}

private static async Task FetchAllPrivateModels()
/// <summary>
/// Processing to get all quickstart models
/// </summary>
/// <returns></returns>
private static async Task FetchAllQuickStartModels()
{
if (!isProcessing)
{
while (true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there a while(true) here ? seems weird / useless

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So without this while, is fetching only one page of model.
But I'm agree, and may be we can use thoses lines to:

 PaginationTokenPublic = "";
 Debug.Log("no next page to fetch.");

To stop the process. I'll check this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix it !

With this fix we also fix another bug, which it was, sometines on redraw or reopening, first models or the first page were in double.

{
isProcessing = true;
string endpoint = $"models?pageSize=15&status=trained&privacy={privacyPublic}";

if (!string.IsNullOrEmpty(PaginationTokenQuickStart))
{
endpoint += $"&paginationToken={PaginationTokenQuickStart}";
}

string response = await ApiClient.RestGetAsync(endpoint);
if (response is null) { return; }

var modelsResponse = JsonConvert.DeserializeObject<ModelsResponse>(response);
if (modelsResponse is null) { return; }

// Treat incoming models
for (int i = 0; i < modelsResponse.models.Count; i++)
{
if (modelsResponse.models[i].tags.Contains("Unity") && !modelsQuickStart.Contains(modelsResponse.models[i]))
{
modelsQuickStart.Add(modelsResponse.models[i]);
}
}

if (modelsResponse.nextPaginationToken is null ||
PaginationTokenQuickStart == modelsResponse.nextPaginationToken)
{
PaginationTokenQuickStart = "";
Debug.Log("no next page to fetch.");
}
else
{
PaginationTokenQuickStart = modelsResponse.nextPaginationToken;
Debug.Log("fetching next page data...");
continue;
}

break;
}

isProcessing = false;
}
}

/// <summary>
/// Processing to get all private models.
/// </summary>
/// <returns></returns>
private static async Task FetchAllPrivateModels()
{
if (!isProcessing)
{
while (true)
{
isProcessing = true;
string endpoint = $"models?pageSize=15&status=trained&privacy={privacyPrivate}";

if (!string.IsNullOrEmpty(PagniationTokenPrivate))
if (!string.IsNullOrEmpty(PaginationTokenPrivate))
{
endpoint += $"&paginationToken={PagniationTokenPrivate}";
endpoint += $"&paginationToken={PaginationTokenPrivate}";
}

string response = await ApiClient.RestGetAsync(endpoint);
Expand All @@ -233,14 +399,14 @@ private static async Task FetchAllPrivateModels()
modelsPrivate.AddRange(modelsResponse.models);

if (modelsResponse.nextPaginationToken is null ||
PagniationTokenPrivate == modelsResponse.nextPaginationToken)
PaginationTokenPrivate == modelsResponse.nextPaginationToken)
{
PagniationTokenPrivate = "";
PaginationTokenPrivate = "";
Debug.Log("no next page to fetch.");
}
else
{
PagniationTokenPrivate = modelsResponse.nextPaginationToken;
PaginationTokenPrivate = modelsResponse.nextPaginationToken;
Debug.Log("fetching next page data...");
continue;
}
Expand All @@ -252,6 +418,10 @@ private static async Task FetchAllPrivateModels()
}
}

/// <summary>
/// Processing to get all public models
/// </summary>
/// <returns></returns>
private static async Task FetchAllPublicModels()
{
if (!isProcessing)
Expand All @@ -261,9 +431,9 @@ private static async Task FetchAllPublicModels()
isProcessing = true;
string endpoint = $"models?pageSize=15&status=trained&privacy={privacyPublic}";

if (!string.IsNullOrEmpty(PagniationTokenPublic))
if (!string.IsNullOrEmpty(PaginationTokenPublic))
{
endpoint += $"&paginationToken={PagniationTokenPublic}";
endpoint += $"&paginationToken={PaginationTokenPublic}";
}

string response = await ApiClient.RestGetAsync(endpoint);
Expand All @@ -285,14 +455,14 @@ private static async Task FetchAllPublicModels()
//* modelsPublic.AddRange(modelsResponse.models);

if (modelsResponse.nextPaginationToken is null ||
PagniationTokenPublic == modelsResponse.nextPaginationToken)
PaginationTokenPublic == modelsResponse.nextPaginationToken)
{
PagniationTokenPublic = "";
PaginationTokenPublic = "";
Debug.Log("no next page to fetch.");
}
else
{
PagniationTokenPublic = modelsResponse.nextPaginationToken;
PaginationTokenPublic = modelsResponse.nextPaginationToken;
Debug.Log("fetching next page data...");
continue;
}
Expand All @@ -318,6 +488,7 @@ public class ModelData
public string id { get; set; }
public string name { get; set; }
public string type { get; set; }
public string[] tags { get; set; }
public ClassData classData { get; set; }
public string privacy { get; set; }
public string status { get; set; }
Expand Down
Loading
Loading