Skip to content

Commit e619106

Browse files
committed
load more functionalities
1 parent c57aa35 commit e619106

File tree

1 file changed

+91
-5
lines changed

1 file changed

+91
-5
lines changed

Pages/PlayMedia.razor

Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,33 @@
245245

246246
<!-- Load More Button -->
247247
<div class="text-center">
248-
<button class="px-6 py-3 bg-soft-blue hover:bg-cartoon-yellow text-amber-800 rounded-full font-bold font-comic transition-colors cartoon-rounded">
249-
Load more videos
250-
</button>
248+
@if (hasMoreVideos)
249+
{
250+
<button @onclick="LoadMoreVideos"
251+
disabled="@isLoadingMore"
252+
class="px-6 py-3 bg-soft-blue hover:bg-cartoon-yellow text-amber-800 rounded-full font-bold font-comic transition-colors cartoon-rounded disabled:opacity-50 disabled:cursor-not-allowed">
253+
@if (isLoadingMore)
254+
{
255+
<div class="flex items-center space-x-2">
256+
<svg class="animate-spin h-4 w-4" fill="none" viewBox="0 0 24 24">
257+
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
258+
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
259+
</svg>
260+
<span>Loading...</span>
261+
</div>
262+
}
263+
else
264+
{
265+
<span>Load more videos</span>
266+
}
267+
</button>
268+
}
269+
else
270+
{
271+
<div class="px-6 py-3 text-amber-600 font-comic text-sm">
272+
No more videos to load
273+
</div>
274+
}
251275
</div>
252276
</div>
253277
</div>
@@ -262,7 +286,11 @@
262286
[Parameter] public string VideId { get; set; } = string.Empty;
263287

264288
private Video? currentVideo;
265-
private IEnumerable<Video> randomVideos = Enumerable.Empty<Video>();
289+
private List<Video> randomVideos = new();
290+
private int currentVideoCount = 8;
291+
private int videosPerLoad = 8;
292+
private bool isLoadingMore = false;
293+
private bool hasMoreVideos = true;
266294

267295
private void GoTOPage(Video video)
268296
{
@@ -301,7 +329,14 @@
301329
{
302330
await VideoService.InitializeAsync();
303331
currentVideo = await VideoService.GetVideoByIdAsync(VideId);
304-
randomVideos = await VideoService.GetRandomVideosAsync(8);
332+
333+
// Reset video list and load initial videos
334+
randomVideos.Clear();
335+
currentVideoCount = videosPerLoad;
336+
hasMoreVideos = true;
337+
338+
var initialVideos = await VideoService.GetRandomVideosAsync(videosPerLoad);
339+
randomVideos.AddRange(initialVideos);
305340

306341
// Reset comments state for new video
307342
show = false;
@@ -313,6 +348,57 @@
313348
}
314349
}
315350

351+
private async Task LoadMoreVideos()
352+
{
353+
if (isLoadingMore || !hasMoreVideos) return;
354+
355+
isLoadingMore = true;
356+
StateHasChanged();
357+
358+
try
359+
{
360+
// Get all videos to check if we have more available
361+
var allVideos = await VideoService.GetAllVideosAsync();
362+
var allVideosList = allVideos.ToList();
363+
364+
// Check if we have more videos to load
365+
if (currentVideoCount >= allVideosList.Count)
366+
{
367+
hasMoreVideos = false;
368+
return;
369+
}
370+
371+
// Load more videos (excluding current video and already loaded videos)
372+
var remainingVideos = allVideosList
373+
.Where(v => v.Id != currentVideo?.Id && !randomVideos.Any(rv => rv.Id == v.Id))
374+
.ToList();
375+
376+
var videosToAdd = remainingVideos
377+
.OrderBy(x => Guid.NewGuid()) // Randomize
378+
.Take(videosPerLoad)
379+
.ToList();
380+
381+
randomVideos.AddRange(videosToAdd);
382+
currentVideoCount += videosToAdd.Count;
383+
384+
// Check if we've loaded all available videos
385+
if (currentVideoCount >= allVideosList.Count || videosToAdd.Count < videosPerLoad)
386+
{
387+
hasMoreVideos = false;
388+
}
389+
}
390+
catch (Exception ex)
391+
{
392+
// Handle error - in a real app you might want to show an error message
393+
Console.WriteLine($"Error loading more videos: {ex.Message}");
394+
}
395+
finally
396+
{
397+
isLoadingMore = false;
398+
StateHasChanged();
399+
}
400+
}
401+
316402
public void Dispose()
317403
{
318404
StateService.OnStateChanged -= StateHasChanged;

0 commit comments

Comments
 (0)