getLauncher method

Future<void> getLauncher(
  1. String launcherId,
  2. GetLauncher getLauncher
)

Implementation

Future<void> getLauncher(String launcherId, GetLauncher getLauncher) async {
  getLauncher.onLoading();

  final url = Uri.parse(
      'https://api.plentrasphere.com/v2/client/?class=default&action=getLauncher&appKey=$appKey&page=1&launcherId=$launcherId');

  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) {
        getLauncher.onError(status);
        getLauncher.onLoadfinished();
        return;
      } else if (code == 404) {
        getLauncher.onNotFound();
        getLauncher.onLoadfinished();
        return;
      } else if (code == 400) {
        if (status == "app-expired") {
          getLauncher.onAppNotActive(jsonData['info']['appName']);
          getLauncher.onLoadfinished();
          return;
        }

        getLauncher.onError(status);
        getLauncher.onLoadfinished();
        return;
      } else if (code == 406) {
        getLauncher.onError(status);
        return;
      }

      if (status == "under-construction") {
        getLauncher.onUnderConstruction();
        getLauncher.onLoadfinished();
        return;
      }

      final info = jsonData['info'];

      if (info['appLocation']['status']) {
        final location = info['appLocation'];
        getLauncher.onAppLocation(
          location['location'],
          (location['lat']),
          (location['lon']),
        );
      }

      final itemsInThisPage = (info['itemsInThisPage']);
      final totalItemCount = (info['totalItemCount']);

      if (jsonData['announcement']['status']) {
        final announcement = jsonData['announcement'];
        getLauncher.onAnnouncement(announcement['announcementBody']);
      }

      if (info['launcherCover']['status']) {
        getLauncher.onLauncherCover(info['launcherCover']['image']);
      }

      if (info['nextPage']['status']) {
        getLauncher.onNextPage(info['nextPage']['page']);
      } else {
        getLauncher.onNoNextPage();
      }

      if (itemsInThisPage != 0) {
        final List<dynamic> items = [];
        final _items = jsonData['items'];

        _items.forEach((element) {
          items.add({
            'itemName': element['itemName'],
            'actionType': element['actionType'],
            'action': element['action'],
            'itemImage': element['thumbnail'],
          });
        });

        getLauncher.onResult(
          info['appName'],
          info['appIcon'],
          info['launcherName'],
          info['launcherThumbnail'],
          totalItemCount,
          itemsInThisPage,
          (info['itemsPerPage']),
          items,
        );
      } else {
        getLauncher.onEmpty(
          info['appName'],
          info['appIcon'],
          info['launcherName'],
          info['launcherThumbnail'],
        );
      }
      getLauncher.onLoadfinished();
    } else {
      getLauncher.onError('HTTP ${response.statusCode}');
      getLauncher.onLoadfinished();
    }
  } catch (e) {
    getLauncher.onError(e.toString());
    getLauncher.onLoadfinished();
  }
}