Skip to content

Commit

Permalink
Fix data fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
dewmal committed Mar 18, 2020
1 parent 9055b6f commit 0133c7c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
16 changes: 11 additions & 5 deletions lib/networking/api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:shared_preferences/shared_preferences.dart';
import '../models/news_article.dart';

class ApiClient {
final String _baseUrl = 'http://covid19.egreen.io:8000';
final String _baseUrl = 'https://api.covid-19.health.gov.lk';

Future<bool> registerUser() async {
final url = '$_baseUrl/user/register';
Expand Down Expand Up @@ -47,23 +47,29 @@ class ApiClient {
return lastMessageId;
}

Future<List<NewsArticle>> getArticleList(startId, endId) async {
Future<List<NewsArticle>> getArticleList(startId, endId,
{forceUpdate = false}) async {
//this will run everytime th user switchs tabs.
List<NewsArticle> articles = [];
//
for (var i = startId; i <= endId; i++) {
NewsArticle article = await getMessage(i);
NewsArticle article = await getMessage(i, forceUpdate: forceUpdate);
articles.add(article);
}

return articles;
}

Future<NewsArticle> getMessage(int id) async {
Future<NewsArticle> getMessage(int id, {forceUpdate = false}) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String lang = prefs.getString('preferred_language');
final sharedPrefId = "alert_$lang--$id";

String alertData = prefs.getString(sharedPrefId);
if (alertData == null) {

if (alertData == null || forceUpdate) {
final url = '$_baseUrl/application/alert/$id/$lang';
print("Requesting data from $url");
final response =
await http.get(url, headers: {'Content-Type': 'application/json'});
if (response.statusCode != 200) {
Expand Down
47 changes: 36 additions & 11 deletions lib/page/screen/dashboard_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ import 'package:selftrackingapp/notifiers/stories_model.dart';
import 'package:selftrackingapp/utils/tracker_colors.dart';
import 'package:share/share.dart';

import 'package:intl/intl.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../models/news_article.dart';
import '../../models/news_article.dart';
import '../../networking/api_client.dart';
import '../../utils/tracker_colors.dart';

enum CounterType { confirmed, recovered, suspected, deaths }

DateFormat dateFormat = DateFormat("dd-MM-yy HH:mm");

class DashboardScreen extends StatefulWidget {
const DashboardScreen({Key key}) : super(key: key);

Expand All @@ -25,56 +29,77 @@ class DashboardScreen extends StatefulWidget {
}

class _DashboardScreenState extends State<DashboardScreen> {
RemoteConfig config;

// Remotely configured values
DateTime lastUpdated = DateTime.now();
int confirmed = 0;
int recovered = 0;
int suspected = 0;
int deaths = 0;
int resetIndexServer = 0;

Timer _timer;

@override
void initState() {
super.initState();

updateRemoteConfig();
updateRemoteConfig().then((val) {
fetchArticles();
});
_timer = Timer.periodic(Duration(minutes: 15), (timer) {
updateRemoteConfig();
});

fetchArticles();
}

Future<void> fetchArticles() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool forceUpdate = true;
// if (config != null) {
// int resetIndex = prefs.getInt("reset_index");
//
// if (resetIndex == null) {
// resetIndex = 0;
// }
//
// if (resetIndexServer > resetIndex) {
// prefs.setInt("reset_index", resetIndex);
// prefs.setInt("last_message_id", 0);
// forceUpdate = true;
// }
//
// print("DAta Request $resetIndex, $resetIndexServer, $forceUpdate");
// }

print("Fetching the articles");
int id = await ApiClient().getLastMessageId();
int lowerID = 1;
if (id >= 10) {
lowerID = id - 9;
}
List<NewsArticle> articles = await ApiClient().getArticleList(
lowerID,
id,
);
List<NewsArticle> articles =
await ApiClient().getArticleList(lowerID, id, forceUpdate: forceUpdate);
articles.forEach((e) {
Provider.of<StoriesModel>(context, listen: false).add(e);
});
}

void updateRemoteConfig() async {
final RemoteConfig config = await RemoteConfig.instance;
Future<void> updateRemoteConfig() async {
config = await RemoteConfig.instance;
final defaults = <String, dynamic>{
'last_updated': '2020-03-17 10:15',
'confirmed': 28,
'recovered': 1,
'suspected': 212,
'deaths': 0
'deaths': 0,
"reset_index": 0
};
await config.setDefaults(defaults);
await config.fetch(expiration: Duration(minutes: 15 - 1));
await config.activateFetched();
setState(() {
resetIndexServer = config.getInt('reset_index');
confirmed = config.getInt('confirmed');
recovered = config.getInt('recovered');
suspected = config.getInt('suspected');
Expand Down Expand Up @@ -144,7 +169,7 @@ class _DashboardScreenState extends State<DashboardScreen> {
padding: const EdgeInsets.only(left: 20.0, top: 10.0),
sliver: SliverToBoxAdapter(
child: Text(
"${AppLocalizations.of(context).translate('dashboard_screen_last_updated')} ${lastUpdated.toString()}",
"${AppLocalizations.of(context).translate('dashboard_screen_last_updated')} ${dateFormat.format(lastUpdated)}",
style: TextStyle(fontWeight: FontWeight.bold),
),
),
Expand Down

0 comments on commit 0133c7c

Please sign in to comment.