Index.razor 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. @page "/"
  2. @using System.Text.Json;
  3. @using VeloeKemonoPartyApp.Data;
  4. @using VeloeKemonoPartyApp.Services;
  5. @inject KemonoHttpClient HttpClient
  6. <!--
  7. <h1>Hello, world!</h1>
  8. Welcome to your new app.
  9. <SurveyPrompt Title="How is Blazor working for you?" />
  10. -->
  11. <div class="text-center bg-blue-100">
  12. <input class="border-2 rounded border-blue-300" @bind-value="SearchText" @bind-value:event="oninput" placeholder="Search by title" />
  13. </div>
  14. @if (!CreatorList.Any())
  15. {
  16. if (Message != "")
  17. {
  18. <div class="p-2 grid grid-cols-1 sm:grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-3">
  19. <div class="alert alert-danger mt-4">
  20. <p>@Message</p>
  21. </div>
  22. </div>
  23. }
  24. else
  25. {
  26. <div class="p-2 grid grid-cols-1 sm:grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-3">
  27. <div class="alert alert-secondary mt-4">
  28. <p>Loading creator list...</p>
  29. </div>
  30. </div>
  31. }
  32. }
  33. else
  34. {
  35. <div class="p-2 grid grid-cols-1 sm:grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-3">
  36. @foreach (var creator in FilteredCreators)
  37. {
  38. <div class="alert alert-secondary mt-4">
  39. <table style="width:100%;">
  40. <tr>
  41. <td>
  42. <img src="@creator.Icon" style="width:20vw;height:20vw;">
  43. </td>
  44. <td>
  45. <div style="">
  46. <a href="/fetchdata/@creator.service/@creator.id">@creator.name</a>
  47. <p>@creator.service</p>
  48. </div>
  49. </td>
  50. <td align="right">
  51. @if(FavoriteList.Any(item=>item.id==creator.id)){
  52. <button class="btn btn-outline-danger" @onclick="@(()=>RemoveFromFavorite(creator))" type="button" value="Add Dir">
  53. <i class="bi bi-dash"></i>
  54. </button>
  55. }
  56. else
  57. {
  58. <button class="btn btn-outline-success" @onclick="@(()=>AddToFavorite(creator))" type="button" value="Add Dir">
  59. <i class="bi bi-plus"></i>
  60. </button>
  61. }
  62. <p>@creator.favorited</p>
  63. </td>
  64. </tr>
  65. </table>
  66. </div>
  67. }
  68. </div>
  69. }
  70. @code{
  71. private string SearchText { get; set; }
  72. private string Message { get; set; } = "";
  73. private List<Creator> CreatorList { get; set; }
  74. private List<Creator> FavoriteList = JsonSerializer.Deserialize<List<Creator>>(Preferences.Get("favorites", "[]"));
  75. protected override async Task OnInitializedAsync()
  76. {
  77. CreatorList = new List<Creator>();
  78. SearchText = "Rukis";
  79. int i = 0;
  80. do
  81. {
  82. try
  83. {
  84. CreatorList = await HttpClient.GetCreatorsList();
  85. return;
  86. }
  87. catch (Exception ex)
  88. {
  89. await Task.Delay(1000);
  90. }
  91. i++;
  92. }
  93. while (i < 5);
  94. Message = "Getting data failed!";
  95. }
  96. private List<Creator> FilteredCreators => CreatorList.Where(
  97. creator => creator.name.ToLower().Contains(SearchText.ToLower())).Take(100).ToList();
  98. private async Task OpenLink(string link)
  99. {
  100. await Launcher.OpenAsync(link);
  101. }
  102. private void AddToFavorite(Creator creator)
  103. {
  104. FavoriteList.Add(creator);
  105. Preferences.Set("favorites",JsonSerializer.Serialize(FavoriteList));
  106. }
  107. private void RemoveFromFavorite(Creator creator)
  108. {
  109. FavoriteList.Remove(creator);
  110. Preferences.Set("favorites", JsonSerializer.Serialize(FavoriteList));
  111. }
  112. }