weather app

main
syneffort 2 years ago
parent 9fed14988e
commit d64950a34d
  1. 24
      MySolution/WeatherApp/App.config
  2. 24
      MySolution/WeatherApp/MainWindow.xaml
  3. 134
      MySolution/WeatherApp/MainWindow.xaml.cs
  4. 74
      MySolution/WeatherApp/Properties/Settings.Designer.cs
  5. 18
      MySolution/WeatherApp/Properties/Settings.settings
  6. 17
      MySolution/WeatherApp/WeatherApp.csproj
  7. 27
      MySolution/WeatherApp/WeatherResponse.cs

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="WeatherApp.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<WeatherApp.Properties.Settings>
<setting name="WindowLeft" serializeAs="String">
<value>0</value>
</setting>
<setting name="WindowTop" serializeAs="String">
<value>0</value>
</setting>
<setting name="FontColor" serializeAs="String">
<value />
</setting>
<setting name="Location" serializeAs="String">
<value />
</setting>
</WeatherApp.Properties.Settings>
</userSettings>
</configuration>

@ -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">
<Window.ContextMenu>
<ContextMenu>
<MenuItem Header="위치">
<TextBox x:Name="tbxLocation" Width="150" KeyDown="tbxLocation_KeyDown"/>
</MenuItem>
<MenuItem Header="색상 반전" Click="ColorReverse_MenuItem_Click"/>
<MenuItem Header="종료" Click="Exit_MenuItem_Click">
</MenuItem>
</ContextMenu>
</Window.ContextMenu>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"/>
@ -15,10 +27,10 @@
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="tbLocation" Grid.Column="1" Text="Location" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Image x:Name="imgWeather" Grid.Column="0" Source="https://openweathermap.org/img/wn/10d@2x.png"/>
<TextBlock x:Name="tbTime" Grid.Column="2" Text="yyyy-MM-dd hh:mm:ss" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<TextBlock x:Name="tbLocation" Grid.Column="1" Text="Location" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="Bold"/>
<TextBlock x:Name="tbTime" Grid.Column="2" Text="yyyy-MM-dd hh:mm:ss" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="Bold"/>
</Grid>
</Window>

@ -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
/// </summary>
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<WeatherResponse>(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();
}
}
}

@ -0,0 +1,74 @@
//------------------------------------------------------------------------------
// <auto-generated>
// 이 코드는 도구를 사용하여 생성되었습니다.
// 런타임 버전:4.0.30319.42000
//
// 파일 내용을 변경하면 잘못된 동작이 발생할 수 있으며, 코드를 다시 생성하면
// 이러한 변경 내용이 손실됩니다.
// </auto-generated>
//------------------------------------------------------------------------------
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;
}
}
}
}

@ -0,0 +1,18 @@
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="WeatherApp.Properties" GeneratedClassName="Settings">
<Profiles />
<Settings>
<Setting Name="WindowLeft" Type="System.Double" Scope="User">
<Value Profile="(Default)">0</Value>
</Setting>
<Setting Name="WindowTop" Type="System.Double" Scope="User">
<Value Profile="(Default)">0</Value>
</Setting>
<Setting Name="FontColor" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="Location" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings>
</SettingsFile>

@ -8,7 +8,22 @@
</PropertyGroup>
<ItemGroup>
<Folder Include="Resources\" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<Compile Update="Properties\Settings.Designer.cs">
<DesignTimeSharedInput>True</DesignTimeSharedInput>
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<None Update="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
</ItemGroup>
</Project>

@ -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> 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; }
}
}
Loading…
Cancel
Save