From 2b03c39dfe4800918c881448b16dfce2d3d337a1 Mon Sep 17 00:00:00 2001 From: Araozu Date: Wed, 29 May 2024 19:10:05 -0500 Subject: [PATCH] Add minial animations to Ui --- albumCover.go | 4 ++ frontend/src/routes/Home.tsx | 62 +++++++++++++++++++++++-------- frontend/wailsjs/go/main/App.d.ts | 2 + frontend/wailsjs/go/main/App.js | 4 ++ session.go | 12 +++++- 5 files changed, 66 insertions(+), 18 deletions(-) diff --git a/albumCover.go b/albumCover.go index 0d5dcc8..5626c45 100644 --- a/albumCover.go +++ b/albumCover.go @@ -107,6 +107,10 @@ func loadAlbumCover(albumId string) { // Tries to load the album cover func (a *App) GetAlbumCover(albumId string) ([]byte, error) { + if albumId == "" { + return nil, nil + } + cacheMutex.Lock() coverInfo, ok := albumCoverCacheInfo[albumId] cacheMutex.Unlock() diff --git a/frontend/src/routes/Home.tsx b/frontend/src/routes/Home.tsx index b2e6772..4b5f1ac 100644 --- a/frontend/src/routes/Home.tsx +++ b/frontend/src/routes/Home.tsx @@ -1,19 +1,33 @@ -import { For, createMemo, createResource, createSignal, onMount } from "solid-js"; -import { GetAlbumCover, GetRandomAlbums } from "../../wailsjs/go/main/App"; +import { For, Show, createEffect, createMemo, createResource, createSignal, onCleanup, onMount } from "solid-js"; +import { GetAlbumCover, GetRandomAlbums, LoadRandomAlbums } from "../../wailsjs/go/main/App"; import { main } from "../../wailsjs/go/models"; export function Home() { const [hidden, setHidden] = createSignal(true); - const [albums] = createResource(GetRandomAlbums); + const [albums, {mutate, refetch}] = createResource>(GetRandomAlbums); onMount(() => { // Fade in the UI setTimeout(() => setHidden(false), 150); }); + const reload = () => { + LoadRandomAlbums(); + mutate((new Array(20)).map(() => null)); + setTimeout(refetch, 100); + }; + return (
-

Random albums

+

+ Random albums + +

{(album) => } @@ -23,8 +37,17 @@ export function Home() { ); } -function Album(props: { album: main.Album }) { - const [coverBytes] = createResource(async() => await GetAlbumCover(props.album.id)); +function Album(props: { album: main.Album | null }) { + const [coverBytes, {mutate, refetch}] = createResource | null>(async() => GetAlbumCover(props.album?.id ?? "")); + + createEffect(() => { + if (!props.album) { + mutate(null); + return; + } + + refetch(); + }); const base64Image = createMemo(() => { if (coverBytes.state !== "ready") return ""; @@ -34,20 +57,27 @@ function Album(props: { album: main.Album }) { return `data:;base64,${bytes}`; }); + const isNull = createMemo(() => !props.album); + const opacity = createMemo(() => `${isNull() ? "opacity-0" : "opacity-100"} transition-opacity`); + return (
- + +
+ + + + -
-
- {props.album.name} +
+ {props.album?.name ?? "..."}
-
- {props.album.albumArtist} +
+ {props.album?.albumArtist ?? "..."}
diff --git a/frontend/wailsjs/go/main/App.d.ts b/frontend/wailsjs/go/main/App.d.ts index d3471c9..70c8fbf 100755 --- a/frontend/wailsjs/go/main/App.d.ts +++ b/frontend/wailsjs/go/main/App.d.ts @@ -8,4 +8,6 @@ export function GetRandomAlbums():Promise>; export function Greet(arg1:string):Promise; +export function LoadRandomAlbums():Promise; + export function Login(arg1:string,arg2:string,arg3:string):Promise; diff --git a/frontend/wailsjs/go/main/App.js b/frontend/wailsjs/go/main/App.js index e757194..bfe213e 100755 --- a/frontend/wailsjs/go/main/App.js +++ b/frontend/wailsjs/go/main/App.js @@ -14,6 +14,10 @@ export function Greet(arg1) { return window['go']['main']['App']['Greet'](arg1); } +export function LoadRandomAlbums() { + return window['go']['main']['App']['LoadRandomAlbums'](); +} + export function Login(arg1, arg2, arg3) { return window['go']['main']['App']['Login'](arg1, arg2, arg3); } diff --git a/session.go b/session.go index 3b61b73..f8454a1 100644 --- a/session.go +++ b/session.go @@ -5,6 +5,7 @@ import ( "fmt" "log" "sync" + "time" "github.com/go-resty/resty/v2" ) @@ -55,7 +56,6 @@ func (a *App) Login(server, username, password string) (bool, error) { // Set global session LoggedUser = successData // Begin to load the list of albums on the background - randomAlbumWaitGroup.Add(1) go loadAlbums(server) return true, nil @@ -68,6 +68,11 @@ func (a *App) Login(server, username, password string) (bool, error) { } } +// Triggers a reload of random albums +func (a *App) LoadRandomAlbums() { + go loadAlbums(serverUrl) +} + // Waits for the random albums to be loaded, and returns them. // This function assumes that the random albums are being loaded in the background. func (a *App) GetRandomAlbums() ([]Album, error) { @@ -80,9 +85,12 @@ func (a *App) GetRandomAlbums() ([]Album, error) { // Loads a list of random albums from the server. func loadAlbums(serverUrl string) { + randomAlbumWaitGroup.Add(1) defer randomAlbumWaitGroup.Done() + log.Print("begin loadAlbums") - client := resty.New() + + time.Sleep(5 * time.Second) var errorData AuthError response, err := client.R().