Skip to content

Commit 1e1cb0f

Browse files
committed
offline check
1 parent cbd080a commit 1e1cb0f

File tree

7 files changed

+152
-18
lines changed

7 files changed

+152
-18
lines changed

lib/main.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:convert';
22

3+
import 'package:connectivity_plus/connectivity_plus.dart';
34
import 'package:flutter/material.dart';
45
import 'package:flutter/services.dart';
56
import 'package:geocoding_resolver/geocoding_resolver.dart';
@@ -24,7 +25,9 @@ Future<void> main() async {
2425
LocationsService(),
2526
dispose: (s) => s.dispose(),
2627
);
27-
di.registerSingleton(AppModel());
28+
final appModel = AppModel(connectivity: Connectivity());
29+
await appModel.init();
30+
di.registerSingleton(appModel);
2831
final weatherModel = WeatherModel(
2932
locationsService: di<LocationsService>(),
3033
openWeather: di<OpenWeather>(),
@@ -42,10 +45,12 @@ Future<void> main() async {
4245
} else {
4346
runApp(
4447
MaterialApp(
48+
debugShowCheckedModeBanner: false,
4549
theme: yaruLight,
4650
home: const Scaffold(
51+
appBar: YaruWindowTitleBar(),
4752
body: Center(
48-
child: Text('NO VALI API KEY FOUND'),
53+
child: Text('NO VALID API KEY FOUND'),
4954
),
5055
),
5156
),

lib/src/app/app.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import '../../constants.dart';
88
import '../../weather.dart';
99
import '../weather/view/city_search_field.dart';
1010
import '../weather/weather_model.dart';
11+
import 'app_model.dart';
12+
import 'offline_page.dart';
1113

1214
class App extends StatelessWidget {
1315
const App({super.key});
@@ -38,6 +40,10 @@ class MasterDetailPage extends StatelessWidget with WatchItMixin {
3840

3941
@override
4042
Widget build(BuildContext context) {
43+
final isOnline = watchPropertyValue((AppModel m) => m.isOnline);
44+
if (!isOnline) {
45+
return const OfflinePage();
46+
}
4147
final model = di<WeatherModel>();
4248
final favLocationsLength =
4349
watchPropertyValue((WeatherModel m) => m.favLocations.length);

lib/src/app/app_model.dart

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,48 @@
1+
import 'dart:async';
2+
3+
import 'package:connectivity_plus/connectivity_plus.dart';
4+
import 'package:flutter/widgets.dart';
15
import 'package:safe_change_notifier/safe_change_notifier.dart';
26

37
class AppModel extends SafeChangeNotifier {
8+
AppModel({required Connectivity connectivity})
9+
: _connectivity = connectivity,
10+
_countryCode = WidgetsBinding
11+
.instance.platformDispatcher.locale.countryCode
12+
?.toLowerCase();
13+
14+
final String? _countryCode;
15+
String? get countryCode => _countryCode;
16+
17+
final Connectivity _connectivity;
18+
StreamSubscription? _subscription;
19+
ConnectivityResult? _result;
20+
21+
bool get isOnline =>
22+
_result == ConnectivityResult.wifi ||
23+
_result == ConnectivityResult.ethernet ||
24+
_result == ConnectivityResult.vpn ||
25+
_result == ConnectivityResult.bluetooth ||
26+
_result == ConnectivityResult.mobile;
27+
28+
Future<void> init() async {
29+
_subscription ??=
30+
_connectivity.onConnectivityChanged.listen(_updateConnectivity);
31+
return _connectivity.checkConnectivity().then(_updateConnectivity);
32+
}
33+
34+
@override
35+
Future<void> dispose() async {
36+
await _subscription?.cancel();
37+
super.dispose();
38+
}
39+
40+
void _updateConnectivity(List<ConnectivityResult> result) {
41+
if (_result == result.firstOrNull) return;
42+
_result = result.firstOrNull;
43+
notifyListeners();
44+
}
45+
446
int _tabIndex = 0;
547
int get tabIndex => _tabIndex;
648
set tabIndex(int value) {

lib/src/app/offline_page.dart

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:yaru/yaru.dart';
3+
4+
import '../../build_context_x.dart';
5+
6+
class OfflinePage extends StatelessWidget {
7+
const OfflinePage({super.key});
8+
9+
@override
10+
Widget build(BuildContext context) {
11+
final theme = context.theme;
12+
return YaruDetailPage(
13+
appBar: YaruWindowTitleBar(
14+
border: BorderSide.none,
15+
title: const Text('Offline'),
16+
backgroundColor: Colors.transparent,
17+
leading: Navigator.of(context).canPop() == true
18+
? const YaruBackButton(
19+
style: YaruBackButtonStyle.rounded,
20+
)
21+
: const SizedBox.shrink(),
22+
),
23+
body: Center(
24+
child: Column(
25+
mainAxisSize: MainAxisSize.min,
26+
children: [
27+
YaruAnimatedVectorIcon(
28+
YaruAnimatedIcons.no_network,
29+
size: 200,
30+
color: theme.disabledColor,
31+
),
32+
Padding(
33+
padding: const EdgeInsets.only(
34+
top: kYaruPagePadding,
35+
left: 40,
36+
right: 40,
37+
),
38+
child: Text(
39+
"It look's like your computer is not connectedto the internet",
40+
textAlign: TextAlign.center,
41+
style: theme.textTheme.headlineMedium?.copyWith(
42+
color: theme.disabledColor,
43+
),
44+
),
45+
),
46+
],
47+
),
48+
),
49+
);
50+
}
51+
}

lib/src/weather/weather_utils.dart

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -118,20 +118,25 @@ WeatherType getWeatherType(WeatherData weatherData) {
118118
).hour;
119119
final night = hour > 20 || hour < 6;
120120

121-
switch (weatherData.shortDescription) {
122-
case 'Clouds':
123-
return night ? WeatherType.cloudyNight : WeatherType.cloudy;
124-
case 'Drizzle':
125-
return WeatherType.lightRainy;
126-
case 'Rain':
127-
return WeatherType.heavyRainy;
128-
case 'Snow':
129-
return WeatherType.heavySnow;
130-
case 'Clear':
131-
return night ? WeatherType.sunnyNight : WeatherType.sunny;
132-
case 'Sunny':
133-
return night ? WeatherType.sunnyNight : WeatherType.sunny;
134-
default:
135-
return WeatherType.thunder;
136-
}
121+
return switch (weatherData.longDescription) {
122+
'overcast clouds' => night ? WeatherType.cloudyNight : WeatherType.cloudy,
123+
'scattered clouds' => night ? WeatherType.cloudyNight : WeatherType.cloudy,
124+
'broken clouds' => night ? WeatherType.cloudyNight : WeatherType.cloudy,
125+
'few clouds' => night ? WeatherType.cloudyNight : WeatherType.cloudy,
126+
'light rain' => WeatherType.lightRainy,
127+
'heavy rain' => WeatherType.heavyRainy,
128+
'light snow' => WeatherType.lightSnow,
129+
'heavy snow' => WeatherType.heavySnow,
130+
'clear sky' => night ? WeatherType.sunnyNight : WeatherType.sunny,
131+
_ => switch (weatherData.shortDescription) {
132+
'Clouds' => night ? WeatherType.cloudyNight : WeatherType.cloudy,
133+
'Drizzle' => WeatherType.lightRainy,
134+
'Rain' => WeatherType.middleRainy,
135+
'Snow' => WeatherType.heavySnow,
136+
'Clear' => night ? WeatherType.sunnyNight : WeatherType.sunny,
137+
'Sunny' => night ? WeatherType.sunnyNight : WeatherType.sunny,
138+
'Wind' => night ? WeatherType.dusty : WeatherType.dusty,
139+
_ => WeatherType.thunder
140+
}
141+
};
137142
}

pubspec.lock

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,22 @@ packages:
7373
url: "https://pub.dev"
7474
source: hosted
7575
version: "1.18.0"
76+
connectivity_plus:
77+
dependency: "direct main"
78+
description:
79+
name: connectivity_plus
80+
sha256: ebe15d94de9dd7c31dc2ac54e42780acdf3384b1497c69290c9f3c5b0279fc57
81+
url: "https://pub.dev"
82+
source: hosted
83+
version: "6.0.2"
84+
connectivity_plus_platform_interface:
85+
dependency: transitive
86+
description:
87+
name: connectivity_plus_platform_interface
88+
sha256: b6a56efe1e6675be240de39107281d4034b64ac23438026355b4234042a35adb
89+
url: "https://pub.dev"
90+
source: hosted
91+
version: "2.0.0"
7692
convert:
7793
dependency: transitive
7894
description:
@@ -369,6 +385,14 @@ packages:
369385
url: "https://pub.dev"
370386
source: hosted
371387
version: "1.11.0"
388+
nm:
389+
dependency: transitive
390+
description:
391+
name: nm
392+
sha256: "2c9aae4127bdc8993206464fcc063611e0e36e72018696cd9631023a31b24254"
393+
url: "https://pub.dev"
394+
source: hosted
395+
version: "0.5.0"
372396
open_weather_client:
373397
dependency: "direct main"
374398
description:

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ dependencies:
3232
xdg_directories: ^1.0.4
3333
path_provider: ^2.1.3
3434
path: ^1.9.0
35+
connectivity_plus: ^6.0.2
3536

3637
dev_dependencies:
3738
flutter_lints: ^3.0.1

0 commit comments

Comments
 (0)