Skip to content

Commit 78025de

Browse files
committed
feat: Connect API
1 parent e90ba89 commit 78025de

File tree

11 files changed

+459
-66
lines changed

11 files changed

+459
-66
lines changed

ios/Runner/Info.plist

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,50 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
4-
<dict>
5-
<key>CFBundleDevelopmentRegion</key>
6-
<string>$(DEVELOPMENT_LANGUAGE)</string>
7-
<key>CFBundleExecutable</key>
8-
<string>$(EXECUTABLE_NAME)</string>
9-
<key>CFBundleIdentifier</key>
10-
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
11-
<key>CFBundleInfoDictionaryVersion</key>
12-
<string>6.0</string>
13-
<key>CFBundleName</key>
14-
<string>weatherbi</string>
15-
<key>CFBundlePackageType</key>
16-
<string>APPL</string>
17-
<key>CFBundleShortVersionString</key>
18-
<string>$(FLUTTER_BUILD_NAME)</string>
19-
<key>CFBundleSignature</key>
20-
<string>????</string>
21-
<key>CFBundleVersion</key>
22-
<string>$(FLUTTER_BUILD_NUMBER)</string>
23-
<key>LSRequiresIPhoneOS</key>
24-
<true/>
25-
<key>UILaunchStoryboardName</key>
26-
<string>LaunchScreen</string>
27-
<key>UIMainStoryboardFile</key>
28-
<string>Main</string>
29-
<key>UISupportedInterfaceOrientations</key>
30-
<array>
31-
<string>UIInterfaceOrientationPortrait</string>
32-
<string>UIInterfaceOrientationLandscapeLeft</string>
33-
<string>UIInterfaceOrientationLandscapeRight</string>
34-
</array>
35-
<key>UISupportedInterfaceOrientations~ipad</key>
36-
<array>
37-
<string>UIInterfaceOrientationPortrait</string>
38-
<string>UIInterfaceOrientationPortraitUpsideDown</string>
39-
<string>UIInterfaceOrientationLandscapeLeft</string>
40-
<string>UIInterfaceOrientationLandscapeRight</string>
41-
</array>
42-
<key>UIViewControllerBasedStatusBarAppearance</key>
43-
<false/>
44-
</dict>
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>$(DEVELOPMENT_LANGUAGE)</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundleName</key>
14+
<string>weatherbi</string>
15+
<key>CFBundlePackageType</key>
16+
<string>APPL</string>
17+
<key>CFBundleShortVersionString</key>
18+
<string>$(FLUTTER_BUILD_NAME)</string>
19+
<key>CFBundleSignature</key>
20+
<string>????</string>
21+
<key>CFBundleVersion</key>
22+
<string>$(FLUTTER_BUILD_NUMBER)</string>
23+
<key>LSRequiresIPhoneOS</key>
24+
<true />
25+
<key>UILaunchStoryboardName</key>
26+
<string>LaunchScreen</string>
27+
<key>UIMainStoryboardFile</key>
28+
<string>Main</string>
29+
<key>UISupportedInterfaceOrientations</key>
30+
<array>
31+
<string>UIInterfaceOrientationPortrait</string>
32+
<string>UIInterfaceOrientationLandscapeLeft</string>
33+
<string>UIInterfaceOrientationLandscapeRight</string>
34+
</array>
35+
<key>UISupportedInterfaceOrientations~ipad</key>
36+
<array>
37+
<string>UIInterfaceOrientationPortrait</string>
38+
<string>UIInterfaceOrientationPortraitUpsideDown</string>
39+
<string>UIInterfaceOrientationLandscapeLeft</string>
40+
<string>UIInterfaceOrientationLandscapeRight</string>
41+
</array>
42+
<key>UIViewControllerBasedStatusBarAppearance</key>
43+
<false />
44+
<key>NSAppTransportSecurity</key>
45+
<dict>
46+
<key>NSAllowsArbitraryLoads</key>
47+
<true />
48+
</dict>
49+
</dict>
4550
</plist>

lib/Cubit/WeatherCubit.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import 'package:flutter_bloc/flutter_bloc.dart';
2+
import 'package:weatherbi/Cubit/WeatherRepository.dart';
3+
import 'package:weatherbi/Cubit/WeatherState.dart';
4+
5+
class WeatherCubit extends Cubit<WeatherState> {
6+
IWeatherRepository _weatherRepository;
7+
WeatherCubit(this._weatherRepository) : super(WeatherInitial());
8+
9+
Future<void> getLocation(String city) async {
10+
try {
11+
emit(WeatherLoading());
12+
var res = await _weatherRepository.getWeather(city);
13+
emit(WeatherDone(res));
14+
} catch (e) {
15+
WeatherError(e);
16+
}
17+
}
18+
}

lib/Cubit/WeatherRepository.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import 'dart:convert';
2+
3+
import 'package:weatherbi/Models/Weather.dart';
4+
import 'package:weatherbi/Utils/Constant.dart';
5+
import 'package:http/http.dart' as http;
6+
7+
abstract class IWeatherRepository {
8+
Future<Weather> getWeather(String city);
9+
}
10+
11+
class WeatherRepository extends IWeatherRepository {
12+
@override
13+
Future<Weather> getWeather(String city) async {
14+
final String _baseUrl = 'api.weatherapi.com';
15+
final String _charactersPath = 'v1/current.json';
16+
final Map<String, String> _queryParameters = <String, String>{
17+
'key': Constant().apiKey,
18+
'q': '$city',
19+
'days': '7',
20+
};
21+
Uri url = Uri.http(_baseUrl, _charactersPath, _queryParameters);
22+
var cevap = await http.get(url);
23+
return Weather.fromJson(jsonDecode(cevap.body));
24+
}
25+
}

lib/Cubit/WeatherState.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import 'package:weatherbi/Models/Weather.dart';
2+
3+
abstract class WeatherState {
4+
const WeatherState();
5+
}
6+
7+
class WeatherInitial extends WeatherState {
8+
const WeatherInitial();
9+
}
10+
11+
class WeatherLoading extends WeatherState {
12+
const WeatherLoading();
13+
}
14+
15+
class WeatherDone extends WeatherState {
16+
final Weather weather;
17+
const WeatherDone(this.weather);
18+
}
19+
20+
class WeatherError extends WeatherState {
21+
final String error;
22+
const WeatherError(this.error);
23+
24+
String get message => null;
25+
}

lib/Models/Weather.dart

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
// To parse this JSON data, do
2+
//
3+
// final weather = weatherFromJson(jsonString);
4+
5+
import 'dart:convert';
6+
7+
Weather weatherFromJson(String str) => Weather.fromJson(json.decode(str));
8+
9+
String weatherToJson(Weather data) => json.encode(data.toJson());
10+
11+
class Weather {
12+
Weather({
13+
this.location,
14+
this.current,
15+
});
16+
17+
Location location;
18+
Current current;
19+
20+
factory Weather.fromJson(Map<String, dynamic> json) => Weather(
21+
location: Location.fromJson(json["location"]),
22+
current: Current.fromJson(json["current"]),
23+
);
24+
25+
Map<String, dynamic> toJson() => {
26+
"location": location.toJson(),
27+
"current": current.toJson(),
28+
};
29+
}
30+
31+
class Current {
32+
Current({
33+
this.lastUpdatedEpoch,
34+
this.lastUpdated,
35+
this.tempC,
36+
this.tempF,
37+
this.isDay,
38+
this.condition,
39+
this.windMph,
40+
this.windKph,
41+
this.windDegree,
42+
this.windDir,
43+
this.pressureMb,
44+
this.pressureIn,
45+
this.precipMm,
46+
this.precipIn,
47+
this.humidity,
48+
this.cloud,
49+
this.feelslikeC,
50+
this.feelslikeF,
51+
this.visKm,
52+
this.visMiles,
53+
this.uv,
54+
this.gustMph,
55+
this.gustKph,
56+
this.airQuality,
57+
});
58+
59+
int lastUpdatedEpoch;
60+
String lastUpdated;
61+
double tempC;
62+
double tempF;
63+
int isDay;
64+
Condition condition;
65+
double windMph;
66+
double windKph;
67+
int windDegree;
68+
String windDir;
69+
double pressureMb;
70+
double pressureIn;
71+
double precipMm;
72+
double precipIn;
73+
int humidity;
74+
int cloud;
75+
double feelslikeC;
76+
double feelslikeF;
77+
double visKm;
78+
double visMiles;
79+
double uv;
80+
double gustMph;
81+
double gustKph;
82+
Map<String, double> airQuality;
83+
84+
factory Current.fromJson(Map<String, dynamic> json) => Current(
85+
lastUpdatedEpoch: json["last_updated_epoch"],
86+
lastUpdated: json["last_updated"],
87+
tempC: json["temp_c"],
88+
tempF: json["temp_f"],
89+
isDay: json["is_day"],
90+
condition: Condition.fromJson(json["condition"]),
91+
windMph: json["wind_mph"].toDouble(),
92+
windKph: json["wind_kph"].toDouble(),
93+
windDegree: json["wind_degree"],
94+
windDir: json["wind_dir"],
95+
pressureMb: json["pressure_mb"],
96+
pressureIn: json["pressure_in"].toDouble(),
97+
precipMm: json["precip_mm"].toDouble(),
98+
precipIn: json["precip_in"],
99+
humidity: json["humidity"],
100+
cloud: json["cloud"],
101+
feelslikeC: json["feelslike_c"].toDouble(),
102+
feelslikeF: json["feelslike_f"].toDouble(),
103+
visKm: json["vis_km"],
104+
visMiles: json["vis_miles"],
105+
uv: json["uv"],
106+
gustMph: json["gust_mph"].toDouble(),
107+
gustKph: json["gust_kph"].toDouble(),
108+
);
109+
110+
Map<String, dynamic> toJson() => {
111+
"last_updated_epoch": lastUpdatedEpoch,
112+
"last_updated": lastUpdated,
113+
"temp_c": tempC,
114+
"temp_f": tempF,
115+
"is_day": isDay,
116+
"condition": condition.toJson(),
117+
"wind_mph": windMph,
118+
"wind_kph": windKph,
119+
"wind_degree": windDegree,
120+
"wind_dir": windDir,
121+
"pressure_mb": pressureMb,
122+
"pressure_in": pressureIn,
123+
"precip_mm": precipMm,
124+
"precip_in": precipIn,
125+
"humidity": humidity,
126+
"cloud": cloud,
127+
"feelslike_c": feelslikeC,
128+
"feelslike_f": feelslikeF,
129+
"vis_km": visKm,
130+
"vis_miles": visMiles,
131+
"uv": uv,
132+
"gust_mph": gustMph,
133+
"gust_kph": gustKph,
134+
"air_quality": Map.from(airQuality).map((k, v) => MapEntry<String, dynamic>(k, v)),
135+
};
136+
}
137+
138+
class Condition {
139+
Condition({
140+
this.text,
141+
this.icon,
142+
this.code,
143+
});
144+
145+
String text;
146+
String icon;
147+
int code;
148+
149+
factory Condition.fromJson(Map<String, dynamic> json) => Condition(
150+
text: json["text"],
151+
icon: json["icon"],
152+
code: json["code"],
153+
);
154+
155+
Map<String, dynamic> toJson() => {
156+
"text": text,
157+
"icon": icon,
158+
"code": code,
159+
};
160+
}
161+
162+
class Location {
163+
Location({
164+
this.name,
165+
this.region,
166+
this.country,
167+
this.lat,
168+
this.lon,
169+
this.tzId,
170+
this.localtimeEpoch,
171+
this.localtime,
172+
});
173+
174+
String name;
175+
String region;
176+
String country;
177+
double lat;
178+
double lon;
179+
String tzId;
180+
int localtimeEpoch;
181+
String localtime;
182+
183+
factory Location.fromJson(Map<String, dynamic> json) => Location(
184+
name: json["name"],
185+
region: json["region"],
186+
country: json["country"],
187+
lat: json["lat"].toDouble(),
188+
lon: json["lon"].toDouble(),
189+
tzId: json["tz_id"],
190+
localtimeEpoch: json["localtime_epoch"],
191+
localtime: json["localtime"],
192+
);
193+
194+
Map<String, dynamic> toJson() => {
195+
"name": name,
196+
"region": region,
197+
"country": country,
198+
"lat": lat,
199+
"lon": lon,
200+
"tz_id": tzId,
201+
"localtime_epoch": localtimeEpoch,
202+
"localtime": localtime,
203+
};
204+
}

0 commit comments

Comments
 (0)