Skip to content

Commit

Permalink
Update HomePage with new Data
Browse files Browse the repository at this point in the history
  • Loading branch information
tayormi committed Mar 16, 2020
1 parent 43c9675 commit 01a78f8
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 57 deletions.
27 changes: 9 additions & 18 deletions lib/blocs/case_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:convert';

import 'package:covid_tracker/models/case_model.dart';
import 'package:covid_tracker/models/general_data_model.dart';
import 'package:covid_tracker/models/home_data_model.dart';
import 'package:covid_tracker/repositories/api_repository.dart';
import 'package:covid_tracker/utils/storageutil.dart';
Expand Down Expand Up @@ -34,8 +35,8 @@ class CaseEmpty extends CaseState {}
class CaseLoading extends CaseState {}

class CaseLoaded extends CaseState {
final HomeDataModel currentData;
final HomeDataModel firstData;
final Result currentData;
final Result firstData;

CaseLoaded({@required this.currentData, @required this.firstData});

Expand All @@ -62,30 +63,20 @@ class CaseBloc extends Bloc<CaseEvent, CaseState> {
}

Stream<CaseState> _mapFetchCaseToState(FetchCase event) async* {
var firstData = new HomeDataModel();
var firstData = new GeneralDataModel();
yield CaseLoading();
try {
final caseModel = await apiRepository.getAllCases();
final suspectedCases = await apiRepository.getSuspectedCases();
final deathCases = await apiRepository.getDeathCases();
final confirmedCases = await apiRepository.getConfirmedCases();
final recoveredCases = await apiRepository.getRecoveredCases();
final currentData = new HomeDataModel(
date: caseModel.date,
suspectedCases: suspectedCases.data,
confirmedCases: confirmedCases.data,
deathCases: deathCases.data,
recoveredCases: recoveredCases.data);
final allData = await apiRepository.getAllCountryData();
final dt = StorageUtil.getString("FirstData");
if(dt.isNotEmpty) {
// Check if there's a first data and save first Data in Shared Preferences
StorageUtil.putString("FirstData", homeDataModelToJson(currentData));
firstData = homeDataModelFromJson(dt);
StorageUtil.putString("FirstData", generalDataModelToJson(allData));
firstData = generalDataModelFromJson(dt);

}
//Save current Data as we will need it later
StorageUtil.putString("CurrentData", homeDataModelToJson(currentData));
yield CaseLoaded(currentData: currentData, firstData: firstData);
StorageUtil.putString("CurrentData", generalDataModelToJson(allData));
yield CaseLoaded(currentData: allData.results[0], firstData: firstData.results[0]);
} catch (_) {
yield CaseError();
}
Expand Down
83 changes: 62 additions & 21 deletions lib/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ class _HomeState extends State<Home> {
cardTitle: 'loading',
caseTitle: 'loading',
currentData: 2344,
newData: null, percentChange: calculateGrowthPercentage(234, 5678),
icon: showGrowthIcon(234, 5678), color: showGrowthColor(234, 5678),
newData: null,
percentChange: calculateGrowthPercentage(234, 5678),
icon: showGrowthIcon(234, 5678),
color: showGrowthColor(234, 5678),
),
),
);
Expand All @@ -100,37 +102,76 @@ class _HomeState extends State<Home> {
child: Column(
children: <Widget>[
GlobalSituationCard(
cardTitle: 'SUSPECTED CASES',
caseTitle: 'Suspected',
currentData: currentData.suspectedCases,
newData: 1457, percentChange: calculateGrowthPercentage(currentData.suspectedCases, firstData.suspectedCases), icon: showGrowthIcon(currentData.suspectedCases, firstData.suspectedCases), color: showGrowthColor(currentData.suspectedCases, firstData.suspectedCases),
cardTitle: 'TOTAL CASES',
caseTitle: 'Total',
currentData: currentData.totalCases,
newData: currentData.totalNewCasesToday,
percentChange: calculateGrowthPercentage(
currentData.totalCases,
currentData.totalNewCasesToday),
icon: showGrowthIcon(currentData.totalCases,
currentData.totalNewCasesToday),
color: Colors.red,
),
SizedBox(height: hp(3)),

GlobalSituationCard(
cardTitle: 'CONFIRMED CASES',
caseTitle: 'Confirmed',
currentData: currentData.confirmedCases,
newData: 1457, percentChange: calculateGrowthPercentage(currentData.confirmedCases, firstData.confirmedCases),
icon: showGrowthIcon(currentData.confirmedCases, firstData.confirmedCases),
color: showGrowthColor(currentData.confirmedCases, firstData.confirmedCases),
cardTitle: 'RECOVERED CASES',
caseTitle: 'Recovered',
currentData: currentData.totalRecovered,
newData: currentData.totalRecovered -
firstData.totalRecovered,
percentChange: calculateGrowthPercentage(
currentData.totalRecovered,
firstData.totalRecovered),
icon: showGrowthIcon(currentData.totalRecovered,
firstData.totalRecovered),
color: Colors.red,
),
SizedBox(height: hp(3)),
GlobalSituationCard(
cardTitle: 'DEATH CASES',
caseTitle: 'Deaths',
currentData: currentData.deathCases,
newData: 1457, percentChange: calculateGrowthPercentage(currentData.deathCases, firstData.deathCases),
icon: showGrowthIcon(currentData.deathCases, firstData.deathCases), color: showGrowthColor(currentData.deathCases, firstData.deathCases),
currentData: currentData.totalDeaths,
newData: currentData.totalNewDeathsToday,
percentChange: calculateGrowthPercentage(
currentData.totalDeaths,
currentData.totalNewDeathsToday),
icon: showGrowthIcon(currentData.totalDeaths,
currentData.totalNewDeathsToday),
color: Colors.red,
),
SizedBox(height: hp(3)),
GlobalSituationCard(
cardTitle: 'RECOVERED CASES',
caseTitle: 'Recovered',
currentData: currentData.recoveredCases,
newData: 1457, percentChange: calculateGrowthPercentage(currentData.recoveredCases, firstData.recoveredCases),
icon: showGrowthIcon(currentData.recoveredCases, firstData.recoveredCases),
color: showGrowthColor(currentData.recoveredCases, firstData.recoveredCases),
cardTitle: 'SERIOUS CASES',
caseTitle: 'Serious',
currentData: currentData.totalSeriousCases,
newData: currentData.totalSeriousCases -
firstData.totalSeriousCases,
percentChange: calculateGrowthPercentage(
currentData.totalSeriousCases,
currentData.totalSeriousCases -
firstData.totalSeriousCases),
icon: showGrowthIcon(currentData.totalSeriousCases,
firstData.totalSeriousCases),
color: Colors.red,
),
// GlobalSituationCard(
// cardTitle: 'DEATH CASES',
// caseTitle: 'Deaths',
// currentData: currentData.deathCases,
// newData: 1457, percentChange: calculateGrowthPercentage(currentData.deathCases, firstData.deathCases),
// icon: showGrowthIcon(currentData.deathCases, firstData.deathCases), color: showGrowthColor(currentData.deathCases, firstData.deathCases),
// ),
// SizedBox(height: hp(3)),
// GlobalSituationCard(
// cardTitle: 'RECOVERED CASES',
// caseTitle: 'Recovered',
// currentData: currentData.recoveredCases,
// newData: 1457, percentChange: calculateGrowthPercentage(currentData.recoveredCases, firstData.recoveredCases),
// icon: showGrowthIcon(currentData.recoveredCases, firstData.recoveredCases),
// color: showGrowthColor(currentData.recoveredCases, firstData.recoveredCases),
// ),
SizedBox(height: hp(3)),
],
),
Expand Down
93 changes: 93 additions & 0 deletions lib/models/general_data_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// To parse this JSON data, do
//
// final generalDataModel = generalDataModelFromJson(jsonString);

import 'dart:convert';

GeneralDataModel generalDataModelFromJson(String str) => GeneralDataModel.fromJson(json.decode(str));

String generalDataModelToJson(GeneralDataModel data) => json.encode(data.toJson());

class GeneralDataModel {
final List<Result> results;
final String stat;

GeneralDataModel({
this.results,
this.stat,
});

factory GeneralDataModel.fromJson(Map<String, dynamic> json) => GeneralDataModel(
results: json["results"] == null ? null : List<Result>.from(json["results"].map((x) => Result.fromJson(x))),
stat: json["stat"] == null ? null : json["stat"],
);

Map<String, dynamic> toJson() => {
"results": results == null ? null : List<dynamic>.from(results.map((x) => x.toJson())),
"stat": stat == null ? null : stat,
};
}

class Result {
final int totalCases;
final int totalRecovered;
final int totalUnresolved;
final int totalDeaths;
final int totalNewCasesToday;
final int totalNewDeathsToday;
final int totalActiveCases;
final int totalSeriousCases;
final Source source;

Result({
this.totalCases,
this.totalRecovered,
this.totalUnresolved,
this.totalDeaths,
this.totalNewCasesToday,
this.totalNewDeathsToday,
this.totalActiveCases,
this.totalSeriousCases,
this.source,
});

factory Result.fromJson(Map<String, dynamic> json) => Result(
totalCases: json["total_cases"] == null ? null : json["total_cases"],
totalRecovered: json["total_recovered"] == null ? null : json["total_recovered"],
totalUnresolved: json["total_unresolved"] == null ? null : json["total_unresolved"],
totalDeaths: json["total_deaths"] == null ? null : json["total_deaths"],
totalNewCasesToday: json["total_new_cases_today"] == null ? null : json["total_new_cases_today"],
totalNewDeathsToday: json["total_new_deaths_today"] == null ? null : json["total_new_deaths_today"],
totalActiveCases: json["total_active_cases"] == null ? null : json["total_active_cases"],
totalSeriousCases: json["total_serious_cases"] == null ? null : json["total_serious_cases"],
source: json["source"] == null ? null : Source.fromJson(json["source"]),
);

Map<String, dynamic> toJson() => {
"total_cases": totalCases == null ? null : totalCases,
"total_recovered": totalRecovered == null ? null : totalRecovered,
"total_unresolved": totalUnresolved == null ? null : totalUnresolved,
"total_deaths": totalDeaths == null ? null : totalDeaths,
"total_new_cases_today": totalNewCasesToday == null ? null : totalNewCasesToday,
"total_new_deaths_today": totalNewDeathsToday == null ? null : totalNewDeathsToday,
"total_active_cases": totalActiveCases == null ? null : totalActiveCases,
"total_serious_cases": totalSeriousCases == null ? null : totalSeriousCases,
"source": source == null ? null : source.toJson(),
};
}

class Source {
final String url;

Source({
this.url,
});

factory Source.fromJson(Map<String, dynamic> json) => Source(
url: json["url"] == null ? null : json["url"],
);

Map<String, dynamic> toJson() => {
"url": url == null ? null : url,
};
}
20 changes: 11 additions & 9 deletions lib/pages/widgets/global_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ class GlobalSituationCard extends StatelessWidget {
Text(this.caseTitle, style: AppTheme.titleStyle.copyWith(fontSize: 14, color: LightColor.darkgrey)),
],
),


Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(this.newData != null ? formatter.format( this.newData) : '-', style: AppTheme.titleStyle.copyWith(fontSize: 20, color: Colors.black)),
SizedBox(height: hp(1)),
Text('New', style: AppTheme.titleStyle.copyWith(fontSize: 14, color: LightColor.darkgrey)),
],
),
SizedBox(height: wp(10)),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Expand All @@ -61,15 +72,6 @@ class GlobalSituationCard extends StatelessWidget {
Text('${percentChange.round()}%', style: AppTheme.titleStyle.copyWith(fontSize: 14, color: color)),
],
),
// SizedBox(height: wp(10)),
// Column(
// crossAxisAlignment: CrossAxisAlignment.start,
// children: <Widget>[
// Text(this.newData != null ? formatter.format( this.newData) : '-', style: AppTheme.titleStyle.copyWith(fontSize: 20, color: Colors.black)),
// SizedBox(height: hp(1)),
// Text('New', style: AppTheme.titleStyle.copyWith(fontSize: 14, color: LightColor.darkgrey)),
// ],
// ),
// Column(
// crossAxisAlignment: CrossAxisAlignment.start,
// children: <Widget>[
Expand Down
9 changes: 5 additions & 4 deletions lib/repositories/api_client.dart
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import 'package:covid_tracker/models/case_model.dart';
import 'package:covid_tracker/models/general_data_model.dart';
import 'package:covid_tracker/models/other_case_model.dart';
import 'package:dio/dio.dart';

import 'api_interceptor.dart';

class ApiClient {
static const baseUrl =
'https://apigw.nubentos.com:443/t/nubentos.com/ncovapi/1.0.0';
'https://thevirustracker.com/free-api';
Dio _dio;
ApiClient() {
BaseOptions options = BaseOptions(
receiveTimeout: 100000, connectTimeout: 100000, baseUrl: baseUrl);
_dio = Dio(options);
_dio.interceptors.add(ApiInterceptor());
}
Future<CaseModel> getAllCases() async {
final url = '$baseUrl/cases';
Future<GeneralDataModel> getAllCountryData() async {
final url = '$baseUrl?global=stats';

try {
final response = await _dio.get(url);
return CaseModel.fromJson(response.data[0]);
return GeneralDataModel.fromJson(response.data);
} on DioError catch (e) {
print(e.error);
throw e.error;
Expand Down
5 changes: 3 additions & 2 deletions lib/repositories/api_repository.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:covid_tracker/models/case_model.dart';
import 'package:covid_tracker/models/general_data_model.dart';
import 'package:covid_tracker/models/other_case_model.dart';
import 'package:covid_tracker/repositories/api_client.dart';
import 'package:flutter/material.dart';
Expand All @@ -7,8 +8,8 @@ class ApiRepository {
final ApiClient apiClient;

ApiRepository({@required this.apiClient}): assert(apiClient != null);
Future<CaseModel> getAllCases() async {
return apiClient.getAllCases();
Future<GeneralDataModel> getAllCountryData() async {
return apiClient.getAllCountryData();
}
Future<OtherCaseModel> getRecoveredCases() async {
return apiClient.getRecoveredCases();
Expand Down
6 changes: 3 additions & 3 deletions lib/utils/calculateGrowth.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

import 'package:flutter/material.dart';

calculateGrowthPercentage(int currentData, int previousData) {
return (100 - (currentData / previousData * 100));
calculateGrowthPercentage(int previousData, int newData) {
return (newData / previousData) * 100;
}
Icon showGrowthIcon(int currentData, int previousData) {
Icon icon;
if(currentData > previousData) {
icon = Icon(Icons.arrow_upward, color: Colors.green,);
icon = Icon(Icons.arrow_upward, color: Colors.red,);
} else {
icon = Icon(Icons.arrow_downward, color: Colors.red,);
}
Expand Down

0 comments on commit 01a78f8

Please sign in to comment.