Skip to content

Commit

Permalink
Fix #16, #17, #18(WPF Conversion🚀🚀)
Browse files Browse the repository at this point in the history
  • Loading branch information
Asteriskx committed Jul 16, 2023
1 parent 80b65aa commit 64eff58
Show file tree
Hide file tree
Showing 35 changed files with 1,386 additions and 5 deletions.
1 change: 1 addition & 0 deletions Sagiri/Sagiri.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<ItemGroup>
<PackageReference Include="SpotifyAPI.Web" Version="7.0.0" />
<PackageReference Include="SpotifyAPI.Web.Auth" Version="7.0.0" />
<PackageReference Include="System.Drawing.Common" Version="4.7.0" />
<PackageReference Include="System.Reactive" Version="6.0.0" />
</ItemGroup>

Expand Down
4 changes: 1 addition & 3 deletions Sagiri/Services/Spotify/SpotifyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using Sagiri.Services.Spotify.Track;
using Sagiri.Services.Spotify.User.Interfaces;
using Sagiri.Util.Common;
using Sagiri.Exceptions;

namespace Sagiri.Services.Spotify
{
Expand Down Expand Up @@ -88,7 +87,6 @@ await Task.Run(() =>
try
{
var observer = Observable.Interval(TimeSpan.FromSeconds(1))
.Where(_ => _CurrentlyPlaying is not null && _CurrentlyPlaying.IsPlaying)
.Select(async _ => _CurrentlyPlaying = await _spotifyClient?.Player?.GetCurrentlyPlaying(new()))
.Select(track => CurrentTrackInfo.GetCurrentTrackInfo(_CurrentlyPlaying))
.Distinct()
Expand All @@ -105,7 +103,7 @@ await Task.Run(() =>
}
catch (Exception ex)
{
throw new SagiriException(ex.Message);
_Logger.WriteLog($"[SpotifyService] - Polling error... {ex.Message}", Logger.LogLevel.Error);
}
}, ct);
}
Expand Down
9 changes: 7 additions & 2 deletions Sagiri/Services/Spotify/Track/CurrentTrackInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class CurrentTrackInfo
public string ArtworkUrl { get; set; }
public string ReleaseDate { get; set; }
public string PreviewUrl { get; set; }
public bool IsPlaying { get; set; }

#endregion Properties

Expand All @@ -32,7 +33,8 @@ public CurrentTrackInfo(
string album = "-",
string artworkUrl = "",
string releaseDate = "1900/1/1",
string previewUrl = "")
string previewUrl = "",
bool isPlaying = false)
{
TrackTitle = trackTitle;
TrackNumber = trackNumber;
Expand All @@ -44,6 +46,7 @@ public CurrentTrackInfo(
ArtworkUrl = artworkUrl;
ReleaseDate = releaseDate;
PreviewUrl = previewUrl;
IsPlaying = isPlaying;
}

#endregion Constructor
Expand Down Expand Up @@ -77,6 +80,7 @@ internal static CurrentTrackInfo GetCurrentTrackInfo(CurrentlyPlaying currentlyP
var artworkUrl = track.Album.Images.Select(x => x.Url).FirstOrDefault();
var releaseDate = track.Album.ReleaseDate;
var previewUrl = track.PreviewUrl;
var isPlaying = currentlyPlaying.IsPlaying;

currentTrackInfo = new CurrentTrackInfo(
trackTitle: trackTitle,
Expand All @@ -88,7 +92,8 @@ internal static CurrentTrackInfo GetCurrentTrackInfo(CurrentlyPlaying currentlyP
album: album,
artworkUrl: artworkUrl,
releaseDate: releaseDate,
previewUrl: previewUrl
previewUrl: previewUrl,
isPlaying: isPlaying
);
}
return currentTrackInfo;
Expand Down
3 changes: 3 additions & 0 deletions SagiriApp/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
</configuration>
31 changes: 31 additions & 0 deletions SagiriApp/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<prism:PrismApplication x:Class="SagiriApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:SagiriApp"
xmlns:prism="http://prismlibrary.com/"
ShutdownMode="OnMainWindowClose" >
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- MahApps -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/Dark.Blue.xaml" />

<!-- Material Design -->
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.teal.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.teal.xaml" />
</ResourceDictionary.MergedDictionaries>

<Style BasedOn="{StaticResource MaterialDesignRaisedButton}" TargetType="{x:Type Button}">
<Setter Property="FontWeight" Value="Normal" />
</Style>

<Style BasedOn="{StaticResource MahApps.Styles.Label}" TargetType="{x:Type Label}">
<Setter Property="FontWeight" Value="Normal" />
</Style>
</ResourceDictionary>
</Application.Resources>
</prism:PrismApplication>
28 changes: 28 additions & 0 deletions SagiriApp/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Windows;

using Prism.Ioc;
using Prism.Mvvm;
using Prism.Unity;

using SagiriApp.ViewModel;
using SagiriApp.Views;

namespace SagiriApp
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : PrismApplication
{
protected override Window CreateShell() => Container.Resolve<MainWindow>();

protected override void RegisterTypes(IContainerRegistry containerRegistry) =>
containerRegistry.RegisterForNavigation<MainWindow>();

protected override void ConfigureViewModelLocator()
{
base.ConfigureViewModelLocator();
ViewModelLocationProvider.Register<MainWindow, SagiriViewModel>();
}
}
}
10 changes: 10 additions & 0 deletions SagiriApp/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Windows;

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
82 changes: 82 additions & 0 deletions SagiriApp/Behavior/PostingFormatTextBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

using Microsoft.Xaml.Behaviors;

using Sagiri.Services.Spotify.Track;
using Sagiri.Util.Common;
using SagiriApp.Interop;
using SagiriApp.Views;

namespace SagiriApp.Behavior
{
class PostingFormatTextBehavior : Behavior<TextBox>
{
private Logger _Logger { get; set; } = Logger.GetInstance;
public string PostingFormat
{
get => (string)GetValue(PostingFormatProperty);
set => SetValue(PostingFormatProperty, value);
}

// Using a DependencyProperty as the backing store for PostingFormat. This enables binding.
public static readonly DependencyProperty PostingFormatProperty =
DependencyProperty.Register(
"PostingFormat",
typeof(string),
typeof(PostingFormatTextBehavior),
new PropertyMetadata(default(string)
)
);

protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.TextChanged += _OnTextChanged;
}

protected override void OnDetaching()
{
base.OnDetaching();
this.AssociatedObject.TextChanged -= this._OnTextChanged;
}

private void _OnTextChanged(object sender, EventArgs e)
{
var activeWindow = Application.Current.Windows
.OfType<Window>()
.SingleOrDefault(x => x.IsActive);

if (activeWindow is SettingWindow sw)
{
try
{
PostingFormat = sw.PostingFormatText.Text;
sw.PreviewText.Text = _RenderPreview(sw);
sw.SettingSave.IsEnabled = true;
}
catch
{
sw.PreviewText.Text = "(!)投稿フォーマットが無効です";
sw.SettingSave.IsEnabled = false;
}
}
}

private string _RenderPreview(SettingWindow sw)
{
CurrentTrackInfo trackInfo = new()
{
Album = "メルト 10th ANNIVERSARY MIX",
Artist = "ryo (supercell) - やなぎなぎ",
TrackTitle = "メルト 10th ANNIVERSARY MIX",
TrackNumber = "1",
ReleaseDate = "2017/12/24",
};

return Helper.GenerateTrackText(sw.PostingFormatText.Text, trackInfo);
}
}
}
26 changes: 26 additions & 0 deletions SagiriApp/Behavior/SettingButtonBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Windows.Controls;

using Microsoft.Xaml.Behaviors;

using SagiriApp.Views;

namespace SagiriApp.Behavior
{
class SettingButtonBehavior : Behavior<Button>
{
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.Click += _OnClick;
}

protected override void OnDetaching()
{
base.OnDetaching();
this.AssociatedObject.Click -= this._OnClick;
}

private void _OnClick(object sender, EventArgs e) => SettingWindow.GetInstance?.Show();
}
}
26 changes: 26 additions & 0 deletions SagiriApp/Behavior/TrackInfoButtonBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Windows.Controls;

using Microsoft.Xaml.Behaviors;

using SagiriApp.Views;

namespace SagiriApp.Behavior
{
class TrackInfoButtonBehavior : Behavior<Button>
{
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.Click += _OnClick;
}

protected override void OnDetaching()
{
base.OnDetaching();
this.AssociatedObject.Click -= this._OnClick;
}

private void _OnClick(object sender, EventArgs e) => TrackInfoWindow.GetInstance?.Show();
}
}
83 changes: 83 additions & 0 deletions SagiriApp/Controls/MessageBoxEx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace SagiriApp.Controls
{
internal class MessageBoxEx : IDisposable
{
#region Property

private System.Threading.Timer _Timer { get; set; }

private string _Text = default!;
private string _Caption = default!;
private int _Interval = default!;
private bool disposedValue;

#endregion Property

#region DLL's

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

#endregion DLL's

#region Constructor

/// <summary>
/// Constructor
/// </summary>
/// <param name="text"> body message </param>
/// <param name="caption"> window title </param>
/// <param name="interval"> closing time </param>
public MessageBoxEx(string text, string caption, int interval)
{
_Text = text;
_Caption = caption;
_Interval = interval;
}

~MessageBoxEx() => Dispose(disposing: false);

#endregion Constructor

public void Show()
{
this._Timer = new System.Threading.Timer((state) =>
{
var window = FindWindow(null, _Caption);

if (window != IntPtr.Zero)
SendMessage(window, 0x0010, IntPtr.Zero, IntPtr.Zero);

this._Timer.Dispose();
}, null, _Interval, Timeout.Infinite);

MessageBox.Show(_Text, _Caption);
}

protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
_Timer = null;
}
disposedValue = true;
}
}

public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}
Binary file added SagiriApp/Icons/None.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SagiriApp/Icons/Sagiri.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SagiriApp/Icons/sagiri_128x128.ico
Binary file not shown.
Binary file added SagiriApp/Icons/sagiri_32x32.ico
Binary file not shown.
Binary file added SagiriApp/Icons/sagiri_48x48.ico
Binary file not shown.
Binary file added SagiriApp/Icons/sagiri_64x64.ico
Binary file not shown.
Binary file added SagiriApp/Icons/spotify_b.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SagiriApp/Icons/spotify_w.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 64eff58

Please sign in to comment.