Skip to content

Commit

Permalink
Cool animations when no music playing.
Browse files Browse the repository at this point in the history
  • Loading branch information
meetox80 committed Sep 25, 2023
1 parent 8815429 commit 5e991a7
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 53 deletions.
2 changes: 2 additions & 0 deletions zstio-tv/LocalMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ internal class LocalMemory
public static string ReplacementsAPIResponse = "";
public static string SpotifyToken = "";
public static string SpotifyRefreshToken;

public static bool SongPlaying = false, SongPlayingBackup = true;
}
}
45 changes: 23 additions & 22 deletions zstio-tv/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,31 @@
</Grid>
</StackPanel>
</StackPanel>
<Image x:Name="handler_bar_zstiofm_image" Margin="0,0,0,-100" Width="298" Height="300" VerticalAlignment="Bottom"/>
<Rectangle Height="201" Width="298" VerticalAlignment="Bottom">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#BF0A0303" Offset="1"/>
<GradientStop Color="#FF0A0303" Offset="0"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Grid x:Name="handler_bar_zstiofm" Height="100" VerticalAlignment="Bottom">
<StackPanel Orientation="Horizontal">
<Grid Width="180" Margin="20,20,0,0">
<StackPanel>
<Label x:Name="handler_bar_zstiofm_title" FontFamily="/Font/InterBold/#Inter" Foreground="White" FontSize="20" Content="No data avalible."/>
<Label x:Name="handler_bar_zstiom_authors" FontFamily="/Font/InterBold/#Inter" Foreground="White" FontSize="15" Content="null" Opacity="0.75" Margin="0,-7.5,0,0"/>
</StackPanel>
</Grid>
<Grid Width="96">
<Image Source="/Resources/zstiofm.png" Height="48" Width="48"/>
</Grid>
</StackPanel>
<Grid x:Name="zstiofm_movementhandler" Margin="0,0,0,0">
<Image x:Name="handler_bar_zstiofm_image" Margin="0,0,0,-100" Width="298" Height="300" VerticalAlignment="Bottom"/>
<Rectangle Height="201" Width="298" VerticalAlignment="Bottom">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#BF0A0303" Offset="1"/>
<GradientStop Color="#FF0A0303" Offset="0"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
<Grid x:Name="handler_bar_zstiofm" Height="100" VerticalAlignment="Bottom">
<StackPanel Orientation="Horizontal">
<Grid Width="180" Margin="20,20,0,0">
<StackPanel>
<Label x:Name="handler_bar_zstiofm_title" FontFamily="/Font/InterBold/#Inter" Foreground="White" FontSize="20" Content="No data avalible."/>
<Label x:Name="handler_bar_zstiom_authors" FontFamily="/Font/InterBold/#Inter" Foreground="White" FontSize="15" Content="null" Opacity="0.75" Margin="0,-7.5,0,0"/>
</StackPanel>
</Grid>
<Grid Width="96">
<Image Source="/Resources/zstiofm.png" Height="48" Width="48"/>
</Grid>
</StackPanel>
</Grid>
</Grid>
</Grid>

<Grid x:Name="handler_content" Width="1066" HorizontalAlignment="Right">
<TabControl x:Name="handler_content_tabcontrol" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="{x:Null}">
<TabItem Header="TabItem" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="{x:Null}" Visibility="Collapsed">
Expand Down
97 changes: 74 additions & 23 deletions zstio-tv/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,49 +88,100 @@ private void WindowLoaded(object sender, RoutedEventArgs e)

// Spotify integration
DispatcherTimer SpotifyCurrentPlaying = new DispatcherTimer();
SpotifyCurrentPlaying.Interval = TimeSpan.FromSeconds(10);
SpotifyCurrentPlaying.Interval = TimeSpan.FromSeconds(5);
SpotifyCurrentPlaying.Tick += SpotifyCurrentPlaying_Tick;
SpotifyCurrentPlaying.Start();
}

private void SpotifyCurrentPlaying_Tick(object sender, EventArgs e)
{
JObject SongResponse = JObject.Parse(SpotifyAuth.GetAPI("me/player/currently-playing"));
string Response = SpotifyAuth.GetAPI("me/player/currently-playing").ToString();
// MessageBox.Show(Response);

if (SongResponse["item"] != null)
if (Response.Contains("ERRinternal") || Response == null || Response == "")
{
string SongName = SongResponse["item"]["name"].ToString();

JArray artistsArray = (JArray)SongResponse["item"]["artists"];
List<string> authors = artistsArray.Select(artist => artist["name"].ToString()).ToList();
string SongAuthors = string.Join(", ", authors);

JObject album = (JObject)SongResponse["item"]["album"];
string SongImage = "";
if (album["images"] != null)
LocalMemory.SongPlaying = false;
} else
{
JObject SongResponse = JObject.Parse(Response);
if (SongResponse["item"] != null)
{
JArray images = (JArray)album["images"];
string SongName = SongResponse["item"]["name"].ToString();

if (images.Count > 0)
JArray artistsArray = (JArray)SongResponse["item"]["artists"];
List<string> authors = artistsArray.Select(artist => artist["name"].ToString()).ToList();
string SongAuthors = string.Join(", ", authors);

JObject album = (JObject)SongResponse["item"]["album"];
string SongImage = "";
if (album["images"] != null)
{
SongImage = images[0]["url"].ToString();
JArray images = (JArray)album["images"];

if (images.Count > 0)
{
SongImage = images[0]["url"].ToString();
}
}

// Set the text, authors, image of zstiofm.
handler_bar_zstiofm_title.Content = SongName;
handler_bar_zstiom_authors.Content = SongAuthors;

BitmapImage SongImageBitmap = new BitmapImage(new Uri(SongImage));
handler_bar_zstiofm_image.Source = SongImageBitmap;

LocalMemory.SongPlaying = true;
}
}

// InAnimation
if (LocalMemory.SongPlaying == true && LocalMemory.SongPlayingBackup == false)
{
LocalMemory.SongPlayingBackup = true;

var SongAnimationIn = new ThicknessAnimation
{
From = new Thickness(-600,0,0,0),
To = new Thickness(0, 0, 0, 0),
Duration = TimeSpan.FromSeconds(1),
EasingFunction = new QuadraticEase { EasingMode = EasingMode.EaseOut }
};

// Set the text, authors, image of zstiofm.
handler_bar_zstiofm_title.Content = SongName;
handler_bar_zstiom_authors.Content = SongAuthors;
var LocalStoryboard = new Storyboard();
LocalStoryboard.Children.Add(SongAnimationIn);

BitmapImage SongImageBitmap = new BitmapImage(new Uri(SongImage));
handler_bar_zstiofm_image.Source = SongImageBitmap;
} else
Storyboard.SetTarget(SongAnimationIn, zstiofm_movementhandler);
Storyboard.SetTargetProperty(SongAnimationIn, new PropertyPath(MarginProperty));

LocalStoryboard.Begin();
}

// OutAnimation
if (LocalMemory.SongPlaying == false && LocalMemory.SongPlayingBackup == true)
{
// No song playing.
LocalMemory.SongPlayingBackup = false;

var SongAnimationOut = new ThicknessAnimation
{
From = new Thickness(0, 0, 0, 0),
To = new Thickness(-600, 0, 0, 0),
Duration = TimeSpan.FromSeconds(1),
EasingFunction = new QuadraticEase { EasingMode = EasingMode.EaseOut }
};

var LocalStoryboard = new Storyboard();
LocalStoryboard.Children.Add(SongAnimationOut);

Storyboard.SetTarget(SongAnimationOut, zstiofm_movementhandler);
Storyboard.SetTargetProperty(SongAnimationOut, new PropertyPath(MarginProperty));

LocalStoryboard.Begin();
}
}

private void ReplacementsCALC_Tick(object sender, EventArgs e) => IReplacements.ConfigureReplacements();

private void ReplacementsGETAPI_Tick(object sender, EventArgs e)
{
// Contact with the api responsible for replacements/substitutions
Expand Down
23 changes: 15 additions & 8 deletions zstio-tv/Modules/SpotifyAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,21 @@ public static void AuthorizeSpotify()
var tokenData = JObject.Parse(responseText);

string accessToken = tokenData["access_token"].ToString();
string refreshToken = tokenData["refresh_token"].ToString(); // Retrieve the refresh token
string refreshToken = tokenData["refresh_token"].ToString();
LocalMemory.SpotifyToken = accessToken;
LocalMemory.SpotifyRefreshToken = refreshToken; // Store the refresh token
LocalMemory.SpotifyRefreshToken = refreshToken;
Console.WriteLine($"Successfully Received AccessToken: {accessToken}");
Console.WriteLine($"Refresh Token: {refreshToken}");

// Send the html repsonse that it is successfully authorized.
var ListenerResponse = context.Response;
ListenerResponse.ContentType = "text/html";
string ListenerResponseContent = " <head><meta charset='UTF-8'></head> <center><h1>Successfully authorized with spotify!</h1><p style='opacity: 0.5; margin-top: -10px;'>Made by lemonek.</p></center> <style>@import url('https://fonts.googleapis.com/css2?family=Inter:wght@700&display=swap'); body{color:white;background-color:#101010;font-family: 'Inter', sans-serif;}</style>";
byte[] ListenerResponseBytes = Encoding.UTF8.GetBytes(ListenerResponseContent);

ListenerResponse.ContentLength64 = ListenerResponseBytes.Length;
ListenerResponse.OutputStream.Write(ListenerResponseBytes, 0, ListenerResponseBytes.Length);
ListenerResponse.Close();
}
}
catch (Exception ex)
Expand All @@ -94,8 +104,6 @@ public static void AuthorizeSpotify()

public static string GetAPI(string APIPoint)
{
// For testing the refreshtoken
// RefreshToken();
try
{
using (var httpClient = new HttpClient())
Expand All @@ -122,22 +130,21 @@ public static string GetAPI(string APIPoint)
else
{
Console.WriteLine($"Error getting currently playing track: {response.StatusCode}");
return null;
return "ERRinternal";
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error getting currently playing track: {ex.Message}");
return null;
return "ERRinternal";
}
}

public static void RefreshToken()
{
try
{

using (var httpClient = new HttpClient())
{
var tokenRequest = new Dictionary<string, string>
Expand All @@ -149,7 +156,7 @@ public static void RefreshToken()
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes($"{Config.SpotifyID}:{Config.SpotifyAuth }")));

var content = new FormUrlEncodedContent(tokenRequest);
var response = httpClient.PostAsync("https://accounts.spotify.com/api/token", content).Result; // TokenURL
var response = httpClient.PostAsync("https://accounts.spotify.com/api/token", content).Result;

if (response.IsSuccessStatusCode)
{
Expand Down

0 comments on commit 5e991a7

Please sign in to comment.