Skip to content

Commit

Permalink
Added event end time
Browse files Browse the repository at this point in the history
  • Loading branch information
NSGolova committed Dec 4, 2024
1 parent f1dad96 commit 4950e4d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
63 changes: 59 additions & 4 deletions Source/8_UI/MainMenu/OtherNews/EventNewsPanel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using BeatLeader.API.Methods;
using BeatLeader.Models;
Expand All @@ -22,8 +23,13 @@ protected virtual void Awake() {
protected override void OnInitialize() {
base.OnInitialize();
header.Setup("BeatLeader Events");
PlatformEventsRequest.SendRequest();
PlatformEventsRequest.AddStateListener(OnRequestStateChanged);
}

protected override void OnRootStateChange(bool active) {
if (active) {
PlatformEventsRequest.SendRequest();
PlatformEventsRequest.AddStateListener(OnRequestStateChanged);
}
}

protected override void OnDispose() {
Expand Down Expand Up @@ -86,16 +92,60 @@ private void PresentList(IEnumerable<PlatformEvent> items) {
MarkScrollbarDirty();
}

private void SetupFeaturePreview(FeaturedPreviewPanel panel, PlatformEvent item) {
private string FormatRemainingTime(TimeSpan span) {
if (span.TotalDays >= 1) {
return $"Ongoing! {(int)span.TotalDays} day{((int)span.TotalDays > 1 ? "s" : "")} left";
}
if (span.TotalHours >= 1) {
return $"Ongoing! {(int)span.TotalHours} hour{((int)span.TotalHours > 1 ? "s" : "")} left";
}
if (span.TotalMinutes >= 1) {
return $"Ongoing! {(int)span.TotalMinutes} minute{((int)span.TotalMinutes > 1 ? "s" : "")} left";
}
return $"Ongoing! {(int)span.TotalSeconds} second{((int)span.TotalSeconds > 1 ? "s" : "")} left";
}

private string ScheduleBottomText(FeaturedPreviewPanel panel, PlatformEvent item) {
string bottomText;
var timeSpan = FormatUtils.GetRelativeTime(item.endDate);
if (timeSpan < TimeSpan.Zero) {
bottomText = "<color=#88FF88>Ongoing!";
timeSpan = FormatUtils.GetRelativeTime(1733336600);
}
var remainingTime = timeSpan;
if (timeSpan < TimeSpan.Zero) {
bottomText = $"<color=#88FF88>{FormatRemainingTime(-timeSpan)}";

// Calculate time until next significant change
remainingTime = -timeSpan;

} else {
var date = FormatUtils.GetRelativeTimeString(timeSpan, false);
bottomText = $"<color=#884444>Ended {date}";
}

TimeSpan updateDelay;
if (remainingTime.TotalDays >= 1) {
updateDelay = TimeSpan.FromDays(Math.Ceiling(remainingTime.TotalDays)) - remainingTime;
}
else if (remainingTime.TotalHours >= 1) {
updateDelay = TimeSpan.FromHours(Math.Ceiling(remainingTime.TotalHours)) - remainingTime;
}
else if (remainingTime.TotalMinutes >= 1) {
updateDelay = TimeSpan.FromMinutes(Math.Ceiling(remainingTime.TotalMinutes)) - remainingTime;
}
else {
updateDelay = TimeSpan.FromSeconds(1);
}

// Schedule update
StartCoroutine(UpdateAfterDelay(updateDelay, panel, item));

return bottomText;
}

private void SetupFeaturePreview(FeaturedPreviewPanel panel, PlatformEvent item) {

string bottomText = ScheduleBottomText(panel, item);
panel.Setup(item.image, item.name, bottomText, "Details", ButtonAction);
return;

Expand All @@ -104,6 +154,11 @@ void ButtonAction() {
}
}

private IEnumerator UpdateAfterDelay(TimeSpan delay, FeaturedPreviewPanel panel, PlatformEvent item) {
yield return new WaitForSeconds((float)delay.TotalSeconds);
string bottomText = ScheduleBottomText(panel, item);
panel.UpdateBottomText(bottomText);
}

private void DisposeList() {
foreach (var post in _list) {
Expand Down
4 changes: 4 additions & 0 deletions Source/8_UI/MainMenu/OtherNews/FeaturedPreviewPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public async void Setup(string previewUrl, string topText, string bottomText, st
}
}

public void UpdateBottomText(string bottomText) {
_bottomText.text = bottomText;
}

protected override void OnInitialize() {
_background._skew = 0.18f;

Expand Down

0 comments on commit 4950e4d

Please sign in to comment.