getHomeLoadMore method
Implementation
Future<void> getHomeLoadMore(
int homePageNumber, GetHomeLoadMore getHomeLoadMore) async {
getHomeLoadMore.onLoading();
final url = Uri.parse(
'https://api.plentrasphere.com/v2/client/?class=default&action=getHome&appKey=$appKey&page=$homePageNumber');
try {
final response = await http.get(url);
if (response.statusCode == 200) {
final jsonData = json.decode(response.body);
final code = jsonData['response']['code'];
final status = jsonData['response']['status'];
if (code == 401) {
getHomeLoadMore.onError(status);
getHomeLoadMore.onLoadfinished();
return;
} else if (code == 404) {
getHomeLoadMore.onNotFound();
getHomeLoadMore.onLoadfinished();
return;
} else if (code == 400) {
if (status == "app-expired") {
getHomeLoadMore.onAppNotActive(jsonData['info']['appName']);
getHomeLoadMore.onLoadfinished();
return;
}
getHomeLoadMore.onError(status);
getHomeLoadMore.onLoadfinished();
return;
} else if (code == 406) {
getHomeLoadMore.onError(status);
return;
}
if (status == "under-construction") {
getHomeLoadMore.onUnderConstruction();
getHomeLoadMore.onLoadfinished();
return;
}
final info = jsonData['info'];
final itemsInThisPage = (info['itemsInThisPage']);
final totalItemCount = (info['totalItemCount']);
if (info['nextPage']['status']) {
getHomeLoadMore.onNextPage(info['nextPage']['page']);
} else {
getHomeLoadMore.onNoNextPage();
}
if (itemsInThisPage != 0) {
final List<dynamic> _items = jsonData['items'];
final List<dynamic> items = [];
_items.forEach((element) {
items.add({
'itemName': element['itemName'],
'actionType': element['actionType'],
'action': element['action'],
'itemImage': element['thumbnail'],
});
});
getHomeLoadMore.onResult(
totalItemCount,
itemsInThisPage,
info['itemsPerPage'],
items,
);
} else {
getHomeLoadMore.onEmpty(info['appName'], info['appIcon']);
}
getHomeLoadMore.onLoadfinished();
} else {
getHomeLoadMore.onError('HTTP ${response.statusCode}');
getHomeLoadMore.onLoadfinished();
}
} catch (e) {
getHomeLoadMore.onError(e.toString());
getHomeLoadMore.onLoadfinished();
}
}