From d64950a34d1277df6f4c3c65e6f0a35403b8fa3c Mon Sep 17 00:00:00 2001 From: syneffort Date: Fri, 3 Nov 2023 17:57:25 +0900 Subject: [PATCH] weather app --- MySolution/WeatherApp/App.config | 24 ++++ MySolution/WeatherApp/MainWindow.xaml | 24 +++- MySolution/WeatherApp/MainWindow.xaml.cs | 134 +++++++++++++++++- .../Properties/Settings.Designer.cs | 74 ++++++++++ .../WeatherApp/Properties/Settings.settings | 18 +++ MySolution/WeatherApp/WeatherApp.csproj | 17 ++- MySolution/WeatherApp/WeatherResponse.cs | 27 ++++ 7 files changed, 310 insertions(+), 8 deletions(-) create mode 100644 MySolution/WeatherApp/App.config create mode 100644 MySolution/WeatherApp/Properties/Settings.Designer.cs create mode 100644 MySolution/WeatherApp/Properties/Settings.settings create mode 100644 MySolution/WeatherApp/WeatherResponse.cs diff --git a/MySolution/WeatherApp/App.config b/MySolution/WeatherApp/App.config new file mode 100644 index 0000000..957ed24 --- /dev/null +++ b/MySolution/WeatherApp/App.config @@ -0,0 +1,24 @@ + + + + +
+ + + + + + 0 + + + 0 + + + + + + + + + + \ No newline at end of file diff --git a/MySolution/WeatherApp/MainWindow.xaml b/MySolution/WeatherApp/MainWindow.xaml index 392ea8e..3626429 100644 --- a/MySolution/WeatherApp/MainWindow.xaml +++ b/MySolution/WeatherApp/MainWindow.xaml @@ -5,9 +5,21 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WeatherApp" mc:Ignorable="d" - Title="MainWindow" Height="40" Width="300" - WindowStyle="None" Topmost="True" - MouseLeftButtonDown="Window_MouseLeftButtonDown"> + Title="MainWindow" Height="40" Width="300" ResizeMode="NoResize" AllowsTransparency="True" Background="Transparent" + WindowStyle="None" Topmost="True" ShowInTaskbar="False" + MouseLeftButtonDown="Window_MouseLeftButtonDown" + Closing="Window_Closing" + Loaded="Window_Loaded"> + + + + + + + + + + @@ -15,10 +27,10 @@ - - - + + + diff --git a/MySolution/WeatherApp/MainWindow.xaml.cs b/MySolution/WeatherApp/MainWindow.xaml.cs index 40ebca5..f515bf1 100644 --- a/MySolution/WeatherApp/MainWindow.xaml.cs +++ b/MySolution/WeatherApp/MainWindow.xaml.cs @@ -1,8 +1,14 @@ -using System; +using Newtonsoft.Json; +using System; using System.Collections.Generic; +using System.Configuration; +using System.Diagnostics; using System.Linq; +using System.Net; using System.Text; +using System.Text.Json.Serialization; using System.Threading.Tasks; +using System.Timers; using System.Windows; using System.Windows.Controls; using System.Windows.Data; @@ -12,6 +18,7 @@ using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; +using WeatherApp.Properties; namespace WeatherApp { @@ -20,9 +27,81 @@ namespace WeatherApp /// public partial class MainWindow : Window { + private Timer _refreshTimer; + private Timer _timeTimer; + private string _location = "koyo"; + public MainWindow() { InitializeComponent(); + InitInstance(); + } + + private void InitInstance() + { + if (Settings.Default["Location"] != null && !string.IsNullOrEmpty(Settings.Default["Location"].ToString())) + _location = Settings.Default["Location"].ToString(); + + tbxLocation.Text = _location; + + GetWeatherInfo(); + + _refreshTimer = new Timer(); + _refreshTimer.Interval = 60 * 1000; + _refreshTimer.Elapsed += _refreshTimer_Elapsed; + _refreshTimer.Start(); + + _timeTimer = new Timer(); + _timeTimer.Interval = 1000; + _timeTimer.Elapsed += _timeTimer_Elapsed; + _timeTimer.Start(); + } + + private void _timeTimer_Elapsed(object? sender, ElapsedEventArgs e) + { + Application.Current.Dispatcher.Invoke(() => + { + tbTime.Text = DateTime.Now.ToString("yyyy-MM-dd tt hh:mm:ss"); + }); + } + + private void _refreshTimer_Elapsed(object? sender, ElapsedEventArgs e) + { + GetWeatherInfo(); + } + + private void SetFontColor(Brush brush) + { + tbLocation.Foreground = brush; + tbTime.Foreground = brush; + } + + private void GetWeatherInfo() + { + using (WebClient client = new WebClient()) + { + try + { + string res = client.DownloadString(@$"https://api.openweathermap.org/data/2.5/weather?q={_location}&APPID="); + WeatherResponse weatherResponse = JsonConvert.DeserializeObject(res); + + Application.Current.Dispatcher.Invoke(() => + { + BitmapImage image = new BitmapImage(); + image.BeginInit(); + image.UriSource = new Uri(@$"https://openweathermap.org/img/wn/{weatherResponse.Weather[0].Icon}.png"); + image.EndInit(); + imgWeather.Source = image; + + tbLocation.Text = _location.ToUpper(); + tbTime.Text = DateTime.Now.ToString("yyyy-MM-dd tt hh:mm:ss"); + }); + } + catch (Exception ex) + { + Debug.WriteLine(ex.Message); + } + } } private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) @@ -30,5 +109,58 @@ namespace WeatherApp if (e.LeftButton == MouseButtonState.Pressed) DragMove(); } + + private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) + { + Settings.Default["WindowLeft"] = this.Left; + Settings.Default["WindowTop"] = this.Top; + Settings.Default.Save(); + } + + private void Window_Loaded(object sender, RoutedEventArgs e) + { + this.Left = (double)Settings.Default["WindowLeft"]; + this.Top = (double)Settings.Default["WindowTop"]; + + if (Settings.Default["FontColor"] == null || Settings.Default["FontColor"].ToString() == "Black") + SetFontColor(Brushes.Black); + else + SetFontColor(Brushes.White); + } + + private void Exit_MenuItem_Click(object sender, RoutedEventArgs e) + { + Application.Current.Shutdown(); + } + + private void ColorReverse_MenuItem_Click(object sender, RoutedEventArgs e) + { + if (tbLocation.Foreground == Brushes.Black) + { + SetFontColor(Brushes.White); + Settings.Default["FontColor"] = "White"; + } + else + { + SetFontColor(Brushes.Black); + Settings.Default["FontColor"] = "Black"; + } + + Settings.Default.Save(); + } + + private void tbxLocation_KeyDown(object sender, KeyEventArgs e) + { + if (e.Key != Key.Enter) + return; + + if (tbxLocation == null || string.IsNullOrEmpty(tbxLocation.Text)) + return; + + _location = tbxLocation.Text; + Settings.Default["Location"] = tbxLocation.Text; + + GetWeatherInfo(); + } } } diff --git a/MySolution/WeatherApp/Properties/Settings.Designer.cs b/MySolution/WeatherApp/Properties/Settings.Designer.cs new file mode 100644 index 0000000..11e3742 --- /dev/null +++ b/MySolution/WeatherApp/Properties/Settings.Designer.cs @@ -0,0 +1,74 @@ +//------------------------------------------------------------------------------ +// +// 이 코드는 도구를 사용하여 생성되었습니다. +// 런타임 버전:4.0.30319.42000 +// +// 파일 내용을 변경하면 잘못된 동작이 발생할 수 있으며, 코드를 다시 생성하면 +// 이러한 변경 내용이 손실됩니다. +// +//------------------------------------------------------------------------------ + +namespace WeatherApp.Properties { + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.7.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default { + get { + return defaultInstance; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("0")] + public double WindowLeft { + get { + return ((double)(this["WindowLeft"])); + } + set { + this["WindowLeft"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("0")] + public double WindowTop { + get { + return ((double)(this["WindowTop"])); + } + set { + this["WindowTop"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("")] + public string FontColor { + get { + return ((string)(this["FontColor"])); + } + set { + this["FontColor"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("")] + public string Location { + get { + return ((string)(this["Location"])); + } + set { + this["Location"] = value; + } + } + } +} diff --git a/MySolution/WeatherApp/Properties/Settings.settings b/MySolution/WeatherApp/Properties/Settings.settings new file mode 100644 index 0000000..4565a65 --- /dev/null +++ b/MySolution/WeatherApp/Properties/Settings.settings @@ -0,0 +1,18 @@ + + + + + + 0 + + + 0 + + + + + + + + + \ No newline at end of file diff --git a/MySolution/WeatherApp/WeatherApp.csproj b/MySolution/WeatherApp/WeatherApp.csproj index b0ae643..af51f71 100644 --- a/MySolution/WeatherApp/WeatherApp.csproj +++ b/MySolution/WeatherApp/WeatherApp.csproj @@ -8,7 +8,22 @@ - + + + + + + True + True + Settings.settings + + + + + + SettingsSingleFileGenerator + Settings.Designer.cs + diff --git a/MySolution/WeatherApp/WeatherResponse.cs b/MySolution/WeatherApp/WeatherResponse.cs new file mode 100644 index 0000000..8179008 --- /dev/null +++ b/MySolution/WeatherApp/WeatherResponse.cs @@ -0,0 +1,27 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WeatherApp +{ + class WeatherResponse + { + [JsonProperty("weather")] + public List Weather { get; set; } + } + + class Weather + { + [JsonProperty("id")] + public int Id { get; set; } + [JsonProperty("main")] + public string Main { get ; set; } + [JsonProperty("description")] + public string Description { get ; set; } + [JsonProperty("icon")] + public string Icon { get; set; } + } +}