initializeWithUrl method

Future<void> initializeWithUrl(
  1. String url, {
  2. int updateFreqDays = 1,
})

Fetches a list of apps to be advertised from the URL (Get), set updateFreqDays to limit the http calls. Ensure that the response matches the AppInfo model otherwise this will fail. Alternative is to use initializeWithApps if you would like the pass the data in directly

Implementation

Future<void> initializeWithUrl(String url, {int updateFreqDays = 1}) async {
  customPrint("Initializing");
  try {
    _prefs = await SharedPreferences.getInstance();
    if (canUpdateAds(updateFreqDays)) {
      customPrint("Calling URL to update ads");
      var response = await http.get(Uri.parse(url));

      // save apps and lastUpdated to local storage
      _apps = AppInfo.fromJsonList(response.body);
      await _prefs.setString(_lsKey.apps.toKeyString(), jsonEncode(_apps));
      await _prefs.setString(
          _lsKey.lastUpdated.toKeyString(), DateTime.now().toString());
    } else {
      customPrint("Loading ads from storage");
      // not enough time has passed to update, fetch from shared preferences
      _apps = AppInfo.fromJsonList(
          _prefs.getString(_lsKey.apps.toKeyString()) ?? '[]');
    }
    customPrint("Initialization Complete");
  } catch (e) {
    customPrint(e.toString());
  }
}