Skip to content

Commit

Permalink
Merge 54aba78 into 6b7f049
Browse files Browse the repository at this point in the history
  • Loading branch information
nosmirck committed Jul 18, 2019
2 parents 6b7f049 + 54aba78 commit 5e6c15b
Show file tree
Hide file tree
Showing 14 changed files with 219 additions and 151 deletions.
10 changes: 8 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@
"configurations": [
{
"name": "development",
"program": "lib/main.dart",
"program": "lib/main_dev.dart",
"request": "launch",
"type": "dart"
},
{
"name": "production",
"name": "uat",
"program": "lib/main_prod.dart",
"request": "launch",
"type": "dart"
},
{
"name": "production",
"program": "lib/main.dart",
"request": "launch",
"type": "dart"
}
]
}
77 changes: 7 additions & 70 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,89 +1,26 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:http/testing.dart';
import 'package:mockito/mockito.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:simple_weather/service_locator.dart';
import 'package:simple_weather/src/app.dart';
import 'package:simple_weather/src/app_config.dart';
import 'package:simple_weather/src/client/mocked/mock_weather_api_impl.dart';
import 'package:simple_weather/src/client/implementations/weather_api_impl.dart';
import 'package:simple_weather/src/client/weather_api.dart';
import 'package:simple_weather/src/services/implementations/location_service_impl.dart';
import 'package:simple_weather/src/services/location_service.dart';
import 'package:simple_weather/src/services/mocked/mock_location_service_impl.dart';

main() async {
await registerDependencies();
runApp(
AppConfig(
environment: Environment.development,
environment: Environment.production,
child: App(),
),
);
}

const weatherJsonResponse = '''{
"coord": {
"lon": -79.39,
"lat": 43.65
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01d"
}
],
"base": "stations",
"main": {
"temp": 297.45,
"pressure": 1019,
"humidity": 60,
"temp_min": 296.15,
"temp_max": 299.15
},
"visibility": 14484,
"wind": {
"speed": 4.1,
"deg": 180
},
"clouds": {
"all": 1
},
"dt": 1532019600,
"sys": {
"type": 1,
"id": 3721,
"message": 0.0046,
"country": "CA",
"sunrise": 1531994053,
"sunset": 1532047985
},
"id": 6167863,
"name": "Downtown Toronto",
"cod": 200
}''';

registerDependencies() async {
ServiceLocator.reset();
final _mockSharedPrefs = MockSharedPreferences();

when(_mockSharedPrefs.getBool(typed(any))).thenReturn(true);
when(_mockSharedPrefs.setBool(typed(any), typed(any)))
.thenAnswer((_) => Future.value(true));

ServiceLocator.registerSingleton<SharedPreferences>(_mockSharedPrefs);
ServiceLocator.registerSingleton<LocationService>(MockLocationService());
ServiceLocator.registerSingleton<WeatherApi>(
MockWeatherApi(
MockClient(
(request) async => Response(weatherJsonResponse, 200),
),
"",
),
);
ServiceLocator.registerSingleton<SharedPreferences>(
await SharedPreferences.getInstance());
ServiceLocator.registerSingleton<LocationService>(LocationServiceImpl());
ServiceLocator.registerSingleton<WeatherApi>(WeatherApiImpl());
}

class MockSharedPreferences extends Mock implements SharedPreferences {}
89 changes: 89 additions & 0 deletions lib/main_dev.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:http/testing.dart';
import 'package:mockito/mockito.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:simple_weather/service_locator.dart';
import 'package:simple_weather/src/app.dart';
import 'package:simple_weather/src/app_config.dart';
import 'package:simple_weather/src/client/mocked/mock_weather_api_impl.dart';
import 'package:simple_weather/src/client/weather_api.dart';
import 'package:simple_weather/src/services/location_service.dart';
import 'package:simple_weather/src/services/mocked/mock_location_service_impl.dart';

main() async {
await registerDependencies();
runApp(
AppConfig(
environment: Environment.development,
child: App(),
),
);
}

registerDependencies() async {
ServiceLocator.reset();
final _mockSharedPrefs = MockSharedPreferences();

when(_mockSharedPrefs.getBool(typed(any))).thenReturn(true);
when(_mockSharedPrefs.setBool(typed(any), typed(any)))
.thenAnswer((_) => Future.value(true));

ServiceLocator.registerSingleton<SharedPreferences>(_mockSharedPrefs);
ServiceLocator.registerSingleton<LocationService>(MockLocationService());
ServiceLocator.registerSingleton<WeatherApi>(
MockWeatherApi(
MockClient(
(request) async => Response(weatherJsonResponse, 200),
),
"",
),
);
}

class MockSharedPreferences extends Mock implements SharedPreferences {}

const weatherJsonResponse = '''{
"coord": {
"lon": -79.39,
"lat": 43.65
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01d"
}
],
"base": "stations",
"main": {
"temp": 297.45,
"pressure": 1019,
"humidity": 60,
"temp_min": 296.15,
"temp_max": 299.15
},
"visibility": 14484,
"wind": {
"speed": 4.1,
"deg": 180
},
"clouds": {
"all": 1
},
"dt": 1532019600,
"sys": {
"type": 1,
"id": 3721,
"message": 0.0046,
"country": "CA",
"sunrise": 1531994053,
"sunset": 1532047985
},
"id": 6167863,
"name": "Downtown Toronto",
"cod": 200
}''';
89 changes: 89 additions & 0 deletions lib/main_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:http/testing.dart';
import 'package:mockito/mockito.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:simple_weather/service_locator.dart';
import 'package:simple_weather/src/app.dart';
import 'package:simple_weather/src/app_config.dart';
import 'package:simple_weather/src/client/mocked/mock_weather_api_impl.dart';
import 'package:simple_weather/src/client/weather_api.dart';
import 'package:simple_weather/src/services/location_service.dart';
import 'package:simple_weather/src/services/mocked/mock_location_service_impl.dart';

main() async {
await registerDependencies();
runApp(
AppConfig(
environment: Environment.tests,
child: App(),
),
);
}

const weatherJsonResponse = '''{
"coord": {
"lon": -79.39,
"lat": 43.65
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01d"
}
],
"base": "stations",
"main": {
"temp": 297.45,
"pressure": 1019,
"humidity": 60,
"temp_min": 296.15,
"temp_max": 299.15
},
"visibility": 14484,
"wind": {
"speed": 4.1,
"deg": 180
},
"clouds": {
"all": 1
},
"dt": 1532019600,
"sys": {
"type": 1,
"id": 3721,
"message": 0.0046,
"country": "CA",
"sunrise": 1531994053,
"sunset": 1532047985
},
"id": 6167863,
"name": "Downtown Toronto",
"cod": 200
}''';

registerDependencies() async {
ServiceLocator.reset();
final _mockSharedPrefs = MockSharedPreferences();

when(_mockSharedPrefs.getBool(typed(any))).thenReturn(true);
when(_mockSharedPrefs.setBool(typed(any), typed(any)))
.thenAnswer((_) => Future.value(true));

ServiceLocator.registerSingleton<SharedPreferences>(_mockSharedPrefs);
ServiceLocator.registerSingleton<LocationService>(MockLocationService());
ServiceLocator.registerSingleton<WeatherApi>(
MockWeatherApi(
MockClient(
(request) async => Response(weatherJsonResponse, 200),
),
"",
),
);
}

class MockSharedPreferences extends Mock implements SharedPreferences {}
4 changes: 2 additions & 2 deletions lib/src/common/constants.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class AppConstants {
static const apiKey = "OWM_API_KEY";
static const imageUrl = 'http://openweathermap.org/img/w/';
static const apiKey = "9330c38e200912fe46e8abd5f99d98e0";
static const imageUrl = 'https://openweathermap.org/img/w/';
}
7 changes: 5 additions & 2 deletions lib/src/views/weather_page.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:simple_weather/src/exceptions/exceptions.dart';
import 'package:simple_weather/src/models/weather_model.dart';
Expand Down Expand Up @@ -64,9 +66,10 @@ class WeatherPage extends StatelessWidget {
builder: (context, AsyncSnapshot<WeatherModel> snapshot) {
if (snapshot.hasError) {
return Center(
child: snapshot.error is NoWeatherException
child: snapshot.error is NoWeatherException ||
snapshot.error is TimeoutException
? Text(LocalizationsProvider.of(context).error)
: Text(snapshot.error),
: Text(snapshot.error.toString()),
);
}
if (!snapshot.hasData) {
Expand Down
Loading

0 comments on commit 5e6c15b

Please sign in to comment.