Skip to content

Commit

Permalink
Merge pull request #106 from covid19cuba/develop
Browse files Browse the repository at this point in the history
Release 0.4.0
  • Loading branch information
leynier authored Apr 8, 2020
2 parents 34cb86d + cf19514 commit 7e5704b
Show file tree
Hide file tree
Showing 79 changed files with 1,592 additions and 1,477 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# Covid19 Cuba Data Mobile Application Changelog

## [0.4.0] - April 7, 2020

### Added

* Charts with deaths and recovered data.
* Settings page
* Option for mode of connection
* Remember entity selected for comparison with Cuba

### Changed

* Change data of few charts

### Fixed

* Fix problem with encoding
* Fix table of provinces and municipalities

## [0.3.1] - April 4, 2020

### Changed
Expand Down
11 changes: 9 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@ void main() async {

await NotificationManager.initialize();

await setUpTasks();
/*int setUpTasksMinutes;
try {
setUpTasksMinutes = PrefService.getInt(Constants.prefSetUpTasksMinutes);
} catch (e) {
log(e.toString());
setUpTasksMinutes = Constants.setUpTasksMinutesDefault;
}
await setUpTasks(setUpTasksMinutes);
await setUpClapsTime();
await setUpClapsTime();*/

// TODO: Bug in advice notifications, meanwhile they should be disabled
/*var notifications = getApplicationNotifications();
Expand Down
13 changes: 3 additions & 10 deletions lib/src/blocs/home_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
}
}
setCubaDataToCache(data);
var countries = await getCountriesDataFromCache();
if (countries == null) countries = await getCountriesData();
setCountriesDataToCache(countries);
yield LoadedHomeState(data: data, countries: countries);
yield LoadedHomeState(data: data);
} on BadRequestException catch (e) {
log(e.toString());
var data = await getCubaDataFromCache();
Expand Down Expand Up @@ -79,9 +76,7 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
try {
var data = await getCubaData();
setCubaDataToCache(data);
var countries = await getCountriesData();
setCountriesDataToCache(countries);
yield LoadedHomeState(data: data, countries: countries);
yield LoadedHomeState(data: data);
} on BadRequestException catch (e) {
log(e.toString());
var data = await getCubaDataFromCache();
Expand Down Expand Up @@ -119,9 +114,7 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
try {
var data = await getCubaData();
setCubaDataToCache(data);
var countries = await getCountriesData();
setCountriesDataToCache(countries);
yield LoadedHomeState(data: data, countries: countries);
yield LoadedHomeState(data: data);
} on BadRequestException catch (e) {
log(e.toString());
var data = await getCubaDataFromCache();
Expand Down
5 changes: 1 addition & 4 deletions lib/src/blocs/states/home_states.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ class LoadingHomeState extends HomeState {}

class LoadedHomeState extends HomeState {
final DataModel data;
final WorldTotalsModel countries;

const LoadedHomeState({@required this.data, @required this.countries})
: assert(data != null),
assert(countries != null);
const LoadedHomeState({@required this.data}) : assert(data != null);

@override
List<Object> get props => [data];
Expand Down
62 changes: 0 additions & 62 deletions lib/src/data_providers/countries_data_provider.dart

This file was deleted.

52 changes: 37 additions & 15 deletions lib/src/data_providers/cuba_data_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,73 @@ import 'dart:convert';
import 'dart:developer';

import 'package:http/http.dart';
import 'package:package_info/package_info.dart';
import 'package:preferences/preferences.dart';

import 'package:covid19cuba/src/models/models.dart';
import 'package:covid19cuba/src/utils/utils.dart';

const urlCubaDataCU = 'https://www.cusobu.nat.cu/covid/data/covid19-cuba.json';
const urlCubaDataCU = 'https://cusobu.nat.cu/covid/api/v1/all.json';
const urlCubaDataIO =
'https://covid19cubadata.github.io/data/covid19-cuba.json';
'https://covid19cuba.github.io/covid19cubadata.github.io/api/v1/all.json';

Future<DataModel> getCubaData() async {
try {
return await getCubaDataFrom(urlCubaDataCU);
} catch (e) {
log(e.toString());
return await getCubaDataFrom(urlCubaDataIO);
var mode = PrefService.getInt(Constants.prefConnectionMode) ??
Constants.ConnectionModeMerge;
switch (mode) {
case Constants.ConnectionModeIntranet:
return await getCubaDataFrom(urlCubaDataCU);
case Constants.ConnectionModeInternet:
return await getCubaDataFrom(urlCubaDataIO);
case Constants.ConnectionModeMerge:
default:
try {
return await getCubaDataFrom(urlCubaDataCU);
} catch (e) {
log(e.toString());
return await getCubaDataFrom(urlCubaDataIO);
}
}
}

Future<DataModel> getCubaDataFrom(String url) async {
var resp = await get(url, headers: {'Accept-Encoding':'gzip, deflate, br'});
var resp = await get(url, headers: {
'Accept-Encoding': 'gzip, deflate, br',
});
if (resp.statusCode == 404) {
throw InvalidSourceException('Source is invalid');
} else if (resp.statusCode != 200) {
throw BadRequestException('Bad request');
}
DataModel result;
try {
result = DataModel.fromJson(jsonDecode(resp.body));
var json = jsonDecode(utf8.decode(resp.bodyBytes));
result = DataModel.fromJson(json);
} catch (e) {
log(e.toString());
throw ParseException('Parse error');
}
try{
try {
int time = (DateTime.now().millisecondsSinceEpoch / 1000).round() - 1;
PrefService.setInt('last_data_update', time);
}
catch (e) {
PrefService.setInt(Constants.prefLastDataUpdate, time);
var packageInfo = await PackageInfo.fromPlatform();
var versionCode = int.parse(packageInfo.buildNumber);
PrefService.setInt(Constants.prefVersionCode, versionCode);
} catch (e) {
log(e.toString());
}
return result;
}

Future<DataModel> getCubaDataFromCache() async {
try {
var data = PrefService.getString('data');
var packageInfo = await PackageInfo.fromPlatform();
var versionCodeNow = int.parse(packageInfo.buildNumber);
var versionCodeOld = PrefService.getInt(Constants.prefVersionCode) ?? 0;
if (versionCodeNow != versionCodeOld) {
return null;
}
var data = PrefService.getString(Constants.prefData);
if (data == null) {
return null;
}
Expand All @@ -60,7 +82,7 @@ Future<DataModel> getCubaDataFromCache() async {
Future<void> setCubaDataToCache(DataModel data) async {
try {
String result = jsonEncode(data.toJson());
PrefService.setString('data', result);
PrefService.setString(Constants.prefData, result);
} catch (e) {
log(e.toString());
}
Expand Down
1 change: 0 additions & 1 deletion lib/src/data_providers/data_providers.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export 'cuba_data_provider.dart';
export 'countries_data_provider.dart';
export 'info_update_provider.dart';
export 'news_provider.dart';
export 'notifications_data_provider.dart';
2 changes: 1 addition & 1 deletion lib/src/data_providers/info_update_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const is_info_updated_url = 'https://www.cusobu.nat.cu/covid/cubadata/status/';
Future<InfoUpdate> isInfoUpdated() async {
int time;
try {
time = PrefService.getInt('last_data_update');
time = PrefService.getInt(Constants.prefLastDataUpdate);
} catch (e) {
log(e.toString());
time = (DateTime.now().millisecondsSinceEpoch / 1000).round() - 1;
Expand Down
2 changes: 2 additions & 0 deletions lib/src/data_providers/notifications_data_provider.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*
import 'dart:math';
import 'package:covid19cuba/src/models/models.dart';
Expand Down Expand Up @@ -46,3 +47,4 @@ List<LocalNotification> getApplicationNotifications() {
}
return notifications;
}
*/
20 changes: 20 additions & 0 deletions lib/src/models/cases_by_mode_of_contagion.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:json_annotation/json_annotation.dart';

import 'package:covid19cuba/src/models/models.dart';

part 'cases_by_mode_of_contagion.g.dart';

@JsonSerializable()
class CasesByModeOfContagion {
Item imported;
Item inserted;
Item autochthonous;
Item unknown;

CasesByModeOfContagion();

factory CasesByModeOfContagion.fromJson(Map<String, dynamic> json) =>
_$CasesByModeOfContagionFromJson(json);

Map<String, dynamic> toJson() => _$CasesByModeOfContagionToJson(this);
}
33 changes: 33 additions & 0 deletions lib/src/models/cases_by_mode_of_contagion.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions lib/src/models/cases_by_nationality.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:json_annotation/json_annotation.dart';

import 'package:covid19cuba/src/models/models.dart';

part 'cases_by_nationality.g.dart';

@JsonSerializable()
class CasesByNationality {
Item foreign;
Item cubans;
Item unknown;

CasesByNationality();

factory CasesByNationality.fromJson(Map<String, dynamic> json) =>
_$CasesByNationalityFromJson(json);

Map<String, dynamic> toJson() => _$CasesByNationalityToJson(this);
}
27 changes: 27 additions & 0 deletions lib/src/models/cases_by_nationality.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions lib/src/models/cases_by_sex.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:json_annotation/json_annotation.dart';

import 'package:covid19cuba/src/models/models.dart';

part 'cases_by_sex.g.dart';

@JsonSerializable()
class CasesBySex {
Item men;
Item women;
Item unknown;

CasesBySex();

factory CasesBySex.fromJson(Map<String, dynamic> json) =>
_$CasesBySexFromJson(json);

Map<String, dynamic> toJson() => _$CasesBySexToJson(this);
}
Loading

0 comments on commit 7e5704b

Please sign in to comment.