You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
166 lines
5.1 KiB
166 lines
5.1 KiB
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;
|
|
using System.Windows.Documents;
|
|
using System.Windows.Input;
|
|
using System.Windows.Media;
|
|
using System.Windows.Media.Imaging;
|
|
using System.Windows.Navigation;
|
|
using System.Windows.Shapes;
|
|
using WeatherApp.Properties;
|
|
|
|
namespace WeatherApp
|
|
{
|
|
/// <summary>
|
|
/// Interaction logic for MainWindow.xaml
|
|
/// </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)
|
|
{
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
|