Skip to content

Commit

Permalink
realized life indices query.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyjaysong committed Jun 5, 2021
1 parent 660c33b commit df42179
Show file tree
Hide file tree
Showing 19 changed files with 437 additions and 28 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 0.0.4

- update sdk.
- realized life indices query.
- 升级 SDK
- 实现 生活指数查询

## 0.0.3

- realized geo search.
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
- [x] 逐天天气查询
- [x] 逐时天气查询
- [x] 中国地区未来2小时内每5分钟降水查询
- [x] 当天生活指数查询
- [x] 三天生活指数查询
- [ ] 其他功能...

## 运行
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package com.fluttercandies.flutter_qweather;

import android.content.Context;

import com.google.gson.Gson;
import com.qweather.sdk.bean.IndicesBean;
import com.qweather.sdk.bean.base.IndicesType;
import com.qweather.sdk.bean.base.Lang;
import com.qweather.sdk.view.QWeather;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.flutter.plugin.common.MethodChannel;

public class ApiIndices {
/// 获取1天生活指数
protected static void getIndices1Day(Context context, Object arguments, final MethodChannel.Result result) {
getIndices("getIndices1Day", context, arguments, result);
}

/// 获取3天生活指数
protected static void getIndices3Day(Context context, Object arguments, final MethodChannel.Result result) {
getIndices("getIndices3Day", context, arguments, result);
}

private static void getIndices(final String name, Context context, Object arguments, final MethodChannel.Result result) {
@SuppressWarnings("unchecked")
HashMap<String, Object> param = (HashMap<String, Object>) arguments;
String location = (String) param.get("location");
@SuppressWarnings("unchecked")
List<String> indicesTypes = (List<String>) param.get("indicesTypes");
List<IndicesType> indicesTypesTmp = new ArrayList<>();
assert indicesTypes != null;
for (String type : indicesTypes) {
switch (type) {
case "SPT":
indicesTypesTmp.add(IndicesType.SPT);
break;
case "CW":
indicesTypesTmp.add(IndicesType.CW);
break;
case "DRSG":
indicesTypesTmp.add(IndicesType.DRSG);
break;
case "FIS":
indicesTypesTmp.add(IndicesType.FIS);
break;
case "UV":
indicesTypesTmp.add(IndicesType.UV);
break;
case "TRAV":
indicesTypesTmp.add(IndicesType.TRAV);
break;
case "AG":
indicesTypesTmp.add(IndicesType.AG);
break;
case "COMF":
indicesTypesTmp.add(IndicesType.COMF);
break;
case "FLU":
indicesTypesTmp.add(IndicesType.FLU);
break;
case "AP":
indicesTypesTmp.add(IndicesType.AP);
break;
case "AC":
indicesTypesTmp.add(IndicesType.AC);
break;
case "GL":
indicesTypesTmp.add(IndicesType.GL);
break;
case "MU":
indicesTypesTmp.add(IndicesType.MU);
break;
case "DC":
indicesTypesTmp.add(IndicesType.DC);
break;
case "PTFC":
indicesTypesTmp.add(IndicesType.PTFC);
break;
case "SPI":
indicesTypesTmp.add(IndicesType.SPI);
break;
case "SKI":
indicesTypesTmp.add(IndicesType.SK);
break;
default:
indicesTypesTmp = Collections.singletonList(IndicesType.ALL);
break;
}
if (indicesTypesTmp.contains(IndicesType.ALL)) break;
}
QWeather.OnResultIndicesListener onResultIndicesListener = new QWeather.OnResultIndicesListener() {
@Override
public void onError(Throwable throwable) {
DebugPrint.print(name + " onError: " + throwable.getLocalizedMessage());
result.success(null);
}

@Override
public void onSuccess(IndicesBean indicesBean) {
Gson gson = new Gson();
String jsonStr = gson.toJson(indicesBean);
DebugPrint.print(name + " onSuccess: " + jsonStr);
result.success(gson.fromJson(jsonStr, Map.class));
}
};
if (name.equals("getIndices1Day")) {
QWeather.getIndices1D(context, location, Lang.ZH_HANS, indicesTypesTmp, onResultIndicesListener);
} else {
QWeather.getIndices3D(context, location, Lang.ZH_HANS, indicesTypesTmp, onResultIndicesListener);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class FlutterQweatherPlugin : FlutterPlugin, MethodCallHandler {
MethodConstants.GetWeatherDaily -> ApiWeather.getWeatherDaily(context, call.arguments, result)
MethodConstants.GetWeatherHourly -> ApiWeather.getWeatherHourly(context, call.arguments, result)
MethodConstants.GetWeatherMinuteLy -> ApiWeather.getWeatherMinuteLy(context, call.arguments, result)
MethodConstants.GetIndices1Day -> ApiIndices.getIndices1Day(context, call.arguments, result)
MethodConstants.GetIndices3Day -> ApiIndices.getIndices3Day(context, call.arguments, result)
else -> result.notImplemented()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ public class MethodConstants {

// 获取中国地区未来2小时内每5分钟降水
public static final String GetWeatherMinuteLy = "GetWeatherMinuteLy";

// 获取1天生活指数
public static final String GetIndices1Day = "GetIndices1Day";

// 获取3天生活指数
public static final String GetIndices3Day = "GetIndices3Day";
}
3 changes: 2 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class _MyAppState extends State<MyApp> {
// await Qweather.instance.setDebug();
await queryWeatherNow();
// FlutterQweather.instance.getWeatherMinuteLy(_location);
FlutterQweather.instance.geoPoiRangeLookup('116.40000,39.88999', PoiType.scenic);
// FlutterQweather.instance.geoPoiRangeLookup('116.40000,39.88999', PoiType.scenic);
// FlutterQweather.instance.getIndices1Day('116.40000,39.88999',indicesTypes: {IndicesType.TRAV});
}

// 查询实时天气
Expand Down
4 changes: 2 additions & 2 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ packages:
path: ".."
relative: true
source: path
version: "0.0.2"
version: "0.0.4"
flutter_test:
dependency: "direct dev"
description: flutter
Expand Down Expand Up @@ -158,4 +158,4 @@ packages:
version: "2.1.0"
sdks:
dart: ">=2.12.0 <3.0.0"
flutter: ">=2.0.0"
flutter: ">=1.20.0"
12 changes: 4 additions & 8 deletions ios/Classes/Api/ApiGeo.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ + (void) geoCityLookup:(id)param result:(FlutterResult)result{
QWeatherConfigInstance.location = paramDic[@"location"];
QWeatherConfigInstance.number = paramDic[@"number"];
QWeatherConfigInstance.range = paramDic[@"range"];
[QWeatherConfigInstance weatherWithInquireType:INQUIRE_TYPE_GEO_CITY_LOOKUP
WithSuccess:^(GeoBaseClass *rep) {
[QWeatherConfigInstance weatherWithInquireType:INQUIRE_TYPE_GEO_CITY_LOOKUP WithSuccess:^(GeoBaseClass *rep) {
[DebugPrint print:[@"geoCityLookup WithSuccess: " stringByAppendingString:rep.description]];
if (![rep.code isEqualToString:@"200"]){
result(NULL);
Expand Down Expand Up @@ -46,8 +45,7 @@ + (void) getGeoTopCity:(id)param result:(FlutterResult)result{
NSDictionary *paramDic = param;
QWeatherConfigInstance.number = paramDic[@"number"];
QWeatherConfigInstance.range = paramDic[@"range"];
[QWeatherConfigInstance weatherWithInquireType:INQUIRE_TYPE_GEO_TOPCITY
WithSuccess:^(GeoBaseClass *rep) {
[QWeatherConfigInstance weatherWithInquireType:INQUIRE_TYPE_GEO_TOPCITY WithSuccess:^(GeoBaseClass *rep) {
[DebugPrint print:[@"geoCityLookup WithSuccess: " stringByAppendingString:rep.description]];
if (![rep.code isEqualToString:@"200"]){
result(NULL);
Expand Down Expand Up @@ -78,8 +76,7 @@ + (void) geoPoiLookup:(id)param result:(FlutterResult)result{
QWeatherConfigInstance.number = paramDic[@"number"];
QWeatherConfigInstance.type = paramDic[@"type"];
QWeatherConfigInstance.city = paramDic[@"city"];
[QWeatherConfigInstance weatherWithInquireType:INQUIRE_TYPE_GEO_POI_LOOKUP
WithSuccess:^(GeoBaseClass *rep) {
[QWeatherConfigInstance weatherWithInquireType:INQUIRE_TYPE_GEO_POI_LOOKUP WithSuccess:^(GeoBaseClass *rep) {
[DebugPrint print:[@"geoCityLookup WithSuccess: " stringByAppendingString:rep.description]];
if (![rep.code isEqualToString:@"200"]){
result(NULL);
Expand Down Expand Up @@ -110,8 +107,7 @@ + (void) geoPoiRangeLookup:(id)param result:(FlutterResult)result{
QWeatherConfigInstance.radius = paramDic[@"radius"];
QWeatherConfigInstance.number = paramDic[@"number"];
QWeatherConfigInstance.type = paramDic[@"type"];
[QWeatherConfigInstance weatherWithInquireType:INQUIRE_TYPE_GEO_POI_RANGE
WithSuccess:^(GeoBaseClass *rep) {
[QWeatherConfigInstance weatherWithInquireType:INQUIRE_TYPE_GEO_POI_RANGE WithSuccess:^(GeoBaseClass *rep) {
[DebugPrint print:[@"geoCityLookup WithSuccess: " stringByAppendingString:rep.description]];
if (![rep.code isEqualToString:@"200"]){
result(NULL);
Expand Down
20 changes: 20 additions & 0 deletions ios/Classes/Api/ApiIndices.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// ApiIndices.h
// flutter_qweather
//
// Created by CyJay on 2021/6/5.
//

#import <Foundation/Foundation.h>
#import <Flutter/Flutter.h>
#import <QWeather/QWeather.h>


@interface ApiIndices : NSObject
/// 获取1天生活指数
+ (void) getIndices1Day:(id)param result:(FlutterResult)result;
/// 获取3天生活指数
+ (void) getIndices3Day:(id)param result:(FlutterResult)result;
/// 获取生活指数
//+ (void) getIndices:(NSString*)name param:(id)param result:(FlutterResult)result;
@end
97 changes: 97 additions & 0 deletions ios/Classes/Api/ApiIndices.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//
// ApiIndices.m
// flutter_qweather
//
// Created by CyJay on 2021/6/5.
//

#import "ApiIndices.h"
#import "../DebugPrint/DebugPrint.h"
@implementation ApiIndices
/// 获取1天生活指数
+ (void) getIndices1Day:(id)param result:(FlutterResult)result{
[ApiIndices getIndices:@"getIndices1Day" param:param result:result];
}
/// 获取3天生活指数
+ (void) getIndices3Day:(id)param result:(FlutterResult)result{
[ApiIndices getIndices:@"getIndices3Day" param:param result:result];
}

/// 获取生活指数
+ (void) getIndices:(NSString*)name param:(id)param result:(FlutterResult)result{
NSDictionary *paramDic = param;
QWeatherConfigInstance.location = paramDic[@"location"];
NSArray<NSString*> *indicesTypes = paramDic[@"indicesTypes"];
NSMutableArray<NSString*> *indicesTypesTmp = [NSMutableArray new];
for (int i = 0; i < indicesTypes.count; i++){
NSString *str = [indicesTypes objectAtIndex:i];
if ([str isEqualToString:@"SPT"]){
[indicesTypesTmp addObject: INDICES_TYPESTRING[INDICES_TYPE_spt]];
} else if ([str isEqualToString:@"CW"]){
[indicesTypesTmp addObject: INDICES_TYPESTRING[INDICES_TYPE_cw]];
} else if ([str isEqualToString:@"DRSG"]){
[indicesTypesTmp addObject: INDICES_TYPESTRING[INDICES_TYPE_drsg]];
} else if ([str isEqualToString:@"FIS"]){
[indicesTypesTmp addObject: INDICES_TYPESTRING[INDICES_TYPE_fis]];
} else if ([str isEqualToString:@"UV"]){
[indicesTypesTmp addObject: INDICES_TYPESTRING[INDICES_TYPE_uv]];
} else if ([str isEqualToString:@"TRAV"]){
[indicesTypesTmp addObject: INDICES_TYPESTRING[INDICES_TYPE_trav]];
} else if ([str isEqualToString:@"AG"]){
[indicesTypesTmp addObject: INDICES_TYPESTRING[INDICES_TYPE_ag]];
} else if ([str isEqualToString:@"COMF"]){
[indicesTypesTmp addObject: INDICES_TYPESTRING[INDICES_TYPE_comf]];
} else if ([str isEqualToString:@"FLU"]){
[indicesTypesTmp addObject: INDICES_TYPESTRING[INDICES_TYPE_flu]];
} else if ([str isEqualToString:@"AP"]){
[indicesTypesTmp addObject: INDICES_TYPESTRING[INDICES_TYPE_ap]];
} else if ([str isEqualToString:@"AC"]){
[indicesTypesTmp addObject: INDICES_TYPESTRING[INDICES_TYPE_ac]];
} else if ([str isEqualToString:@"GL"]){
[indicesTypesTmp addObject: INDICES_TYPESTRING[INDICES_TYPE_gl]];
} else if ([str isEqualToString:@"MU"]){
[indicesTypesTmp addObject: INDICES_TYPESTRING[INDICES_TYPE_mu]];
} else if ([str isEqualToString:@"DC"]){
[indicesTypesTmp addObject: INDICES_TYPESTRING[INDICES_TYPE_dc]];
} else if ([str isEqualToString:@"PTEC"]){
[indicesTypesTmp addObject: INDICES_TYPESTRING[INDICES_TYPE_ptfc]];
} else if ([str isEqualToString:@"SPI"]){
[indicesTypesTmp addObject: INDICES_TYPESTRING[INDICES_TYPE_spi]];
} else if ([str isEqualToString:@"SKI"]){
[indicesTypesTmp addObject: INDICES_TYPESTRING[INDICES_TYPE_ski]];
} else {
indicesTypesTmp = [NSMutableArray arrayWithObject:INDICES_TYPESTRING[INDICES_TYPE_all]];
break;
}
}
QWeatherConfigInstance.indices = indicesTypesTmp;
INQUIRE_TYPE inquireType = INQUIRE_TYPE_INDICES_1D;
if ([name isEqualToString:@"getIndices3Day"]){
inquireType = INQUIRE_TYPE_INDICES_3D;
}
[QWeatherConfigInstance weatherWithInquireType:inquireType WithSuccess:^(IndicesBaseClass *rep) {
[DebugPrint print:[name stringByAppendingString:[@" WithSuccess: " stringByAppendingString:rep.description]]];
if (![rep.code isEqualToString:@"200"]){
result(NULL);
return;
}
NSMutableArray<NSDictionary*> *dailyList = [NSMutableArray new];
for (IndicesDaily *one in rep.daily) {
NSDictionary *oneIndicesDaily = @{@"date": one.date, @"type": one.type, @"name":one.name,
@"level":one.level, @"category": one.category, @"text": one.text};
[dailyList addObject:oneIndicesDaily];
}
NSDictionary *refer = @{@"licenseList": rep.refer.license, @"sourcesList": rep.refer.sources};
NSDictionary *basic = @{@"fxLink": rep.fxLink, @"updateTime": rep.updateTime};
NSMutableDictionary *dic = [NSMutableDictionary new];
[dic setValue:rep.code forKey:@"code"];
[dic setValue:refer forKey:@"refer"];
[dic setValue:basic forKey:@"basic"];
[dic setValue:dailyList forKey:@"dailyList"];
result(dic);
} faileureForError:^(NSError *error) {
[DebugPrint print:[name stringByAppendingString:[@" faileureForError: " stringByAppendingString:error.localizedDescription]]];
result(NULL);
}];
}
@end
12 changes: 4 additions & 8 deletions ios/Classes/Api/ApiWeather.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ @implementation ApiWeather
/// 获取当前天气
+ (void) getWeatherNow:(id _Nullable)param result:(FlutterResult)result{
QWeatherConfigInstance.location = param;
[QWeatherConfigInstance weatherWithInquireType:INQUIRE_TYPE_WEATHER_NOW
WithSuccess:^(WeatherBaseClass *rep) {
[QWeatherConfigInstance weatherWithInquireType:INQUIRE_TYPE_WEATHER_NOW WithSuccess:^(WeatherBaseClass *rep) {
[DebugPrint print:[@"getWeatherNow WithSuccess: " stringByAppendingString:rep.description]];
if (![rep.code isEqualToString:@"200"]){
result(NULL);
Expand Down Expand Up @@ -51,8 +50,7 @@ + (void) getWeatherDaily:(id _Nullable)param result:(FlutterResult)result{
}else if ([dailyNum isEqualToNumber: @15]){
inquireType = INQUIRE_TYPE_WEATHER_15D;
}
[QWeatherConfigInstance weatherWithInquireType:inquireType
WithSuccess:^(WeatherBaseClass *rep) {
[QWeatherConfigInstance weatherWithInquireType:inquireType WithSuccess:^(WeatherBaseClass *rep) {
[DebugPrint print:[@"getWeatherDaily WithSuccess: " stringByAppendingString:rep.description]];
if (![rep.code isEqualToString:@"200"]){
result(NULL);
Expand Down Expand Up @@ -94,8 +92,7 @@ + (void) getWeatherHourly:(id _Nullable)param result:(FlutterResult)result{
}else if ([hourlyNum isEqualToNumber: @168]){
inquireType = INQUIRE_TYPE_WEATHER_168H;
}
[QWeatherConfigInstance weatherWithInquireType:inquireType
WithSuccess:^(WeatherBaseClass *rep) {
[QWeatherConfigInstance weatherWithInquireType:inquireType WithSuccess:^(WeatherBaseClass *rep) {
[DebugPrint print:[@"getWeatherHourly WithSuccess: " stringByAppendingString:rep.description]];
if (![rep.code isEqualToString:@"200"]){
result(NULL);
Expand Down Expand Up @@ -125,8 +122,7 @@ + (void) getWeatherHourly:(id _Nullable)param result:(FlutterResult)result{
/// 获取中国地区未来2小时内每5分钟降水
+ (void) getWeatherMinuteLy:(id _Nullable)param result:(FlutterResult)result{
QWeatherConfigInstance.location = param;
[QWeatherConfigInstance weatherWithInquireType:INQUIRE_TYPE_WEATHER_MINUTELY
WithSuccess:^(WeatherMinutelyBaseClass *rep) {
[QWeatherConfigInstance weatherWithInquireType:INQUIRE_TYPE_WEATHER_MINUTELY WithSuccess:^(WeatherMinutelyBaseClass *rep) {
[DebugPrint print:[@"getWeatherMinuteLy WithSuccess: " stringByAppendingString:rep.description]];
if (![rep.code isEqualToString:@"200"]){
result(NULL);
Expand Down
Loading

0 comments on commit df42179

Please sign in to comment.