-
Notifications
You must be signed in to change notification settings - Fork 5
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,12 +12,15 @@ public class Models : EditorWindow | |
|
||
#region Public Fields | ||
|
||
public static List<ModelData> modelsQuickStart = new(); | ||
public static List<ModelData> modelsPrivate = new(); | ||
public static List<ModelData> modelsPublic = new(); | ||
|
||
public static List<TexturePair> texturesQuickStart = new(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"; | ||
|
||
|
@@ -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); | ||
|
@@ -129,6 +182,7 @@ private static async void PopulatePublicModels() | |
modelsPublic.Clear(); | ||
await FetchAllPublicModels(); | ||
FetchAllPublicTextures(); | ||
|
||
modelsUI.RedrawPage(0); | ||
} | ||
|
||
|
@@ -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) | ||
|
@@ -174,6 +281,9 @@ private static void FetchAllPrivateTextures() | |
} | ||
} | ||
|
||
/// <summary> | ||
/// Processing to get all public textures. | ||
/// </summary> | ||
private static void FetchAllPublicTextures() | ||
{ | ||
|
||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is there a while(true) here ? seems weird / useless There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So without this while, is fetching only one page of model.
To stop the process. I'll check this There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -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; | ||
} | ||
|
@@ -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) | ||
|
@@ -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); | ||
|
@@ -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; | ||
} | ||
|
@@ -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; } | ||
|
There was a problem hiding this comment.
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