Changed posts to be object orientated instead

This commit is contained in:
BOT Alex 2023-05-16 15:35:02 +02:00
parent c5ef374135
commit f67676a160
1 changed files with 51 additions and 15 deletions

View File

@ -18,7 +18,7 @@
<MudOverlay @bind-Visible="isOverlayVisible" Style="backdrop-filter:blur(5px)" DarkBackground="true" AutoClose="true"> <MudOverlay @bind-Visible="isOverlayVisible" Style="backdrop-filter:blur(5px)" DarkBackground="true" AutoClose="true">
<MudPaper Elevation="3" Class="rounded-xl"> <MudPaper Elevation="3" Class="rounded-xl">
<MudImage Class="rounded-xl" Elevation="4" Src="@GetImage(selectedImage)" Width="750" Height="750"/> <MudImage Class="rounded-xl" Elevation="4" Src="@selectedPost.ImagePath" Width="750" Height="750"/>
<MudContainer Class="pa-5 d-flex align-center justify-content-start"> <MudContainer Class="pa-5 d-flex align-center justify-content-start">
<MudText> <MudText>
Damn Damn
@ -34,15 +34,15 @@
<MudGrid Spacing="6" Justify="Justify.Center"> <MudGrid Spacing="6" Justify="Justify.Center">
@for (int i = 0; i < 200; i++) @for (int i = 0; i < 200; i++)
{ {
int postId = (LoadedImages.Count > i) ? LoadedImages[i] : GetRandomImageNumber(); Post post = (LoadedImages.Count > i) ? posts[i] : GetRandomPost();
<MudItem @onclick="() => ClickPost(postId)"> <MudItem @onclick="() => ClickPost(post)">
<MudPaper Class="md-8" Style="position:relative"> <MudPaper Class="md-8" Style="position:relative">
<MudContainer Style="position:absolute;" Class="mt-4"> <MudContainer Style="position:absolute;" Class="mt-4">
<MudText Style="background-color:rgba(0, 0, 0, 0.55); backdrop-filter:blur(2px)" Class="pl-2 rounded-t-lg" Color="Color.Secondary" Typo="Typo.h6">@("@" + RandomUsernames[postId])</MudText> <MudText Style="background-color:rgba(0, 0, 0, 0.55); backdrop-filter:blur(2px)" Class="pl-2 rounded-t-lg" Color="Color.Secondary" Typo="Typo.h6">@("@" + post.Publisher)</MudText>
</MudContainer> </MudContainer>
<MudContainer> <MudContainer>
<MudImage Class="rounded-lg my-4" Src="@GetImage(postId)" Height="250" Width="250"></MudImage> <MudImage Class="rounded-lg my-4" Src="@post.ImagePath" Height="250" Width="250"></MudImage>
</MudContainer> </MudContainer>
</MudPaper> </MudPaper>
</MudItem> </MudItem>
@ -61,19 +61,25 @@
} }
List<int> LoadedImages = new List<int>(); List<int> LoadedImages = new List<int>();
List<Post> posts = new List<Post>();
private Random random = new Random(); private Random random = new Random();
const int numImages = 211; const int numImages = 211;
int GetRandomImageNumber() Post GetRandomPost()
{ {
int selectedImage; int selectedPostNum;
reselect: reselect:
selectedImage = random.Next(0, numImages); selectedPostNum = random.Next(1, numImages);
if (LoadedImages.Contains(selectedImage)) if (LoadedImages.Contains(selectedPostNum))
goto reselect; goto reselect;
LoadedImages.Add(selectedImage); LoadedImages.Add(selectedPostNum);
return selectedImage;
var selectedPost = new Post(selectedPostNum);
Console.WriteLine("Selected: " + selectedPost.Id);
posts.Add(selectedPost);
return selectedPost;
} }
string GetImage(int imageIndex) string GetImage(int imageIndex)
@ -88,18 +94,48 @@
// TODO: Add a "save post" button to each post. // TODO: Add a "save post" button to each post.
string[] RandomUsernames = new string[1000]; static string[]? RandomUsernames;
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
RandomUsernames = (await Http.GetStringAsync("RandomUsernames.txt")).Split("\n"); RandomUsernames = (await Http.GetStringAsync("RandomUsernames.txt")).Split("\n");
} }
int selectedImage = 0; Post selectedPost = new Post(-1);
void ClickPost(int postImage) void ClickPost(Post post)
{ {
selectedImage = postImage; selectedPost = post;
isOverlayVisible = true; isOverlayVisible = true;
} }
public class Post
{
public Post(int imageIndex)
{
this.Id = imageIndex;
}
public int Id { get; set; }
public int ImageIndex { get => Id; }
public string ImagePath { get => $@"\images\AnimeImages\AnimeImage ({ImageIndex}).jpeg"; }
private string publisher = "Unknown";
public string Publisher
{
get
{
if (publisher == "Unknown" && RandomUsernames != null)
publisher = RandomUsernames[Id];
return publisher;
}
set
{
publisher = value;
}
}
}
} }