Skip to content

Commit

Permalink
add: weather module
Browse files Browse the repository at this point in the history
  • Loading branch information
panxuc committed Aug 4, 2023
1 parent 2bff975 commit 7c59c2f
Show file tree
Hide file tree
Showing 12 changed files with 312 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,6 @@ FodyWeavers.xsd

# JetBrains Rider
*.sln.iml

# API
Secrets.cs
Empty file removed android/XPrism/Hyperlink.cs
Empty file.
10 changes: 6 additions & 4 deletions android/XPrism/XPrism/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

<ScrollView>
<VerticalStackLayout Margin="24">
<Button Text="发送信息" ImageSource="material_message_white.png" ContentLayout="Left, 0" Clicked="OnSendClicked" />
<Label></Label>
<Button Text="关于 XPrism" ImageSource="material_information_white.png" ContentLayout="Left, 0" Clicked="OnAboutClicked" />
<Button Text="设置天气" ImageSource="material_weather_partly_cloudy_white.png" ContentLayout="Left, 0" Clicked="OnWeatherClicked" />
<Label></Label>
<Button Text="发送信息" ImageSource="material_message_white.png" ContentLayout="Left, 0" Clicked="OnSendClicked" />
<Label></Label>
<Button Text="关于 XPrism" ImageSource="material_information_white.png" ContentLayout="Left, 0" Clicked="OnAboutClicked" />
</VerticalStackLayout>
</ScrollView>

</ContentPage>
6 changes: 6 additions & 0 deletions android/XPrism/XPrism/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ public MainPage()
Application.Current.UserAppTheme = AppTheme.Light;
}

public async void OnWeatherClicked(object sender, System.EventArgs e)
{
// 打开天气页面
await Navigation.PushAsync(new WeatherPage());
}

public async void OnSendClicked(object sender, System.EventArgs e)
{
// 打开发送页面
Expand Down
5 changes: 4 additions & 1 deletion android/XPrism/XPrism/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace XPrism;
using CommunityToolkit.Maui;

namespace XPrism;

public static class MauiProgram
{
Expand All @@ -7,6 +9,7 @@ public static MauiApp CreateMauiApp()
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseMauiCommunityToolkit()
.ConfigureFonts(fonts =>
{
});
Expand Down
2 changes: 1 addition & 1 deletion android/XPrism/XPrism/Platforms/Android/MainApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace XPrism;

[Application]
[Application(UsesCleartextTraffic = true)]
public class MainApplication : MauiApplication
{
public MainApplication(IntPtr handle, JniHandleOwnership ownership)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 6 additions & 6 deletions android/XPrism/XPrism/SendPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ public SendPage()
InitializeComponent();
}

void OnEditorTextChanged(object sender, TextChangedEventArgs e)
public void OnEditorTextChanged(object sender, TextChangedEventArgs e)
{
string oldText = e.OldTextValue;
string newText = e.NewTextValue;
string myText = editor.Text;
//string oldText = e.OldTextValue;
//string newText = e.NewTextValue;
//string myText = editor.Text;
}

void OnEditorCompleted(object sender, EventArgs e)
public void OnEditorCompleted(object sender, EventArgs e)
{
string text = ((Editor)sender).Text;
//string text = ((Editor)sender).Text;
}
}
67 changes: 67 additions & 0 deletions android/XPrism/XPrism/WeatherPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XPrism.WeatherPage"
Title="设置天气">
<ScrollView>
<VerticalStackLayout Margin="10">
<Label Text="选择城市:" FontSize="Title" />
<!--<Picker x:Name="picker" Title="选择一个城市">
<Picker.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>北京</x:String>
<x:String>上海</x:String>
<x:String>深圳</x:String>
<x:String>广州</x:String>
<x:String>杭州</x:String>
<x:String>成都</x:String>
<x:String>南京</x:String>
<x:String>苏州</x:String>
<x:String>重庆</x:String>
<x:String>武汉</x:String>
</x:Array>
</Picker.ItemsSource>
</Picker>-->

<Entry x:Name="cityEntry" Placeholder="请输入城市名称" />

<Button Text="查询" Clicked="OnSearchClicked" />

<Label Text="查询结果:" FontSize="Title" />
<StackLayout Orientation="Horizontal">
<Label Text="城市:" />
<Label x:Name="cityLabel" />
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="天气:" />
<Label x:Name="weatherLabel" />
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="温度:" />
<Label x:Name="temperatureLabel" />
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="风向:" />
<Label x:Name="windDirectionLabel" />
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="风速:" />
<Label x:Name="windSpeedLabel" />
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="湿度:" />
<Label x:Name="humidityLabel" />
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="更新时间:" />
<Label x:Name="reportTimeLabel" />
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="数据来源:" />
<Label x:Name="sourceLabel" />
</StackLayout>


</VerticalStackLayout>
</ScrollView>
</ContentPage>
214 changes: 214 additions & 0 deletions android/XPrism/XPrism/WeatherPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
#define OPENWEATHERMAP // OPENWEATHERMAP or ACCUWEATHER

using Android.App;
using System.Text.Json;

namespace XPrism;

#if OPENWEATHERMAP
public partial class WeatherPage : ContentPage
{
public string url = "http://api.openweathermap.org/data/2.5/weather";
public string city = "";
public string apiKey = Secrets.OpenWeatherAPI; // Get your own API key from https://openweathermap.org/api
public string units = "metric";
public string lang = "zh_cn";
public string fullUrl = "";

public WeatherPage()
{
InitializeComponent();
cityLabel.Text = "N/A";
weatherLabel.Text = "N/A";
temperatureLabel.Text = "N/A";
windDirectionLabel.Text = "N/A";
windSpeedLabel.Text = "N/A";
humidityLabel.Text = "N/A";
reportTimeLabel.Text = "N/A";
sourceLabel.Text = "OpenWeatherMap";
}

public async void OnSearchClicked(object sender, System.EventArgs e)
{
city = cityEntry.Text;
if (city != "")
{
fullUrl = $"{url}?q={city}&appid={apiKey}&units={units}&lang={lang}";
HttpClient client = new HttpClient();
var response = await client.GetAsync(fullUrl);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
var weather = JsonSerializer.Deserialize<Weather>(content);
if (weather.cod != 200)
{
await DisplayAlert("错误", "城市名称错误", "确定");
return;
}
cityLabel.Text = city;
weatherLabel.Text = weather.weather[0].description;
temperatureLabel.Text = weather.main.temp.ToString() + "℃";
windDirectionLabel.Text = weather.wind.deg.ToString() + "°";
windSpeedLabel.Text = weather.wind.speed.ToString() + "m/s";
humidityLabel.Text = weather.main.humidity.ToString() + "%";
reportTimeLabel.Text = weather.dt.ToString();
}
else
{
await DisplayAlert("错误", "城市名称错误", "确定");
return;
}
}
}
}
class Weather
{
public int cod { get; set; }
public WeatherMain main { get; set; }
public WeatherWind wind { get; set; }
public WeatherWeather[] weather { get; set; }
public int dt { get; set; }
}
class WeatherMain
{
public float temp { get; set; }
public int humidity { get; set; }
}
class WeatherWind
{
public float speed { get; set; }
public float deg { get; set; }
}
class WeatherWeather
{
public string description { get; set; }
}
#endif
#if ACCUWEATHER
public partial class WeatherPage : ContentPage
{
public string url = "http://dataservice.accuweather.com/currentconditions/v1/";
public string locationKey = "";
public string apiKey = Secrets.AccuWeatherAPI;
public string language = "zh-CN";
public string details = "true";
public string fullUrl = "";

public WeatherPage()
{
InitializeComponent();
cityLabel.Text = "N/A";
weatherLabel.Text = "N/A";
temperatureLabel.Text = "N/A";
windDirectionLabel.Text = "N/A";
windSpeedLabel.Text = "N/A";
humidityLabel.Text = "N/A";
reportTimeLabel.Text = "N/A";
}

public async void OnSearchClicked(object sender, System.EventArgs e)
{
switch (picker.SelectedIndex)
{
case 0:
locationKey = "101924";
break;
case 1:
locationKey = "106577";
break;
case 2:
locationKey = "58194";
break;
case 3:
locationKey = "102255";
break;
case 4:
locationKey = "106832";
break;
case 5:
locationKey = "106774";
break;
case 6:
locationKey = "105570";
break;
case 7:
locationKey = "105571";
break;
case 8:
locationKey = "102144";
break;
case 9:
locationKey = "103847";
break;
default:
locationKey = "";
fullUrl = "";
break;
}
if (locationKey != "")
{
fullUrl = $"{url}{locationKey}?apikey={apiKey}&language={language}&details={details}";
HttpClient client = new HttpClient();
var response = await client.GetAsync(fullUrl);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
if (content == "[]")
{
cityLabel.Text = "N/A";
weatherLabel.Text = "N/A";
temperatureLabel.Text = "N/A";
windDirectionLabel.Text = "N/A";
windSpeedLabel.Text = "N/A";
humidityLabel.Text = "N/A";
reportTimeLabel.Text = "N/A";
return;
}
else
{
var weather = JsonSerializer.Deserialize<Weather>(content);
cityLabel.Text = picker.Items[picker.SelectedIndex];
weatherLabel.Text = weather.WeatherText;
temperatureLabel.Text = weather.Temperature.Metric.Value.ToString() + "℃";
windDirectionLabel.Text = weather.Wind.Direction.Localized;
windSpeedLabel.Text = weather.Wind.Speed.Metric.Value.ToString() + "km/h";
humidityLabel.Text = weather.RelativeHumidity.ToString() + "%";
reportTimeLabel.Text = weather.LocalObservationDateTime.ToString();
}
}
}
}
}

public class Weather
{
public DateTime LocalObservationDateTime { get; set; }
public string WeatherText { get; set; }
public Temperature_ Temperature { get; set; }
public Wind_ Wind { get; set; }
public int RelativeHumidity { get; set; }
public class Metric_
{
public double Value { get; set; }
public string Unit { get; set; }
}
public class Temperature_
{
public Metric_ Metric { get; set; }
}
public class Direction_
{
public string Localized { get; set; }
}
public class Speed_
{
public Metric_ Metric { get; set; }
}
public class Wind_
{
public Direction_ Direction { get; set; }
public Speed_ Speed { get; set; }
}

}
#endif
3 changes: 3 additions & 0 deletions android/XPrism/XPrism/XPrism.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
<MauiXaml Update="SendPage.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
<MauiXaml Update="WeatherPage.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
</ItemGroup>

</Project>

0 comments on commit 7c59c2f

Please sign in to comment.